The Nano 33 IoT is a significant improvement of the Nano Every, allowing you to extend everyday functionalities, such as to monitor and control sensors and actuators remotely without needing an additional WiFi module to establish a communication with an IoT platform.

Refer to the Arduino's official documentation and check out all the board's specifications.

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

1. Download the latest version of the Arduino IDE if you do not have it already.

2. In the Arduino IDE, click on Sketch -> Include Library -> Manage Libraries

3. 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 the library and matches it with the one installed on the board or the shield.
  • WiFiNINAFirmwareUpdater: The sketch that must be loaded to allow the firmware and certificates any updates through the integrated plugin of Arduino Software (IDE) rel. 1.8.5 or later.

4.  Go to Boards Manager from Tools > Board > Boards Manager and search for “Arduino SAMD”. Install the one that have listed the “Arduino Nano 33 IoT”.

IMPORTANT NOTE: Make sure to install the latest version of the boards. Once the board is installed, you ought to be able to visualize it in the board's manager as "Arduino NANO 33 IoT". If the board is not listed, go back to the Boards Manager and click "Update".

2.Sending (POST) Data to Ubidots 

1. 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.

2. After making sure your board is successfully connected with the computer, 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 where is indicated.

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

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

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

#define DEVICE_LABEL "arduino-nano-33"
#define TOKEN "PUT_YOUR_TOKEN_HERE"

char const * VARIABLE_LABEL_1 = "sensor";
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 Nano 33 IoT";
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 = "xxxxxxx"; // Put here your SSID name
char const * SSID_PASS = "xxxxxxx"; // 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);

3. 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; select it to verify your code.

 

4. UPLOAD the code into your “Arduino Nano 33 IoT”. To do this, choose the "right-arrow" icon besides the "check mark" icon. 

5. 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 sent correctly.

6. Refer to the Device section of your Ubidots account and see how a new device was automatically created.

The variable called "sensor" is posting the readings taken from the analog input of the Arduino board.

3.Retrieve (GET) Data from Ubidots

1. 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 desired to obtain where is indicated:

#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 Nano 33 IoT";
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);
 }

2. 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. 

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?