Skip to content

Instantly share code, notes, and snippets.

@yunqu
Last active March 26, 2020 04:50
Show Gist options
  • Save yunqu/6bd023d9af1dade0a59f559f0596d0fc to your computer and use it in GitHub Desktop.
Save yunqu/6bd023d9af1dade0a59f559f0596d0fc to your computer and use it in GitHub Desktop.
PYNQ interacting with Azure IoT

PYNQ and Azure IoT

This article records the steps required to make Azure IoT work with PYNQ image v2.5. I am using Ultra96v2 as an example in this article.

Setup

Follow the instructions for Raspberry Pi to set up the device.

When you create the IoT hub, remember to choose 'Central US' as the region. See below as an example; we choose ultra96debug as the IoT hub name.

Once the IoT hub is ready, you can add a device by following the online tutorial. In this article we will just name our device ultra96. You can check the device on your portal. Note down the device connection string, which will be used later.

Follow the online tutorial until you have to configure the sample application. Instead of doing the steps in the online tutorial, use the commands below to build the application.

sudo apt-get install git-core
git clone https://github.com/yunqu/iot-hub-c-raspberrypi-client-app.git
cd ./iot-hub-c-raspberrypi-client-app
sudo chmod u+x setup.sh
sudo ./setup.sh

It may take a while for the above commands to run. It generates random data and publish the data on the IoT hub. We also refrain from using Raspberry Pi packages.

Run

After the setup is done, you can now run the following commands:

sudo ./app '<DEVICE CONNECTION STRING>'

The connection string is the "Primary Connection String" you copied in the previous step.

Note: The program may ask you for permission to collect data, please choose "N" as the data collection may lead to segmentation fault.

If everything is properly done, you should be able to see something similar to the following:

Messages will be published every 10 seconds.

Visualize

There are multiple ways to visualize your data. In this article we will use a web application to show the sensor data. Throughout this section, please make sure your board is still sending data (keep the executable app always running).

1. Setup on Azure Cloud

In Azure cloud shell, run the following:

az extension add --name azure-iot
az iot hub consumer-group create --hub-name YourIoTHub --name YourConsumerGroupName
az iot hub show-connection-string --hub-name YourIotHub --policy-name service

This will show the connection string, something like: "HostName={YourIotHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}".

Keep a note of (1) your consumer group name and (2) the connection string.

2. Run Web App on Windows

On your local windows machine, install git and nodejs.

After that you can run the commands below (remember to replace the consumer group name and the connection string; don't quote the strings):

git clone https://github.com/Azure-Samples/web-apps-node-iot-hub-data-visualization.git
cd web-apps-node-iot-hub-data-visualization
set EventHubConsumerGroup=YourConsumerGroupName
set IotHubConnectionString=YourIoTHubConnectionString
npm install
npm start

A message like "Successfully created the EventHub Client from IoT Hub connection string" should show up.

Open a browser to http://localhost:3000. You should be able to see the sensor data get updated every 10 seconds.

And your windows terminal will look like:

Summary

In this article we have followed the Raspberry Pi tutorial to interact with the Azure cloud. We have a long-running application constantly sending messages to the cloud. A web application is used to show the published messages.

References

  1. Connect Raspberry Pi to Azure IoT Hub (C)
  2. Visualize real-time sensor data from your Azure IoT hub in a web application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment