Untitled

 avatar
unknown
plain_text
5 months ago
754 B
3
Indexable
import Adafruit_DHT
import RPi.GPIO as GPIO
import time

# DHT11 Setup
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

# MQ2 Setup (assuming digital output)
MQ2_PIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(MQ2_PIN, GPIO.IN)

try:
    while True:
        # Read DHT11
        humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)
        
        # Read MQ2
        smoke_detected = GPIO.input(MQ2_PIN)

        if humidity is not None and temperature is not None:
            print(f"Temp={temperature}°C  Humidity={humidity}%")
        else:
            print("Failed to retrieve data from humidity sensor")

        if smoke_detected:
            print("Smoke detected!")

        time.sleep(2)

except KeyboardInterrupt:
    GPIO.cleanup()
Editor is loading...
Leave a Comment