The MKR WIFI 1010 is a significant improvement on the MKR 1000 WIFI. It's equipped with an ESP32 module. This board aims to speed up and simplify the prototyping of WiFi based IoT applications thanks to the flexibility of the ESP32 module and its low power consumption.

By following this guide you will be able to make POST and GET HTTP request to Ubidots in just a couple of minutes!    

Requirements

Step-by-Step

  1. Setting up the Arduino IDE
  2. Sending (POST) Data to Ubidots 
  3. Retrieve (GET) Data from Ubidots 
  4. Summary

1.Setting up the Arduino IDE

  • Download the latest version of the Arduino IDE if you do not already have it.
  • In the Arduino IDE, click on Sketch -> Include Library -> Manage Libraries.

Search for WiFiNINA library and install the latest version available on your computer. Make sure the firmware version of the board matches with the library. If the firmware needs an update, please use the following utilities : 

  • CheckWiFiNINAFirmwareVersion : Reads the required firmware number required from library and matches with the one installed on the board or the shield.
  • WiFiNINAFirmwareUpdater : The sketch that must be loaded to allow the firmware and certificates update process through the integrated plugin of Arduino Software (IDE) rel. 1.8.5 or later.

2.Sending (POST) Data to Ubidots 

In order to POST data to Ubidots, first you have to select the board and port in the options Tools of the Arduino IDE bar.

After make sure your board is successfully connected with the computer, open the Arduino IDE and paste the sample code below. Once you have pasted the code, you will need to assign your Ubidots TOKEN, SSID (WiFi Name) and Password of the available network.

/********************************
 * Libraries included
 *******************************/

#include <SPI.h>
#include <WiFiNINA.h>
#include <avr/dtostrf.h>

/********************************
 * Constants and objects
 *******************************/

#define DEVICE_LABEL "DEVICE_LABEL"
#define TOKEN "PUT_YOUR_TOKEN_HERE"

char const * VARIABLE_LABEL_1 = "PUT_YOUR_VARIABLE_LABEL_HERE";
char const *SERVER="industrial.api.ubidots.com";
//Replace the above line if you are an Educational user char const *SERVER="things.ubidots.com";

const int HTTPPORT= 443;
char const *AGENT="Arduino MKR Wifi 1010";
char const *HTTP_VERSION = " HTTP/1.1\r\n";
char const *VERSION ="1.0";
char const *PATH= "/api/v1.6/devices/";

char const * SSID_NAME = ""; // Put here your SSID name
char const * SSID_PASS = ""; // Put here your password

int status = WL_IDLE_STATUS;

WiFiSSLClient client;

/********************************
 * Auxiliar Functions
 *******************************/

void printWiFiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void getResponseServer() {
  Serial.println(F("\nUbidots' Server response:\n"));
  while (client.available()) {
    char c = client.read();
    Serial.print(c); // Uncomment this line to visualize the response on the Serial Monitor
  }
}

void waitServer() {
  int timeout = 0;
  while (!client.available() && timeout < 5000) {
    timeout++;
    delay(1);
    if (timeout >= 5000) {
      Serial.println(F("Error, max timeout reached"));
      break;
    }
  }
}

void sendData(char* payload) {
  int contentLength = strlen(payload);

  /* Connecting the client */
  if (client.connect(SERVER, HTTPPORT)) {
    Serial.println("connected to server");
   
    client.print(F("POST "));
    client.print(PATH);    
    client.print(DEVICE_LABEL);
    client.print(F("/"));
    client.print(HTTP_VERSION);
    client.print(F("Host: "));
    client.print(SERVER);
    client.print(F("\r\n"));  
    client.print(F("User-Agent: "));
    client.print(AGENT);
    client.print(F("\r\n"));
    client.print(F("X-Auth-Token: "));
    client.print(TOKEN);
    client.print(F("\r\n"));
    client.print(F("Connection: close\r\n"));
    client.print(F("Content-Type: application/json\r\n"));
    client.print(F("Content-Length: "));
    client.print(contentLength);
    client.print(F("\r\n\r\n"));
    client.print(payload);
    client.print(F("\r\n"));
   
    Serial.print(F("POST "));
    Serial.print(PATH);    
    Serial.print(DEVICE_LABEL);
    Serial.print(F("/"));
    Serial.print(HTTP_VERSION);
    Serial.print(F("Host: "));
    Serial.print(SERVER);
    Serial.print(F("\r\n"));
    Serial.print(F("User-Agent: "));
    Serial.print(AGENT);
    Serial.print(F("\r\n"));
    Serial.print(F("X-Auth-Token: "));
    Serial.print(TOKEN);
    Serial.print(F("\r\n"));
    Serial.print(F("Connection: close\r\n"));
    Serial.print(F("Content-Type: application/json\r\n"));
    Serial.print(F("Content-Length: "));
    Serial.print(contentLength);
    Serial.print(F("\r\n\r\n"));
    Serial.print(payload);
    Serial.print(F("\r\n"));
   
    waitServer();
    getResponseServer();
  }

    /* Disconnecting the client */
  client.stop();
}

/********************************
 * Main Functions
 *******************************/

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);
 
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
 
  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }
 
  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SSID_NAME);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(SSID_NAME, SSID_PASS);
    // wait 10 seconds for connection:
    delay(10000);
  }
  Serial.println("Connected to wifi");
  printWiFiStatus();
}

void loop(){

  char payload[200];
  char str_val_1[30];
 
  /*4 is the total lenght of number,maximun number accepted is 99.99*/
  float value = analogRead(A0);
  dtostrf(value, 4, 2, str_val_1);
  sprintf(payload, "%s","");
  sprintf(payload, "{\"");
  sprintf(payload, "%s%s\":%s", payload, VARIABLE_LABEL_1, str_val_1);
  sprintf(payload, "%s}", payload);
 
  //Send the payload to Ubidots
  sendData(payload);
  delay(5000);

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. 

UPLOAD the code into your “Arduino MKR WIFI 1010”. To do this, choose the "right-arrow" icon beside the "check mark" icon. 

To verify the device's connectivity and the server response, open the serial monitor by selecting the "magnifying glass" icon in the top right corner of the Arduino IDE to check if the data is being send correctly.

After you follow the above steps, you would be able to see a response in the serial monitor like the one below.

Finally in your Ubidots account you will be able to see the data posted in your  device.

3.Retrieve (GET) Data from Ubidots

With the following sample code you will be able to GET the last values of a variable. Be sure to assign your Ubidots TOKEN, SSID (WiFi Name), Password, Device Label and Variable Label  that want to GET 

#include <SPI.h>
#include <WiFiNINA.h>
#include <avr/dtostrf.h>

#define DEVICE_LABEL "DEVICE_LABEL"
#define TOKEN "PUT_YOUR_TOKEN_HERE"

char const * VARIABLE_LABEL = "VARIABLE_LABEL";
char const *SERVER="industrial.api.ubidots.com";
//Replace the line above this if you are an Educational user char const *SERVER="things.ubidots.com";
const int HTTPPORT= 443;
char const *AGENT="Arduino MKR Wifi 1010";
char const *HTTP_VERSION = " HTTP/1.1\r\n";
char const *VERSION ="1.0";
char const *PATH= "/api/v1.6/devices/";
char ssid[] = "PUT_YOUR_WIFI_SSID_HERE";
char pass[] = "PUT_YOUR_WIFI_PASSWORD_HERE";

int status = WL_IDLE_STATUS;

WiFiSSLClient client;

/********************************
 * Auxiliar Functions
 *******************************/

void printWiFiStatus() {

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your board's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

void getFromUbidots(char* response) {

  /* Connecting the client */
  client.connect(SERVER, HTTPPORT);

  if (client.connected()) {
    /* Builds the request GET - Please reference this link to know all the request's structures https://ubidots.com/docs/api/ */

    client.print(F("GET "));
    client.print(PATH);    
    client.print(DEVICE_LABEL);
    client.print(F("/"));
    client.print(VARIABLE_LABEL);
    client.print(F("/values/?page_size=2/"));
    client.print(HTTP_VERSION);
    client.print(F("Host: "));
    client.print(SERVER);
    client.print(F("\r\n"));
    client.print(F("User-Agent: "));
    client.print(AGENT);
    client.print(F("/"));
    client.print(VERSION);
    client.print(F("\r\n"));
    client.print(F("X-Auth-Token: "));
    client.print(TOKEN);
    client.print(F("\r\n"));
    client.print(F("Connection: close\r\n"));
    client.print(F("Content-Type: application/json\r\n\r\n"));

    Serial.println(F("Making request to Ubidots:\n"));
    Serial.print(F("GET "));
    Serial.print(PATH);    
    Serial.print(DEVICE_LABEL);
    Serial.print(F("/"));
    Serial.print(VARIABLE_LABEL);
    Serial.print(F("/values/?page_size=2/"));
    Serial.print(HTTP_VERSION);
    Serial.print(F("Host: "));
    Serial.print(SERVER);
    Serial.print(F("\r\n"));
    Serial.print(F("User-Agent: "));
    Serial.print(AGENT);
    Serial.print(F("\r\n"));
    Serial.print(F("X-Auth-Token: "));
    Serial.print(TOKEN);
    Serial.print(F("\r\n"));
    Serial.print("Content-Type: application/json\r\n\r\n");

    waitServer();
    getResponseServer(response);
  }

  else {
    Serial.println("Connection Failed ubidots - Try Again");
    }
}

void waitServer() {

  int timeout = 0;
  while (!client.available() && timeout < 5000) {
    timeout++;
    delay(1);
    if (timeout >= 5000) {
      Serial.println(F("Error, max timeout reached"));
      break;
    }
  }
}

void getResponseServer(char* response) {

    /* Reads the response from the server */
    int i = 0;
    sprintf(response, "");
    if (client.available() > 0) {
      while (client.available()) {
        char c = client.read();
        //Serial.print(c); // Uncomment this line to visualize the response on the Serial Monitor
        response[i++] = c;
        if (i >= 699){
          break;
        }
      }
    }
    for (int j = i; j < strlen(response) - 1; j++) {
      response[j++] = '\0';
    }
    /* Disconnecting the client */
    client.stop();
}

char* parseUbiResponse(char* data, int dstSize=700){

  // parses the answer
  char dst[dstSize];
  int i = 0;
  int k = 0;
  for (i = 0; i < strlen(data); i++) {
    if (data[i] == '[') {  // First character
      while (data[k] != ']') {
        dst[k] = data[i];
        k++;
        i++;
        if (data[i] == ']') {
          dst[k] = data[i];
          break;
        }
      }
    }
  }

  // Fills char with null values
  for (int j = k + 1; j < dstSize;) {
    dst[j] = '\0';
    j++;
  }
  /*Serial.println("dst:");
  Serial.println(dst);*/
  return dst;
}

/********************************
 * Main Functions
 *******************************/

void setup() {

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(10000);
  }

  Serial.println("Connected to wifi");
  printWiFiStatus();
}

void loop(){

  if (client.connect(SERVER, HTTPPORT)) {
    /* Calls the Ubidots Function */
    char* response = (char *) malloc(sizeof(char) * 700);
    sprintf(response, "");
    getFromUbidots(response);

    // Memory space to store the request result
    char* results = parseUbiResponse(response);
    Serial.println("results:");
    Serial.println(results);
    Serial.println();
    free(response);
  }

  else {

    Serial.println("Could not connect to cloud");
    Serial.println("Attemping again in 5 seconds ....");

  }

  delay(5000);
 }


After pasting the code, verify that you do not have any errors and upload the code to the device. Once the code is successfully uploaded, open the serial monitor to see the response of the server and obtain the last values of the variable. You should get a response similar to the one below.

4.Summary

With this simple tutorial you are able to POST & GET data to/from Ubidots, now it's 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?