Tutorial #2 Node-red and Raspberry Pi: Bot Sends Message to Telegram Group When Infrared Sensor is Triggered

in nodered •  7 years ago 

In this tutorial, we are going create a bot that sends push notification when infrared sensor is triggered. With this kind of technology, it notifies the owner when certain event occurs. Also, we do not need any extra node installation.
nodered.jpg

Hardware that you gonna use:

  • Raspberry Pi 3 with 8GB MicroSD installed with Raspbian OS*
  • 3 Female to Female Jumper Wire
  • Infrared Sensor or PIR Sensor
  • Micro USB Power Adapter or Power Bank
  • Screwdriver

*If you don’t know how to install Raspbian OS, you might want to go here

Step 1: Create Your Own Bot

Before we get started, you need to have a Telegram account. You can use your account from web browser or from your phone.

  • Open your web telegram and find @botfather at search.
    search.PNG
  • To start it,type: /newbot
  • Follow the instruction given by @botfather such as giving your chatbot name. Follow @botfather until it’s finish, like figure below:
    Picture1.png
  • Tips: it is better to name your bot with “_bot” or ”Bot” at the end of the name.
  • Once you finished, create Telegram group and add your bot in that telegram group.
  • Upgrade it to supergroup.

STEP 2: Assemble the Wiring

Follow with these schematic:
Schematic.png

Raspberry Pi 3 PinPIR Sensor
2VCC
7Out
6GND

*Sometime there are some infrared sensors only show “+” instead VCC, but it also can be interpreted as VCC and “-” as GND since that sensor are quite small in size.

STEP 3: Let's NODE-Red it

*Insert micro usb to raspberry pi and wait until it powers up.

  • Ensure your raspberry pi has internet connection.

  • Open terminal by clicking the terminal icon at top left or press keyboard “Ctrl+Alt+T”

  • Type “node-red-start” and copy the IP address
    Inkedterminal_LI.jpg

  • Open web browser and paste the IP address that is shown in terminal.
    DQmTsV5oGxLJa3MgJWrqQJ43a5SfdKJDrqtY1yjWJuBRD4J.png

  • Drag rpi-gpio-in node from Raspberry Pi Group to Flow.
    pin_7-3.PNG

  • In node properties, at Pin section, choose number 7(GPIO 04) and Debounce Value set to 500ms and click Done
    Pin_7-2.PNG

  • In Function group, drag switch node to detect if that IR is triggered, into flow and set as below, and Click Done.
    IR_Trigger-2.PNG

  • In Function group, drag another switch node to select if the value give from GPIO is 0, into flow and set as below, and Click Done.
    value==0-`.PNG

*Explanation: In this node, node-red checks the value of numbers that are carried from gpio node here, before this node can carried forward to the next node.

  • In Input group, drag http request node into flow
    http_request2.PNG

Set your http node properties as below:
Method: GET
URL: https://api.telegram.org/bot<YOUR_API_TOKEN>/sendMessage?chat_id=-100<YOUR_CHAT_ID>&text=<YOUR_TEXT>
Name: Chatbot push notification

*Copy your API Token from @BotFather earlier, and paste in <YOUR_API_TOKEN>
**To get chat id from supergroup, go to web telegram and go to your telegram group you have created and at the URL from browser, your chatID is in between letter “S” and character “_”. Copy it and paste is at <YOUR_CHAT_ID>
chatID.PNG

  • Connect each node as shown below. And Click Deployto test it.
    first.PNG

  • Now your bot is working but then the bot sends several messages in a second. So, in order to reduce the redundancy of message from chat, we need to drag function node from Function group. And set the property shown below:
    Function dont always sent-1.PNG

And link the function node from IR_Trigger node, and it will look like this.
final.PNG

  • Finally click again button Deploy to deploy your final version of bot push notification.
  • And you’re done. Congratulation.

Here the screenshot from phone
Screenshot_20180219-215552.png

Success-Kid copy.jpg

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @amir3hamzah! You received a personal award!

1 Year on Steemit

Click here to view your Board

Do not miss the last post from @steemitboard:

SteemWhales has officially moved to SteemitBoard Ranking
SteemitBoard - Witness Update

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @amir3hamzah! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!