#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>
String wifiSSID = "Wokwi-GUEST";
String wifiPassword = "";
String tbHost = "demo.thingsboard.io";
String tbToken = "LwYiG2iVKxRDMP4E9bXA";
void connectWifi() {
WiFi.begin(wifiSSID.c_str(), wifiPassword.c_str());
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void sendDataToThingsBoard(String motion) {
HTTPClient http;
String url = "http://" + tbHost + "/api/v1/" + tbToken + "/telemetry";
String payload = "{\"motion\":" + motion + "}";
http.begin(url);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0) {
Serial.println("Data sent to ThingsBoard successfully");
} else {
Serial.println("Error sending data to ThingsBoard");
}
http.end();
}
const int led =5; // led is connected to esp32 D5
const int pir =19; // pir D is connected to D19
int pirstate = LOW; // assuming no motion
int value = 0; // to read pin status
void setup(){
pinMode(led,OUTPUT); // led as output
pinMode(pir,INPUT);
Serial.begin(115200);
connectWifi();
}
void loop(){
const int IP=digitalRead(pir);
String IPmotion = String(IP);
Serial.println(IP);
delay(100);
if(IP==1){
digitalWrite(led,HIGH);
delay(1000);
}
else{
digitalWrite(led,LOW);
delay(1000);
}
sendDataToThingsBoard(IPmotion);
}