#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include "ThingsBoard.h"
#include "DHTesp.h"
#include <LiquidCrystal_I2C.h>
#define CURRENT_VIRMWARE_TITLE "TEST"
#define CURRENT_VIRMWARE_VERSION "1.0.0"
#define WIFI_SSID "Wokwi_GUEST"
#define WIFI_PASSWORD ""
#define TOKEN "ZaenulAkbar21"
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define SERIAL_DEBUG_BAUD 115200
const int DHT_PIN = 15;
DHTesp dhtSensor;
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
LiquidCrystal_I2C lcd(0x27, 16, 2);
byte SimbolDerajat[] = {
B00110,
B01001,
B01001,
B00110,
B00000,
B00000,
B00000,
B00000,
};
void InitWiFi()
{
Serial.println("Connecting to AP...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay (500);
Serial.println(".");
}
}
void reconnect() {
status = WiFi.status();
if ( status !=WL_CONNECTED ) {
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay (500);
Serial.println(".");
}
Serial.println("Connected to AP");
}
}
void setup() {
lcd.init();
lcd.createChar(0, SimbolDerajat);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Sensor Suhu &");
lcd.setCursor(0, 1);
lcd.print("Kelembapan");
delay (4000);
lcd.clear();
Serial.begin(SERIAL_DEBUG_BAUD);
Serial.println();
InitWiFi();
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
delay (1000);
if (WiFi.status() != WL_CONNECTED) {
reconnect();
}
if (!tb.connected()) {
Serial.println("Connecting to:");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.print(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(" Kelembapan : ");
Serial.println(data.humidity);
lcd.setCursor(0, 0);
lcd.print("Suhu :" + String(data.temperature));
lcd.write(0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Kelembapan :" + String(data.humidity) + "%");
tb.loop();
tb.loop();
}