Skip to main content
All CollectionsConnect your Devices
Connect a ChipKIT Uno32 to Ubidots over HTTP
Connect a ChipKIT Uno32 to Ubidots over HTTP

Learn to transmit data to Ubidots IoT Application Development Platfrom over HTTP using chipKIT Uno32.

Sergio M avatar
Written by Sergio M
Updated over a week ago

ChipKIT Uno32 is an open source hardware prototyping platform based on Arduino, but featuring a Microchip PIC32 microcontroller. It is compatible with many Arduino shields, code examples, reference materials and other resources making it a simple to use and easily adjustable board for POCs and low-impact Industrial IoT Applications.

By the end of this guide you will be able to send data to Ubidots using the chipKIT Uno32.

Requirements

Step-by-Step

  1. Setting up the Arduino IDE

  2. Sending (POST) Data to Ubidots

  3. Summary

1. Setting up the Arduino IDE

1. To be able to work with the chipKIT platform in the Arduino IDE, you will need to install the chipKIT platform using the preconfigured Arduino Board Manager. If you are not familiar with adding a board with the Arduino IDE, simply refer to this article for additional guidance.

The Board Manager URL for the chipKIT is:

https://github.com/chipKIT32/chipKIT-core/raw/master/package_chipkit_index.json

2. With the chipKIT platform installed, select the chipKIT device you are working with. In this tutorial, we are working with a “chipKIT UNO”. To select your board from the Arduino IDE, select Tools –> Board chipKIT UNO”.

2. Sending (POST) Data to Ubidots

With the following sample code you will be able to post the readings taken from ANALOG pin A0 of the chipKIT UNO to Ubidots IoT Application Development Platform.

1. To POST your first value in Ubidots, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign the following parameters:

  • The SSID of the WiFi and the Password of the network

  • The Variable ID of the variables you will be updating in Ubidots. If you do not have the variable IDs, simply create a shell variables and then update your firmware to contain the correct Variable IDs. To locate the variable IDs, simply click here. 

#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>

String token="xxxxxxxxxxx";
String idPot="xxxxxxxxxxx";
char * szIPServer = "industrial.api.ubidots.com";    // server to connect to
unsigned short portServer = 80;
const char * szSsid = "xxxxxx";//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

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: industrial.api.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()
  }
}

2. Next, Verify your code within the Arduino IDE. To do this, in the top left corner of our Arduino IDE you will see the "Check Mark" icon; press it to verify your code. 

3. Upload the code into your chipKIT UNO. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

4. To verify the connectivity of the device and the data sent, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to see the connectivity logs. 

NOTE: If no response is seen in the serial monitor, try unplugging the device and then plugging it again. Also, make sure the baud rate of the Serial monitor is set to the same one specified in your code 9600 

At this point, the variables assigned in the code will be updating the analog readings the taken from the board.

3. Summary 

With this simple tutorial you are able to POST data to Ubidots with the ease of the Arduino IDE and an chipKIT UNO.

Now its time to create Ubidots Dashboards to visualize your data and deploy your IoT solution!  :) 

Other readers have also found useful...

Did this answer your question?