#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!)

import os
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
print(getCPUtemperature())

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

import os
import iotc
def getCPUtemperature():
res = os.popen('vcgencmd measure_temp').readline()
return(res.replace("temp=","").replace("'C\n",""))
def sendTempAndConnectionDate():
print("Sending temp and connection date ..")
propJson = "{\"rpiTemp\": \"" + str(getCPUtemperature()) + "\"}"
iotc.sendProperty(propJson)
telemetryMessage = "{\"temp\": " + str(getCPUtemperature()) + "}"
iotc.sendTelemetry(telemetryMessage)

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

Advertisement

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: