Jump to content
Electronics-Lab.com Community

Node-Red Telegram Bot with Temperature Logger Part-1


Recommended Posts

Node-RED is a powerful tool for creating and connecting IoT applications with a graphical interface. It allows you to drag and drop nodes that can perform various functions, such as reading sensors, sending messages, storing data, and more. One of the nodes that Node-RED supports is the Telegram bot node, which enables you to communicate with your IoT devices using the popular messaging platform Telegram.

In this article, I will show you how to use Node-RED and Telegram bot to send temperature data from a DHT11 sensor connected to a Raspberry Pi. You will be able to ask your Telegram bot for the current temperature and receive a reply with the sensor reading. You will also be able to set up a notification system that will alert you when the temperature exceeds a certain threshold.

To follow this tutorial, you will need the following components:

  • A Raspberry Pi with Node-RED installed (you can follow this guide to install Node-RED on your Raspberry Pi)
  • A DHT11 temperature and humidity sensor
  • A Telegram account and a Telegram bot token (you can follow this guide to create a Telegram bot and get its token)

 

Get PCBs For Your Projects Manufactured
3_t46n7cozgf.PNG?auto=compress%2Cformat&
 

You must check out PCBWAY for ordering PCBs online for cheap!

You get 10 good-quality PCBs manufactured and shipped to your doorstep for cheap. You will also get a discount on shipping on your first order. Upload your Gerber files onto PCBWAY to get them manufactured with good quality and quick turnaround time. PCBWay now could provide a complete product solution, from design to enclosure production. Check out their online Gerber viewer function. With reward points, you can get free stuff from their gift shop. Also, check out this useful blog on PCBWay Plugin for KiCad from here. Using this plugin, you can directly order PCBs in just one click after completing your design in KiCad.

Step 1: Connect the DHT11 sensor to the Raspberry Pi

The first step is to connect the DHT11 sensor to the Raspberry Pi using the breadboard and the jumper wires. The DHT11 sensor has four pins: VCC, Data, NC (not connected), and GND. Connect the VCC pin to the 3.3V pin of the Raspberry Pi, the GND pin to the GND pin of the Raspberry Pi, and the Data pin to the GPIO 4 of the Raspberry Pi.

Step 2: Install the node-red-contrib-dht-sensor node

To read data from the DHT11 sensor, we will use a Node-RED node called node-red-contrib-dht-sensor. To install this node, open a terminal on your Raspberry Pi and run the following command:

npm install node-red-contrib-dht-sensor
image_dzzWbWqipo.png?auto=compress%2Cfor
 

Then restart Node-RED by running:

node-red-stop
node-red-start

Then add the DHT11 node and open the properties then choose your DHT11 pin.

image_ACcqe5Kvgy.png?auto=compress%2Cfor
 

Here is the simple flow to get the DHT11 temperature and humidity data.

image_yYC0IuNEKG.png?auto=compress%2Cfor
 

And here are the debug log results.

image_JLf8uiVYWw.png?auto=compress%2Cfor
 
Step 3: Setting up the Telegram Bot

Go to Google Play or App Store, download, and install Telegram. In my case, I'm using telegram web. First, search for “botfather” and click the BotFather as shown below.

image_TaVEFkDdmH.png?auto=compress%2Cfor
 

Next, start the BotFather, and use /newbot to create a new bot.

image_hr6z9lNAAb.png?auto=compress%2Cfor
 

Next, name your bot.

image_ec1cs7iae2.png?auto=compress%2Cfor
 

Then, mention the username.

image_pVgWWxT0DW.png?auto=compress%2Cfor
 

Finally, it will show you the api key.

image_B8DZT12evC.png?auto=compress%2Cfor
 
Step 4: Creating a user for Telegram Bot

Anyone that knows your bot username can interact with it. To make sure that we ignore messages that are not from our Telegram account (or any authorized users), you can get your Telegram User ID.

In your Telegram account, search for “IDBot

image_TXpvgVbasS.png?auto=compress%2Cfor
 

Start a conversation with that bot and type /getid. You will get a reply with your user ID. Save that user ID because you’ll need it later in this tutorial.

Step 5: Configure the Telegram bot node

To communicate with the Telegram bot, we will use a Node-RED node called node-red-contrib-telegrambot. To install this node, open a terminal on your Raspberry Pi and run the following command:

npm install node-red-contrib-telegrambot
image_m2XXQz5GrN.png?auto=compress%2Cfor
 

Then restart Node-RED by running:

node-red-stop
node-red-start

Then open your Node-RED editor in your browser (usually http://raspberrypi:1880) and drag a Telegram receiver node and a Telegram sender node from the palette to the workspace. Double-click on the Telegram receiver node and click on the pencil icon next to Bot configuration.

image_5NqHSeYmhe.png?auto=compress%2Cfor
 

Enter your bot name and token that you obtained from BotFather in step 1. Then click on Add and Done.

image_Eoo89Fhmxt.png?auto=compress%2Cfor
 

Step 6: Create a flow to request temperature readings

To request temperature readings from the DHT11 sensor, we will create a simple flow that consists of three nodes: a Telegram receiver node, a DHT11 sensor node, and a Telegram sender node.

In this flow, if you trigger the node, it will send you the temp and humi data to the Telegram bot.

image_KEA0i1fpIO.png?auto=compress%2Cfor
 

Here is the function that transfers the sensor data to the Telegram bot.

Note: In the chatID add your telegram chat ID.
image_aVum1iRboc.png?auto=compress%2Cfor
 

Here is the complete JSON flow:

[
    {
        "id": "eb8f9c0d054be30c",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f2f9819ae972ae60",
        "type": "rpi-dht22",
        "z": "eb8f9c0d054be30c",
        "name": "",
        "topic": "rpi-dht22",
        "dht": "11",
        "pintype": 1,
        "pin": "7",
        "x": 640,
        "y": 240,
        "wires": [
            [
                "9113d1cb21f22ab6",
                "ea63aa67.c972f"
            ]
        ]
    },
    {
        "id": "9113d1cb21f22ab6",
        "type": "debug",
        "z": "eb8f9c0d054be30c",
        "name": "debug 5",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 800,
        "y": 200,
        "wires": []
    },
    {
        "id": "ea63aa67.c972f",
        "type": "template",
        "z": "eb8f9c0d054be30c",
        "name": "",
        "field": "payload",
        "fieldType": "msg",
        "format": "handlebars",
        "syntax": "mustache",
        "template": "{\"chatId\": XXXXXXXXXXXXXXX,\n\"type\":\"message\",\n\"content\":\"Temperature : {{payload}}, Humidity : {{humidity}}\"}",
        "output": "json",
        "x": 820,
        "y": 260,
        "wires": [
            [
                "9e00d0a7.d5ccf",
                "600063bd96d765e6"
            ]
        ]
    },
    {
        "id": "9e00d0a7.d5ccf",
        "type": "debug",
        "z": "eb8f9c0d054be30c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 990,
        "y": 200,
        "wires": []
    },
    {
        "id": "600063bd96d765e6",
        "type": "telegram sender",
        "z": "eb8f9c0d054be30c",
        "name": "roboerto_bot",
        "bot": "ae1a60539b8e5308",
        "haserroroutput": true,
        "outputs": 2,
        "x": 1030,
        "y": 260,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "b6d651f1ea520a5a",
        "type": "inject",
        "z": "eb8f9c0d054be30c",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 420,
        "y": 240,
        "wires": [
            [
                "f2f9819ae972ae60"
            ]
        ]
    },
    {
        "id": "ae1a60539b8e5308",
        "type": "telegram bot",
        "botname": "roboerto_bot",
        "usernames": "",
        "chatids": "",
        "baseapiurl": "",
        "updatemode": "polling",
        "pollinterval": "300",
        "usesocks": false,
        "sockshost": "",
        "socksprotocol": "socks5",
        "socksport": "6667",
        "socksusername": "anonymous",
        "sockspassword": "",
        "bothost": "",
        "botpath": "",
        "localbotport": "8443",
        "publicbotport": "8443",
        "privatekey": "",
        "certificate": "",
        "useselfsignedcertificate": false,
        "sslterminated": false,
        "verboselogging": false
    }
]

 

Conclusion

In this article, you learned how to use Node-RED and Telegram bot to send temperature data from a DHT11 sensor connected to a Raspberry Pi. You also learned how to request sensor readings and receive alerts using Telegram commands and messages. You can use this project as a starting point for your own IoT applications that involve Node-RED, Telegram bot, and Raspberry Pi. You can also extend this project by adding more sensors, nodes, and functions to create more complex and interactive flows. Have fun!

Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
  • Create New...