IoT Xmas Lights Revisited
Hickmat 100000QA3T Visits (8529)
Due to work pressures I didn't get time to do make much in the way of enhancements to the basic set up of my IoT controlled Christmas lights from last year. I had plans for enabling then with presence awareness and adding a scrolling message but all fell by the wayside I did however get some time to move the code to one of my Arduino's and do some enhancements with the NodeRED flow and core state management so I thought that would be worth a blog entry.
So what did I do... At a high level it breaks down as follows:
The Arduino port was very simple. I just lifted the sample recipe from HERE and updated it to support the light controlling commands I needed leaving my messageArrived code looking as follows:
Next I needed to create an event which I could post to indicate that the Arduino had restarted so the current light's state could be sent down from my NodeRED flow. To do this I created #define for a reconnect event and added code to the end of the
Finally I added some code to the overall flow so that I only issue an a lights status event message if the state of any of the lights has changed since the last time round the loop. I did this to optimise the amount of "noise" sent to my NodeRED flow. So with the Arduino code in place I needed to re-work my NodeRED flow. In my previous flow I had the Web and SMS flows separated out from the main control flow, this time I decided to smash them all together resulting in a flow which looks like:
Here you can see that I've added an "IBM IoT In" node to receive the reconnect event. On receipt of this event the flow reads the current state record from Cloudant and then issues a set command for each of the lights. This makes sure the lights match the state held in the database.
You can also see I added an "Inject" node to run a flow to do a random flash of the lights. The node is set to run every 15mins and when it fires the flow sets a loop counter to 20, calls a function to generate a random number between 1 and 4, this is then mapped to a light colour and the current state of that light colour is flipped. Finally the loop counter is decremented by one and if the counter is greater than 0 a non null message is fired out which make the call to control the lights and also loop back round via a "Delay" node which is set to delay for 3 seconds.
So now I have a nice shinny new set of lights in my house which flash randomly every 15mins and which in can easily control via the Web and even SMS. Job done
Happy Christmas everyone and I wish you all and prosperous New Year - I trust you have all been asking Santa for a Bluemix account and some IoT Devices to play with