//#include <ESP8266WiFi.h>
#include <Wire.h>
#include <WiFi.h>
#include <Arduino.h>
#include <PubSubClient.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define WIFI_SSID "Wokwi-GUEST" // WIFI SSID
#define WIFI_PW "" // WIFI PASSWORD
#define MQTT_BROKER "test.mosquitto.org" // MQTT BROKER
#define MQTT_PORT 1883 // MQTT BROKER PORT
#define MQTT_TOPIC "iot/perjuangan/v1" // MQTT TOPIC
#define LDR_PIN 2
#define DHT_PIN 12
#define DHTTYPE DHT22
DHT dht(DHT_PIN, DHTTYPE);
LiquidCrystal_I2C lcd(0x27, 20, 4);
int cek;
float hum;
float temp;
unsigned long lastMsg = 0;
#define MSG_BUFFER_SIZE (50)
char msg[MSG_BUFFER_SIZE];
int value = 0;
WiFiClient espClient; // ESP8266 Wifi Client
PubSubClient client(espClient); // MQTT Client using the Wifi-Client
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived in topic: "); // Print out the topic
Serial.println(topic);
Serial.print("Message:"); // Print Received message
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
}
void setup() {
pinMode(LDR_PIN, INPUT);
pinMode(DHT_PIN, INPUT);
lcd.init();
lcd.backlight();
lcd.setBacklight(HIGH);
pinMode(LED_BUILTIN, OUTPUT); // Set up pin IO modes
digitalWrite(LED_BUILTIN, HIGH); // Turn off the lights
Serial.begin(115200);
WiFi.begin(WIFI_SSID, WIFI_PW); // Start Wi-Fi connection
while (WiFi.status() != WL_CONNECTED) { // Waiting for connection success
delay(250);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to the WiFi network");
delay(100);
digitalWrite(LED_BUILTIN, LOW); // Turn on the light to indicate Wi-Fi success
Serial.println("-------------------------");
Serial.println("Hello from ESP8266");
Serial.print("Connected to hotspot: ");
Serial.println(WIFI_SSID);
Serial.print("IP address is: ");
Serial.println(WiFi.localIP());
Serial.println("-------------------------");
client.setSocketTimeout(60);
client.setKeepAlive(5);
client.setServer(MQTT_BROKER, MQTT_PORT); // Start MQTT connection
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266_Receive", "", "" )) { // Sign in to the broker
Serial.println("Connected to broker");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe(MQTT_TOPIC);
}
void loop() {
if (!client.connected()) {
Serial.print("!");
delay(250);
} else {
client.loop();
}
delay(10);
delay(1000);
hum = dht.readHumidity();
temp= dht.readTemperature();
lcd.setCursor(0,0);
lcd.print("Humidity : ");
lcd.print(hum);
lcd.print("%");
lcd.setCursor(0,1);
lcd.print("Temperature : ");
lcd.print(temp);
lcd.println("C");
lcd.setCursor(0,3);
lcd.print("Room : ");
if (digitalRead(LDR_PIN) == LOW) {
lcd.print("TERANG");
client.publish("iot/perjuangan/v1", "GELAP");
} else {
lcd.print("GELAP ");
client.publish("iot/perjuangan/v1", "TERANG");
}
}