Connecting the Particle Cloud to IoT Hub

When the Particle Cloud is connected to IoT Hub, event data you publish on the device with Particle.publish is forwarded to Azure. The configuration is very straightforward and uses Particle's Integrations feature. To create the integration, navigate to the Particle Console and select Integrations as shown below:

Click New Integration and Azure IoT Hub to configure the forwarding of events. To connect to IoT Hub, you will need a Shared Access Policy. If you open up the IoT Hub you created earlier in the Azure Portal, you will find the Shared Access Policies link that lists the existing ones and allows you to create a new one. You can also use the following command to list the policies:

az iot hub policy list --hub-name <IoT_Hub_Name>

The iothubowner policy has all possible rights:

  • RegistryWrite: create and update devices in this IoT Hub's device registry; indeed, every device needs to be added to the registry before it is allowed to send data
  • RegistryRead
  • ServiceConnect: allows sending and receiving on cloud endpoints; you need this to send data to IoT Hub
  • DeviceConnect: allows sending and receiving on device endpoints

You can use the iothubowner policy to connect the Particle Cloud to this IoT Hub but it is recommended to create a new policy with the required rights. Create a new policy called particlepolicy with the following command:

az iot hub policy create --hub-name <IoT_Hub_Name> --name particlepolicy --permissions RegistryWrite  DeviceConnect ServiceConnect

To configure the policy you need the primary key of the policy which can be retrieved with the following command:

az iot hub policy show --hub-name <IoT_Hub_Name> --name particlepolicy

The JSON output from the above command contains the primaryKey field. Use the value of that field in the Integration configuration screen as the Shared Policy Key.

Be sure to type the Event Name above. Particle just shows an example here but you are in control of this name. Naturally, you should use the event name you choose here as the event name you pass to Particle.publish.

When you view the integration in the Particle Console, you get a lot of useful information including some logs to check if events are sent to IoT Hub:

Above you see the event data as it is sent to Azure IoT Hub. The event contains the ID of the Particle Photon as coreid. The Particle Core is the predecessor of the Photon and the name still persists. Don't we all have that in the software we write?

We should now check if there is indeed data ending up in our IoT Hub. But first, run the following command:

az iot hub show-stats --name <IoT_Hub_Name>

The above command outputs the number of devices in the IoT Hub's registry. The totalDeviceCount should have increased by one if you only connected one Particle Photon. The Particle Integration takes care of creating devices in the registry for you and it uses the coreid (which is unique) as the device's name (property in IoT Hub is deviceid).

To see the actual data that is received, use Device Explorer (for Windows; see https://github.com/Azure/azure-iot-sdk-csharp/releases):

In the Configuration tab, set the connection string for your IoT Hub (use az iot hub show-connection-string to obtain it). Then, in the Data tab, select the Device ID and click Monitor. The events will show up as they are received in their native JSON format.

Note

We are only sending a single value from the Photon. In a more realistic scenario, you would send more data like temperature, humidity, CO2 levels, fine dust measurements etc... In that case, you would format the data on the device as JSON and then use the Particle IoT Hub integration with Advanced Settings to control the event data you forward to IoT Hub and use parameters to pick up values from the JSON you sent from the device.

results matching ""

    No results matching ""