/**
ESP32 + DHT22 Example for Wokwi
//by:gas
*/
#include "DHTesp.h"
#include "WiFi.h"
#include "ThingSpeak.h"
#define SECRET_SSID "Wokwi-GUEST" // replace with your WiFi network name
#define SECRET_PASS "" // replace with your WiFi password
#define SECRET_CH_ID 0000000 // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "XYZ" // replace XYZ with your channel write API Key
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password
int keyIndex = 0; // your network key Index number (needed only for WEP)
WiFiClient client;
unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
const int DHT_PIN = 15;
int led = 26;
int relay = 25;
int PIR = 27;
int state = 0;
int val = 0;
float t ;
float h ;
DHTesp dhtSensor;
void setup() {
Serial.begin(115200);
dhtSensor.setup(DHT_PIN, DHTesp::DHT22);
pinMode(led,OUTPUT);
pinMode(PIR,INPUT);
pinMode(relay, OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo native USB port only
}
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client); // Initialize ThingSpeak
}
void loop() {
TempAndHumidity data = dhtSensor.getTempAndHumidity();
Serial.println("Temperatur: " + String(data.temperature, 2) + "°C");
Serial.println("Humidity: " + String(data.humidity, 1) + "%");
Serial.println("---");
if (data.temperature>=26 || data.humidity >= 30){
digitalWrite(relay, HIGH);
Serial.println("Relay ON");
}
else{
digitalWrite(relay,LOW);
Serial.println("Relay OFF");
}
delay(500);
val = digitalRead(PIR);
if(val == HIGH){
digitalWrite(led, HIGH);
Serial.println("Makanan Habis");
}
else{
digitalWrite(led, LOW);
Serial.println("Makanan Tersedia");
}
delay(500);
}}}