Posty:bit — Post Box Sensor with micro:bit & Bluetooth Advertising
Based on my earlier post to use micro:bit to monitor the post box, I want to enhance the functionality to utilize the bluetooth advertising function from micro:bit.
Let’s modify the transmitter program to use the bluetooth advertiser and create a simple Android program to scan the bluetooth advertised by micro:bit. For this case, we don’t need the receiver.
Transmitter
Go to micro:bit editor and create a new project. Firstly, we need to add the bluetooth package into the editor, click “Add Package” and choose “bluetooth” to add the bluetooth services.
When you add the Bluetooth package, the radio package will be removed. Click “Remove package(s) and add bluetooth” button.
Here is the source code of the program:
And the corresponding JavaScript:
If you notice in the second part of the code, I created a delay for 3 minutes after you power on the micro:bit in order to get the steady value from the digital pin P0.
Once the mail is detected, the program will stop looping and run the bluetooth advertising until the button B is pressed (device is reset — user has acknowledged that he/she has checked the mail box).
Secure the micro:bit
We need to secure the micro:bit so no one can connect to our micro:bit. Go to Project Settings and select “Passkey pairing: Button press and 6 digit key to pair”
Pairing the micro:bit with Android Smartphone
Before we can use the device, we need to pair the micro:bit with Android smartphone. Press the button A and B on micro:bit together, keep the two buttons held down. Press and then release the reset button and you will see “PAIRING MODE!” starts to scroll on the display. Once you see this, you can release buttons A and B.
Now go to your Android phone, click Settings and select Bluetooth. You will see the micro:bit name on the available devices list. The micro:bit will display a left pointing arrow and the Android smartphone will pop up a box into which you will be invited to enter a “pin”.
Press button A on micro:bit and micro:bit will display a sequence of 6 random numbers. Note down those numbers and enter the 6 digits which the micro:bit displayed into your Android smartphone in the pop-up box provided and then select “done”. If you entered the correct number the micro:bit will display a check mark. If you made a mistake it will display a “X” and you should repeat the process to try again. For more details, please refer to bluetooth pairing on micro:bit.
Create the Android App
We have completed with the micro:bit codes part, now is time to create an Android app to monitor the bluetooth advertising from the micro:bit. If there is, we assume there is a mail in your post box!
Complete source code can be found on GitHub:
To use it, enable first the bluetooth on the smartphone and click “Check Mail”.
Hope you enjoy reading my creation with micro:bit and until next time!