#include "ArduinoJson.h"
#include <WiFi.h>
#include "PubSubClient.h"
#include <LiquidCrystal_I2C.h>
// Define Constants
const char *SSID = "WOKWI-Guest"; // Replace with your WiFi SSID
const char *PASSWORD = ""; // Replace with your WiFi password
const char *MQTTServer = "broker.hivemq.com"; // MQTT broker address
const char *MQTT_Topic_HienThi = "IOT/PhatTrien/HienThi"; // MQTT topic for displaying messages
const char *MQTT_Topic_Nguong = "IOT/PhatTrien/Nguong"; // MQTT topic for threshold value
const char *MQTT_ID = "08538910-a63d-45af-ab5b-3ca7131928c4"; // Your unique MQTT client ID
int Port = 1883; // MQTT port
const int ldr_pin = A0; // Analog pin for the LDR
const float GAMMA = 0.7; // LDR gamma value
const float RL10 = 50; // LDR RL10 value
// Global Variables
WiFiClient espClient; // WiFi client
PubSubClient client(espClient); // MQTT client
LiquidCrystal_I2C lcd(0x27, 20, 4); // LCD display
float nguong = 0; // Threshold value (initialize to 0)
// Function to connect to WiFi
void WIFIConnect() {
Serial.println("Connecting to SSID: " + String(SSID));
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected");
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
}
// Function to reconnect to the MQTT broker
void MQTT_Reconnect() {
while (!client.connected()) {
if (client.connect(MQTT_ID)) {
Serial.print("MQTT Topics connected: ");
Serial.print(MQTT_Topic_HienThi);
Serial.print(", ");
Serial.print(MQTT_Topic_Nguong);
Serial.println("");
client.subscribe(MQTT_Topic_HienThi);
client.subscribe(MQTT_Topic_Nguong);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
// Function to handle incoming messages from the MQTT broker
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.println(topic);
String stMessage;
lcd.clear();
char receivedMessage[length + 1];
for (int i = 0; i < length; i++) {
receivedMessage[i] = (char)message[i];
}
receivedMessage[length] = '\0';
lcd.setCursor(0, 0);
lcd.print(receivedMessage);
if (strcmp(topic, MQTT_Topic_Nguong) == 0) { // Check if the message is for the threshold topic
nguong = atof(receivedMessage); // Convert the received string to a float
Serial.print("New threshold value: ");
Serial.println(nguong);
} else if (strlen(receivedMessage) > 20) {
for (int i = 0; i < strlen(receivedMessage) - 20; i++) {
lcd.scrollDisplayLeft();
delay(500);
}
}
}
// Function to read the LDR value and convert it to lux
float ldr_display() {
int analogValue = analogRead(ldr_pin);
Serial.println(analogValue); // Print the analog value for debugging
float voltage = analogValue / 1024.0 * 5.0; // Convert to voltage (assuming 5V reference)
float resistance = 10000.0 * voltage / (5.0 - voltage); // Calculate resistance (assuming a 10k resistor)
float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA)); // Calculate lux using the LDR formula
return lux;
}
// Function to publish the LDR value to the MQTT broker
void publishLDR() {
float ldr_values = ldr_display();
Serial.println(ldr_values);
client.publish(MQTT_Topic_HienThi, String(ldr_values).c_str()); // Publish to the "HienThi" topic
}
// Setup function
void setup() {
lcd.init();
lcd.backlight();
pinMode(ldr_pin, INPUT); // Define ldr_pin as an input
Serial.begin(115200);
WIFIConnect();
client.setServer(MQTTServer, Port);
client.setCallback(callback);
}
// Loop function
void loop() {
delay(100);
if (!client.connected()) {
MQTT_Reconnect();
}
client.loop();
// Publish LDR value only if it's above the threshold
if (ldr_display() > nguong) {
publishLDR();
}
}