#RaspberryPi – Putting all together to display device temperature using #AzureIoT and #docker. Privilege permissions and other lessons learned

Hi!

Today challenge was based on an easy one

How do I get a Raspberry Pi temperature using Python?

The lines to do this are quite simple, with the following lines we can get the absolute value for the device temperature, in Celsius (of course!)

Easy! My next step was to add a new [Device Property] to my [Device Template] in Azure IoT. I’ll store this as a temp string, so this is fine.

azure iot device template for raspberry pi with the temperature as device property

The lines to send the temperature as a device property are part of the following code sample. I also track the temperature as telemetry so I can work with the history of the device temp

So far, so good!

Now it was time to package all this in a docker image and run it from a container. I got an ugly surprise when I realize that I got an exception trying to get the device temperature

VCHI Initialization failed

Time to read and learn more about docker and containers on Raspberry Pi.

In the official documentation of [Docker Run, see references] I found a couple of options which may help me. There are 2 options to allow me access to the device temperature

  • Run the container with the specific path to the device I want to grant privileged access for my container
  • Run the container with the [–privileged] argument to enable access to all devices on the host

Of course, the 2nd one is easier, but much more dangerous

When the operator executes docker run –privileged, Docker will enable access to all devices on the host as well as set some configuration in AppArmor or SELinux to allow the container nearly all the same access to the host as processes running outside containers on the host. Additional information about running with –privileged is available on the Docker Blog.

I didn’t think twice and run my image with the [–privileged] argument.

sudo docker run --privileged -p 80:80 <Image ID>

And now I can get an amazing history and track of information using docker, containers and Azure IoT with a Raspberry Pi

azure iot dashboard displaying temperature history as a telemetry

Happy coding!

References

Advertisements

#Azure – Sending custom Telemetry and Event information from a #RaspberryPi device to #AzureIoT Central

Hi!

Azure IoT Central is one of the amazing services we can use on Azure. I was wondering how easy is to use a Raspberry Pi using Raspbian and Azure IoT and here is my experience.

Let’s assume we had a device up to date using Raspbian, our next step will be to create an Azure IoT Central application. The official step by step is the main resource for this

Create an
Azure IoT Central application (see references)

Once we have our application, we can quickly create a new Raspberry Pi device and use it. However, I’ll do an extra step, lessons learned as a handsome developer

Create a Device Template

Go to [Device Templates] and create a new template

azure iot central create new device template

For Raspberry Pi, I’ll name this [Raspberry Pi Dev]

azure iot central create new device template raspberry pi dev

So now, I can add a new real device, in the Devices section from the left menu

azure iot central raspberry pi dev add new real device

Once you create a new real device, is important to copy and save for later the connection information. To access this, go to the top right [Connect] button

azure iot central raspberry pi dev real device connect information

Almost there, there is an official tutorial that explain how to send random telemetry information with a Python script in a Raspberry Pi. I’ll use it as base for this scenario.

Connect a
Raspberry Pi to your Azure IoT Central application (Python) (see references)

For this demo, I’ll add a custom telemetry property and a custom event to the device. Since I won’t use the device to track temperature, accelerometer, and more, I think it make sense to track some custom information.

So, I’ll go back to my Device Template definition and I’ll add a new Telemetry, named [t1], with the following information.

azure iot central raspberry pi dev new telemetry information

And now, I can run a custom version of my script that will send new telemetry information, for [t1]. Sample in line 18

After a couple of minutes running the sample script, I can see the telemetry information for T1. In this view, I enabled [Temperature] and [T1] to display the timeline.

azure iot central raspberry pi dev real device dashboard telemetry

And, next step will be to add an event, which is also a very important uses case in Azure IoT. Back in the Device Template, I add a new event named [event1]

azure iot central raspberry pi dev new event information

And added some extra lines of code to send also an event between telemetry, Line 22

In the following image, we can see how the events appears in the timeline, and we can also get some extra details clicking on each event.

azure iot central raspberry pi dev real device dashboard telemetry and events

Very cool! Next steps will be to integrate this with some image recognition scenarios.

Happy Coding!

Greetings @ Burlington

El Bruno

References