//Lahan1
#include <WiFi.h>
#include "ThingsBoard.h"
#include "DHTesp.h"
//WiFi Setup
#define WIFI_SSID "Wokwi-GUEST"
#define WIFI_PASSWORD ""
//ThingsBoard Setup
#define CURRENT_FIRMWARE_TITLE "TEST"
#define CURRENT_FIRMWARE_VERSION "1.0.0"
#define TOKEN "KH9oyDX195RAyrG8b8dN"
#define THINGSBOARD_SERVER "thingsboard.cloud"
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
//DHT Setup
const int DHT_PIN = 15;
DHTesp dhtSensor;
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() {
Serial.begin(115200);
Serial.println();
InitWiFi();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
delay(1000);
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
if (!tb.connected()) {
// Connect to the ThingsBoard
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.sendTelemetryFloat("Suhu", data.temperature);
tb.sendTelemetryFloat("Kelembaban", data.humidity);
Serial.print("Suhu : ");
Serial.print(data.temperature);
Serial.print(" Kelembaban : ");
Serial.println(data.humidity);
tb.loop();
}