Skip to content

Instantly share code, notes, and snippets.

@BretStateham
Last active May 2, 2019 01:36
Show Gist options
  • Save BretStateham/0fb8f74fc651e3d061259dde758bb6f6 to your computer and use it in GitHub Desktop.
Save BretStateham/0fb8f74fc651e3d061259dde758bb6f6 to your computer and use it in GitHub Desktop.
Running the Azure IoT Device Client for Python Simulated Device Quickstart with Python 3.7 on macOS

Running the Azure IoT Device Client for Python Simulated Device Quickstart with Python 3.7 on macOS

Using the following docs as a references:

Installing Python 3.7 using Homebrew (brew)

  1. Ensure Homebrew is installed

  2. Update brew:

    brew update
  3. Install python 3.7, boost, and boost-python3 using brew:

    Note: The python package installed below installs python 3.x, not python 2.x

    brew install python boost boost-python3
  4. 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!!!!!!

  5. 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 python3 and pip3

    python3 --version

    with output similar to:

    Python 3.7.0
    
  6. 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/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/python3
  7. 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)
  8. And that it is being run from /usr/local/bin/pip3 :

    which pip3

    Should return:

    /usr/local/bin/pip3
    
  9. Use pip to install the azure-iothub-device-client package:

    Note: Remember to use pip3

    pip3 install azure-iothub-device-client

    And verify it installed successfully:

    Successfully installed azure-iothub-device-client-1.4.3

Install and configure the Azure CLI

  1. Install the Azure CLI

  2. Login to the Azure CLI

Create your Azure Resource Group, IoT Hub, and Device

  1. Add the Azure CLI to create a Resoure Group, IoT Hub, and IoT Device:

    az extension add --name azure-cli-iot-ext
    
  2. Create a Resource Group in the location of your choice (e.g. mygroup in westus):

    az group create --name mygroup --location westus
    
  3. 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
    
  4. 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 the Azure IoT Samples for Python

  1. Download and extract the azure iot for python sdk samples: Azure IoT Samples for Python

  2. 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 and Run the Sample:

  1. 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="
  2. Run SimulatedDevice.py:

    Note: Remember to use python3

    python3 SimulatedDevice.py
  3. 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
    ...

Monitor your Iot Hub Messages with the Azure CLI

  1. 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}'
    
    ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment