D4E1: Detection and Notification for Opening a Door by Combining Micro:bit and Telegram

by FrederikDC in Circuits > Assistive Tech

603 Views, 2 Favorites, 0 Comments

D4E1: Detection and Notification for Opening a Door by Combining Micro:bit and Telegram

scheme.jpg

This project was developed to support (informal) caregivers who work with people that suffer from Alzheimer's and who exhibit wandering behaviour. This project detects when the door of the patient's room is opened during the night and sends a message to the (informal) caregiver via the Telegram app so that they are informed that the patient has left the room.

This project uses a micro:bit connected to a Raspberry Pi. The Raspberry Pi runs a Node-RED application that sends a message to a Telegram bot when the micro:bit's accelerometer indicates that the door has been opened.

Schematically, the project looks like this:

  • micro:bit connects via bluetooth to the Raspberry Pi
  • Raspberry Pi connects via Wifi with the Telegram API

Little to no code was written to realize this project as MakeCode for micro:bit and the Node-RED environment are both no-code / low-code environments.

Supplies

supplies.jpg

The following materials were used during this project:

  • A Raspberry Pi model 3 B+ including
  • A power adapter
  • A 16GB SD card for storage (you can also use a 8GB card)
  • A micro:bit including
  • A USB cable to connect to a computer
  • A battery holder with batteries so that the micro:bit can work independently from a computer
  • Some supplies to make the door handle
  • A cardboard roll
  • A pair of scissors
  • Some tape

Programming the Micro:bit

mb-heart.jpg
nrf-connect.jpg
mb-check.jpg

If you have little or no experience with the micro:bit, first read the getting started tutorial on how to connect the micro:bit to your computer and transfer a program from your machine to the micro:bit.

The code on the micro:bit itself is very simple:

  • At startup:
  • an icon of a heart is shown
  • the accelerometer is made available via bluetooth
  • When connecting via bluetooth:
  • a check mark icon is shown
  • When disconnecting via bluetooth:
  • a cross icon is shown.

There are several possibilities to design this application and getting it on the micro:bit:

  1. Open your Finder or File Explorer, drag the .hex file you can find at the end of this step onto the micro:bit
  2. Create a new project on makecode.microbit.org. Use the following settings:
  3. under Project Settings, the checkmark “No pairing required” must be checked
  4. support for bluetooth can be added via Advanced → Extensions
  5. complete the program as described in the steps above
  6. Option 3: clone the project via makecode: https://makecode.microbit.org/_DHkMFbR1RAJH
  7. Option 4: clone the project via github: https://frederikduchi.github.io/bluetooth-node-red/

All options besides the first one allows you to change the icons and add extra functionality to the application.

Once the application is on the micro:bit, test whether everything works. You can do this via an app on your smartphone such as nRF Connect. Through this app you can connect to the micro:bit. If the check mark appears on the LED display, everything works fine. Also make sure you see the accelerometer between the characteristics.

Installing and Configuring the Raspberry Pi

pi-install1.jpg
pi-install2.jpg
pi-install3.jpg
pi-configure1.jpg

In this step the operating system Raspberry Pi OS Lite (32 bit) is installed on the Pi along with the further configuration of the Pi such as setting up SSH and the Wifi. Finally, you need to execute some commands to get bluetooth working.

Installation

  1. Download the Raspberry Pi Imager tool and install it.
  2. Remove the SD Card from the Pi and put it in your laptop
  3. Launch the Raspberry Pi Imager
  4. Click CHOOSE OS
  5. Choose Raspberry Pi OS Lite (32 bit) as we don't need a full Desktop version.
  6. Click CHOOSE STORAGE and choose your SD Card
  7. Click on the gear icon for the following settings:
  8. Give your Pi a name. For example: homeserver.local
  9. Make sure SSH access is enabled. This way you don't need an external screen or keyboard for further configuration
  10. Set a username (for example: pi) and a password (for example: apple).
  11. Set your WiFi settings
  12. Optionally set the region settings

Finally, click WRITE and choose Yes at the prompt that all files will be deleted. After a few minutes, the OS is installed and the SD Card can be reinserted into the Pi.

Configuration

Boot the Pi by plugging in the power adapter. After a few seconds it will already be booted as the lite version of the operating system has been installed.

Before starting with Node-Red, a few libraries must first be installed in order to work with bluetooth. We will install it using SSH. This is a protocol that allows you to connect to the Pi via another device and is normally installed by default on both Windows and Mac OS.

Open your Terminal or Command Line en enter:

ssh pi@homeserver.local

where:

  • ssh is the command
  • pi is the user name of your Pi
  • homeserver.local is the name of your Pi

Type yes when asked for confirmation to connect and finally enter the password for the pi user which is in my case apple.

We are now in the command line of the Pi and can use commands to install additional libraries. Enter:

sudo apt-get install bluetooth bluez libbluetooth-dev libusb-1.0.0-dev libudev-dev

Type yes when asked and all the required libraries will be installed. Next type

sudo usermod -G bluetooth -a pi

to run the bluetooth process as the pi user. Finally reboot the pi by running

sudo reboot

The Pi is now completely installed and ready for use.

Installing and Configuring Node-red

install-nr-1.jpg
install-nr-2.jpg

This step installs Node-Red which is a visual programming language that allows you to develop a complete application by linking different blocks (nodes) together. All installation steps can be found in the Node-Red documentation.

Connect to your Pi using SSH and login with your credentials (password is apple)

ssh pi@homeserver.local

Enter this command and confirm with y. Answer N on the question to install Pi-specific libraries since we do not need them for this project.

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Wait for a couple of minutes until the installation completes. You can check the log file for error if you want to.

Make sure that Node-Red will start immediately after the Pi has been booted by entering

sudo systemctl enable nodered.service

Finally reboot the Pi

sudo reboot


Test if the installation was successful by opening your browser and navigate to http://homeserver.local:1880/. If you see the Node-Red welcome screen, you are good to go. Otherwise you might need to check for the IP address of your Pi and use http://[ip-of-you-pi]:1880 instead.

"Writing Code" in Node-Red

complete.jpg
botfather.jpg
nr-import.jpg
chatid.jpg

This step develops our application in Node-red that will run on the Pi. The application will connect with the micro:bit using bluetooth and check the acceleration of the micro:bit. If it tilts on the X-axis during the night, the application sends a notification to a Telegram both.

Prerequisite: installing libraries

First we need to install 2 additional libraries for Node-Red. This can be done through the browser interface, however I prefer the command-line (SSH) because it offers a beter overview of the installation process.

Connect with the Pi again using SSH and your credentials

ssh pi@homeserver.local

Navigate to the folder where you installed Node-Red

cd ~/.node-red

Install the generic ble (Bluetooth Low Energy) library. This can take a couple of minutes. Meanwhile, check the documentation of this library.

npm install node-red-contrib-generic-ble

We also need a library to use the Telegram Bots API. Install this and check the documentation.

npm install node-red-contrib-telegrambot

Finally restart Node-red or even the entire Pi.

node-red-restart


Check if the installation of the additional libraries has was successful by refreshing the browser on http://homeserver.local:1880. You should see the nodes Generic BLE In and Generic BLE Out in the left pane. If you scroll down you should also see nodes for the Telegram API.

Prerequisite: setting up a Telegram bot

Telegram is a chat application just like Messenger or Whatsapp. The major advantage of Telegram is that it has a free to use API which allows users to set up a chat bot really easy. You set up a bot using the following steps:

  1. Download the Telegram app on your phone and create your account.
  2. You can use the app to create a bot, however I find it easier to use the web app
  3. Search for @BotFather to create a new bot
  4. type /start followed by /newbot to create a new bot:
  5. Set a name for your bot. This can by any name and include spaces
  6. Set a username for your bot. This must with "bot" and cannot include spaces
  7. You receive a token that can be used in Node-Red to communicate with the bot.


Import the application

All applications of Node-Red can be imported and exported in JSON format which makes the extremely sharable with others. You can find the code in my GitHub repo.

  1. Copy the JSON from microbit-telegram-flow.json.
  2. Navigate to your Node-Red dashboard (http://homeserver.local:1880)
  3. Click the hamburger menu in the upper right corner and select Import
  4. Paste the JSON in the textfield and select import to new flow
  5. Click the IMPORT button


Configure the Telegram bot

The entire application is imported in a new flow. You need to configure the chat bot using the token you generated in the previous step.

  1. Double click the node with the name "Telegram sender"
  2. Click the pencil next to the textfield with the content Microbot
  3. Enter your token in the corresponding field and click UPDATE followed by DONE

Finally you need to set the chatId which is the Id linked to your Telegram account.

  1. Navigate to the Telegram web app
  2. Search for the user @RawDataBot and type something in the chat window
  3. You receive a JSON response. Check for the id in the chat section
  4. Copy this id and go back to Node-Red
  5. Search for the node "Create Message" and replace the xxxxxx with your chat ID.


Test the application

You can start the application by clicking the DEPLOY button in the upper right corner. However, the application will only work between midnight and 7am and after 8pm. You can change these values by double clicking the node "Is it Night".

Once the application is started, you see a number appearing in the debug window on the right side (Menu --> View --> Debug Window). When you flip the micro:bit to the left or the right, it should send a message to your Telegram bot. If not, check the debug window for error messages and check if the micro:bit and Telegram bot are found by de Node-Red application. This is indicated with a green light under the corresponding nodes.

The Code Explained

D4E1: micro:bit notifies Telegram that a door was opened. The Node-RED application - Part 1
D4E1: micro:bit notifies Telegram that a door was opened. The Node-RED application - Part 2

A complete overview of how the application works can be found in the video. Be sure to also check the Node-Red YouTube channel for further tutorials and demos.

Assembling the Door Handle

cardboard-cutout.jpg
cardboard-with-mb.jpg
cardboard-with-bat.jpg
final.jpg
  1. Take a cardboard roll and cut an opening in it with scissors so that the display of the micro:bit is visible.
  2. Cut a second opening at the top so that the battery can be connected.
  3. Secure the battery with adhesive tape and place the micro:bit firmly against the edge of the roll so that it can easily be slid over a door handle.

Testing the Final Project

D4E1: micro:bit notifies Telegram that a door was opened

Make sure the Raspberry Pi is within bluetooth distance of your micro:bit. Also ensure that the Node-Red application is up and running.

Test your application. You should receive a notification every time the door opens.