Using the following docs as a references:
- Quickstart: Send telemetry from a device to an IoT hub and read the telemetry from the hub with a back-end application (Python)
- Homebrew and Python
-
Update brew:
brew update
-
Install python 3.7, boost, and boost-python3 using brew:
Note: The
pythonpackage installed below installs python 3.x, not python 2.xbrew install python boost boost-python3
-
CLOSE YOUR TERMINAL WINDOW, AND OPEN A FRESH TERMINAL WINDOW!!!!!!
SERIOUSLY CLOSE YOUR TERMINAL WINDOW, AND OPEN A FRESH TERMINAL WINDOW!!!!!!
I REALLY MEAN IT! CLOSE YOUR TERMINAL WINDOW, AND OPEN A FRESH TERMINAL WINDOW!!!!!! -
Verify that your python version is 3.7.x:
Note: The brew install for python3 creates symlinks for python3 and pip3 names, not python and pip (see: ls -al /usr/local/bin/p*) so you will need to explicity run them with
python3andpip3python3 --version
with output similar to:
Python 3.7.0 -
Verify that python is being run from
/usr/local/bin/python3:which python3
The path needs to be under
/usr/local/bin/not/usr/bin:Note: macOS comes with Python 2.7 installed, and it is access from the
/usr/binpath. If your python is coming from there, you aren't running the brew installed version. Brew installs all of it's stuff under/usr/local/usr/local/bin/python3
-
Verify that your pip3 version is 18.0 or later, run:
pip3 --version
And verify it is 18.0 or later:
pip 18.0 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
-
And that it is being run from
/usr/local/bin/pip3:which pip3
Should return:
/usr/local/bin/pip3 -
Use pip to install the azure-iothub-device-client package:
Note: Remember to use
pip3pip3 install azure-iothub-device-client
And verify it installed successfully:
Successfully installed azure-iothub-device-client-1.4.3
-
Add the Azure CLI to create a Resoure Group, IoT Hub, and IoT Device:
az extension add --name azure-cli-iot-ext -
Create a Resource Group in the location of your choice (e.g. mygroup in westus):
az group create --name mygroup --location westus -
Create an Azure IoT Hub, and optionally retrieve it's
iothubownerconnection string (e.g. myhub with the S1 sku):az iot hub create --name myhub -g mygroup --sku S1 az iot hub show-connection-string --hub-name myhub --output table -
Create an iot device in the hub, and get it's connection string (e.g. pydevice)
az iot hub device-identity create -d pydevice --hub-name myhub az iot hub device-identity show-connection-string -d pydevice --hub-name myhubAssume it's connection string is the following:
HostName=myhub.azure-devices.net;DeviceId=pydevice;SharedAccessKey=cx...ms=
-
Download and extract the azure iot for python sdk samples: Azure IoT Samples for Python
-
Change into the
/azure-iot-samples-python-master/iot-hub/Quickstarts/simulated-device folder:cd azure-iot-samples-python-master/iot-hub/Quickstarts/simulated-device
-
Modify (using the editor of your choice) the SimulatedDevice.py file and upate the connection string to match that for your iot device identity. For example:
CONNECTION_STRING = "HostName=myhub.azure-devices.net;DeviceId=pydevice;SharedAccessKey=cx...ms="
-
Run
SimulatedDevice.py:Note: Remember to use
python3python3 SimulatedDevice.py
-
Verify the output (You can press Ctrl-C to quit the sample):
bsmac:simulated-device bretstateham$ python SimulatedDevice.py IoT Hub Quickstart #1 - Simulated device Press Ctrl-C to exit IoT Hub device sending periodic messages, press Ctrl-C to exit Sending message: {"temperature": 29.75,"humidity": 64.86} IoT Hub responded to message with status: OK Sending message: {"temperature": 34.59,"humidity": 64.81} IoT Hub responded to message with status: OK ...
-
With the sample code still running from above, open a terminal (or Cloud Shell) where the azure cli 2.0 is installed and monitor events (messages) in your iothub . For example for my myhub hub:
hacker@Azure:~$ az iot hub monitor-events -n myhub Starting event monitor, use ctrl-c to stop... event: origin: pydevice payload: '{"temperature": 32.61,"humidity": 76.55}' event: origin: pydevice payload: '{"temperature": 22.99,"humidity": 72.19}' ...