#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
const int relay = 25;
// WiFi
const char* ssid = "Wokwi-GUEST";
const char* password = "";
// MQTT Broker
const char* mqtt_server = "broker.emqx.io";
const int mqtt_port = 1883;
const char* mqtt_username = "emqx";
const char* mqtt_password = "public";
const char* temperatureTopic = "emqx/suhuaff";
const char* humidityTopic = "emqx/humdaff";
const char* ledTopic = "emqx/ledaff";
#define DHTPIN 15
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
long now = millis();
long lastMeasure = 0;
WiFiClient espClient;
PubSubClient client(espClient);
void callback(char *topic, byte *payload, unsigned int length){
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char) payload[i]);
messageTemp += (char)payload[i];
}
Serial.println();
}
void wifiConn () {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
//delay(500);
delay(1000);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the Wi-Fi network");
//Connecting to a mqtt server
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
//Connecting Proccess
while (!client.connected()) {
String client_id = "esp32-client-";
client_id += String(WiFi.macAddress());
Serial.printf("The client %s connects to the public MQTT broker\n", client_id.c_str());
//WiFi Condition
if (client.connect(client_id.c_str(), mqtt_username, mqtt_password)) {
Serial.println("Public EMQX MQTT broker connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
}
void setup(){
// Set software serial baud to 9600;
Serial.begin(9600);
pinMode(relay, OUTPUT); // << tambahkan ini
// Calling wifiConn function
wifiConn(); // Ini cara memanggil fungsi wifi Conn
// Start and initialize the dht object
dht.begin();
}
void loop() {
// Repeat sending sensor data
client.loop();
/* Fungsi Millis untuk menjalankan waktu internal setiap milli seconds
pada ESP32 secara independent..
ketika millis di baca maka millis akan terus
menghitung waktu walau pun ESP32 nya sedang menjalan kan
program yang lain
*/
now = millis();
// Scanning Process
if(now - lastMeasure > 2000){
lastMeasure = now;
float h = dht.readHumidity();
float t = dht.readTemperature();
float hic = dht.computeHeatIndex(t, h, false);
// Check if any reads failed and exit early (to try again)
if(isnan(h) || isnan(t)){
Serial.println("Failed to read from DHT sensor!");
return;
}else{
// Cliet Publish
client.publish(humidityTopic, String(h).c_str());
client.publish(temperatureTopic, String(t).c_str());
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.println(" *C ");
boolean z;
if(t > 28 && h <= 60) {
digitalWrite(relay, LOW);
int z = 1;
Serial.println("Relay OFF | Suhu Dingin");
client.publish(ledTopic, String(z).c_str());
}else if(t > 23 && t <= 28){
digitalWrite(relay, LOW);
Serial.println("Relay OFF | Suhu Dingin ");
}else if(t < 23 && h > 60){
digitalWrite(relay, HIGH);
Serial.println("Relay ON | Suhu Panas ");
}
/*
boolean z; // << ini tambahkan
if((t > 28)&&(h > 60)){
digitalWrite(relay, HIGH);
int z = 1;
Serial.print("Relay: ON ");
client.publish(ledTopic, String(z).c_str());
}else{
digitalWrite(relay, LOW);
int z = 0;
Serial.print("Relay: OFF ");
client.publish(ledTopic, String(z).c_str());
*/
}
}
}