#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DHT_PIN 15
#define DHT_TYPE DHT22
#define LED 12
#define RELAY 4
const char* ssid = "Wokwi-GUEST";
const char* password = "";
#define client_server "test.mosquitto.org"
const int port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHT_PIN, DHT_TYPE);
bool led_state = false, led_state_platform = false, relay_state = false, relay_state_platform = false;
String led_mode = "automatic", relay_mode = "automatic";
LiquidCrystal_I2C oled(0x27, 16, 2);
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str())) {
Serial.println("Connected");
client.subscribe("thariq/prog/led");
client.subscribe("thariq/prog/relay");
client.subscribe("thariq/prog/mode/led");
client.subscribe("thariq/prog/mode/relay");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Koneksi WiFi...");
}
Serial.println("Terhubung ke WiFi");
dht.begin();
pinMode(LED, OUTPUT);
pinMode(RELAY, OUTPUT);
oled.init();
oled.backlight();
oled.setCursor(0,0);
oled.print("Temperature &");
oled.setCursor(8,1);
oled.print("Humidity");
delay(2000);
client.setServer(client_server, port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
String led_situation, relay_situation;
if (!isnan(temperature) && !isnan(humidity)) {
if (temperature >= 40.00) {
led_state = true;
relay_state = true;
} else {
led_state = false;
relay_state = false;
}
bool choose_led_state = led_mode == "automatic" ? led_state : led_state_platform;
if (choose_led_state) {
digitalWrite(LED, HIGH);
led_situation = "ON";
}
else {
digitalWrite(LED, LOW);
led_situation = "OFF";
}
bool choose_relay_state = relay_mode == "automatic" ? relay_state : relay_state_platform;
if (choose_relay_state) {
digitalWrite(RELAY, HIGH);
relay_situation = "ON";
}
else {
digitalWrite(RELAY, LOW);
relay_situation = "OFF";
}
client.publish("thariq/prog/temp", String(temperature).c_str());
client.publish("thariq/prog/hum", String(humidity).c_str());
client.publish("thariq/prog/status/led", led_situation.c_str());
client.publish("thariq/prog/status/relay", relay_situation.c_str());
}
delay(20);
oled.setCursor(0,0); // Set kursor LCD di baris 1, kolom 1
oled.println ("Temp: " + String(temperature) + " C "); // Tampilkan suhu
oled.setCursor(0,1); // Set kursor LCD di baris 2, kolom 1
oled.println ("Hum: " + String(humidity) + " % "); // Tampilkan kelembaban
delay(1000);
}
void callback(char* topic, byte* message, unsigned int length) {
String messageTemp;
for (int i = 0; i < length; i++) {
messageTemp += (char)message[i];
}
if (String(topic) == "thariq/prog/led") {
if (messageTemp == "on") {
led_state_platform = true;
}
else {
led_state_platform = false;
}
}
if (String(topic) == "thariq/prog/relay") {
if (messageTemp == "on") {
relay_state_platform = true;
}
else {
relay_state_platform = false;
}
}
if (String(topic) == "thariq/prog/mode/led") {
led_mode = messageTemp;
led_state_platform = led_state;
}
if (String(topic) == "thariq/prog/mode/relay") {
relay_mode = messageTemp;
relay_state_platform = relay_state;
}
}