Temperature alert url not defined problem





getting url not defined problem and sensor_ value not defined problem can any one solve it

Hi @precioussrikanth55,

Here is the corrected code for the same:

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.threshold:
        print("Sensor value has exceeded threshold")
        message = "Alert! Sensor value has exceeded " + str(conf.threshold) + \
                  ". The current value is " + str(sensor_value)
        telegram_status = send_telegram_message(message)
        print("This is the Telegram status:", telegram_status)

    # Step 4
    time.sleep(10)

Create a config file to hold the configuration variables for Boltiot and Telegram. Type the command sudo nano conf.py to create a new file and edit it in the shell.

Paste the following lines of code into the conf file and press CTRL + X followed by ENTER key to save it.

"""Configurations for telegram_alert.py"""
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 @ symbol.
telegram_bot_id = "botXXXX"           # This is the bot ID of the created Telegram Bot. Paste after bot text.
threshold = 250                       # Threshold beyond which the alert should be sent
  1. bolt_api_key → This is your API key for the Bolt Cloud. Visit this link to find your API key. Paste the API key inside the double inverted commas.
  2. device_id → This is the ID of your Bolt device. It will be something like BOLT12345. Paste the device ID inside the double inverted commas.
  3. telegram_chat_id → This is the channel ID which we have got when creating the channel on Telegram. It will be under the permanent link name of the channel. Paste the channel id after the @ symbol. Do not keep any spaces.
  4. telegram_bot_id → This is the ID of the Telegram bot we have got when creating the Bot. It will be mentioned under the “BotFather” channel as token. Paste the token after the “bot” text. Do not keep any spaces.
  5. threshold → This is the threshold beyond which the alert should be sent. Configure this value to receive alerts accordingly.

Add the configurations to your conf.py file and save it.

Please check your code properly and make sure you dont miss out on the above mentioned steps.