Friday, April 22, 2016

Particle-Losant Integration using Webhooks

As I’ve posted on my blog before, I use a Spark Core to control the lights in my bedroom. In my first blog post, I described the original hardware and firmware. In the second blog post, I described the hardware and software updates, and also how I integrated it with IFTTT. Recently, I wrote a post about integrating Liquor Lights into the project. I had thought that these changes had fixed the remaining issues, but I was still experiencing problems where the lights would turn on and off without input. I had to think of a way to log what what happening so that I could dive in and see what is actually going wrong.

My Spark Lamp project’s hardware


Note: I took all of these screen captures back when Losant was called Structure. Besides the name, everything should be the same.

The launch of Losant generated a lot of buzz on Twitter because they were giving away a free IoT kit by using their product to send a tweet. I went through the tutorial to get the free kit, but in the meantime, I learned that this product would help me solve the problem of logging all of the actions that my Spark Lamp project was taking. Since I started my project shortly after the launch, I implemented my solution using webhooks. Webhooks are great because they are not platform-specific, but they do require more steps to get everything working. If you do plan on using a Particle device exclusively, it seems that Losant has recently released a native way to connect a Particle Photon to the Losant IoT platform.


When using webhooks, the first thing to do is to create an application in Losant, and then in the “More” tab on the right, select “Webhooks”. You can think of each webhook as an individual input. In my case, I created three webhooks: one for the lampState, one for the lightSwitchState, and one for the liquorSwitchState.


I also created a device called “Spark Lamp”, that contained five attributes. These attributes can be thought of as the variables that are updated in response to the webhook’s data. Three of my attributes correspond to the three webhooks and the other two are dillonLamp and saraLamp, which are just on/off data of individual lamps that are stored in lampState.


Workflows are the real brains of Losant. They let you receive data from the webhooks and store them in device attributes. Again, I’ve made three separate workflows for the main three data pieces. They all work by receiving a webhook and then setting attribute. I’ve included debugging blocks in each, because at the time they were really important to see what is going on, but I think that most of the details are shown in a separate debugging tab when viewing a workflow. get LampState is the most complicated because it uses some functions to split up the 2-bit binary number that is received from the lampState workflow into two 1-bit binary numbers of dillonLamp and saraLamp, while also setting lampState along the way. get lightSwitchState and get liquorSwitchState are similar but they deal with Boolean values so no extra processing is required. There is also a virtual button included in the liquor workflow so that I can toggle the Liquor Lights from inside this workflow.


The dashboard is used for displaying the data from the device attributes. I use a time series graph to display all of the times that variables change. It’s not the most effective graph for this type of data, but it is interesting to determine patterns of when the lights are used. The data table is the most useful for debugging because it logs exactly when the data changes. The only complaint I have is that it is difficult to determine why the device is behaving like it is (but maybe that’s a good use of some machine learning!). Finally, I display the state of the Liquor Light in a gauge. This is mainly for fun, although it would be interesting to create a custom block that shows a light bulb as on or off. While creating this dashboard, I experimented with the event list. I can see it being very helpful if you can code in error statuses to your application so you can catch them. I don’t know what is causing my problem, so I can’t necessarily create an event to trigger when it breaks.

Well there you have it. I hope this post was helpful for anyone that wants to connect an IoT device to Losant’s cloud for fun or profit! To read more of my thoughts, please follow me on Twitter: @Dillon1337

No comments:

Post a Comment