Thursday, July 12, 2018

Publish Temperature, Humidity to Telegram using Rasp Pi, NodeRed


Hi,

In my previous post, I explained how to get temperature, humidity using DHT11 sensor using Node Red, Raspberry Pi . If you have not completed it, request you to refer to the below post : 

https://johnsylvester-johnsylvester.blogspot.com/2018/07/getting-started-with-raspberry-pi-dht11.html

I. Telegram Bot creation and Configuration:

a) Go to Telegram and search for BotFather and give
 
1 )  /newbot
2) Choose a name for your bot, in my case name is johnsylvester
3) Choose Bot name , keep in mind, it should end with "Bot" keyword , in my case it is "JohnSylvesterBot"






b)  You will get a Congratulations message. Save the Bot Token  , it will be required for configuring Node Red Telegram module.


c)  If you like to send messages to groups, you need to enable it as shown below in BotFather

1) /setjoingroups
2) it will ask for bot name : @JohnSylvesterBot
3) Give keyword : Enable , you will get a Success message saying it is enabled.




d) Exit BotFather and search for newly created Bot, in my case, it is johnsylvester bot :



e) Start johnsylvester bot  using /start command:



f) Search for "IDBot"  and start it using /start command.

 1) Give the newly created Bot name : JohnSylvesterBot
 2) Issue /getid to get the Bot id and save it, it will be required for the Node Red Flow.













II. Installing Telegram in Node Red :


a) First install all Telegram Node Red modules going to manage palettes as shown below :

b) Take the Node Red Flow from the earlier post  and complete the Node Red flow as shown below :

https://johnsylvester-johnsylvester.blogspot.com/2018/07/getting-started-with-raspberry-pi-dht11.html


c) Configure the Telegram Sender node with Bot Name and Token saved earlier as follows :



   Click Add and then Done :

d) Give the below coding in the Function node connecting Telegram Sender .Provide the chatId which was saved from IDBot   /getid : 




var msgcontent = msg.payload;

jsonmsg = {};

jsonmsg["chatId"] = "XXXXX";
jsonmsg["type"] = "message";
jsonmsg["content"] = msgcontent;
msg.payload = jsonmsg;
return msg;


Node Red Flow ( chatId masked ): 

[{"id":"dd8b2bf2.74c1d8","type":"inject","z":"ca6ebb28.cf4718","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":105,"y":147,"wires":[["d4fc5c1f.04397"]]},{"id":"d4fc5c1f.04397","type":"rpi-dht22","z":"ca6ebb28.cf4718","name":"","topic":"rpi-dht22","dht":"11","pintype":"0","pin":4,"x":354,"y":149,"wires":[["815e3de7.d095"]]},{"id":"815e3de7.d095","type":"function","z":"ca6ebb28.cf4718","name":"function","func":"var temp = msg.payload;\nvar hum  = msg.humidity;\n\nmsg.payload = \"The Temperature at Chennai is : \" + temp + \" C humidity : \"  + hum + \" %\";\n\nreturn msg;","outputs":1,"noerr":0,"x":515,"y":205,"wires":[["9a83148c.58fd18","5bad32f3.17b5ec"]]},{"id":"9a83148c.58fd18","type":"debug","z":"ca6ebb28.cf4718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":752,"y":214,"wires":[]},{"id":"ff2ed10c.1f0da","type":"telegram sender","z":"ca6ebb28.cf4718","name":"","bot":"af6e0a39.4fd4f8","x":798,"y":412,"wires":[[]]},{"id":"5bad32f3.17b5ec","type":"function","z":"ca6ebb28.cf4718","name":"Construct Telegram msg","func":"var msgcontent = msg.payload;\n\njsonmsg = {};\n\njsonmsg[\"chatId\"] = \"XXXXXX\";\njsonmsg[\"type\"] = \"message\";\njsonmsg[\"content\"] = msgcontent;\nmsg.payload = jsonmsg;\nreturn msg;\n","outputs":1,"noerr":0,"x":600,"y":331,"wires":[["ff2ed10c.1f0da"]]},{"id":"af6e0a39.4fd4f8","type":"telegram bot","z":"","botname":"JohnSylvesterBot","usernames":"","chatids":"","baseapiurl":"","pollinterval":"300"}]

III. Deploy & Test the Node Red Flow :

a) Deploy the flow.
b) Click the Inject module.

Output will appear as follows :


Now , you have successfully got the temperature & humidity published to Telegram and debug screen.


Kindly share your comments to improve my posting.

No comments:

Post a Comment

Mastering IoT Development

Hi, Before reading this post, I would recommend to read the post : https://johnsylvester-johnsylvester.blogspot.com/2018/09/iot-key-co...