
Creating an light alarm clock with the MXChip!
7 - 9 minutes
Introduction
This project uses a WS2812B LED strip controlled by an MXChip, if you want to see how this is done read this blog post first.
Now I don't know about you but I hate waking up to a blaring alarm, it's definitely not the way I want to be starting the day. A light alarm clock tries to remedy this by waking you up gradually with light, rather then all at once with sound. And now that we have the MXChip set up and controlling the LED strip it seemed like the perfect project (especially now i'm back to University and 9ams)!
First Step: Remote control
The first step to this project is developing a way to control the colour and brightness of the LED strip without having to re-flash the MXChip every time. I found the solution in Jim Bennets great blog post about controlling an IoT device from your phone. He writes about using devices twins to set an RGB and brightness value on the cloud and then writing a small amount of code on the MXChip to pull that information and set the LED colours accordingly.
void parseTwinMessage(DEVICE_TWIN_UPDATE_STATE updateState,const char *message){Serial.printf("Time to decode this\n");rgbLEDR = getTwinData(updateState, message, "rgbLEDR");rgbLEDG = getTwinData(updateState, message, "rgbLEDG");rgbLEDB = getTwinData(updateState, message, "rgbLEDB");dither = getTwinData(updateState, message, "rgbDither");brightness = getTwinData(updateState, message, "rgbBrightness");Serial.printf("Red: %d, Green: %d, Blue: %d\n", rgbLEDR, rgbLEDG, rgbLEDB);rgbLed.setColor(rgbLEDR, rgbLEDG, rgbLEDB);showColour(rgbLEDR, rgbLEDG, rgbLEDB, dither, brightness);Screen.print(3, " > Updating...");}
This code is only a snippet of whats needed. The full code will be available to downloaded at the end.
HTTP Endpoint
Now while technically we can remotely change the colour of the lights we have to do so by updating a JSON file manually. Thankfully we can automate this with a little bit of Node.js and the azure-iothub package.
var iothub = require('azure-iothub');var connectionString = 'Your connection string';var registry = iothub.Registry.fromConnectionString(connectionString);registry.getTwin('JoesMXChip', function (err, twin) {if (err) {console.error(err.constructor.name + ': ' + err.message);} else {var patch = {properties: {desired: {"rgbLEDR": 255,"rgbLEDG": 128,"rgbLEDB": 64,"rgbDither": false,"rgbBrightness": 100}}};twin.update(patch, function (err) {if (err) {console.error('Could not update twin: ' +err.constructor.name + ': ' +err.message);} else {console.log(twin.deviceId + ' twin updated successfully');}});}});
Now we can update the lights by typing the colours into a JavaScript file! Ok ok so not much of an improvement but thats where the cloud comes in, by using Azures serverless solution we can expose this JavaScript to a HTTP endpoint and update the LEDs by making a request and including the RGB and brightness values as URL parameters.
Azure makes creating a serverless function a breeze, it's as easy as creating a new Function App resource, assigning it to a resource group and choosing what runtime stack you want (.NET, Python, Java, e.t.c) in this case we want Node.js.
Now we have a Function app we only have to make a few small
changes to our code. First we need to change the hardcoded
RGB values to the data thats being passed in with the HTML
requests, this can be done like so
parseInt(req.query.r)
for each of the values.
Secondly we need to export the function by simply wrapping
it in
module.exports async function (context, req) {}
When all is done you will have a URL like this example.azurewebsites.net/api/example?r=255&g=100&b=0&d=false&br=1 and by changing the RGB values in the URL you should be able to update the physical LED strip!
The alarm clock
Now we can change the LEDs colour (and more importantly for this application brightness) we need to trigger the lights to come on and gradually increase in brightness to wake us up. This can be achieved with an Azure Logic App. Logic Apps have a trigger and then work through a series of steps and trigger APIs. For this project we will be using a time trigger and setting the logic app to run every day at 6:50. From there we will make a HTTP request to the endpoint we just made to turn the lights on with a brightness of 1%. From there we can string together a delay and another HTTP call gradually increasing the brightness of the lights!.

Conclusion
Overall we have created an Azure function and exposed it to a HTTP endpoint to change the colour and brightness of the LED strip and a logic app to send requests to this endpoint periodicity to gradually increase the brightness, and let me tell you it works great!