#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include "secrect.h" // Include your secret.h file
#define LDR1_PIN 34 // ADC pin where the first LDR is connected
#define LED1_PIN 5 // GPIO pin connected to the first LED
#define LDR2_PIN 35 // ADC pin where the second LDR is connected
#define LED2_PIN 18 // GPIO pin connected to the second LED
int threshold1 = 2000; // Threshold for the first LDR
int threshold2 = 2000; // Threshold for the second LDR
WiFiClientSecure net; // Create a WiFiClientSecure object
PubSubClient client(net); // Create a PubSubClient object using the secure WiFi client
// Define MQTT topic
#define AWS_IOT_PUBLISH_TOPIC "your/lights/status" // Combined topic for both LEDs and LDRs
#define AWS_IOT_SUBSCRIBE_TOPIC "your/control/topic" // Topic for subscribing to control messages
void setup() {
Serial.begin(115200); // Start serial communication for debugging
pinMode(LED1_PIN, OUTPUT); // Set LED1 pin as OUTPUT
pinMode(LDR1_PIN, INPUT); // Set LDR1 pin as INPUT
pinMode(LED2_PIN, OUTPUT); // Set LED2 pin as OUTPUT
pinMode(LDR2_PIN, INPUT); // Set LDR2 pin as INPUT
// Connect to WiFi
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
// Configure the secure connection for MQTT
net.setCACert(AWS_CERT_CA);
net.setCertificate(AWS_CERT_CRT);
net.setPrivateKey(AWS_CERT_PRIVATE);
// Set up MQTT
client.setServer(AWS_IOT_ENDPOINT, 8883);
client.setCallback(messageHandler); // Set the callback function for incoming messages
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(THINGNAME)) {
Serial.println("connected");
client.subscribe(AWS_IOT_SUBSCRIBE_TOPIC); // Subscribe to control topic
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// Read the LDR values (0-4095 for 12-bit ADC on ESP32)
int ldrValue1 = analogRead(LDR1_PIN);
int ldrValue2 = analogRead(LDR2_PIN);
Serial.print("LDR1 Value: ");
Serial.println(ldrValue1); // Print the LDR1 value to the serial monitor
Serial.print("LDR2 Value: ");
Serial.println(ldrValue2); // Print the LDR2 value to the serial monitor
// Prepare the JSON document for both LEDs and LDRs
StaticJsonDocument<300> doc;
// Check LDR1 status
if (ldrValue1 < threshold1) {
digitalWrite(LED1_PIN, HIGH); // Turn ON LED1 (indicates low light)
Serial.println("Street light 1 is ON");
// Update JSON document
doc["light1"]["status"] = "Street light 1 is working fine";
doc["light1"]["ldrValue"] = ldrValue1;
} else {
digitalWrite(LED1_PIN, LOW); // Turn OFF LED1 (indicates sufficient light)
Serial.println("Street light 1 is OFF");
// Update JSON document
doc["light1"]["status"] = "Default: Detected in street light 1.....!!!";
doc["light1"]["ldrValue"] = ldrValue1;
}
// Check LDR2 status
if (ldrValue2 < threshold2) {
digitalWrite(LED2_PIN, HIGH); // Turn ON LED2 (indicates low light)
Serial.println("Street light 2 is ON");
// Update JSON document
doc["light2"]["status"] = "Street light 2 is working fine";
doc["light2"]["ldrValue"] = ldrValue2;
} else {
digitalWrite(LED2_PIN, LOW); // Turn OFF LED2 (indicates sufficient light)
Serial.println("Street light 2 is OFF");
// Update JSON document
doc["light2"]["status"] = "Default: Detected in street light 2.....!!!";
doc["light2"]["ldrValue"] = ldrValue2;
}
// Publish the combined status as JSON to a single topic
char jsonBuffer[512];
serializeJson(doc, jsonBuffer);
client.publish(AWS_IOT_PUBLISH_TOPIC, jsonBuffer);
delay(500); // Small delay to avoid flooding the serial monitor
}
// Callback function to handle incoming messages
void messageHandler(char* topic, byte* payload, unsigned int length) {
Serial.print("Incoming message on topic: ");
Serial.println(topic);
// Handle incoming messages here (e.g., turn on/off the LEDs based on commands)
payload[length] = '\0'; // Null-terminate the payload to make it a string
String message = String((char*)payload);
Serial.println("Message: " + message);
// Example: Control both LEDs based on the message
if (message == "LED1_ON") {
digitalWrite(LED1_PIN, HIGH); // Turn ON LED1
} else if (message == "LED1_OFF") {
digitalWrite(LED1_PIN, LOW); // Turn OFF LED1
}
if (message == "LED2_ON") {
digitalWrite(LED2_PIN, HIGH); // Turn ON LED2
} else if (message == "LED2_OFF") {
digitalWrite(LED2_PIN, LOW); // Turn OFF LED2
}
}