#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include "DHTesp.h"
#include "ThingsBoard.h"
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
#define TOKEN "sBGU1mlUTo9ypc0XKPMu" //GANTI SESUAI AKUN TB ANDA
#define THINGSBOARD_SERVER "thingsboard.cloud" // FIXED
const int DHT_PIN = 15;
DHTesp dhtSensor;
void InitWiFi()
{
Serial.println("Connecting to AP ...");
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println("Connected to AP");
}
void reconnect()
{
status = WiFi.status();
if ( status != WL_CONNECTED)
{
WiFi.begin("Wokwi-GUEST", "");
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
Serial.println("Connected to AP");
}
}
void setup()
{
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
InitWiFi();
}
void loop()
{
// put your main code here, to run repeatedly:
delay(1000); // this speeds up the simulation
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);
Serial.print("Suhu : ");
Serial.print(data.temperature);
Serial.print(" Kelembaban : ");
Serial.println(data.humidity);
checkSuhu();
tb.loop();
}
void checkSuhu()
{
TempAndHumidity data = dhtSensor.getTempAndHumidity();
}