You can leverage one’s Azure IoT service client SDKs to build the IoT hub and interact with devices from the cloud. The service SDK is needed to:
- Manage the device identity registry (CRUD operations for devices).
- Send messages to devices (C2D messages)
- interact with Device Twins and Invoke Device Direct Methods
You need the device client SDK to:
- Send event data to Azure IoT Hub
- Recieve messages from Azure IoT Hub
- Communicate with Azure IoT Hub via AMQP, MQTT or HTTP protocols
- Synchronize to Azure IoT Hub Device Twin or Module Twin with Azure IoT Hub from a device or module
- Implement Azure IoT Hub Direct Device / Modules Methods on devices / modules
- Implement Azure IoT Device / Module Management features on devices / modules
- Implement Azure IoT Edge and custom modules connecting to Edge Hub
To install these two SDKs you have to open the terminal on the Pi and use the following commands:
pip install azure-iothub-service-client pip install azure-iothub-device-client
If pip is not already installed use this command to install it:
For python3:
sudo apt-get install python3-pip
For python2:
sudo apt-get install python-pip
But the installation should probably be done like this:
Build the Azure IoT Hub SDKs for Python on Linux
Installs needed to compile the SDKs for Python from souce code
Because the Azure IoT SDKs for Python are wrappers on top of the SDKs for C, you will need to compile the C libraries if you want or need to generate the Python libraries from source code. You will notice that the C SDKs are brought in as submodules to the current repository. In order to setup your development environment to build the C binaries, you need to follow the instructions here:
- Clone the Azure IoT Python SDK Repository
git clone --recursive https://github.com/Azure/azure-iot-sdk-python.git
- For Ubuntu, you can use apt-get to install the right packages:
sudo apt-get update sudo apt-get install -y git cmake build-essential curl libcurl4-openssl-dev libssl-dev uuid-dev
- Verify that CMake is at least version 2.8.12:
cmake --version
For information about how to upgrade your version of CMake to 3.x on Ubuntu 14.04, read How to install CMake 3.2 on Ubuntu 14.04?.
- Verify that gcc is at least version 4.4.7:
gcc --version
For information about how to upgrade your version of gcc on Ubuntu 14.04, read How do I use the latest GCC 4.9 on Ubuntu 14.04?.
Compile the Python modules
The Python iothub_client and iothub_service_client modules support python versions 2.7.x, 3.4.x, 3.5.x or 3.6.x. Know the appropriate version you would like to build the library with for the following instructions.
- Clone the Azure IoT Python SDK Repository
git clone --recursive https://github.com/Azure/azure-iot-sdk-python.git
- Ensure that the desired Python version (2.7.x, 3.4.x, 3.5.x or 3.6.x) is installed and active. Run
python --version
orpython3 --version
at the command line to check the version. - Open a shell and navigate to the folder build_all/linux in your local copy of the repository.
- Run the
./setup.sh
script to install the prerequisite packages and the dependent libraries.- Setup will default to python 2.7
- To setup dependencies for python version greater than 3, run
./setup.sh --python-version X.Y
where „X.Y“ is the python version (e.g. 3.4, 3.5 or 3.6)
- Run the
./build.sh
script.- Build will default to python 2.7
- To build with python version greater than 3, run
./build.sh --build-python X.X
where „X.Y“ is the python version (e.g. 3.4, 3.5 or 3.6)
- After a successful build, the
iothub_client.so
Python extension module is copied to the device / samples and service / samples folders. Visit these folders for instructions on how to run the samples.