#define THINGER_SERIAL_DEBUG
#define _DISABLE_TLS_
#include <ThingerESP32.h>
#include <DHTesp.h>
// setting thinger.io device
#define IOT_SERVER "sridadibjn01"
#define IOT_USER "kokoadi"
#define IOT_PASS "b0j0n3g0r0"
// setting WiFi ESP32
#define SSID "Wokwi-GUEST"
#define password ""
// setting Ultrasonic
#define ECHO 13
#define TRIG 12
// setting lampu
#define LAMP 5
// setting DHT
#define DHT_PIN 15
// setting variabel hitung
unsigned long waktu = 0;
double jarak = 0;
long suhu = 0;
long kelembapan = 0;
// deklrasi hardware
ThingerESP32 thing(IOT_USER, IOT_SERVER, IOT_PASS);
DHTesp dht;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(LAMP, OUTPUT);
dht.setup(DHT_PIN, DHTesp::DHT22);
thing.add_wifi(SSID, password);
thing["Lampu"] << [](pson& in){
if(in.is_empty()){
in = (bool) digitalRead(LAMP);
} else {
digitalWrite(LAMP, in? HIGH:LOW);
}
};
thing["sensorGerak"] >> [](pson& out){
sensorUltra();
jarak = waktu / 58;
out["hitung"] = jarak;
};
thing["sensorKelembapan"] >> [](pson& out){
sensorDHTKelembapan();
if (isnan(kelembapan)) {
Serial.println("Sensor belum kebaca");
kelembapan = 0;
return;
}
out["kelembapan"] = (long) kelembapan;
};
thing["sensorSuhu"] >> [](pson& out){
sensorDHTSuhu();
if (isnan(suhu)) {
Serial.println("Sensor belum kebaca");
suhu = 0;
return;
}
out["suhu"] = (long) suhu;
};
thing["sensorSuhuKelembapan"] >> [](pson& out){
sensorDHT();
if (isnan(suhu) || isnan(kelembapan)) {
Serial.println("Sensor belum kebaca");
suhu = 0;
kelembapan = 0;
return;
}
out["kelembapan"] = (long) kelembapan;
out["suhu"] = (long) suhu;
};
}
void loop() {
delay(20);
thing.handle();
}
void sensorUltra(){
digitalWrite(TRIG, HIGH);
delayMicroseconds(100);
digitalWrite(TRIG, LOW);
// hitung waktu data
waktu = pulseIn(ECHO, HIGH);
Serial.println(waktu/58);
}
void sensorDHT(){
TempAndHumidity data = dht.getTempAndHumidity();
kelembapan = data.humidity;
suhu = data.temperature;
Serial.println(kelembapan);
Serial.println(suhu);
}
void sensorDHTKelembapan(){
TempAndHumidity data = dht.getTempAndHumidity();
kelembapan = data.humidity;
Serial.println(kelembapan);
}
void sensorDHTSuhu(){
TempAndHumidity data = dht.getTempAndHumidity();
suhu = data.temperature;
Serial.println(suhu);
}