Connecting a Raspberry Pi with Sense HAT add-on board to Azure IoT Hub by using Python SDK. This tutorial is inteded to demo functionality of Azure IoT Hub (like Direct Methods and Device Twins) by using the Azure IoT Hub Python SDK.
This tutorial uses a Raspberry Pi 3 together with the Sense HAT add-on board.
Furthermore an Azure IoT Hub is required. Check out this tutorial on how to create an Azure IoT Hub. With the free tier you can send up to 8'000 messages per day to your IoT Hub which should be enough to get started.
After installing your Sense HAT, setup Raspian Stretch Lite on your Raspberry Pi according to official installation documentation or check out this blog post by Oliver Scheer which provides a good guide on how to setup a Raspberry Pi. Setup keyboard layout, wifi and enable SSH according to official configuration documentation.
I recommend walking through this excellent tutorial for making yourself comfortable with the Sense HAT add-on board. Python modules for Sense HAT should already be present when using the latest Raspbian release.
In case they are not, install them with the following command:
sudo apt install sense-hat
The pisensor.py script makes use of configparser to read and store configuration like send interval and iot hub connection string in a separate config file. Therefor installation of Python module configparser is necessary:
pip install configparser
Since I was unable to get a working environment by using the SDK modules from PyPi (pip install azure-iothub-device-client
), I compiled the SDKs for Python from souce code. Check out the official instructions to compile Azure IoT Hub Python SDK here
There are two ways how Python will find the binary modules needed.
- The binary modules are downloaded to Python local store using PIP.
- You copy the necessary binaries to the folder where you are running the Python application from.
The script will by default use MQTT protocol to connect to your Azure IoT Hub. Only thing you have to do is to add your IoT Hub connection string to the DEFAULT section in the pisensor.conf
config file.
You should now be ready to run the pisensor.py
script on your Raspberry Pi by using the following command:
python pisensor.py
If you want to run the script in the background and detach it from console, you could leverage nohub:
nohub python pisensor.py &
- Stefan Johner - Initial work - sjohner
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see License for details.