#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
const int PIRinput = 17;
int pirState = LOW;
#define Buzzer 4
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
int interval = 5000;
long previousMillis = 0;
int IntervalR = 3000; // Interval at which to toggle LED3 (milliseconds)
long previousMillisR = 0;
bool ledStateR = LOW;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_10145291"; // yourStudentID must be unique
int PORTNUM = 1883;
void setup_wifi()
{
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED )
{
delay(1000);
Serial.print(".");
}
Serial.println();
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("Given IP by the router to ESP32 is ");
Serial.println(WiFi.localIP());
}
void connectMQTT()
{
while(!client.connected() )
{
Serial.println("Connecting to MQTT ...");
if (client.connect(espClientName) ) //, mqttUser, mqttPassword) )
{
Serial.println("Connected");
MQTTSubscribe();
}
else
{
Serial.print("Failed with state ");
Serial.print(client.state() );
delay(2000);
}
}
}
void callback(String topic, byte* payload, unsigned int length)
{
String messageTemp;
Serial.print("Message received in topic: ");
Serial.print(topic);
Serial.print(" length is: ");
Serial.println(length);
Serial.print("Data received from broker: ");
for (int i = 0; i<length; i++)
{
Serial.print( (char)payload[i] );
messageTemp += (char)payload[i];
}
}
void MQTTSubscribe()
{
client.subscribe("JCE/IoT");
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(PIRinput, INPUT);
pinMode(23, OUTPUT);
pinMode(18, OUTPUT);
Serial.begin(9600);
pinMode(Buzzer, OUTPUT);
lcd.init(); //set LCD
lcd.backlight();
setup_wifi();
setup_MQTT();
}
void loop()
{
const int IP = digitalRead(PIRinput);
delay(100);
long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
if (IP == 1) // If motion is detected by the PIR sensor
{
String payload = String(PIRinput);
client.publish("JCE/IoT", payload.c_str());
digitalWrite(23, HIGH);
digitalWrite(18, LOW);
if (pirState == LOW) // Update pirState to indicate motion detection
{
Serial.println("Bin is opening!");
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Bin is opening");
pirState = HIGH;
}
else
{
if (pirState == HIGH) // Update pirState to indicate motion detection
{
Serial.println("Bin is closing...");
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Bin is closing");
pirState = LOW;
delay(3000);
Serial.println("Bin is closed.");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Bin is closed...");
delay(500);
digitalWrite(Buzzer, HIGH); //buzzer make sound
delay(1000);
digitalWrite(Buzzer, LOW);
}
digitalWrite(23, LOW);
digitalWrite(18, HIGH);
}
}
}
if ( !client.connected() )
{
connectMQTT();
}
client.loop();
}