Ayush Kumar
Published

Smoke Detector

In this project, smoke is detected using MQ-2 sensor and alarm will be on according to it.

BeginnerFull instructions provided1 hour799
Smoke Detector

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
MQ-2 Smoke Detector
×1
Buzzer
Buzzer
×1
Resistor 330 ohm
Resistor 330 ohm
×1
LED (generic)
LED (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Telegram
Bolt Cloud
Bolt IoT Bolt Cloud
Windows 10
Microsoft Windows 10

Story

Read more

Schematics

Circuit Connection

Code

code.py

Python
This is the main program which we will execute.
import boltiot, requests, time, json, conf

def send_telegram_message(message):
    """Sends message via Telegram"""
    url = "https://api.telegram.org/" + conf.telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": conf.telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request("POST",url,params=data)
        telegram_data = json.loads(response.text)
        return telegram_data["ok"]
    except Exception as e:
        print("An error occurred in sending the alert message via Telegram")
        print(e)
        return False


mybolt = boltiot.Bolt(conf.api_key,conf.device_id)
print("MQ-2 Sensor is warming up.")
time.sleep(10)	#Allowing MQ-2 to warm up
while True:
	response = mybolt.analogRead('A0')
	data = json.loads(response)
	if data["success"] != 1:
		print("Unable to read data from sensor.")
		time.sleep(5)
		continue
	sensor_value = int(data["value"])
	print("Sensor Value: ",sensor_value)
	if sensor_value >= conf.threshold:
		mybolt.digitalWrite('4','HIGH')
		print("Sending message to Telegram")
		telegram_status = send_telegram_message("Warning! Smoke has been detected.")
		print("This is the Telegram Status: ",telegram_status)
	else:
		mybolt.digitalWrite('4','LOW')
	time.sleep(5)

conf.py

Python
This is configuration file.
api_key = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"	# This is your Bolt Cloud API Key
device_id = "BOLTXXXXXXX"	# This is the device ID of Bolt wifi module
telegram_chat_id = "@XXXXX"	 # This is the channel ID of the created Telegram channel. Paste after '@'
telegram_bot_id = "botXXXXXX"	# This is the bot Token of the created Telegram Bot. Paste after 'bot'
threshold = 685

Credits

Ayush Kumar

Ayush Kumar

2 projects • 0 followers

Comments