ChipKIT Uno32 is an open source hardware prototyping platform based on Arduino, but featuring a Microchip PIC32 microcontroller. It is also compatible with many Arduino shields, code examples, reference materials and other resources. After doing this tutorial you’ll be able to read the environmental noise level using chipKIT Uno32 and send its value to Ubidots.

Requirements

Setup

  1. Download Arduino IDE version 1.6.5 or higher, here.
  2. From within the Arduino IDE, go to File->Preferences dialog box.
  3. Look at the text entry field called “Additional Boards Manager URLs:”.
  4. just copy/paste the following URL into that text field “https://github.com/chipKIT32/chipKIT-core/raw/master/package_chipkit_index.json”. Then click OK to close.
  5. Now select the Tools->Board->Board Manager menu from the Arduino IDE, and it will open up the Boards Manager window. From there, scroll down until you see the chipKIT board.
  6. Click once on any of the text in the chipKIT section, and you will see a button appear that says “Install”. It will take some time to download all of the chipKIT components and install them, but when it’s done, you can click the Close button to close the Board Manager window.
  7. Now choose a chipKIT UNO board from the Tools->Board

To use this code, put your Ubidots token and variable ID where indicated, as well as the WiFi settings. After uploading each example to your Arduino, open the Serial monitor to check the results. If no response is seen, try unplugging your ChipKit UNO and then plugging it again. Make sure the baud rate of the Serial monitor is set to the same one specified in your code.

Send one value to Ubidots

Create a new file called “noiseLevel” and copy the following code. Don’t forget to replace TOKEN, ID and WiFi settings with the ones in your Ubidots account:

#include <Wprogram.h>
#include <Wire.h>
#include <WiFiShieldOrPmodWiFi_G.h>                     // This is for the MRF24WGxx on a pmodWiFi or WiFiShield
#include <DNETcK.h>
#include <DWIFIcK.h>

char * szIPServer = "things.ubidots.com";    // server to connect to
unsigned short portServer = 80;
const char * szSsid = "Atom House Medellin";//SSID
const char * szPassPhrase = "atommed2014";//passphrase
#define USE_WPA2_PASSPHRASE //Choose the security your network use.
      //#define USE_WPA2_KEY
      //#define USE_WEP40
      //#define USE_WEP104
      //#define USE_WF_CONFIG_H
#if defined(USE_WPA2_PASSPHRASE)
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &status)

#elif defined(USE_WPA2_KEY)

    DWIFIcK::WPA2KEY key = { 0x27, 0x2C, 0x89, 0xCC, 0xE9, 0x56, 0x31, 0x1E,
                            0x3B, 0xAD, 0x79, 0xF7, 0x1D, 0xC4, 0xB9, 0x05,
                            0x7A, 0x34, 0x4C, 0x3E, 0xB5, 0xFA, 0x38, 0xC2,
                            0x0F, 0x0A, 0xB0, 0x90, 0xDC, 0x62, 0xAD, 0x58 };
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, key, &status)

#elif defined(USE_WEP40)

    const int iWEPKey = 0;
    DWIFIcK::WEP40KEY keySet = {    0xBE, 0xC9, 0x58, 0x06, 0x97,     // Key 0
                                    0x00, 0x00, 0x00, 0x00, 0x00,     // Key 1
                                    0x00, 0x00, 0x00, 0x00, 0x00,     // Key 2
                                    0x00, 0x00, 0x00, 0x00, 0x00 };   // Key 3
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)

#elif defined(USE_WEP104)

    const int iWEPKey = 0;
    DWIFIcK::WEP104KEY keySet = {   0x3E, 0xCD, 0x30, 0xB2, 0x55, 0x2D, 0x3C, 0x50, 0x52, 0x71, 0xE8, 0x83, 0x91,   // Key 0
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 1
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 2
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Key 3
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)

#elif defined(USE_WF_CONFIG_H)

    #define WiFiConnectMacro() DWIFIcK::connect(0, &status)

#else   // no security - OPEN

    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, &status)

#endif

String token="1LReH7fFyYXWRdqIBKkf3jdpEji6Kb1wVfN571HwAMzFG1uB18J8B4UGD8Nw";
String idPot="53eba4657625420fe5b4992e";
const int sampleWindow=50;
unsigned tStart = 0;
double value;
typedef enum //States of the state machine
{
    NONE = 0,
    CONNECT,
    TCPCONNECT,
    WRITE,
    READ,
    CLOSE,
    DONE,
} STATE;

STATE state = CONNECT;
TcpClient tcpClient;

void setup() {
    Serial.begin(9600);
    Serial.println("WiFiTCPEchoClient 1.0");
    Serial.println("Digilent, Copyright 2012");
    Serial.println("Wait a few seconds");
}

void loop() {
  unsigned long startMillis= millis();  // Start of sample window
  double peakToPeak = 0;   // peak-to-peak level
  double signalMax = 0;
  double signalMin = 1024;
    while (millis() - startMillis < sampleWindow)
  {
    value = analogRead(A0);
    if (value < 1024)  // toss out spurious readings
    {
      if (value > signalMax)
      {
        signalMax = value;  // save just the max levels
      }
      else if (value < signalMin)
      {
        signalMin = value;  // save just the min levels
      }
    }
  }
   value = ((signalMax - signalMin)* 3.3) / 1024; //to volts
   value=20*log10(value*50000);//to dbs
   if(save_value(String(int(value)),idPot))
    {
      Serial.println("potenciometer value in Ubidots:");
      Serial.println(value);
    }
    delay(1000);
}

boolean save_value(String value,String idvariable)//Send value to Ubidots
{
  int cbRead=0;
  String var = "{\"value\":"+ value + "}";
  int num = var.length();
  String le = String(num);  
  String message = "POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1\nContent-Type: application/json\nContent-Length: "+le+"\nX-Auth-Token: "+token+"\nHost: things.ubidots.com\n\n"+var+"\n\n";
  byte rgbWriteStream[message.length()]  ;
  message.getBytes(rgbWriteStream,message.length() + 1);
  int cbWriteStream = sizeof(rgbWriteStream);
  state_machine(rgbWriteStream,cbWriteStream);
  return true;
}

boolean state_machine(byte rgbWriteStream[], int cbWriteStream)//Handle the TCP connection
{
    byte rgbRead[1024];

    int conID = DWIFIcK::INVALID_CONNECTION_ID;
    DNETcK::STATUS status;
  for(;;)
  {
    int cbRead = 0;
    switch(state)
    {
       case CONNECT:
            if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
            {
                Serial.print("Connection Created, ConID = ");
                Serial.println(conID, DEC);
                state = TCPCONNECT;
            }
            else
            {
                Serial.print("Unable to connection, status: ");
                Serial.println(status, DEC);
                state = CLOSE;
            }
            break;
       case TCPCONNECT:
            DNETcK::begin();
            tcpClient.connect(szIPServer, portServer);
            state = WRITE;
             break;
       case WRITE:
            if(tcpClient.isConnected())
                {    
                  Serial.println("Got Connection");
                  tcpClient.writeStream(rgbWriteStream, cbWriteStream);
                  Serial.println("Bytes Read Back:");
                  state = CLOSE;
                  tStart = (unsigned) millis();
                }
            break;          
        case CLOSE:
            tcpClient.close();
            state = DONE;
            break;
        case DONE:    
            state = TCPCONNECT;
            return cbRead;        
        default:
            break;
    }  
    DNETcK::periodicTasks(); // keep the stack alive each pass through the loop()
  }
}
Did this answer your question?