Skip to main content
All CollectionsConnect your Devices
Connect the Arduino Nano 33 IoT with Ubidots over HTTP
Connect the Arduino Nano 33 IoT with Ubidots over HTTP

Learn how to connect the Arduino Nano 33 IoT to Ubidots Application Development Platform over HTTP.

Sergio M avatar
Written by Sergio M
Updated over 3 months ago

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

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";

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,maximum 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:

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

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

/********************************
 * Constants and objects
 *******************************/
#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 = "PUT_YOUR_WIFI_SSID_HERE";

char const *SERVER="industrial.api.ubidots.com";
//Replace the line above this if you are an Educational user char const *SERVER="industrial.api.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("/lv"));
    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("/lv"));
    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();
}

float parseUbiResponse(char* data, int dstSize=700){
  float error_value = -3.4028235E+8;
  char parsed[20];
  char dst[20];
  int len = strlen(data);  // Length of the answer char array from the server

  for (int i = 0; i < len - 2; i++) {
    if ((data[i] == '\r') && (data[i + 1] == '\n') && (data[i + 2] == '\r') && (data[i + 3] == '\n')) {
      strncpy(parsed, data + i + 4, 20);  // Copies the result to the parsed
      parsed[20] = '\0';
      break;
    }
  }

  /* Extracts the the value */
  uint8_t index = 0;

  // Creates pointers to split the value
  char* pch = strchr(parsed, '\n');
  if (pch == NULL) {
    return error_value;
  }

  char* pch2 = strchr(pch + 1, '\n');

  if (pch2 == NULL) {
    return error_value;
  }

  index = (int)(pch2 - pch - 1);

  sprintf(dst, "%s", pch);
  dst[strlen(dst) - 1] = '\0';

  float result = atof(dst);
  return result;
}

/********************************
 * 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
    float results = parseUbiResponse(response);
    Serial.print("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?