icon

Connecting micro:bit and Huskylens to MIT APP Inventor

0 331 Easy

This tutorial aims to guide you through creating a mobile app that can control a micro:bit using Bluetooth. You'll be able to integrate this setup with a Huskylens, an AI camera, to develop a home security system that sends information and interacts with your phone.

 

Connecting App Inventor to a micro:bit can be challenging. You'll need specific versions of the BLE and micro:bit extensions in App Inventor, and you may encounter Android permissions that can add to the complexity. However, after browsing several threads in the App Inventor community, I finally made the project work successfully.

 

Here are the materials you'll need for this process:

 

1.micro:bit V2 (I also tried with V1.5, but it encountered the 020-no free memory error.)

2.An Android phone (Unfortunately, iPhones do not support the BLE function in APP inventor.)

3.MIT App Inventor This is an online app editor. You can use it directly through your browser to design and download the app to run your applications. (http://ai2.appinventor.mit.edu/)

4.BLE Extension (http://iot.appinventor.mit.edu/#/bluetoothle/bluetoothleintro)

5.micro:bit Extension with UART Support (http://iot.appinventor.mit.edu/assets/resources/com.bbc.microbit.profile.aix).

STEP 1
APP Inventor Basic Connection

The overall structure of the program was based on the official tutorial:

MIT_App_Inventor_Basic_Connection.pdf

 

Please note that the Bluetooth permissions have changed with Android 12, which means the original tutorial may not function as expected. I found a helpful thread discussing this issue, and I've downloaded Anke's sample code to use as a starting point.

Here's how it looks, with a basic layout and several imported extensions:

1.Import extensions

Import the BLE and micro:bit extensions. Drag the extensions into the editing area to add them to the project. 

2. Choose Bluetooth devices

Click on the non-visible micro:bit extension and select the Bluetooth device in the properties. (You'll need to select the Bluetooth device for every micro:bit extension you plan to use later.

3.Design the layout of the program.

Drag the components from the palette to design the interface. Rename the components for easier identification in the Blocks Editor.

4.Set up the functionalities.

Switch to the Blocks Editor view to edit the code. In the program, we'll set up the functionality for four buttons.

5.Install the App to your phone

If you have an App Inventor app on your phone, you can go to 'Connect > AI Companion' to view and test the app design on your device. 

Alternatively, you can go to 'Build > Android App (.apk)' to download the app and install it on your phone."

 

6.Program the micro:bit

Go to MakeCode (https://makecode.microbit.org/).

Click on “Extensions”, search for bluetooth to add the bluetooth extension.

Write code for the micro:bit.

You'll need to go to project settings and enable the 'No Pairing Required' option to make the micro:bit visible to any Bluetooth devices. Then download the code to the micro:bit.

7.Test the Connection

Click 'Scan'. When the micro:bit appears in the list, click 'Stop Scan'. Select the micro:bit and click 'Connect'. Once the micro:bit is successfully connected, a checkmark will be displayed on the micro:bit's LED.

STEP 2
Huskylens Home Security System

Once you've established this basic connection, you'll be able to showcase micro:bit data within the app or even control the micro:bit directly from the app.

 

Let's begin setting up the Huskylens home security system, which sends alerts when unknown faces appear in front of your door.

 

1.Connect Huskylens to micro:bit.

You can use any micro:bit extension board with an I2C (IIC) port to connect the Huskylens, I'm using the IO Extender for micro:bit from DFRobot.

Connect the Huskylens to the IIC port with a cable.

2.Learn Faces with Huskylens.

Connect the extension board to a power source and switch it on to activate the Huskylens. Dial the function button until the word "Face recognition" is displayed at the top of the screen.

Point the HuskyLens at any faces. When a face is detected, it will be automatically selected by a white frame with words "Face" on the screen.

SingleFaceDetection.png

Point the “+” symbol at a face, short press the "learning button" to learn the face. If the same face is detected by HuskyLens, a blue frame with words "Face: ID1" will be displayed on the screen, which indicates that HuskyLens has learned the face and can recognize it now.

FaceRecognitionSingle.png

The Face:ID1 will be designated as the "host" of the house, while any other unrecognized faces will be categorized as "strangers."

3.Program the micro:bit.

Add the Huskylens extension.

Configure the Huskylens connection and functionality within the "on start" block. Continuously monitor for recognized faces within the "forever" block. If the detected face ID = 1, send the string "1"; otherwise, send the string "2" via bluetooth.

 

Sample code shown below:

4.Design the App

Create two new labels: "Huskylens Message" will display the Bluetooth message from the micro:bit. We will use this message to update the "Status" of the system, revealing the identity of the recognized face.

Switch to the Blocks Editor view to edit the code.

Please note that the stringValue received from Bluetooth is in list format. You will need to select the first item of the list and store it as an integer variable.

5.Test the App

License
All Rights
Reserved
licensBg
0