Ubidots Community

Connection of Arduino Nano 33

/********************************

  • Libraries included

*******************************/

#include <SPI.h>

#include <WiFiNINA.h>

#include <avr/dtostrf.h>

/********************************

  • Constants and objects

*******************************/

#define DEVICE_LABEL “arduino-nano-33”

#define TOKEN “BBFF-EGuBSGUOgag02kB4gv2VBfo0P7G073”

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 = “Liew’s iPhone”; // Put here your SSID name

char const * SSID_PASS = “1234567890”; // 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);

}

This is the code I am using. However, I am not using industrial version of ubidots. Instead, I am using STEM version of ubidots. Is there anything that I need to change?

This is the output results.

16:05:26.974 → Attempting to connect to SSID: Liew’s iPhone