Connecting a BBC micro:bit to the internet

Published 07-16-2020 10:50 AM 3,601 Views
Microsoft

The BBC micro:bit is a fantastic little device. It's small, relatively cheap for a device of its capabilities, has coding tools for developers of all abilities, and has a huge ecosystem of tutorials and projects to do. Young kids use them to code with block-based coding with Microsoft MakeCode, older kids will use Python or JavaScript in MakeCode. They have some sensors and LEDs built in, and you can connect to a whole ecosystem of add-ons from sensors to robots. 

 

A BBC micro:bitA BBC micro:bit

What it can't do is connect to the internet. At least not directly...

 

I recently put together a 'smart garden ornament' project as part of our JulyOT month of IoT - the idea being to add sensors to bird boxes, gnomes and other such garden ornaments and bring this sensor data together in an on-line dashboard using IoT Central. This project is designed for families or schools to work on together, gathering data from your neighborhood or school. These sensors would be powered by BBC micro:bits and coded using blocks. The problem came with getting this data to the cloud.

 

My solution to this took advantage of two things - the radio and the USB connection. The micro:bit has a radio that can broadcast data to other micro:bits in the vicinity (the range in clear air is about 70m). It also has a USB port that can not only be used for power and programming, but you can also send data along it.

 

This means I can connect a micro:bit to a device with an internet connection (such as a Raspberry Pi) via USB and send messages from the micro:bit to the Pi. The Pi then listens on the serial port for the messages, then sends them on to IoT Central. This forms my central 'Hub'.

 

My sensor micro:bits then need to send radio messages with the data they want to send to the cloud, these are detected by the 'Hub' micro:bit, routed through the Pi and send to IoT Central.

 

project-message-flow-single-microbit.png

 

There is a size limit on radio messages of 19 characters. This meant I had to send encoded messages - using things like a single character for the telemetry type (e.g. 't' for temperature) and a single character for the device ID. The Pi then decodes these to the actual values and sends them on.

 

You can read more on how I did this, see the code and learn how to build this yourself by checking out the project on GitHub:

 

github.com/jimbobbennett/smart-garden-ornaments 

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-1527220%22%20slang%3D%22en-US%22%3EConnecting%20a%20BBC%20micro%3Abit%20to%20the%20internet%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1527220%22%20slang%3D%22en-US%22%3E%3CP%3EThe%20%3CA%20href%3D%22https%3A%2F%2Fmicrobit.org%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EBBC%20micro%3Abit%3C%2FA%3E%20is%20a%20fantastic%20little%20device.%20It's%20small%2C%20relatively%20cheap%20for%20a%20device%20of%20its%20capabilities%2C%20has%20coding%20tools%20for%20developers%20of%20all%20abilities%2C%20and%20has%20a%20huge%20ecosystem%20of%20tutorials%20and%20projects%20to%20do.%20Young%20kids%20use%20them%20to%20code%20with%20block-based%20coding%20with%20%3CA%20href%3D%22https%3A%2F%2Fwww.microsoft.com%2Fmakecode%3FWT.mc_id%3Dmakecode-educatorblog-jabenn%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EMicrosoft%20MakeCode%3C%2FA%3E%2C%20older%20kids%20will%20use%20Python%20or%20JavaScript%20in%20MakeCode.%20They%20have%20some%20sensors%20and%20LEDs%20built%20in%2C%20and%20you%20can%20connect%20to%20a%20whole%20ecosystem%20of%20add-ons%20from%20sensors%20to%20robots.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22microbit.png%22%20style%3D%22width%3A%20382px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F205847iF9AD1F8F5461AF17%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22microbit.png%22%20alt%3D%22A%20BBC%20micro%3Abit%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EA%20BBC%20micro%3Abit%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EWhat%20it%20can't%20do%20is%20connect%20to%20the%20internet.%20At%20least%20not%20directly...%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20recently%20put%20together%20a%20-ERR%3AREF-NOT-FOUND-'smart%20garden%20ornament'%20project%26nbsp%3Bas%20part%20of%20our%20-ERR%3AREF-NOT-FOUND-JulyOT%20month%20of%20IoT%20-%20the%20idea%20being%20to%20add%20sensors%20to%20bird%20boxes%2C%20gnomes%20and%20other%20such%20garden%20ornaments%20and%20bring%20this%20sensor%20data%20together%20in%20an%20on-line%20dashboard%20using%20-ERR%3AREF-NOT-FOUND-IoT%20Central.%20This%20project%20is%20designed%20for%20families%20or%20schools%20to%20work%20on%20together%2C%20gathering%20data%20from%20your%20neighborhood%20or%20school.%20These%20sensors%20would%20be%20powered%20by%20BBC%20micro%3Abits%20and%20coded%20using%20blocks.%20The%20problem%20came%20with%20getting%20this%20data%20to%20the%20cloud.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMy%20solution%20to%20this%20took%20advantage%20of%20two%20things%20-%20the%20radio%20and%20the%20USB%20connection.%20The%20micro%3Abit%20has%20a%20radio%20that%20can%20broadcast%20data%20to%20other%20micro%3Abits%20in%20the%20vicinity%20(the%20range%20in%20clear%20air%20is%20about%2070m).%20It%20also%20has%20a%20USB%20port%20that%20can%20not%20only%20be%20used%20for%20power%20and%20programming%2C%20but%20you%20can%20also%20send%20data%20along%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThis%20means%20I%20can%20connect%20a%20micro%3Abit%20to%20a%20device%20with%20an%20internet%20connection%20(such%20as%20a%20Raspberry%20Pi)%20via%20USB%20and%20send%20messages%20from%20the%20micro%3Abit%20to%20the%20Pi.%20The%20Pi%20then%20listens%20on%20the%20serial%20port%20for%20the%20messages%2C%20then%20sends%20them%20on%20to%20IoT%20Central.%20This%20forms%20my%20central%20'Hub'.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMy%20sensor%20micro%3Abits%20then%20need%20to%20send%20radio%20messages%20with%20the%20data%20they%20want%20to%20send%20to%20the%20cloud%2C%20these%20are%20detected%20by%20the%20'Hub'%20micro%3Abit%2C%20routed%20through%20the%20Pi%20and%20send%20to%20IoT%20Central.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22project-message-flow-single-microbit.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F205855iA0F5B5F4D5AFBAEA%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20title%3D%22project-message-flow-single-microbit.png%22%20alt%3D%22project-message-flow-single-microbit.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20is%20a%20size%20limit%20on%20radio%20messages%20of%2019%20characters.%20This%20meant%20I%20had%20to%20send%20encoded%20messages%20-%20using%20things%20like%20a%20single%20character%20for%20the%20telemetry%20type%20(e.g.%20't'%20for%20temperature)%20and%20a%20single%20character%20for%20the%20device%20ID.%20The%20Pi%20then%20decodes%20these%20to%20the%20actual%20values%20and%20sends%20them%20on.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYou%20can%20read%20more%20on%20how%20I%20did%20this%2C%20see%20the%20code%20and%20learn%20how%20to%20build%20this%20yourself%20by%20checking%20out%20the%20project%20on%20GitHub%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E-ERR%3AREF-NOT-FOUND-github.com%2Fjimbobbennett%2Fsmart-garden-ornaments%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1527220%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-center%22%20image-alt%3D%22microbit.png%22%20style%3D%22width%3A%20382px%3B%22%3E-ERR%3AREF-NOT-FOUND-%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EA%20BBC%20micro%3Abit%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20style%3D%22font-family%3A%20inherit%3B%22%3EEver%20wanted%20to%20connect%20a%20BBC%20micro%3Abit%20to%20the%20internet%20to%20gather%20data%3F%20Here's%20a%20nifty%20technique%20to%20do%20so%20via%20a%20'Hub'.%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Jul 16 2020 10:51 AM
Updated by: