The project is very straightforward and simple. LM35 as shown in figure 1 is a temperature sensor whose voltage varies with respect to the surrounding temperature. It has three terminals mainly Vcc, Output and Ground. A buzzer is used which has two terminals, the one with longer leg is positive and the shorter one is negative. See figure 2. They are connected using Bolt Wifi Module with proper connections (explained later). When the temperature rises above the required condition either in a factory or just in a room, the buzzer is set high to indicate the change in temperature so that the concerned people can take necessary action. If they are far away a message will be generated on time and will be sent to notify them using Telegram with the help of Bolt wifi module, Digital Ocean and Putty.
The required circuit connection is shown below in figure 3. The Vcc pin (left most pin) of LM35 is connected to 5V pin on Bolt Wifi Module. The Output pin (middle pin) of LM35 is connected to the Analog pin A0. The ground pin (right most pin) of LM35 is connected to the Gnd pin of Bolt Wifi module. The positive pin of the Buzzer is connected to the Digital pin 0. It can be connected to any digital pin as there are 5 digital pins in Bolt Wifi module. And the negative pin is grounded as shown below.
a) Connect the Bolt wifi module to bolt cloud using the link https://cloud.boltiot.com
b) Open Products from the Dashboard and click on “Add Product”.
c) Further give a name to your product. Select Input devices to connect to bolt. And GPIO option to collect data.
d) Configure the Bolt wifi module by selecting A0 pin and then click on save.
e) Press on the link button to link the bolt wifi module to the product created.
f) When the device is connected, it will show the device is online. Copy the Device Id as it will be used for further configuration.
g) Go to the API section to get your API key.
f) Create a Digital ocean account using the link https://www.digitalocean.com/ and create a droplet wherein choose 64-bit Ubuntu 16.04.6. Later choose adequate storage ($5/ month plan is recommended) and select the nearest city to you as the data center.
g) Finalize and create your account. You also have an option of deactivating your account when you don’t need it anymore.
h) To access the droplet download Putty if you have windows OS from the link https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
i) After downloading Putty MSI on your PC/laptop, install it. A window pops up on your screen. Enter the IP address you received on your email id in the “Host name (or IP address)”. The IP address can also be copied from the droplet in Digital Ocean Website.
j) Click open. Login as root and type the password as given in your mail id and press enter.
Also the password entered will not be visible.
4. Creating Telegram bot and channel
Download Telegram app from Play store/ App store and sign in. A telegram channel is nothing but similar to a group in Whatsapp.
A channel can be created if you go to home and select "New Channel". Set the channel as public and provide a permanent link when asked in the next screen.
Next a bot for the channel is created to post alerts in the channel created. Search for "BotFather" in the home screen of the app. In BotFather chat box, type "/start" to get started with the process. To create a new bot, type "/newbot". Choose a name for your bot and also a username. That's it! Bot is created.
To add the bot to the created Channel, open the channel details and click on Administrators. In there search for your bot username and add it. Make sure that the bot has the rights to post messages by enabling it. Now we will look into the code by which we can enable sending messages.
5. CodeIn Putty to create a new python file use the below command
sudo nano filename.py
Now let’s open a python file to note down the configurations.
Refer the above figure and create configuration python file conf.py
Save and close the file.
Open a new file to write the main code. There are 2 sections in the code.
- Creating two functions for collecting the sensor value and sending the message in telegram.
- To compare the sensor value with that of the threshold value and determining to switch on the buzzer and send an alert if the value exceeds.
In first section,
We import necessary files and libraries like time, json, requests, Bolt. And also we import our configuration file which we created before. Http requests are made by importing requests. Json is a library used for handling all operations on json objects. Time is used to manage sleep time.
We access the api key and device id from conf.py by creating an object mybolt. We define a function get_sensor_value_from_pin to access the latest data from the temperature sensor through the particular pin. We convert the response to json data for ease of use. Before converting it back to normal response we check whether the request made was successful. If it returns anything other than 1 then it was unsuccessful. The code returns -999 if there is an error.
The function send_telegram_message is defined to send message via Telegram. Here url is created with having the respective telegram bot id into account so that the message is sent to the respective people. A dictionary is created using the variable name data. It contains the chat/channel id which specifies bot which channel the message has to be posted. The text inside the data contains the message that should be sent.
A HTTP request is made to the Telegram servers using the URL constructed earlier with the data. This request is the “POST”.
The text is converted to JSON format so that we can check the status of the message. The “ok” field of the telegram_data returns a Boolean value which gives the status of the message. If true, the message was sent successfully, else unsuccessful. The try-except block returns false if any errors occur in try block.
In second section,
We declare an infinite while loop. We call the function get_sensor_value() and try to get the value from the analog pin A0. If there’s an error in acquiring the sensor value then the function returns -999. Thus, we wait for 10 seconds then go back to the beginning of the loop using continue command and repeat the process.
Once we are successful in receiving the sensor value we compare it with the threshold value which is specified in conf.py. If the value exceeds the threshold then the alarm is set by switching on the buzzer. And an alert message is created and sent to the concerned people by calling the function send_telegram_message(). The alarm is set for a minute and the program waits for another 10 seconds and repeats the whole process again.
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:
buzzer= mybolt.digitalWrite(0, ‘HIGH’)
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(30)
buzzer= mybolt.digitalWrite(0, ‘LOW’)
time.sleep(10)
To run the code use command
sudo python3 filename.py
Replace the filename with the name you have given to your python file. To stop the code from running press CTRL+C. The console output when the program is executed is shown below.
When the program runs successfully, the buzzer is set high and a telegram message is sent instantly as shown below.
Comments
Please log in or sign up to comment.