0% found this document useful (0 votes)
0 views

ESP32_MQTT_Sensor_System (1)

This document is an Arduino sketch for an ESP32-based project that connects to WiFi and an MQTT broker to control a smart door system. It includes functionalities for password authentication, sensor readings (temperature, humidity, gas detection), and controlling a servo and LED based on the input. The system also utilizes an OLED display and a 7-segment display to show status and sensor data.

Uploaded by

pmdinh26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

ESP32_MQTT_Sensor_System (1)

This document is an Arduino sketch for an ESP32-based project that connects to WiFi and an MQTT broker to control a smart door system. It includes functionalities for password authentication, sensor readings (temperature, humidity, gas detection), and controlling a servo and LED based on the input. The system also utilizes an OLED display and a 7-segment display to show status and sensor data.

Uploaded by

pmdinh26
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <Arduino.

h>
#include <WiFi.h>
#include <PubSubClient.h>
#include <DHT.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TM1637Display.h>
#include <ESP32Servo.h>

const char *ssid = "Wokwi-GUEST";


const char *password = "";

const char *mqtt_broker = "broker.emqx.io";


const char *mqtt_user = "emqx";
const char *mqtt_pass = "public";
const int mqtt_port = 1883;

WiFiClient espClient;
PubSubClient client(espClient);

DHT dht(13, DHT22);


Adafruit_SSD1306 display(128, 64, &Wire, -1);
TM1637Display display7seg(16, 17);
Servo servo;

const char* correctPassword = "1234";


String inputPassword = "";
bool doorState = false;
bool ledSwitchState = false;
int tm1637Value = 0;

unsigned long lastSend = 0;


const unsigned long interval = 3000;

void callback(char *topic, byte *payload, unsigned int length);

void setup() {
Serial.begin(9600);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("\nWiFi connected");

client.setServer(mqtt_broker, mqtt_port);
client.setCallback(callback);

while (!client.connected()) {
String clientId = "esp32-" + String(WiFi.macAddress());
if (client.connect(clientId.c_str(), mqtt_user, mqtt_pass)) {
Serial.println("MQTT connected");
} else {
Serial.print("MQTT failed, state: ");
Serial.println(client.state());
delay(2000);
}
}

// Initialize hardware
pinMode(14, OUTPUT); // LED
pinMode(23, OUTPUT); // Success LED
pinMode(0, INPUT_PULLUP); // Button
pinMode(2, OUTPUT); // Buzzer
pinMode(34, INPUT); // Gas DOUT
servo.attach(5);
dht.begin();

if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("OLED failed");
for(;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Enter password:");
display.display();

display7seg.setBrightness(0x0f);

// Subscribe to MQTT topics


client.subscribe("topic/password");
client.subscribe("topic/led_switch");
client.subscribe("topic/tm1637");
}

void callback(char *topic, byte *payload, unsigned int length) {


String msg;
for (unsigned int i = 0; i < length; i++) {
msg += (char)payload[i];
}
Serial.println(msg);

if (strcmp(topic, "topic/password") == 0) {
inputPassword = msg;
display.clearDisplay();
display.setCursor(0, 0);
display.print("Password: ");
display.println(msg);
if (msg == correctPassword) {
display.println("Unlock successful");
doorState = true;
digitalWrite(23, HIGH);
client.publish("topic/door_state", "Open");
} else {
display.println("Wrong password");
doorState = false;
digitalWrite(23, LOW);
client.publish("topic/door_state", "Closed");
}
display.display();
} else if (strcmp(topic, "topic/led_switch") == 0) {
ledSwitchState = (msg == "1" || msg == "true");
digitalWrite(14, ledSwitchState);
} else if (strcmp(topic, "topic/tm1637") == 0) {
tm1637Value = msg.toInt();
if (tm1637Value >= 0 && tm1637Value <= 1000) {
display7seg.showNumberDec(tm1637Value, false);
}
}
}

void loop() {
client.loop();

// Check button for password reset


if (digitalRead(0) == LOW) {
delay(50); // Simple debounce
if (digitalRead(0) == LOW) {
inputPassword = "";
doorState = false;
digitalWrite(23, LOW);
client.publish("topic/door_state", "Closed");
display.clearDisplay();
display.setCursor(0, 0);
display.println("Enter password:");
display.display();
while (digitalRead(0) == LOW); // Wait for button release
}
}

unsigned long now = millis();


if (now - lastSend >= interval) {
lastSend = now;

// Read sensors
float h = dht.readHumidity();
float t = dht.readTemperature();
bool gasDetected = digitalRead(34) == LOW;
int gasValue = analogRead(35);
int lightValue = analogRead(32);
digitalWrite(18, LOW);
delayMicroseconds(2);
digitalWrite(18, HIGH);
delayMicroseconds(10);
digitalWrite(18, LOW);
long duration = pulseIn(19, HIGH);
float distance = duration * 0.034 / 2;

// Publish sensor data


if (!isnan(t)) {
client.publish("topic/temperature", String(t, 2).c_str());
}
if (!isnan(h)) {
client.publish("topic/humidity", String(h, 2).c_str());
}
client.publish("topic/gas", String(gasValue).c_str());
client.publish("topic/light", String(lightValue).c_str());
client.publish("topic/distance", String(distance, 2).c_str());
client.publish("topic/tm1637", String(tm1637Value).c_str());

// Control servo
if (distance >= 99 && distance <= 101) {
servo.write(180);
} else {
servo.write(0);
}

// Control buzzer
if (gasDetected) {
tone(2, 1000);
} else {
noTone(2);
}

// Update LED
digitalWrite(14, ledSwitchState);

// Update displays
display7seg.showNumberDec(tm1637Value, false);
if (inputPassword == "") {
display.clearDisplay();
display.setCursor(0, 0);
display.println("Enter password:");
display.display();
}

// Serial output
Serial.print(h);
Serial.print(" ");
Serial.print(t);
Serial.print(" ");
Serial.print(gasValue);
Serial.print(" ");
Serial.print(lightValue);
Serial.print(" ");
Serial.println(distance);
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy