Anishma
Published © GPL3+

BoltPlantBot

BoltPlantBot glows LED Grow light for light intensity below threshold and alerts with buzzer and telegram bot message with anomaly detection

IntermediateFull instructions provided15 hours391
BoltPlantBot

Things used in this project

Hardware components

Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LED (generic)
LED (generic)
×1
Buzzer
Buzzer
×1
Resistor 330 ohm
Resistor 330 ohm
×2
LDR, 5 Mohm
LDR, 5 Mohm
×1
Male/Male Jumper Wires
×7
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1

Software apps and online services

Telegram
Jupyter Notebook
Jupyter Notebook
Bolt Cloud
Bolt IoT Bolt Cloud

Story

Read more

Schematics

BoltPlantBot Circuit Diagram

This is the circuit setup and connection of BoltPlantBot

Code

Code for BoltPlantBot

Python
Run the entire code in jupyter notebook
""" BOLT PLANT BOT CONFIGURATIONS """

bolt_api_key = "26651a77-b86d-44aa-888e-ba3XXXXXXXX"           
device_id = "BOLTXXXXXXXX"                  
telegram_chat_id = "@my_plant_status_bot"          
telegram_bot_id = "bot1187250775:AAEBePFHHOie9QAoFHch1XajXXXXXXXXX" 
critical_threshold = 500
FRAME_SIZE = 100
MUL_FACTOR = 2

""" IMPORTING PACKAGES """
import requests             
import json                 
import time
import math,statistics
from boltiot import Bolt

mybolt = Bolt(bolt_api_key,device_id)
 
""" FUNCTIONS """
def get_sensor_value_from_pin(pin):
    
    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("Oops!...Something went wrong.Please check!!!")
        print(e)
        return -999


def send_telegram_message(message):

    url = "https://api.telegram.org/" + telegram_bot_id + "/sendMessage"
    data = {
        "chat_id": telegram_chat_id,
        "text": message
    }
    try:
        response = requests.request(
            "POST",
            url,
            params=data
        )
        print("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("Error occurred!!! while sending message")
        print(e)
        return False

def compute_bounds(history_data,frame_size,factor):
    if len(history_data)<frame_size :
        return None

    if len(history_data)>frame_size :
        del history_data[0:len(history_data)-frame_size]
    Mn=statistics.mean(history_data)
    Variance=0
    for data in history_data :
        Variance += math.pow((data-Mn),2)
    Zn = factor * math.sqrt(Variance / frame_size)
    High_bound = history_data[frame_size-1]+Zn
    Low_bound = history_data[frame_size-1]-Zn
    return [High_bound,Low_bound]

""" DRIVER CODE """

history_data=[]

while True:
    
    sensor_value = get_sensor_value_from_pin("A0")    
    print("The current sensor value is:", sensor_value)
    
    if sensor_value == -999:
        print("Request Unsuccessfull")
        time.sleep(5)
        continue
    
    elif sensor_value == critical_threshold:
        print("Check Me")
        message = "Alert! Critical point reached " + \
                  "Current value is " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("Telegram status:", telegram_status)
        mybolt.digitalWrite("0", "LOW")
        mybolt.digitalWrite("1","100")
        time.sleep(1)
        mybolt.digitalWrite("1","LOW")
        
    elif sensor_value < critical_threshold:
        print("Save me..I am not receiving enough light")
        message = "Alert! I need minimum of " + str(critical_threshold) + \
                  "But Current value is " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("Telegram status:", telegram_status)
        mybolt.digitalWrite("0", "HIGH")
        mybolt.digitalWrite("1", "HIGH")
        time.sleep(1)
        mybolt.digitalWrite("1", "LOW")
        
    elif sensor_value > critical_threshold:
        print("I am fine...")
        message = "I am Fine:) Normal light receiving..."+\
                  "Current value = " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("Telegram status:", telegram_status)
        mybolt.digitalWrite("0", "LOW")
        
    time.sleep(5)
    
#Anomaly detection

    response = mybolt.analogRead('A0')
    data = json.loads(response)
    if data['success'] != 1:
        print("There was an error while retriving the data.")
        time.sleep(5)
        continue
    sensor_value=0
    try:
        sensor_value = int(data['value'])
    except e:
        print("There was an error while parsing the response: ",e)
        continue

    bound = compute_bounds(history_data,FRAME_SIZE,MUL_FACTOR)
    if not bound:
        required_data_count=FRAME_SIZE-len(history_data)
        history_data.append(int(data['value']))
        time.sleep(5)
        continue

    try:
        if sensor_value > bound[0] :
            print("Anomaly Detected")
            telegram_status = send_telegram_message("Sudden Increase Anomaly Detected")
        elif sensor_value < bound[1]:
            print("Anomaly Detected")
            telegram_status = send_telegram_message("Sudden Decrease Anomaly Detected")
        history_data.append(sensor_value);
    except Exception as e:
        print ("Error",e)
    time.sleep(10)

Credits

Anishma

Anishma

1 project • 0 followers

Comments