// ----Library------
#include "DHTesp.h"
#include <WiFi.h>
//------------------
//-----Variabel yang menyatakan sebagai client-----
WiFiClient client;
//-------------------------------------------------
//-----deklarasi PIN DHT22-------------
const int DHT_PIN = 15;
//-------------------------------------
//-----deklarasi PIN ultrasonik--------
const int trig = 2;
const int echo = 4;
//-------------------------------------
//-----deklarasi PIN aktuator----------
const int ledM = 14;
const int ledK = 27;
const int ledH = 26;
const int ledP = 12;
const int ledB = 13;
//-------------------------------------
//------deklarasi variable DHT22-------
DHTesp dhtSensor;
//-------------------------------------
void setup() {
//--------baudrate---------
Serial.begin(115200);
//-------------------------
WiFi.disconnect();
//--------Connecting Access Point------
WiFi.begin("Wokwi-GUEST", ""); //keterangan ("SSID","Password")
//-------------------------------------
//--------Status WiFi------------------
while (WiFi.status() != WL_CONNECTED){
delay(300);
Serial.print(".");
}
//-------------------------------------
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
//---------Menampilkan IP Address yang didapatkan ESP32------
Serial.println(WiFi.localIP());
//-----------------------------------------------------------
//---------Inisialisasi input dan output-----------
pinMode(echo, INPUT);
pinMode(trig, OUTPUT);
pinMode(ledM, OUTPUT);
pinMode(ledK, OUTPUT);
pinMode(ledH, OUTPUT);
pinMode(ledP, OUTPUT);
pinMode(ledB, OUTPUT);
//-------------------------------------------------
//--------inisialisasi dan set up DHT22------------
dhtSensor.setup(DHT_PIN, DHTesp::DHT22); //keterangan (PIN DHT22, Setup Library)
//-------------------------------------------------
}
//----------prosedur rumus nilai jarak---------------
float ultrasonik(){
digitalWrite(trig, LOW); //keterangan Trigger off pancaran ultrasonik
delayMicroseconds(2);
digitalWrite(trig, HIGH); //keterangan Trigger on pancaran ultrasonik
delayMicroseconds(10);
digitalWrite(trig, LOW); //keterangan Trigger off pancaran ultrasonik
//--------hasil pembacaan ultrasonik---------
int durasiPantulan = pulseIn(echo, HIGH);
return durasiPantulan * 0.034 / 2;
//-------------------------------------------
}
void loop() {
//---------membaca nilai suhu dan kelembaban-------------------
TempAndHumidity data = dhtSensor.getTempAndHumidity(); //keterangan TempAndHumidity (default library) data (variabel baru)
//-------------------------------------------------------------
Serial.println("Temp: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.print("Jarak= ");
Serial.println(ultrasonik());
Serial.println("---");
float jarakPer = ultrasonik();
if(jarakPer>150){
digitalWrite(ledH, HIGH);
digitalWrite(ledK, LOW);
digitalWrite(ledM, LOW);
// digitalWrite(ledP, LOW);
// digitalWrite(ledB, LOW);
}
else if(jarakPer<=150 && jarakPer>100){
digitalWrite(ledK, HIGH);
digitalWrite(ledH, LOW);
digitalWrite(ledM, LOW);
// digitalWrite(ledP, LOW);
// digitalWrite(ledB, LOW);
}
else if(jarakPer<=100){
digitalWrite(ledM, HIGH);
digitalWrite(ledK, LOW);
digitalWrite(ledH, LOW);
// digitalWrite(ledP, LOW);
// digitalWrite(ledB, LOW);
}
if(data.temperature<25){
digitalWrite(ledP, HIGH);
digitalWrite(ledB, LOW);
// digitalWrite(ledM, LOW);
// digitalWrite(ledK, LOW);
// digitalWrite(ledH, LOW);
}
else if (data.temperature>29){
digitalWrite(ledP, LOW);
digitalWrite(ledB, HIGH);
// digitalWrite(ledM, LOW);
// digitalWrite(ledK, LOW);
// digitalWrite(ledH, LOW);
}
delay(1000);
}