#include <WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <ESP32Servo.h>
// parameters for Wi-Fi connection
const char* WIFI_SSID = "Wokwi-GUEST";
const char* PASSWORD = "";
// parameters for MQTT-server
const char* MQTT_CLIENT_ID = "ESPLab3";
const char* MQTT_BROKER_IP = "broker.mqttdashboard.com";
const int MQTT_BROKER_PORT = 1883;
const char* MQTT_USER = "";
const char* MQTT_PASSWORD = "";
const char* MQTT_TOPIC = "ESP32_photoresistor_HC-ST04";
WiFiClient wifiClient;
PubSubClient client(wifiClient);
Servo servo;
Servo servo1;
// limit values
const int MAX_LIGHT = 1200; // max lightning value for 12-bit ADC
const int MIN_DISTANCE = 200; // min distance in cm
// sensors pins
const int LIGHT_SENSOR_PIN = 34;
const int TRIG_PIN = 4;
const int ECHO_PIN = 2;
const int LED_PIN = 5;
const int SERVO_PIN = 12;
// ESP32 work permission
bool deviceEnabled = true;
bool objectDetected = false;
bool lightDetected = false;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
servo.attach(SERVO_PIN);
servo1.attach(18);
// Wi-Fi connection
WiFi.begin(WIFI_SSID, PASSWORD);
Serial.print("Attempting to connect to Wi-Fi -> ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println("Connected to Wi-Fi successfully!");
// MQTT-server configuration
client.setServer(MQTT_BROKER_IP, MQTT_BROKER_PORT);
client.setCallback(callback);
// Connection to MQTT-server
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("Connected to MQTT-server successfully!");
} else {
Serial.println("Failed to connect to MQTT-server :(");
}
// Topic subscription
client.subscribe(MQTT_TOPIC);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
if (deviceEnabled) {
checkSensors();
}
}
void reconnect() {
while (!client.connected()) {
Serial.println("Attempting to connect to MQTT-server");
if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
Serial.println("Connected to MQTT-server successfully!");
client.subscribe(MQTT_TOPIC);
} else {
Serial.print("Failed to connect to MQTT-server :(");
Serial.print(client.state());
Serial.println("Retrying in 3 seconds");
delay(3000);
}
}
}
void callback(char* MQTT_TOPIC, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(MQTT_TOPIC);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
DynamicJsonDocument doc(1024);
deserializeJson(doc, payload, length);
String command = doc["command"].as<String>();
if (command == "on") {
deviceEnabled = true;
Serial.println("Device enabled");
} else if (command == "off") {
deviceEnabled = false;
digitalWrite(LED_PIN, LOW);
Serial.println("Device disabled");
}
}
void checkSensors() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH);
int distance = duration / 58;
if (distance > MIN_DISTANCE) {
if (!objectDetected) {
client.publish(MQTT_TOPIC, "{\"alert\":\"object gone\"}");
objectDetected = true;
}
} else {
if (objectDetected) {
client.publish(MQTT_TOPIC, "{\"alert\":\"object detected\"}");
objectDetected = false;
}
servo.write(90);
delay(500);
servo.write(0);
}
if (distance > 300) {
if (!objectDetected) {
client.publish(MQTT_TOPIC, "{\"alert\":\"object gone\"}");
objectDetected = true;
}
} else {
if (objectDetected) {
client.publish(MQTT_TOPIC, "{\"alert\":\"object detected\"}");
objectDetected = false;
}
servo1.write(90);
delay(500);
servo1.write(0);
}
}