Publish Data from Micro:bit v2 to Qubitro IoT Platform

Qubitro
3 min readApr 29, 2022

Publish built-in sensors’ BBC Micro:bit v2 data to Qubitro IoT platform through MQTT protocol.

In this tutorial, we will learn how to publish sensor readings from BBC Micro:bit v2 to Qubitro IoT platform through the MQTT protocol.

Qubitro is the fastest way to build IoT applications with predictable pricing, developer-friendly features, and scalability you’ll love. You can connect your hardware to Qubitro, collect the data and create data visualization on your own dashboard. For more information about Qubitro, you can check its official site: qubitro.com.

We will use the BBC Micro:bit v2, a pocket-sized microcontroller based on 32 bit Arm Cortex-M4. It’s equipped with Bluetooth Low Energy, radio antenna, 5x5 LED matrix, two buttons, MEMS microphone, speaker, and several sensors like touch sensor, light, accelerometer, magnetometer, and temperature sensor. For more information about BBC Micro:bit v2, you can check its official site: microbit.org.

Step 1. Create Qubitro Project

Signup or login to Qubitro Portal, then create a new project. Click the New Project button, add the project name & description, then click Create Project button.

Then create a new device inside your project. Choose your device connectivity method, in this case, MQTT. Then enter device details name, description, brand, model, and location.

After that, you’ll get your credentials (host, port, username, password & clientId) to connect to the Qubitro MQTT broker. Please note these values because we’ll use them on our code later. You can check these values on the device Settings menu. Your username & clientId are your Qubitro Device ID, and your password is your Qubitro Device Token.

Step 2. Code the Micro:bit

Go to Microsoft Makecode editor and create a new project. Then, Install ESP8266 extension: click the gear icon, choose Extensions, search for ESP8266 and install the one named iot-environment-kit, an ESP8266 extension by Elecfreaks.

You can also code using JavaScript or Python:

***code.js***
```javascript
basic.showIcon(IconNames.Heart)
ESP8266_IoT.initWIFI(SerialPin.P16, SerialPin.P2, BaudRate.BaudRate115200)
ESP8266_IoT.connectWifi("your_ssid", "your_pwd")
basic.forever(function () {
ESP8266_IoT.setMQTT(
ESP8266_IoT.SchemeList.TCP,
"YOUR_QUBITRO_DEVICE_ID",
"YOUR_QUBITRO_DEVICE_ID",
"YOUR_QUBITRO_DEVICE_TOKEN",
""
)
ESP8266_IoT.connectMQTT("broker.qubitro.com", 1883, false)
ESP8266_IoT.publishMqttMessage("{\\\"temperature\\\":" + input.temperature() + "}", "YOUR_QUBITRO_DEVICE_ID", ESP8266_IoT.QosList.Qos0)
basic.pause(5000)
})
```
***code.py***
```python
basic.show_icon(IconNames.HEART)
ESP8266_IoT.init_wifi(SerialPin.P16, SerialPin.P2, BaudRate.BAUD_RATE115200)
ESP8266_IoT.connect_wifi("your_ssid", "your_pwd")
def on_forever():
ESP8266_IoT.set_mqtt(ESP8266_IoT.SchemeList.TCP,
"YOUR_QUBITRO_DEVICE_ID",
"YOUR_QUBITRO_DEVICE_ID",
"YOUR_QUBITRO_DEVICE_TOKEN",
"")
ESP8266_IoT.connect_mqtt("broker.qubitro.com", 1883, False)
ESP8266_IoT.publish_mqtt_message("{\\\"temperature\\\":" + str(input.temperature()) + "}",
"YOUR_QUBITRO_DEVICE_ID",
ESP8266_IoT.QosList.QOS0)
basic.pause(5000)
basic.forever(on_forever)
```

Download its hex file, then insert it into the Micro:bit v2. Once done, Micro:bit will display a heart icon on its 5x5 LED matrix, connect to the WiFi and publish data to Qubitro every 5 seconds.

Results

On the Qubitro device Data tab, you’ll see the data stream from your Micro:bit. You can create data visualization on the Analytics tab or create your own centralized dashboard in the “Monitoring” section.

--

--

Qubitro
Qubitro

Written by Qubitro

Infrastructure for the Internet of Things solutions. Build connected solutions faster than ever.

No responses yet