-
Notifications
You must be signed in to change notification settings - Fork 132
Open
Description
Setup
- Adafruit ESP32 board
- PlatformIO with Arduino framework
- Thingsboard Cloud with X.509 certificate chain provisioning
Issue
When I call tb.sendTelemetryData, my tb connection crashes / resets, and I am not able to send data data to Thingsboard
Confirmed working
- Wifi (can see connected on local network)
- Cert chain: when I call tb.connect(), the device is provisioned correctly on Thingsboard
Sample logs
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
Connected to ThingsBoard!
Loop start
192.168.4.83
Sending data to ThingsBoard...
[TB] Sending data to server over topic (v1/devices/me/attributes) with data ({"temperature":25})
Result: 1
Loop start
192.168.4.83
Reconnecting to ThingsBoard...
Reconnected to ThingsBoard!
Sending data to ThingsBoard...
[TB] Sending data to server over topic (v1/devices/me/attributes) with data ({"temperature":25})
main.cpp
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#define THINGSBOARD_ENABLE_DEBUG 1
#include <ThingsBoard.h>
#include <Arduino_MQTT_Client.h>
#include "secrets.h"
const int LED_PIN = 13;
// MQTT broker details
constexpr char MQTT_SERVER[] = "mqtt.thingsboard.cloud";
constexpr int MQTT_PORT = 8883;
constexpr char THINGSBOARD_TOKEN[] = "provision";
constexpr uint16_t MAX_MESSAGE_SEND_SIZE = 256U;
constexpr uint16_t MAX_MESSAGE_RECEIVE_SIZE = 256U;
constexpr char TEMPERATURE_KEY[] = "temperature";
WiFiClientSecure wifiClient;
Arduino_MQTT_Client mqttClient(wifiClient);
ThingsBoard tb(mqttClient, MAX_MESSAGE_RECEIVE_SIZE, MAX_MESSAGE_SEND_SIZE);
void setup()
{
// Configure LED pin as output
pinMode(LED_PIN, OUTPUT);
// Initialize serial communication
Serial.begin(9600);
// Connect to WiFi
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
// Set up certificates for secure connection (found in secrets.h)
wifiClient.setCACert(CA_CERT);
wifiClient.setCertificate(CLIENT_CERT);
wifiClient.setPrivateKey(CLIENT_KEY);
// Connect to ThingsBoard
Serial.println("Connecting to ThingsBoard...");
if (!tb.connect(MQTT_SERVER, THINGSBOARD_TOKEN, MQTT_PORT))
{
Serial.println("Failed to connect to ThingsBoard");
}
else
{
Serial.println("Connected to ThingsBoard!");
}
}
void loop()
{
Serial.println("Loop start");
digitalWrite(LED_PIN, HIGH); // Turn LED on
delay(200);
digitalWrite(LED_PIN, LOW); // Turn LED off
delay(200);
Serial.println(WiFi.localIP());
// Keep the MQTT connection alive
if (!tb.connected())
{
Serial.println("Reconnecting to ThingsBoard...");
if (tb.connect(MQTT_SERVER, THINGSBOARD_TOKEN, MQTT_PORT))
{
Serial.println("Reconnected to ThingsBoard!");
}
else
{
Serial.println("Failed to reconnect");
delay(2000);
}
}
// Send some data periodically
if (tb.connected())
{
Serial.println("Sending data to ThingsBoard...");
bool result = tb.sendTelemetryData(TEMPERATURE_KEY, 25.0);
Serial.println("Result: " + String(result));
}
tb.loop();
}
Metadata
Metadata
Assignees
Labels
No labels