from machine import Pin, ADC, PWM, UART
import machine
import dht
import network
import time
import BlynkLib
# =====================================================
# WIFI CONFIGURATION
# =====================================================
SSID = "YOUR_WIFI"
PASSWORD = "YOUR_PASSWORD"
# =====================================================
# BLYNK CONFIGURATION
# =====================================================
BLYNK_AUTH = "YOUR_AUTH_TOKEN"
blynk = BlynkLib.Blynk(BLYNK_AUTH)
# =====================================================
# PINS
# =====================================================
DHT_PIN = 14
MQ2_PIN = 34
TRIG_PIN = 5
ECHO_PIN = 18
SERVO_PIN = 27
LED_PIN = 2
UART_TX = 17
UART_RX = 16
# =====================================================
# INITIALIZE DEVICES
# =====================================================
led = Pin(LED_PIN, Pin.OUT)
dht_sensor = dht.DHT22(Pin(DHT_PIN))
mq2 = ADC(Pin(MQ2_PIN))
mq2.atten(ADC.ATTN_11DB)
mq2.width(ADC.WIDTH_12BIT)
trigger = Pin(TRIG_PIN, Pin.OUT)
echo = Pin(ECHO_PIN, Pin.IN)
servo = PWM(Pin(SERVO_PIN), freq=50)
uart = UART(1, baudrate=9600, tx=Pin(UART_TX), rx=Pin(UART_RX))
# =====================================================
# WIFI CONNECTION
# =====================================================
print("Connecting WiFi...")
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(SSID, PASSWORD)
while not wifi.isconnected():
time.sleep(0.5)
print(".", end="")
print("\nWiFi Connected")
print(wifi.ifconfig())
# =====================================================
# SERVO FUNCTIONS
# =====================================================
def set_servo(angle):
duty = int((angle / 180) * 102 + 26)
servo.duty(duty)
def door_open():
set_servo(90)
def door_closed():
set_servo(0)
# =====================================================
# ULTRASONIC FUNCTION
# =====================================================
def distance():
trigger.off()
time.sleep_us(2)
trigger.on()
time.sleep_us(10)
trigger.off()
while echo.value() == 0:
pulse_start = time.ticks_us()
while echo.value() == 1:
pulse_end = time.ticks_us()
pulse = time.ticks_diff(pulse_end, pulse_start)
cm = pulse / 58.0
return cm
# =====================================================
# GAS DETECTION
# =====================================================
GAS_THRESHOLD = 2200
# =====================================================
# MAIN LOOP
# =====================================================
while True:
blynk.run()
# ---------------- DHT ----------------
try:
dht_sensor.measure()
temperature = dht_sensor.temperature()
humidity = dht_sensor.humidity()
except:
temperature = 0
humidity = 0
# ---------------- MQ2 ----------------
gas_value = mq2.read()
gas = gas_value > GAS_THRESHOLD
# ---------------- Distance ----------------
try:
dist = distance()
except:
dist = 999
# ---------------- Logic ----------------
if gas:
led.on()
door_open()
door = "OPEN"
elif dist <= 30:
led.off()
door_open()
door = "OPEN"
else:
led.off()
door_closed()
door = "CLOSED"
# ---------------- UART ----------------
message = "{:.1f},{:.1f},{:.1f},{},{},{}\n".format(
temperature,
humidity,
dist,
gas_value,
int(gas),
door
)
uart.write(message)
# ---------------- Serial ----------------
print(message)
# ---------------- Blynk ----------------
try:
blynk.virtual_write(0, temperature)
blynk.virtual_write(1, humidity)
blynk.virtual_write(2, dist)
blynk.virtual_write(3, gas_value)
blynk.virtual_write(4, door)
except:
pass
# Loop every 250 ms
time.sleep_ms(250)Loading
esp32-devkit-v1
esp32-devkit-v1