Valentina
Published © GPL3+

Humidity sensing and alerting system using Bolt IoT

It monitors if the humidity lies in the specified range, if not sends a alert message via Telegram and even gives out an alarm using buzzer

BeginnerFull instructions provided5 hours703
Humidity sensing and alerting system using Bolt IoT

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
Buzzer
Buzzer
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×1
Male/Female Jumper Wires
Male/Female Jumper Wires
×1
Breadboard (generic)
Breadboard (generic)
×1
DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud

Hand tools and fabrication machines

Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires
Wire Stripper & Cutter, 18-10 AWG / 0.75-4mm² Capacity Wires

Story

Read more

Schematics

Circuit

This is how circuit connections are done

Code

Configuration file

Python
Configuration details of telegram app and bolt device for file with main code
bolt_api_key = "XXXX"               # This is your Bolt Cloud API Key
device_id = "XXXX"                  # This is the device ID and will be similar to BOLTXXXX where XXXX is some numbers
telegram_chat_id = "@XXXX"          # This is the channel ID of the created Telegram channel. Paste after @
telegram_bot_id = "botXXXX"         # This is the bot ID of the created Telegram Bot. Paste after bot
min_value = 204.8                     # minimumvalue beyond which alert should be sent
max_value = 614.4                     # maximumvalue beyond which alert should be sent

Main code

Python
Humidity value is picked up , compared with threshold range and accordingly alert is generated when necessary
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 conf                     # config file

mybolt = Bolt(conf.bolt_api_key, conf.device_id)

def get_sensor_value_from_pin(pin):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        response = mybolt.analogRead(pin)
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        sensor_value = int(data["value"])
        return sensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999

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
        )
        print("This is the Telegram URL")
        print(url)
        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
    sensor_value = get_sensor_value_from_pin("A0")    
    print("The current sensor value is:", sensor_value)
    
    # Step 2
    if sensor_value == -999:
        print("Request was unsuccessfull. Skipping.")
        time.sleep(10)
        continue
    
    # Step 3
    if sensor_value > conf.max_value or sensor_value < conf.min_value:
        print("Sensor value is not in range")
        message = "Alert! Sensor value is not in range " + str(conf.min_value)+ "-"+ str(conf.min_value) + ". The current value is " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)
	res = mybolt.digitalWrite('0', 'HIGH')
	
    else
	res = mybolt.digitalWrite('0', 'LOW')

    # Step 4
    time.sleep(10)

Credits

Valentina
1 project • 1 follower

Comments