PragmaticPhilBjarke Gotfredsen ☒
Published © GPL3+

Connect to Ubidots IoT Platform with BBC micro:bit + XinaBox

Use Wi-Fi to connect your BBC micro:bit to the Ubidots IoT platform using the XinaBox starter kit

EasyProtip30 minutes69
Connect to Ubidots IoT Platform with BBC micro:bit + XinaBox

Things used in this project

Hardware components

BBC micro:bit board
BBC micro:bit board
×1
CW01
XinaBox CW01
Wi-Fi Core (ESP8266/ESP-12F)
×1
IP01
XinaBox IP01
An interface module equipped to power and program other modules via a USB A connector.
×1
BM01
XinaBox BM01
A bridge to connect your XinaBox xChips to a micro:bit
×1
XC10
XinaBox XC10
This SKU is a 10 pack - we only need 2 for this project, although I use 3. These are standard uBus connectors.
×1

Software apps and online services

Ubidots
Ubidots
You will need an UbiDots account - Step 1 shows you how to sign up for a free 30 day trial

Story

Read more

Schematics

Flashing the CW01

Use the xFlasher app provided to flash the binary code provided onto the CW01
Flashingcw01 x87lwc29mt

Editing and Flashing the microbit code

Customise the microPython code - add your Wi-Fi details and Default Token from Ubidots - then flash onto the micro:bit
Editingflashingmicrobitcode quaf5swoas

Assembling the IoT Starter Kit

Connect the micro:bit to the BM01, then connect that to the CW01 / IP01 we assembled in STEP 2.
Assembling iot kit syhuuo8wyy

Code

MicroPython code for the micro:bit

MicroPython
Update this code as detailed in Flashing code onto you micro:bit
from microbit import *
import random
# NOTE: we import random here cos we are going to send a random number to Ubidots

uart.init(baudrate=9600, bits=8, parity=None, stop=1, tx=pin20, rx=pin19)
# NOTE: gets the uart (serial / USB) port on the micro:bit ready to write to the CW01

def CW01(parm):
    # NOTE: the variable above called '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
    # NOTE: you won't need all the above for the basic stuff in the tutorial, but it will come in useful as you explore and experiment.
    
    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)     # NOTE: this means that a tick is displayed when data is being transmitted correctly
    elif int(data[:1]) == 0:
        display.show(Image.NO)      # NOTE: this means that a cross is displayed when an attempt to send data fails
    else:
        display.show(str(data)[3:4])
    sleep(500)


display.show(Image.SQUARE)          # NOTE: the square is shown on your micro:bit while it is connecting to Ubidots
sleep(2000)
uart.write("$")                     # NOTE: Cleans out the serial buffer
sleep(100)
uart.write("+9@?$")                 # NOTE: Reboots the CW01
sleep(5000)                         # NOTE: long delay is necessary - we need to give Wi-Fi time to sort itself out.
uart.write("$")                     # NOTE: Clean out Serial buffer, again.
sleep(500)

CW01("+1@WIFINAME@WIFIPASSWORD$") 
# EDIT GUIDELINES: you MUST enter the name and password of the Wi-Fi network you are trying to connect to in the line above.

CW01("+2@UBIDOTSDEFAULTTOKEN@?$")
# EDIT GUIDELINES: you MUST enter the DEFAULT TOKEN from Ubidots in the line above.

CW01("+3@things.ubidots.com@1883$")
# NOTE: above line tells the micro:bit where to send the data to - its a Ubidots URL - you can leave it as is.

CW01("+6@/v1.6/devices/YOURMICROBIT/number/lv/$")
# NOTE: above is an ID for our CW01 - you can leave it as is.

while True:
    
    CW01("+4@YOURMICROBIT@temperature@" + str(temperature()) + "$")
    CW01("+4@YOURMICROBIT@a@"+str(button_a.is_pressed())+"$")
    CW01("+4@YOURMICROBIT@b@"+str(button_b.is_pressed())+"$")
    CW01("+4@YOURMICROBIT@Random_0to9@"+str(random.randint(0, 10))+"$")
    # NOTE: the lines above tell the micro:bit to send data to Ubidots (to the URL we entered above).
    # EDIT GUIDELINES: you MAY change 'YOURMICROBIT' above - it is just a name that you can assign.  You can leave it as is if you like.
    # EDIT GUIDELINES: the name you enter above is also used in the Ubidots platform.
    # EDIT GUIDELINES: NB - you MUST ensure the name used above and in Ubidots is IDENTICAL.  See STEP 3 (Configuring the Ubidots Dashboard) of the tutorial 

    sleep(500)

Binary file for CW01

C/C++
Flash this binary file onto your CWO1 as described in the schematic titled Flashing the CW01
No preview (download only).

XinaBox Uploader for Windows

C/C++
Use the XinaBox Uploader to flash code from your Windows machine on the CW01
No preview (download only).

XinaBox Uploader for OS

C/C++
Use the XinaBox Uploader to flash code from your OS machine onto the CW01
No preview (download only).

Credits

PragmaticPhil

PragmaticPhil

6 projects • 7 followers
Hobbyist + EdTech consultant
Bjarke Gotfredsen ☒

Bjarke Gotfredsen ☒

4 projects • 12 followers
CEO, Founder and Inventor of the XinaBox tech.

Comments

Add projectSign up / LoginAbout BBC micro:bit