I am getting this kind of error help me sovle this error

import json, requests, time
from boltiot import Bolt

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 numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O 
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier



"""Configurations for home_automation.py"""
# Bolt IoT Credentials
api_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"     #API Key of Bolt Cloud
device_id  = "BOLTxxxxxxx"                            #Device ID
# Telegram Credentials
telegram_chat_id = "@Team15_bot"
telegram_bot_id = "bot1786812Mc9dXJKGvMxxqzxHw"

URL = "https://api.telegram.org/" + telegram_bot_id

mybolt = Bolt(api_key, device_id)

light_pin1 = "12"
light_pin2 = "6"

last_message_id = None
last_text = None

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

def get_LDRsensor_value_from_pin(pin1):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        mybolt.serialWrite("LDR")
        response = mybolt.serialRead('10')
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        LDRsensor_value = int(data["value"])
        return LDRsensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999

def get_rainsensor_value_from_pin(pin2):
    """Returns the sensor value. Returns -999 if request fails"""
    try:
        mybolt.serialWrite("Rain")
        response = mybolt.serialRead('10')
        data = json.loads(response)
        if data["success"] != 1:
            print("Request not successfull")
            print("This is the response->", data)
            return -999
        rainsensor_value = int(data["value"])
        return rainsensor_value
    except Exception as e:
        print("Something went wrong when returning the sensor value")
        print(e)
        return -999

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

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


def check_device_status():
    try:
        response = mybolt.isOnline()
        response = json.loads(response)
        if response["value"] == "online":
            print("Device is Online")
            return True
        else:
            print("Device is offline")
            send_telegram_message("Device is offline")
            return False
    except Exception as e:
        print("An error occurred in Checking device status.")
        print(e)
        return False

def send_telegram_message(message):
    """Sends message via Telegram"""
    print("Sending telegram message .....")
    url = URL + "/sendMessage?text=" + message + "&chat_id=" + telegram_chat_id
    try:
        response = requests.get(url)
        content = response.content.decode("utf8")
        js = json.loads(content)
        if js["ok"] == True:
            print("Messgae sent successfully.")
            return True
        else:
            print("Message on sent. Response: " + str(js["ok"]))
            print(js)
            return False
    except Exception as e:
        print("An error occurred in sending message via Telegram")
        print(e)
        return False

def get_last_message():
    """Gets last message from Telegram"""
    print("Getting last message .....")
    url = URL + "/getUpdates"
    try:
        text = None
        response = requests.get(url)
        content = response.content.decode("utf8")
        js = json.loads(content)
        num_updates = len(js["result"])
        last_update = num_updates - 1
        message_id = js["result"][last_update]["channel_post"]["message_id"]
        text = js["result"][last_update]["channel_post"]["text"]
        print("This is the last message : " + text)
        return (text, message_id)
    except Exception as e:
        print("An error occurred in getting message from Telegram")
        print(e)

while True:
    # Step 1 : Check Device Status
    print("Checking device Status .....")
    response = check_device_status()
    if response != True:
        time.sleep(10)
        continue
    # Step 2 : Sending Welcome message with options.
    message = "Welcome to Advanced Irrigation System\nMade with ❤\n" + "-" * 55 + "\nSelect Mode -\n1. Manual ON\n2. Manual OFF\n3. AUTO\n4. Venting Mode"
    response = send_telegram_message(message)
    if response != True:
        continue
    time.sleep(10)
    # Step 3 : Getting last message from telegram
    text, message_id = get_last_message()
    # Step 4 : Checking with previous message & message_id
    if (text !=last_text) or (message_id != last_message_id):
        if (text == "1") or (text == "Manual on") or (text == "manual on"):
            mybolt.digitalWrite('1','HIGH')
            mybolt.digitalWrite('2','HIGH')

            message = "Manual ON active, Irrigation valve fully open!"
            print(message)
            r = requests.get('https://cloud.boltiot.com/remote/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxa/servoMultiWrite?pins=3,4&values=180,0&deviceName=BOLT290609')
            send_telegram_message(message)
        elif (text == "2") or (text == "Manual OFF") or (text == "manual off"):
            mybolt.digitalWrite('1','LOW')
            mybolt.digitalWrite('2','LOW')
            
            message = "Manual OFF active, Irrigation valve fully closed!"
            print(message)
            r = requests.get('https://cloud.boltiot.com/remote/-a/servoMultiWrite?pins=3,4&values=0,0&deviceName=BOLT290609')
            send_telegram_message(message)
        elif (text == "3") or (text == "AUTO") or (text == "auto"):
            mybolt.digitalWrite('1','HIGH')
            mybolt.digitalWrite('2','LOW')
            message = "AUTO MODE active!"
            print(message)
            send_telegram_message(message)
            time.sleep(5)
            levelsensor_value = get_levelsensor_value_from_pin('A1')
            print("The level sensor value is:", levelsensor_value)
            time.sleep(5)

            if levelsensor_value == -999:
                print("Request was unsuccessfull. levelsensor Skipping.")
                time.sleep(10)
                continue
            rainsensor_value = get_rainsensor_value_from_pin('A3')
            print("The rain sensor value is:", rainsensor_value)
            time.sleep(5)

            if rainsensor_value == -999:
                print("Request was unsuccessfull. rainsensor Skipping.")
                time.sleep(10)

            LDRsensor_value = get_LDRsensor_value_from_pin('A5')
            print("The light sensor value is:", LDRsensor_value)
            time.sleep(5)

            if LDRsensor_value == -999:
                print("Request was unsuccessfull. LDRsensor Skipping.")
                time.sleep(10)
                continue

            humidity = get_Humidity()
            print("The humidity sensor value is:", humidity)
            time.sleep(5)

            if humidity == -999:
                print("Request was unsuccessfull. levelsensor Skipping.")
                time.sleep(10)
                continue

            temperature = get_Temperature()
            print("The temp sensor value is:", temperature)
            time.sleep(5)

            if temperature == -999:
                print("Request was unsuccessfull. levelsensor Skipping.")
                time.sleep(10)
                continue
            if levelsensor_value <=40:
                message = "Acquiring Sensor Data"
                print(message)
                send_telegram_message(message)
                data= pd.read_csv('3848684 (33).csv')
                message = "Predicting Valve Opening."
                print(message)
                send_telegram_message(message)
                time.sleep(5)
                data.columns
                data.head()
                data.info()
                data.drop(["d_id",],axis=1,inplace=True)
                data.drop(["time_stamp",],axis=1,inplace=True)
                data.info()
                y=data.Valve.values
                #normalization
                x_data=data.drop(["Valve"], axis=1)
                x=(x_data-np.min(x_data))/(np.max(x_data) -np.min(x_data))
                x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.6,random_state=22)
                dt= DecisionTreeClassifier()
                dt.fit(x_train,y_train)
                print("score :",dt.score(x_test,y_test))
                prediction = dt.predict([[levelsensor_value,LDRsensor_value,rainsensor_value]])
                print(prediction)
                value = str(prediction[0])
                message = "Irrigation Valve at: "+value+"."
                print(message)
                send_telegram_message(message)
                r = requests.get('https://cloud.boltiot.com/remote/xxxxxxxxxxxxxxxxxxxxa/servoWrite?pin=3&value='+value+'&deviceName=BOLT290609')
                time.sleep(10)

                last_text = text
                last_message_id = message_id

            else:

                r = requests.get('https://cloud.boltiot.com/remote/1d5840cd-95f5-4eb6-9747-2987b455f67a/servoMultiWrite?pins=3,4&values=0,180&deviceName=BOLT290609')
                message = 'Level Crossed Threshold, AUTO Venting with Irrigation Valve Closed!'
                print(message)
                send_telegram_message(message)
                message = 'Level Value: '+str(levelsensor_value)+"."
                print(message)
                send_telegram_message(message)


        elif (text == "4") or (text == "Venting") or (text == "venting"):
            mybolt.digitalWrite('1','LOW')
            mybolt.digitalWrite('2','HIGH')
            message = "Venting operation Active, Irrigation Valve Closed!"
            print(message)
            send_telegram_message(message)
            r = requests.get('https://cloud.boltiot.com/remote/api key/servoMultiWrite?pins=3,4&values=0,180&deviceName=BOLT290609')
        time.sleep(10)

    else:
        text = last_text

        if (text == "1") or (text == "Manual on") or (text == "manual on"):
            mybolt.digitalWrite('1','HIGH')
            mybolt.digitalWrite('2','HIGH')
            
            message = "Manual ON, Irrigation valve fully open!"
            print(message)
            r = requests.get('https://cloud.boltiot.com/remote/api/servoWrite?pin=3&value=180&deviceName=BOLT290609')
            send_telegram_message(message)
        elif (text == "2") or (text == "Manual OFF") or (text == "manual off"):
            mybolt.digitalWrite('1','LOW')
            mybolt.digitalWrite('2','LOW')
            
            message = "Manual OFF, Irrigation valve fully closed!"
            print(message)
            r = requests.get('https://cloud.boltiot.com/remote/apia/servoWrite?pin=3&value=0&deviceName=BOLT290609')
            send_telegram_message(message)
        elif (text == "3") or (text == "AUTO") or (text == "auto"):

            mybolt.digitalWrite('1','HIGH')
            mybolt.digitalWrite('2','LOW')
            message = "AUTO MODE active!"
            print(message)
            send_telegram_message(message)
            time.sleep(5)
            levelsensor_value = get_levelsensor_value_from_pin('A1')
            print("Level sensor value is:", levelsensor_value)
            time.sleep(5)

            if levelsensor_value == -999:
                print("Request was unsuccessfull. levelsensor Skipping.")
                time.sleep(10)
                continue
            rainsensor_value = get_rainsensor_value_from_pin('A3')
            print("The rain sensor value is:", rainsensor_value)
            time.sleep(5)

            if rainsensor_value == -999:
                print("Request was unsuccessfull. rainsensor Skipping.")
                time.sleep(10)

            LDRsensor_value = get_LDRsensor_value_from_pin('A5')
            print("The light sensor value is:", LDRsensor_value)
            time.sleep(5)

            if LDRsensor_value == -999:
                print("Request was unsuccessfull. LDRsensor Skipping.")
                time.sleep(10)
                continue

            humidity = get_Humidity()
            print("The humidity sensor value is:", humidity)
            time.sleep(5)

            if humidity == -999:
                print("Request was unsuccessfull. levelsensor Skipping.")
                time.sleep(10)
                continue

            temperature = get_Temperature()
            print("The temp sensor value is:", temperature)
            time.sleep(5)

            if temperature == -999:
                print("Request was unsuccessfull. levelsensor Skipping.")
                time.sleep(10)
                continue
            if levelsensor_value <=40:
                message = "Acquiring Sensor Data"
                print(message)
                send_telegram_message(message)
                data= pd.read_csv('3848684 (33).csv')
                message = "Predicting Valve Opening."
                print(message)
                send_telegram_message(message)
                time.sleep(5)
                data.columns
                data.head()
                data.info()
                data.drop(["d_id",],axis=1,inplace=True)
                data.drop(["time_stamp",],axis=1,inplace=True)
                data.info()
                y=data.Valve.values
                #normalization
                x_data=data.drop(["Valve"], axis=1)
                x=(x_data-np.min(x_data))/(np.max(x_data) -np.min(x_data))
                x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.6,random_state=22)
                dt= DecisionTreeClassifier()
                dt.fit(x_train,y_train)
                print("score :",dt.score(x_test,y_test))
                prediction = dt.predict([[levelsensor_value,LDRsensor_value,rainsensor_value]])
                print(prediction)
                value = str(prediction[0])
                message = "Irrigation Valve at: "+value+"."
                print(message)
                send_telegram_message(message)
                r = requests.get('https://cloud.boltiot.com/remote/api/servoWrite?pin=3&value='+value+'&deviceName=BOLT290609')
                time.sleep(10)

                last_text = text
                last_message_id = message_id

            else:

                r = requests.get('https://cloud.boltiot.com/remote/api/servoMultiWrite?pins=3,4&values=0,180&deviceName=BOLT290609')
                message = 'Level Crossed Threshold, AUTO Venting with Irrigation Valve Closed!'
                print(message)
                send_telegram_message(message)
                message = 'Level Value: '+str(levelsensor_value)+"."
                print(message)
                send_telegram_message(message)

        elif (text == "4") or (text == "Venting") or (text == "Venting"):
            mybolt.digitalWrite('1','LOW')
            mybolt.digitalWrite('2','HIGH')
            
            message = "Venting operation Active, Irrigation Valve Closed!"
            print(message)
            send_telegram_message(message)
            r = requests.get('https://cloud.boltiot.com/remote/apia/servoMultiWrite?pins=3,4&values=0,180&deviceName=BOLT290609')
    time.sleep(10)

    This is the code.

And this is the error I am facing

Checking device Status …
Device is offline
Sending telegram message …
Message on sent. Response: False
{‘ok’: False, ‘error_code’: 400, ‘description’: ‘Bad Request: chat not found’}

First important thing, you just shared your API KEY and now you are vulnerable, I request you to immediately remove that from this post and also generate new API key, immediately.

Second thing, the error says your device is offline that means either it is not powered up or it is not connected to internet and Bolt Cloud. So, check that too.

1 Like

tq sir, but blt iot is powered and connected.
iot module is giving alert of sensor values

Hi @arun997722,

Your send_telegram_message function seems incorrect. Use the below line instead.

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
    )
    content = response.content.decode("utf8")
    js = json.loads(content)
    if js["ok"] == True:
        print("Messgae sent successfully.")
        return True
    else:
        print("Message on sent. Response: " + str(js["ok"]))
        print(js)
        return False

Do let me know in case you need any other information.

1 Like

sir are u the one who have done iot project on advance irrigation system and published in hackster.io

@rahul.singh1 sir are u the one who have done iot project on advance irrigation system and published in hackster.io

@rahul.singh1 @techieonmail


this is the error i am facing can u help me solve it.

If you haven’t stored your API KEY and Bolt ID in conf.py file than you shouldn’t use it in code like in tutorials. Just remove conf as I can see you have stored all variables inside your main file.

Hey Arun! Can you please specify whether you have created any configuration file or conf.py file for storing variables such as API_key and Bolt_device_id
Also where have you stored these variables telegram_bot_id and telegram_chat_id.

hello Sir @rahul.singh1
I am really not able to understand , what’s happening . I am getting errors from twilio, mailgun and telegram all please help me out

@kumarabhayb14

from 1st screenshot i think you have not imported requests module correctly please check its spelling error also check in your code for sending telegram message that “response=requests.request(“POST”,url,params=data)” this line is correct and url variable should be proper and correct.

for SMS alert using Twilio , it is not a reliable service as its free it uses shared server and it can take time to send message atleast 2-5 mins or more. sometime it doesn’t responds and your request is not processed.

@kumarabhayb14
first of all thank you for respect but iam just another student no need for sir.

I think either your telegram chat id or bot id is wrong please cross check every letter and try to write code in single line like this response code should be in one line

response = requests.request(
“POST”,
url,
params=data
)
right way - response = requests.request(“POST”,url,params=data)

sometime it gives error if not written in same line and do this for other code to which is written in many lines.

@sachinormalia7 Thankyou . I replaced my chat id from another channel in telegram called get id , i get a chat id in form of numbers. I used that and it worked .
Thankyou for your valuable time.

@arun997722 Sir first check whether api key and device id are entered properly. Also make sure the bolt iot module along with the circuit is connected to a laptop or smartphone by usb. It is also recommended that the recipient smartphone must have stable internet connection and To:- number is entered properly

Hope this solves the problem.

Do reply when issue gets solved.