/* Lab Trial Test 1
* Name: TINAGARAN
* Class: Iot
* Date: 10/1/25
*/
#include <WiFi.h>
#include <WiFiClient.h>
#include <PubSubClient.h>
#define TRIG_PIN 2 // Ultrasonic Trigger Pin
#define ECHO_PIN 18 // Ultrasonic Echo Pin
#define LED_PIN 23 // Red LED for full bin indicator
int ledPin = 16; // choose the pin for the LED
int inputPin = 21; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int LEDstate_R= 1;
long previousMillis_R = 0;
int interval_R = 2000;
char red_led = 23;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* hostname = "broker.hivemq.com";
WiFiClient espClient;
PubSubClient client(espClient);
const char* espClientName = "esp32Client_TINAGARAN"; // yourStudentID must be unique
int PORTNUM = 1883;
void MQTTSubscribe()
{
}
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 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 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];
}
/// Handle your topics and payload here
}
long readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
return pulseIn(ECHO_PIN, HIGH) * 0.034 / 2; // Convert to cm
}
void setup_MQTT()
{
client.setServer(hostname, PORTNUM);
client.setCallback(callback);
}
void setup()
{
Serial.begin(9600);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_PIN, OUTPUT);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
pinMode(red_led,OUTPUT);
setup_wifi();
setup_MQTT();
}
void loop()
{
if ( !client.connected() )
{
connectMQTT();
}
client.loop();
long distance = readDistance();
Serial.print("Distance: ");
Serial.println(distance);
if (distance > 300) {
digitalWrite(LED_PIN, LOW); // Turn on red LED
Serial.println("MQTT Publish: Dustbin is Full");
client.publish("iot/3164479r" ,"On");
} else {
digitalWrite(LED_PIN, HIGH); // Turn off red LED
Serial.println("MQTT Publish: Dustbin is not Full");
client.publish("iot/3164479r" ,"Off");
}
delay(5000);
{
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH) {
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4