I am trying to set up ThingsBoard in Arduino IDE.
Is the issue that my device doesn’t have an ESP32 chip? It uses a different wifi chip. I tried using the ThingsBoard arduino SDK library, and it wouldn’t work on my device, so I tried to do it the way shown below, only using the ArduinoHttpClient library. I don’t understand why this doesn’t work. Is there just no way to connect a device that doesn’t have an ESP32 to ThingsBoard? How does it know that my device is not an ESP32 when it receives the HTTP request? I also don’t really know how to write HTTP requests so I don’t know if the code is just wrong or if this actually won’t work with the hardware I’ve got.
#include <WiFi101.h>
#define DEBUG_HTTP
#include <ArduinoHttpClient.h>
#include <SPI.h>
// WiFi network info
const char* ssid = "ssid";
const char* password = "password";
const char* token = "token";
// ThingsBoard server info
const char* thingsboardServer = "thingsboard.cloud";
const uint16_t port = 80U;
WiFiClient client;
void setup() {
Serial.begin(115200);
//connect to wifi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("WiFi connected");
}
void loop() {
// Send POST request to ThingsBoard
if (sendDataToThingsBoard()) {
Serial.println("Data sent successfully");
} else {
Serial.println("Failed to send data");
}
delay(5000);
}
bool sendDataToThingsBoard() {
Serial.println(client.connect(thingsboardServer, port));
if (client.connect(thingsboardServer, port)) {
Serial.println("Connected to ThingsBoard");
String payload = "{"temperature":25}";
String url = "/api/v1/";
url += token;
url += "/telemetry";
client.print("POST ");
client.print(url);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(thingsboardServer);
client.println("Content-Type: application/json");
client.print("Content-Length: ");
client.println(payload.length());
client.println();
client.println(payload);
delay(1000);
client.stop();
return true;
} else {
Serial.println("Connection to ThingsBoard failed");
return false;
}
}
It won’t connect to ThingBoard. When I’ve tried printing out the response/status thing that it’s supposed to receive to get an error code (like 401 or whatever) it just says -3 for some reason? I don’t understand how to do this. The device connects to wifi just fine, and I’ve used it successfully with ThingSpeak no problem. I’ve even printed out the wifi connection status again at a bunch of different points in the code and it was fine.
lickingBatteries is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.