Software apps and online services
In a recent Hackster how-to I described how to use the XinaBox IoT starter kit to connect a micro:bit to the HiveMQ IoT platform and send / receive data. All using Wi-Fi.
In this post I will show you how to connect to the Ubidots web platform - it provides some really cool tools, such as data visualisations (or 'graphs' to anyone else over 40), programmable if-then events and 'synthetic variables'. The possibilities, for both fun and learning outcomes, are vast!
Note: this post is 'stand-alone': Everything you need to get your Wi-Fi based XinaBox IoT starter kit connected to Ubidots is detailed in this post, so you don't need to refer to my previous how-to at all. That does mean it is similar in places to my previous one, so sorry for any repetition. This one goes a bit further in terms of how the data from the micro:bit is used in the Ubidots dashboard.
Follow the steps below in order:
You will need an Ubidots account. I signed up for a free 30 day trial for the purpose of this and it was a quick and easy process:
- go to https://ubidots.com/
- Click Get Started For Free
- Enter the registration data required then click Create My App
- When your account is created click Go To My Account
There are a ton of online resources to help you with all the Ubidots steps - if you get stuck with something check out their their knowledge base.
Use the xFlasher app attached to this project to flash the CW01 code provided onto the CW01. Win and mac versions of the xFlasher are available.
The process is detailed in the attached schematic Flashing the CW01.
NB - use the CW01 code attached to THIS post. If you worked through my previous post on connecting to HiveMQ you DO need to reflash your CW01 with the code from this project. The CW01 code is in beta at the moment and we'll be updating it as we add support for more and more IoT platforms.
You will need to register the micro:bit you are going to use on your Ubidots dashboard. Go to the dashboard view.
For this step you will need to give your micro:bit a name. I call mine "YOURMICROBIT" below and in the microPython code, but you can change it if you like. In Step 4 below we will use that name, EXACTLY as you type it in Ubidots, in the microPython code we flash onto the micro:bit.
- Click on Devices
- Click on the blue + sign to add a new device.
- From the list provided select Wi-FI and Blank. Note - XinaBox is listed here, but don't select it for this as its the micro:bit that is sending the data. You would use this if you are connecting XinaBox xChips to Ubidots without a micro:bit attached.
- Ensure the Device Name and Device Label are the name you are giving to your micro:bit then click Create.
The device you just added should now show up in the list when you click Devices / Devices.
Your Ubidots dashboard will accept data from a device that identifies itself with the name you gave it above... as long as it also sends the correct ID (called the Default Token) when it sends a message. To find the default token:
- Click the drop-down arrow next to the account icon in the top right
- Click on API Credentials
- Copy the Default Token - we use this in our microPython code in Step 4.
That's all you need to do in Ubidots for now. We'll come back to Ubidots in Step 6 when we set up the dashboard that will show the data from the micro:bit.
I think of it like this: the IoT platform needs to know what devices, out of the billions out there, to listen to and react to. It also needs to ensure it is not duped into listening to an imposter. So your Ubidots dashboard listens for messages from devices with specific names. When it sees they are sending a message it checks the message for a secret code (the Default Token). The micro:bit, on the other hand, needs to know where to send a message to (Ubidots URL). When it sends a message it needs to send its name and the secret code. Its those details that we need to set in Ubidots and on the micro:bit.
In Step 3 we told Ubidots what the name of our device was ("YOURMICROBIT")
We also copied the Default Token.
You now need to update the microPython code that is attached to this project. Follow the instructions in the attached schematic titled Editing and Flashing the Microbit Code. The code also has loads of comments to help you customise and understand it.
One of the many fun aspects of working with XinaBox chips is clipping them together to form a circuit… it is as easy as using Lego!
Follow the instructions in the attached schematic titled Assembling the IoT Starter Kit. At the end of this process your xCircuit should look something like this:
I have used 3 of the uBus connectors above - you only need 2. I use the extra one because it adds redundancy as well as strength. And it is how XinaBox xChips are connected in their space-faring Thinsats (check this link for more info on that).
Firstly you need to power it up:
- I plugged the IP01 into a power bank and that worked fine for me.
- It also works when the IP01 is plugged into a USB port on my laptop.
- It didn't like it when I powered both microbit and IP01, whichever way I did it. Also, Wi-Fi is too power-hungry for the circuit to run from the micro:bit battery pack.
Let the micro:bit boot and wait a few moments. Watch for visual progress info on the micro:bit LEDs as below:
Troubleshooting tip: you can also use xFlasher to view activity on the CW01 while it is running. Plug the IP01 (with CW01 attached) into a USB port on your laptop then start up xFlasher. When it is running click on SERIAL and you will see real time status updates from the CW01.
At this stage you've worked through all the previous steps. Your circuit is powered and is running and your micro:bit is intermittently showing the tick.
Data is being sent to Ubidots. We need to make sure Ubidots is picking it up.
Open up your Ubidots dashboard (with the micro:bit running and showing the tick), then wait a few seconds - until the CW01 briefly shows a green LED. Then:
- Click on Devices and select YOURMICROBIT
If everything is working properly, you should see each of the variables you are sending from your micro:bit listed here! You should also see the values change as new data is sent from the micro:bit to Ubidots.
The Ubidots platform is great overall, but one thing I especially appreciated was the way it picked up the data my micro:bit was sending and automatically created variables in my dashboard. Kudos to Ubidots for what is a top-notch UX. I am still a beginner at this IoT stuff and its still a bit daunting - touches like that really help the learning journey.
You should see something like this (with 1 orange block for each variable you send from the micro:bit):
You are now ready to start doing something with the data.
At last we are ready to get to the really fun bit - using the data we are sharing.
There are reams of great tutorials about how to master Ubidots, and I am a beginner at best. I'll take you through 4 things I did with the data:
1:Making a visualisation of the temperaturedata
- Click Data / Dashboard
- Click the blue plus sign on the right
- Select Line Chart then click + variables
- Click YOURMICROBIT from the Devices list then select temperature (you may need to scroll to see the temperature variable listed). Select with the green tick.
- Edit some of the fields - be adventurous! Then click the green tick to finish.
You should now see a chart on your dashboard that resembles the one below:
2:Send an email by clicking the a-button on your micro:bit:
You can trigger all sort of things to happen in response to certain conditions - below I will show you how to send an email:
- Click on Data / Events
- Click on the blue + sign on the right
We now set our 'if' trigger. If something happens it will trigger Ubidots to react in a certain way...
- Click to select variables then click on YOURMICROBIT and select a
- Edit the rest of the line so it reads value is equal to 1 for 0 minutes
- Click the blue forward icon lower down on the screen.
What you've just done is create a trigger - Ubidots is ready to react if you click the a button on your micro:bit. We now need to specify what that reaction, called an action, will be:
- Click on the yellow plus sign to create a new action type
- Click send email
- Enter your email address. Enter a message - if its your first time try to channel a bit of Neil Armstrong.
- Click the green tick button to save your event.
- Make sure you remember to click the blue forward icon lower down on the screen to finalise your event.
- In the final screen you can specify when the event is active. Leave this blank for now and finalise your event by clicking on the green arrow in the screen (which should look a bit like the one below):
When you are done click the a button on your micro:bit - make sure you hold it down for a few seconds. All being well an email should show up in your inbox shortly after :).
3:How random are random numbers on your micro:bit?:
In the microPython code you edited back in STEP 4 you may have noticed that we are sending a random number from 0 to 9 to Ubidots.
Lets take a look at the distribution - are any of the numbers chosen more frequently than others?
- In Ubidots go to Data / Dashboards
- Click the blue + button on the top right
- Select histogram. In the data field select Device / YOURMICROBIT / random_0to9
- In the number of bins field enter 10 then click the green + to finish
All being well, as data is received you should see the histogram update, and it will tell you which 'random' numbers are more or less likely to be picked. My histogram looked like this:
4: Using button clicks to mark a point on a chart
If you have a sensor connected to an IoT platform, and if the circumstances are interesting, you may want to make a mark so that later on you can check the corresponding sensor readings. For example: you are measuring air quality and you can smell something nasty in the air - you want to mark the point in time you smelt that smell, so later on you can see if the readings responded.
Below I will show you how to set up a 2-axis chart that will show temperature and will record when the b button on the micro:bit is pressed:
- Click Data / Dashboard
- Click the blue plus sign on the right and select Double Axis
- Click Add Left Axis Variable and choose YOURMICROBIT / temperature
- Click Add Right Axis Variable and choose YOURMICROBIT / b
- Set the Right Y-axis range to min = 0, max = 1. Edit the other chart fields to suit your tastes then click the green + sign to save the chart.
The chart will show the temperature over time, as per example 1 above. In addition, when you click the b-button on your micro:bit (remember to hold it down for a few seconds) a mark is made on the chart (see my example below). Far more interesting implementations of this are conceivable:
Consider a system to measure air quality. We have 1 sensor that measures CO2 and another measuring UV radiation. We are interested in the effects of UV rad on CO2 levels, so we set up a chart that shows both measures. We can now see how the 2 measures change on the same chart. This will give us clues as to whether they correlate, and if so how. Then why not add what we did in step 2 and trigger an email when CO2 and UV reach certain levels.
My 2-axis chart looked like this:
The first time you get this stuff to work it is pretty epic, but what we've gone through here is entry-level stuff that barely scratches the surface of what you can do in Ubidots or with IoT in general. The potential that IoT on micro:bit offers, both in terms of fun projects but also learning journeys, is vast and I can't wait to see the kind of applications eager young minds come up with!
Ubidots has a great knowledge base and their online support was very helpful (my thanks to S, the operator who sorted me out late on a Friday night :). There are also a wealth of projects and ideas that the community has generated, so get on Google and see the huge potential.
And please watch out for my next blog in this series, where I'll show you how a single micro:bit with the XinaBox IoT starter kit attached can enable a whole classroom of other micro:bits to join the IoT platform.