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 2.7, boost, and boost-python using brew:
brew install python@2 boost boost-python
-
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 2.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
python3
andpip3
python --version
with output similar to:
Python 2.7.15
-
Verify that python is being run from
/usr/local/bin/python
:which python
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/bin
path. 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/python
-
Verify that your pip version is 18.0 or later, run:
pip --version
And verify it is 18.0 or later:
pip 18.0 from /usr/local/lib/python2.7/site-packages/pip (python 2.7)
-
And that it is being run from
/usr/local/bin/pip
:which pip
Should return:
/usr/local/bin/pip
-
Use pip to install the azure-iothub-device-client package:
pip 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
iothubowner
connection 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 myhub
Assume 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
:python 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}' ...