Vetrivel Palaniappan
Published © LGPL

Plant light monitoring system

Determine whether your plants acquire the right amount of sunshine during the day and get notified via Telegram.

IntermediateFull instructions provided2 hours880
Plant light monitoring system

Things used in this project

Hardware components

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

Software apps and online services

Digital Ocean
PuTTY
Telegram
Telegram is available in play store, app store and windows store.
Bolt Cloud
Bolt IoT Bolt Cloud
Bolt IoT Android App
Bolt IoT Android App

Story

Read more

Schematics

Circuit connection

Code

conf.py, Configuration file

Python
This is the file that contains Bolt and Telegram credentials.
""" BOLT PLANT BOT CONFIGURATIONS """
bolt_api_key = "XXXXXXXX-a74d-66cd-777e-baXXXXXXXXXX"           
device_id = "BOLTXXXXXXX"                  
telegram_chat_id = "@plant_light_monitor"          
telegram_bot_id = "bot2205643668:BBCBeNETRLie9oSHmmXXXXXXXXXXXXXXXXX" 
critical_threshold = 500
FRAME_SIZE = 100
MUL_FACTOR = 2

lightt.py

Python
This is the main file.
""" BOLT PLANT BOT CONFIGURATIONS """

bolt_api_key = "XXXXXXXX-a74d-66cd-777e-baXXXXXXXXXX"           
device_id = "BOLTXXXXXXX"                  
telegram_chat_id = "@plant_light_monitor"          
telegram_bot_id = "bot2205643668:BBCBeNETRLie9oSHmmXXXXXXXXXXXXXXXXX" 
critical_threshold = 500
FRAME_SIZE = 100
MUL_FACTOR = 2

""" IMPORTING NECESSARY 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

Vetrivel Palaniappan

Vetrivel Palaniappan

1 project • 0 followers
Thanks to Bolt IoT.

Comments