PragmaticPhilBjarke Gotfredsen ☒
Published © GPL3+

Simple Wi-Fi-Based IoT on BBC micro:bit with XinaBox

Get started with Wi-Fi based IoT on your BBC micro:bit. Share two-way data with an IoT Platform using the simple XinaBox IoT starter kit.

BeginnerProtip30 minutes1,224
Simple Wi-Fi-Based IoT on BBC micro:bit with XinaBox

Things used in this project

Hardware components

BBC micro:bit board
BBC micro:bit board
×1
CW01
XinaBox CW01
IoT Starter Kit 1: The Wi-Fi Core (ESP8266/ESP-12F)
×1
BM01
XinaBox BM01
IoT Starter Kit 2: micro:bit bridge. Connect any xChip to a micro:bit using this bridge
×1
XC10
XinaBox XC10
IoT Starter Kit 3: 2 xbus connectors to connect the CW01 to the BM01, and the CW01 to the IP01
×2
IP01
XinaBox IP01
IoT Starter Kit 4: USB Programming Interface (FT232R) - provides power to the circuit and is used to flash code onto the CW01
×1

Story

Read more

Schematics

Flashing the CW01

Follow these steps to connect your IP01 to the CW01 and then flash the provided code onto the CW01

Flashing the micro:bit

Follow these steps to edit the Python code provided and to flash it onto your micro:bit

Assembling the IoT Starter Pack

Connect the IP01 / CW01 circuit with the BM01 and the micro:bit

Connecting to an IoT Platform

Follow these steps to connect the circuit you have built in earlier steps to the HiveMQ IoT platform

Pulling it all together (1)

Some tips and handy info for after you've worked through the first 4 instruction sheets

Pulling it all together (2)

Some tips on what to look for in the Hive dashboard and how to send a message to your micro:bit

Code

Python Code for Microbit

MicroPython
Flash this code onto your micro:bit as described in the instruction section "Flashing the Microbit"
from microbit import *
uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin20, rx=pin19)

def CW01(parm):
    # NOTE: Parm must follow this format:
    # start with a '+'
    # then a number 1,2,3 etc. See below
    # then a seperator '@'
    # then a parameter followed by a seperator '@'
    # last parameter is ended with a end symbol '$'
    # Commands:
    # 1 for WiFi:          "+1@ssid@secret$"
    # 2 for login to MQTT: "+2@username@password$"
    # 3 for MQTT server:   "+3@server@port$"
    # 4 for ubidot:        "+4@device@variable@value$" (creates a MQTT publish)
    # 5 for MQTT publish:  "+5@topic@payload$"
    # 6 for MQTT subscribe:"+6@topic$" (maximum 1 subscription)
    # always start with "$" just to clean out the serial buffer

    display.clear()
    uart.write(parm)
    sleep(500)
    data = uart.readline()
    while(data is None):
        data = uart.readline()

    if int(data[:1]) == 1:
        display.show(Image.YES)
    elif int(data[:1]) == 0:
        display.show(Image.NO)
    else:
        display.scroll(data[1:-1])
    # NOTE: You can't use the print command - it becomes input to the CW01 (its a uart thing)
    # NOTE: If you get an error in the code above it means you are not connecting to Wi-Fi (it fails the 'cast' to int if not connected)
    # NOTE: When its working properly, the code above will show a TICK every time data is correctly sent via Wi-Fi

    sleep(500)

display.show(Image.SQUARE)
sleep(2000)
uart.write("$") # Clean out Serial buffer
sleep(100)
uart.write("+9@?$") # Reboot CW01
sleep(5000)
uart.write("$") # Clean out Serial buffer
sleep(500)
# EDIT guidelines - Don't change the sleep durations above - at least not until you are connecting succesfully - some need to be long.

CW01("+1@WIFINAME@WIFIPASSWORD$")
# EDIT guidelines - You MUST change the code above - enter your Wi-Fi name and password.
# EDIT guidelines - be careful to ensure you don't edit the @ or $ signs - just change "WIFINAME" and "WIFIPASSWORD"

CW01("+2@?@?$")
CW01("+3@broker.hivemq.com@1883$")
CW01("+6@/v1.6/devices/OC03/state/lv$")
# EDIT guidelines - In the line above the bit "/v1.6/devices/" is an identifier you will need to use in the IoT platform.  
# EDIT guidelines - DON'T change "/v1.6/devices/" - change YOURNAME later in this code to personalise your dashboard
# EDIT guidelines - when setting up a subscription in HIVEMQ you will click ADD NEW TOPIC SUBSCRIPTION
# EDIT guidelines - in the Topic field you will need to enter /v1.6/devices/YOURNAME/  - ensure this is identical, including the forward slashes

while True:

    CW01("+4@YOURNAME@temperature@" + str(temperature()) + "$")
    # EDIT guidelines - you can edit the line above to send other data to the IoT platform
    CW01("+4@YOURNAME@a@"+str(button_a.is_pressed())+"$")
    CW01("+4@YOURNAME@b@"+str(button_b.is_pressed())+"$")
    # EDIT guidelines - Above we are naming the micro:bit YOURNAME.  You need to enter this into the Hive dashboard
    # EDIT guidelines - Change the word YOURNAME to something - just make sure you change it in all 3 places above and ONLY change YOURNAME
  
    # NOTE: You will need to hold down a button for a while for it to register in the IoT platform - check the code to see if you can see why!
   
    sleep(500)

Binary file for CW01

C/C++
Flash this code onto the CW01 module (see instructions section "Flashing the CW01")
No preview (download only).

XinaBox Uploader for windows

C/C++
Use this executable on a windows machine to flash code onto the CW01. See instructions section "Flashing the CW01".
No preview (download only).

XinaBox Uploader for MacOS

C/C++
Use this executable on an Apple machine to flash code onto the CW01. See instructions section "Flashing the CW01".
No preview (download only).

Credits

PragmaticPhil

PragmaticPhil

13 projects • 10 followers
Hobbyist: some of this I write from scratch, some I work with developers and test / edit. Java + micro:bit are my thing. Work at XinaBox.cc.
Bjarke Gotfredsen ☒

Bjarke Gotfredsen ☒

3 projects • 16 followers
CEO, Founder and Inventor of the XinaBox tech.

Comments