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

Hello @zalexysuckas010 ,

Thank you for sharing your code and the details of your issue. I’d be happy to help you troubleshoot the connection problem you’re experiencing.

First, it’s important to note that Ubidots STEM and Ubidots Industrial might have different number of devices included, but the URL to make requests remains the same.

Here are some steps to help you troubleshoot your connection issue:

  1. Check Your Wi-Fi Credentials: Ensure that your SSID and password (SSID_NAME and SSID_PASS) are correctly entered. Double-check for any special characters or spaces in your SSID and password. Also, ensure that the Arduino Nano 33 IoT is in range of your Wi-Fi network.
  2. Verify Your Ubidots Token: Make sure your Ubidots token (TOKEN) is correct and matches the token associated with your STEM account.
  3. Check the Ubidots Documentation: There is an article on Ubidots help center that may help you check the code and steps you are following to connect the device. Connect the Arduino Nano 33 IoT with Ubidots over HTTP | Ubidots Help Center
  4. Debugging Output: Your code appears to include debugging output via the Serial Monitor. Ensure that you have the Serial Monitor open and set to the correct baud rate (9600) to see any error messages or debugging information…
  5. Update Libraries: Make sure you are using the latest version of the Arduino WiFiNINA library. You can update the library through the Arduino Library Manager.
  6. Endpoint and Headers: Double-check the Ubidots documentation for the correct endpoint, headers, and content type. Ensure that you are using the correct HTTPS port (443).
  7. Test with HTTP Requests: Consider trying a simple HTTP GET request to Ubidots using the WiFiSSLClient library to see if you can establish a basic connection before sending data.

Remember to make one change at a time and test after each change to isolate the issue. I hope these steps help you resolve your connectivity problem. If you encounter any specific error messages or issues during the troubleshooting process, please share them, and I can provide more targeted assistance.