#include <WiFi.h>
#include <ThingsBoard.h>
#include <Arduino_MQTT_Client.h>
#define pir 33
#define ssid "Wokwi-GUEST"
#define pass ""
#define server "demo.thingsboard.io"
#define token "lhkn2vsfznkk0zp2ba0j"
constexpr uint16_t MAX_MESSAGE_SIZE = 256U;
WiFiClient espClient;
Arduino_MQTT_Client mqttClient(espClient);
ThingsBoard tb(mqttClient, MAX_MESSAGE_SIZE);
void connectToThingsBoard() {
if (!tb.connected()) {
Serial.println("Connecting to ThingsBoard server");
if (!tb.connect(server, token)) {
Serial.println("Failed to connect to ThingsBoard");
} else {
Serial.println("Connected to ThingsBoard");
}
}
}
void setupWiFi(){
delay(100);
Serial.print("\nConnecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.print("\nConnected to ");
Serial.println(ssid);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
setupWiFi();
}
void loop() {
int mot = digitalRead(pir);
Serial.println(mot);
// put your main code here, to run repeatedly:
delay(1000); // this speeds up the simulation
if (!tb.connected()) {
connectToThingsBoard();
}
tb.sendTelemetryData("MOT", mot);
tb.loop();
}