#include <DHTesp.h> // DHT for ESP32 library
#include <WiFi.h> // WiFi control for ESP32
#include "ThingsBoard.h" //thingsboard library
#include <NewPing.h> //utrasonic library Newping
#define COUNT_OF(x) ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
#define WIFI_AP_NAME "Wokwi-GUEST"
#define WIFI_PASSWORD ""
#define THINGSBOARD_SERVER "thingsboard.cloud"
#define THINGSBOARD_ACCESSTOKEN "dSzxaKXa1llOKnnP2vE3"
#define SERIAL_DEBUG_BAUD 115200
WiFiClient espClient;
ThingsBoard tb(espClient);
int status = WL_IDLE_STATUS;
//sensor dht 22 real menggunakam ds18b20
DHTesp dht;
#define DHT_PIN 15
float temperature = 0;
// relay pompa pengisian dan pembuangan
#define pumpIn 4 // led orange simulasi pompa pengisian
#define pumpOut 5 //led ungu simulasi pompa pebuangan
//sensor ultrasonic JSN-SR04t
#define Trig 14
#define Echo 12
#define MAX_DISTANCE 400
float ketinggian_air ;
NewPing sonar = NewPing(Trig, Echo, MAX_DISTANCE);
// pin led indikator suhu air
#define ledSuhuPanas 26 //led merah
#define ledSuhuNormal 33 //led hijau
#define ledSuhuDingin 25 //led biru
#define ledwifi 21 //led kuning
void InitWiFi() {
Serial.println("Connecting to AP ...");
// attempt to connect to WiFi network
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//led kuning indikator koneksi wifi
digitalWrite(ledwifi, HIGH);
}
Serial.println("Connected to AP");
}
void reconnect() {
// Loop until we're reconnected
status = WiFi.status();
if ( status != WL_CONNECTED) {
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
//led kuning indikator koneksi wifi
digitalWrite(ledwifi, LOW);
delay(200);
digitalWrite(ledwifi, HIGH);
delay(200);
}
Serial.println("Connected to AP");
}
}
// Main application loop delay
int quant = 20;
// Period of sending a temperature/humidity data.
int send_delay = 100;
// Time passed after temperature/humidity data was sent, milliseconds.
int send_passed = 0;
//baca jarak sensor ultrasonic
void baca_jarak() {
int Cm = sonar.ping_cm();
ketinggian_air = 400 - Cm; //sensor di letakan 50cm dari permukaan air (titik 0 cm)
}
void setup() {
Serial.begin(SERIAL_DEBUG_BAUD);
WiFi.begin(WIFI_AP_NAME, WIFI_PASSWORD);
InitWiFi();
pinMode(ledwifi, OUTPUT);
pinMode(Trig, OUTPUT);
pinMode(Echo, INPUT);
pinMode(pumpIn, OUTPUT);
pinMode(pumpOut, OUTPUT);
pinMode(ledSuhuNormal, OUTPUT);
pinMode(ledSuhuDingin, OUTPUT);
pinMode(ledSuhuPanas, OUTPUT);
dht.setup(DHT_PIN, DHTesp::DHT22);
}
void loop() {
delay(quant);
baca_jarak();
send_passed += quant;
// Check if it is a time to send DHT22 temperature and humidity
if (send_passed > send_delay) {
Serial.println();
Serial.print("Sending data... ");
TempAndHumidity lastValues = dht.getTempAndHumidity();
if (isnan(lastValues.humidity) || isnan(lastValues.temperature)) {
Serial.println("Failed to read from DHT sensor!");
} else {
temperature = lastValues.temperature;
Serial.print("temperature: ");
Serial.print(temperature);
Serial.print(" Ketinggian Air: ");
Serial.print(ketinggian_air);
Serial.println(" CM");
}
send_passed = 0;
}
//mengirim data suhu dan ketinggian air ke thingsboard
tb.sendTelemetryInt("temperature", temperature);
tb.sendTelemetryInt("ketinggian_air", ketinggian_air);
//mengirim lokasi tambak ke thingsboard
tb.sendTelemetryFloat("lat", -6.259099);
tb.sendTelemetryFloat("long", 107.735073);
//mengirim data indikator led ke thingsboard
tb.sendTelemetryInt("suhunormal", digitalRead(ledSuhuNormal));
tb.sendTelemetryInt("suhupanas", digitalRead(ledSuhuPanas));
tb.sendTelemetryInt("suhudingin", digitalRead(ledSuhuDingin));
tb.sendTelemetryInt("pompaisi", digitalRead(pumpIn));
tb.sendTelemetryInt("pompabuang", digitalRead(pumpOut));
// Reconnect to WiFi, if needed
if (WiFi.status() != WL_CONNECTED) {
reconnect();
return;
}
// Reconnect to ThingsBoard, if needed
if (!tb.connected()) {
// Connect to the ThingsBoard
Serial.print("Connecting to: ");
Serial.print(THINGSBOARD_SERVER);
Serial.print(" with token ");
Serial.println(THINGSBOARD_ACCESSTOKEN);
if (!tb.connect(THINGSBOARD_SERVER, THINGSBOARD_ACCESSTOKEN)) {
Serial.println("Failed to connect");
return;
}
}
//monitoring suhu air tambak dan indikator suhu air berdasarkan warna led
/*pengaturan pompa isi dan buang untuk controlling suhu air tambak
pompa isi akan meyala saat suhu lebih besar dari 34 derajat
led merah menyala*/
if (temperature > 34) {
digitalWrite(ledSuhuPanas, HIGH);
digitalWrite(ledSuhuNormal, LOW);
digitalWrite(ledSuhuDingin, LOW);
digitalWrite(pumpIn, HIGH);
Serial.print("temperature: ");
Serial.print(temperature);
Serial.print(" ketinggian_air :");
Serial.print(ketinggian_air);
Serial.println(" CM");
Serial.println(" pompa pengisian nyala");
}
/*pengaturan pompa isi dan buang untuk controlling suhu air tambak
pompa pembuangan air akan meyala saat suhu lebih kecil dari 26 derajat
led biru menyala*/
else if (temperature < 26 ) {
digitalWrite(ledSuhuPanas, LOW);
digitalWrite(ledSuhuNormal, LOW);
digitalWrite(ledSuhuDingin, HIGH);
digitalWrite(pumpOut, HIGH);
Serial.print("temperature: ");
Serial.print(temperature);
Serial.print(" ketinggian_air :");
Serial.print(ketinggian_air);
Serial.println(" CM");
Serial.println(" pompa pembuangan nyala");
}
/*pengaturan pompa isi dan buang untuk controlling suhu air tambak
pompa pengisian dan pembuangan akan mati saat suhu berada di atas 26 derajat dan di bawah 34 derajat
led hijau menyala*/
else {
digitalWrite(ledSuhuPanas, LOW);
digitalWrite(ledSuhuNormal, HIGH);
digitalWrite(ledSuhuDingin, LOW);
Serial.println(" pompa pengisian mati");
digitalWrite(pumpIn, LOW);
Serial.println(" pompa pembuangan mati");
digitalWrite(pumpOut, LOW);
}
tb.loop();
tb.loop();
}