#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#endif
#include <WiFi.h>
#include "DHTesp.h"
#include "ThingsBoard.h"
#define CURRENT_FIRMWARE_TITLE "TEST"
#define CURRENT_FIRMWARE_VERSION "1.0.0"
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
const int DHT_PIN = 15;
const int PIN_PIR = 27;
DHTesp dhtSensor;
#define TOKEN "MqP0o0VreSZCCMa5U54A"
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define SERIAL_DEBUG_BAUD 115200
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
void InitWiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect(){
status = WiFi.status();
if (status !=WL_CONNECTED){
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() !=WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
void setup(){
pinMode(PIN_PIR, INPUT_PULLUP);
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop(){
int pinSensor = digitalRead(PIN_PIR);
if(pinSensor ==1){
Serial.println("GERAKAN TERDETEKSI");
delay(300);
}
delay(1000);
if (WiFi.status() !=WL_CONNECTED){
reconnect();
}
if(!tb.connected()){
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print("with token ");
Serial.println(TOKEN);
if (!tb.connect(THINGSBOARD_SERVER, TOKEN)){
Serial.println("Failed to connect");
return;
}
}
Serial.println("Sending data...");
TempAndHumidity data = dhtSensor.getTempAndHumidity();
tb.sendTelemetryInt("temperature", data.temperature);
tb.sendTelemetryFloat("humidity", data.humidity);
tb.sendTelemetryInt("pinSensor", PIN_PIR);
Serial.print("suhu : ");
Serial.print(data.temperature);
Serial.print("kelembapan : ");
Serial.println(data.humidity);
tb.loop();
tb.loop();
}