How to connect a Particle Photon to the Losant IoT Platform

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.

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.


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".


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.



 Next, follow the same steps for the SparkJson 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.

There are 6 Comments

What is Losant?

Losant is an enterprise IoT platform that makes it easy to build secure, complex connected experiences.

Losant’s platform enables you to intelligently manage your devices, collect and visualize data, and ultimately take action on data through highly customizable workflows - all in real-time.

Get Started For Free

How to connect a Particle Photon to the Losant IoT Platform