Siddharth Bhandarkar
Published

Real-Time Noise Pollution Alert

A miniature representation of sending alert to the police about the real-time random sound intensity around the hospital via Telegram.

IntermediateProtip6 hours173
Real-Time Noise Pollution Alert

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
USB Cable Assembly, USB Type A Plug to Micro USB Type B Plug
USB Cable Assembly, USB Type A Plug to Micro USB Type B Plug
×1
SparkFun Sound Detector (with Headers)
SparkFun Sound Detector (with Headers)
×1
Jumper wires (generic)
Jumper wires (generic)
×3

Software apps and online services

Telegram

Hand tools and fabrication machines

python.microbit.org
micro:bit python.microbit.org

Story

Read more

Schematics

Circuit connections of Bolt Iot Module and Sound Sensor

Here the Bolt IoT Wi-Fi Module is connected to the Sound sensor with the jumper wires via its attributes

Code

Real time Noise Pollution Alert

Python
There are two code files namely configuration.py and alert.py that should be created separately
“”1. Configurations for Telegram alert message. File name configuration.py ””
BOLT_API_KEY = XXXXXXXXX	# This is your Bolt cloud API Key.
DEVICE_ID = XXXXXXXXX	# This is the ID number of your Bolt device.
TELEGRAM_CHAT_ID = @XXXXXXXXX	# This is the channel ID of the channel created in the Telegram. Paste after @.
TELEGRAM_BOT_ID = botXXXXXXXXX	# This is the bot ID of the bot created in the Telegram. Paste after bot.
THRESHOLD = 80	# Threshold beyond which the alert should be sent.

""2. Main code file. File name alert.py""

import requests                 # for making HTTP requests
import json                     # library for handling JSON data
import time                     # module for sleep operation
from boltiot import Bolt        # importing Bolt from boltiot module
import configuration            # configuration file
mybolt = Bolt(configuration.BOLT_API_KEY, configuration.DEVICE_ID)
def get_sound_sensor_value_from_pin(pin):
try:
  response = mybolt.analogRead(pin)
  data = json.loads(response)
      if data["success"] != 1:
            print("Request not successful")
            print("This is the response->", data)
            return -999
    	sound_sensor_value = int(data["value"])
    	return sound_sensor_value
except Exception as e:
print("Something went wrong when returning the sensor value")
     	print(e)
     return -999
def send_telegram_message(message):
url = "https://api.telegram.org/" + configuration.TELEGRAM_BOT_ID + "/sendMessage"
data = {
        "chat_id": configuration.TELEGRAM_CHAT_ID,
        "text": message
}
try:
        response = requests.request(
            "GET",
            url,
            params = data
        )
        print("This is the Telegram response")
        print(response.text)
        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
while True:
    #Step 1
    sound_sensor_value = get_sound_sensor_value_from_pin("A0")    
    print("The current sensor reading is:", sound_sensor_value)
    
    #Step 2
    if sound_sensor_value == -999:
	print("Request was unsuccessful. Skipping.")
	    time.sleep(10)
      continue
     
     #Step 3
    if sound_sensor_value >= configuration.THRESHOLD:
      print("Sensor value has exceeded threshold")
      message = "Alert! Noise disturbance around the XYZ Hospital. Random and          unidentified sound intensity has crossed " + str(configuration.THRESHOLD) +       str(dB) + \
    The current sound sensor reading is  + str(sound_sensor_value) + str(dB)     + str(To, The Police Incharge, Immediate action required. Thankyou.)
telegram_status = send_telegram_message(message)
print("This is the Telegram status:", telegram_status)

# Step 4
    time.sleep(10)

Credits

Siddharth Bhandarkar

Siddharth Bhandarkar

1 project • 1 follower

Comments

Add projectSign up / LoginAbout BBC micro:bit