Reputation: 1
I installed the community version of Thingsboard and created a device, then sent data using the PC terminal and received data, but when I used MQTT Explorer, I sent data and Thingsboard did not receive it. Not only that, when I tried the Esp32 project also got an error that could not connect to Thingsboard server.
#include <DHTesp.h>
#include <WiFi.h>
#include <ThingsBoard.h>
#include <Arduino_MQTT_Client.h>
#define pinDht 15
DHTesp dhtSensor;
#define WIFI_AP "Wokwi-GUEST"
#define WIFI_PASS ""
#define TB_SERVER "thingsboard.cloud"
#define TOKEN "zQSkY28E5bisV4LyAKLY"
constexpr uint16_t MAX_MESSAGE_SIZE = 128U;
WiFiClient espClient;
Arduino_MQTT_Client mqttClient(espClient);
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
void connectToWiFi() {
Serial.println("Connecting to WiFi...");
int attempts = 0;
while (WiFi.status() != WL_CONNECTED && attempts < 20) {
WiFi.begin(WIFI_AP, WIFI_PASS, 6);
delay(500);
Serial.print(".");
attempts++;
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("\nFailed to connect to WiFi.");
} else {
Serial.println("\nConnected to WiFi");
}
}
void connectToThingsBoard() {
if (!tb.connected()) {
Serial.println("Connecting to ThingsBoard server");
if (!tb.connect(TB_SERVER, TOKEN)) {
Serial.println("Failed to connect to ThingsBoard");
} else {
Serial.println("Connected to ThingsBoard");
}
}
}
void sendDataToThingsBoard(float temp, int hum) {
// Tạo đối tượng JSON
StaticJsonDocument<256> data;
data["temperature"] = temp;
data["humidity"] = hum;
// Gửi dữ liệu lên ThingsBoard (truyền cả đối tượng JSON và kích thước)
if (tb.sendTelemetryJson(data, measureJson(data))) {
Serial.println("Data sent successfully!");
} else {
Serial.println("Failed to send data!");
}
}
void setup() {
Serial.begin(115200);
dhtSensor.setup(pinDht,DHTesp::DHT22);
connectToWiFi();
connectToThingsBoard();
}
void loop() {
connectToWiFi();
TempAndHumidity data =dhtSensor.getTempAndHumidity();
float temp = data.temperature;
int hum = data.humidity;
Serial.println(temp);
Serial.println(hum);
if (!tb.connected()) {
connectToThingsBoard();
}
sendDataToThingsBoard(temp, hum);
delay(3000);
tb.loop();
}
My project link: https://wokwi.com/projects/423607708915256321
I'm sure I've copied the right acces token !
Upvotes: -3
Views: 37