How To Connect a Particle Photon to the Losant IoT Platform

Brandon Cannaday
Brandon Cannaday | 3 minute read

The Particle Photon is an inexpensive and capable microcontroller with an embedded WiFi chip. It also includes a web-based IDE that makes it simple to program. It's a popular choice for IoT systems, and when combined with the Losant IoT Platform, it makes building complex connected solutions fast and easy.

Before you can connect the Photon to Losant, follow Particle's instructions to properly configure and connect the Photon to your WiFi.

Update: June 21st, 2017
Since publishing this article, we've released an official Particle Integration. We now recommend using the integration instead of the instructions in this tutorial.

Update: August 3st, 2017
We've written an updated artcle that walks you through how to use our official Particle Integration. Read that article here.

This guide will be reporting the value of a temperature sensor connected to the Photon's analog input. We'll also be toggling the onboard LED (connected to D7) using Losant commands. This combination covers Losant's two main communication mechanisms - state and commands.

If you haven't already, create an account and an application on Losant. Next, add a device to your Losant application for the Photon. For this example, there's a temperature probe connected to the Photon, so the firmware we're about to write will be reporting tempF and tempC attributes.

New Device form within the Applications tab of the Losant IoT platform.

Next you'll need some access keys to properly authenticate the Photon to Losant. After creating the access key, remember to copy the secret somewhere safe. Losant does not store the secret.

Now let's start writing some code. Create a new app and name it whatever you want. This example is named "Losant".

A new particle app titled Losant with code to the right.

Losant does not have an official SDK for Particle at this time. Fortunately the underlying MQTT protocol is easy to use directly. The first thing to do is include the community contributed MQTT and JSON libraries. Start by clicking the libraries button on the left navigation, then type "MQTT" in the search field. Once found, click the result to get it added to your app.

Search for MQTT within Losant’s community library.

Window within Losant Libraries with code and an option to Include In App.

 Next, follow the same steps for the SparkJson library.

Search for SparkJson within Losant’s community library.

With both libraries added, your app should now look like this:

Now let's set up some variables for the device ID and access keys you generated earlier.

You'll need to change the values of the LOSANT_DEVICE_ID, LOSANT_ACCESS_KEY, and LOSANT_ACCESS_SECRET defines. We then use the device ID to generate the MQTT topics to publish and subscribe to for this specific device.

Next, let's setup the MQTT client and the callback that will be invoked whenever a command is sent from Losant to this device.

Here we create the MQTT client and define the callback function to be invoked when something is published to the command topic. In Losant, commands are sent as JSON with a name and an optional payload. What commands are supported is entirely up to the device's firmware. In this example, we're looking for a command with the name "toggle". When we see it, we simply toggle the onboard LED (D7).

Let's now look at the setup function and the function used to connect to Losant's MQTT broker.

All the setup function is doing is initializing the serial interface (for debugging) and setting the onboard LED GPIO to an output, so we can turn it on and off. Particle includes a very useful command line tool that can be used to monitor the serial output of your devices. It's very useful when debugging.

The connect function sits in a loop attempting to connect to the Losant MQTT broker. Once connected it subscribes to the command topic.

The last function to look at is the loop.

The loop function sends the temperature state to Losant once a second. State is also represented as JSON. We're using the SparkJson library, which is a Particle-compatible wrapper around ArduinoJson to build the JSON for us.

You can now flash this source to your Particle and see that temperature data is being sent to Losant. You can also test the LED toggle command using the send command feature on the device page.

The full source code for this guide is available on GitHub. If you have any questions or comments about getting your Particle device connected to Losant, please leave them below.