Particle is a maker of inexpensive IoT hardware. They also have a web-based IDE, which makes it easy to connect your device to the internet. We've built an official Particle Integration to make it easy to connect a Particle device to our platform to build real-time connected solutions.
This is a quick reference on how to connect Particle with the Losant IoT Platform.
Prerequisites:
Let’s get started. Here's some sample code that I'll be using for this post:
Look at line 19. You’ll see the Particle.publish
method being called. This triggers a Particle Event named “climate_reading” with the voltage and temperature. To save resources, notice that we are sending twopieces of data in one call with a “:” as a delimiter. Next, we’ll create an integration in Losant that can listen for these events to trigger a workflow. In that workflow, we’ll parse the data.
Let’s create an integration next. Integrations are connections to external MQTT brokers, Pub/Sub messaging suppliers, or other third-party web services. These connections can then be used to trigger workflows and output messages from your application. In this case, we are using the Particle integration to trigger a workflow every time the “climate_reading” event is published.
Click on “Integrations” in main application menu, then click on “Add Integration”.
Set the following details on your integration:
Let’s create a workflow inside Losant. Now, we will drag the Particle node onto the canvas. Select the Integration that you just created.
Then, connect a Function node to the Particle node. In the Function node, add this code:
That code parses the data in the Particle event and adds it back onto the payload.
Next, drag a Debug node to the canvas and connect it to the Function node. Then, deploy your workflow. If your Particle device is running, you should be able to see the data coming into the Debug panel.
If you want to store the device state, you’ll need to create a device in Losant to track the state of your Particle board.
In the main application menu, click on “Devices” -> “Add Device”, then click on “Create Blank Device”.
Set the following details on your device:
Notice in step 3, we are tagging the device with the “particle_device_id”. This is useful because we’ll use that tag to get the device and update its state.
Now, go back to the workflow that you previously created. Drag the Device: Get node, and connect it to the Function node.
Set the following details on your Device: Get node...
This will allow you to look up the device by the Particle Device ID, which comes from the payload.
Drag and connect the Device State node to the Device: Get node. Set these details on your Device State node:
This will update the state of your device. To test if your configuration is working, click on "Devices" -> "Climate Sensor" in the main application bar, then click on “Debug”. You should see a list of your most recent states under “Recent Device States”.
Let’s look at the code on your Particle device. If you look at line 9, you’ll notice Particle.function("ledToggle",ledToggle);
That line of code registers a function on the Particle board that listens for the ledToggle
command that can be called from Losant. To do this, Drag the Particle Call node and connect it to the Device State node. Set these details:
This will turn the LED on after the text message is sent. Next, we’ll make the LED turn off after 5 seconds. Drag the Delay node to canvas and connect it to the Particle Call Node. Set seconds to “5”.
Then, connect another Particle Call node to the Delay node. Set these parameters:
Now you can deploy your code and test again.
To visualize the data, you can set up a dashboard. In the main platform navigation, go to “Dashboards” -> “Create Dashboard”.
First, we’ll add a Time Series Graph to view when the door was opened and closed. Locate the Time Series Graph block, and click “Customize”.
Set these options:
If you want to chart the voltage, you can click “Add Segment” and follow the same steps for the voltage attribute.
Click Add Block
Next, we’ll add a Data Table block to view the logs. Click “Add Block” under the gear icon in the top right. Look for the “Data Table” block and click “Customize”.
Set these options:
Go to the Configure Table, scroll down and click “Add Column” -> “Attribute”. Set the Source to “temperatureF”. Repeat those steps to add the “voltage” attribute, and click “Add Block”. Now, you’re set with a dashboard.
These are all the essential building blocks to integrating a Particle application with the Losant IoT Platform. If you build something interesting, post it on our forums. If you have any questions/comments, please post them below.