ArjunRajendran
Published © GPL3+

Rain Alert System (RAS) Using Bolt IoT

A unique system for the identification of rainfall, which can be utilized further in futuristic applications of IoT.

IntermediateFull instructions provided2 hours1,102
Rain Alert System (RAS) Using Bolt IoT

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
The core one!
×1
Rain Drop Detector
This is the part used for sensing!
×1
Rain drop controller
Used as an interfacing unit! which provides an analog and digital output.
×1
Jumper wires (generic)
Jumper wires (generic)
×5

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
SMS Messaging API
Twilio SMS Messaging API
Mailgun
Used for mailing service!
Telegram Bot father
Used for sending telegram messages.

Story

Read more

Schematics

Rain Alert System(RAS)

I have connected VCC pin of controller to the 5v pin of bolt module.
A0 pin of controller to the A0 pin of bolt module.
GND pin of controller to the GND pin of the bolt module.
+ve and -ve terminal of detector to the controller terminals.

Code

Rain_Alert_system.py

Python
This program used for detecting the rainfall and alert the user!
import requests                 # for making HTTP requests
import json                     # library for handling JSON data
import time                     # module for sleep operation

from boltiot import Email,Sms,Bolt  # importing Bolt from boltiot module
# config file
"""Configurations for Rain_alert.py"""
bolt_api_key = "980fa1a1-d93b-f2c35a6adc78"                 # Bolt Cloud API Key
device_id = "BOLT60977"                    # Device ID 
telegram_chat_id = "@arjun_temper"            #channel ID of the created Telegram channel,Paste after @ symbol.
telegram_bot_id = "bot873174785:AVG48UV7R7gwhLwXiG7VLGa53UQ"           # Telegram bot ID 
SID = 'AC641d6db42357df39dba21a314f8'    #Twillio sid
AUTH_TOKEN = 'ef2bb5c753c088852ff1f3a38e6'  #Twillio token
FROM_NUMBER = '+133451623' #your Twillio number
TO_NUMBER = '+9186088459'  #Recipient Number
MAILGUN_API_KEY = 'a25e18948e0c61dbfeb-a77-764fe7b7' #Mailgun api 
SANDBOX_URL= 'sandbox22f028b14534abf62fe6592f31c8.mailgun.org'   #Sandbox url
SENDER_EMAIL = 'test@sandbox22f028b1434abff31c8.mailgun.org' #sender mail id
RECIPIENT_EMAIL = 'sr.arjun3@gmail.com' #recipient mail id
threshold=1024
threshold1=950
threshold2=850
threshold3=500
threshold4=10 







mybolt = Bolt(bolt_api_key, device_id)
sms = Sms(SID, AUTH_TOKEN, TO_NUMBER, FROM_NUMBER)
mailer = Email(MAILGUN_API_KEY,SANDBOX_URL,SENDER_EMAIL,RECIPIENT_EMAIL)

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/" + telegram_bot_id + "/sendMessage"
    data = {
        "chat_id":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:
    # Reading  sensor  value
    sensor_value = get_sensor_value_from_pin("A0")
    print("The current sensor value is:",sensor_value)
    # Check if it fails
    if sensor_value == -999:
        print("Request was unsuccessfull. Skipping.")
        time.sleep(10)
        continue
    
    # Initial comparison
    if sensor_value == threshold:
        print("Checking for rain")
        print("The sensor value is still",sensor_value)        
        

    #950
    if sensor_value >= threshold1 and sensor_value < threshold:
       print("There is possibility for modereate rain")
       print("The sensor value is ",sensor_value)
       response0 = mailer.send_email("Rain Alert", "The Current sensor value is " +str(sensor_value)+"There is possibility for moderate rain,Don't Worry-Rain alert system")
       response_text = json.loads(response0.text)
       print("Response received from Mailgun is: " + str(response_text['message']))
       response1 = sms.send_sms("The Current sensor value is " +str(sensor_value)+"There is possibility for moderate rain-Rain alert system")
       print("Response received from Twilio is: " + str(response1))
       print("Status of SMS at Twilio is :" + str(response1.status))
       message = "There is possibility for moderate rain" + \
                  ". The current value is " + str(sensor_value)+"-Rain alert system"
       telegram_status = send_telegram_message(message)
       print("This is the Telegram status:", telegram_status)


    #850
    if sensor_value >= threshold2 and sensor_value <= threshold1:
       print("The Rain is falling down in a lightweight manner")
       print("The sensor value is ",sensor_value)
       message = "Rain Alert! There ia possibility for light rainfal " + \
                  ". The current value is " + str(sensor_value)+"-Rain alert system"
       telegram_status = send_telegram_message(message)
       print("This is the Telegram status:", telegram_status)
       response0 = mailer.send_email("Rain Alert", "The Current sensor value is " +str(sensor_value)+"There is possibility for light rainfall-Rain alert system")
       response_text = json.loads(response0.text)
       print("Response received from Mailgun is: " + str(response_text['message']))
       response1 = sms.send_sms("The Current sensor value is " +str(sensor_value)+"There is possibility for light rainfall!-Rain alert system")
       print("Response received from Twilio is: " + str(response1))
       print("Status of SMS at Twilio is :" + str(response1.status))

    #500
    if sensor_value >= threshold3 and sensor_value <= threshold2:
       print("Heavy rain is falling hurry up!")
       print("The sensor value is ",sensor_value)
       message = "Rain Alert! Heavy Rain is falling,Hurry up!"+ \
                  ". The current value is " + str(sensor_value)+"-Rain alert system"
       telegram_status = send_telegram_message(message)
       print("This is the Telegram status:", telegram_status)
       print("The sensor value is still",sensor_value)
       response0 = mailer.send_email("Rain Alert", "The Current sensor value is " +str(sensor_value)+"There is possibility for Heavy rainfall -Rain alert system")
       response_text = json.loads(response0.text)
       print("Response received from Mailgun is: " + str(response_text['message']))
       response1 = sms.send_sms("The Current sensor value is " +str(sensor_value)+"There is possibility for Heavy Rainfall,Hurry up!-Rain alert system")
       print("Response received from Twilio is: " + str(response1))
       print("Status of SMS at Twilio is :" + str(response1.status))

    #0and10
    if sensor_value == 0 or (sensor_value >= threshold4 and sensor_value <= threshold3):
       print("Massive Rain with thunder is falling")
       print("The sensor value is ",sensor_value)
       message = "Rain Alert!Massive rain with thunder is falling,Hurry up! " + \
                  ". The current value is " + str(sensor_value)+"-Rain alert system"
       telegram_status = send_telegram_message(message)
       print("This is the Telegram status:", telegram_status)
       print("The sensor value is still",sensor_value)
       response0 = mailer.send_email("Rain Alert", "The Current sensor value is " +str(sensor_value)+"There is possibility for Maasive rain is falling!-Rain alert system")
       response_text = json.loads(response0.text)
       print("Response received from Mailgun is: " + str(response_text['message']))
       response1 = sms.send_sms("The Current sensor value is " +str(sensor_value)+"Massive rain is falling,Hurry up!-Rain alert system")
       print("Response received from Twilio is: " + str(response1))
       print("Status of SMS at Twilio is :" + str(response1.status))




    # Step 4
    time.sleep(5)

Credits

ArjunRajendran

ArjunRajendran

1 project • 2 followers

Comments