Skip to content

IoT Edge module for uploading config every five minutes

rbeeler edited this page Feb 13, 2020 · 9 revisions

Setup a Python Module

Go to your Command Prompt: Azure IoT Edge: New IoT Edge solution. Follow the prompts and provide the information it asks for. Make sure you choose Python for your development language.

One of the steps it will ask you about is the Container Registry. This is different from the previous task on creating your Python Module. I have had trouble getting my environment working and believe this may help. There is now a Container Registry created called iotEdgeRegistryS2 in the Azure account. Use that one when it prompts you for the image repository: An image repository includes the name of your container registry and the name of your container image. Your container image is pre-populated from the name you provided in the last step. Replace localhost:5000 with <iotedgeregistrys2.azurecr.io>.

Next you will select your target platform: Open the command palette and search for Azure IoT Edge: Set Default Target Platform for Edge Solution. In the command palette, select the target architecture from the list of options. I chose the default amd64.

At this point you should have a basic template for your python module. Next we need to update the code so it will perform the task we want. This is the code that I have found from python documentation that closest resembles what we need to do. One thing that needs to be changed is the time it runs - right now it is every 30 secods for testing - but we want it to be every 5 minutes eventually.

Note: My issue with this code is a dependency that is not working although I have installed it. So, this code in not good - it also is not completely suited for what we need. This is something I will have to continuously work on to fix for our team although this sprint is over. dependency: iothub_client

import time import sys import iothub_client import os from iothub_client import IoTHubClient, IoTHubClientError, IoTHubTransportProvider, IoTHubClientResult, IoTHubError

CONNECTION_STRING = "{deviceConnectionString}" PROTOCOL = IoTHubTransportProvider.HTTP

FILENAME = 'configuration.txt'

def blob_upload_conf_callback(result, user_context): if str(result) == 'OK': print ( "...file uploaded successfully." ) else: print ( "...file upload callback returned: " + str(result) )

def iothub_file_upload_sample_run(): try: print ( "IoT Hub file upload sample, press Ctrl-C to exit" )

    `client = IoTHubClient(CONNECTION_STRING, PROTOCOL)`

    `client.upload_blob_async(FILENAME, FILENAME, os.path.getsize(FILENAME), blob_upload_conf_callback, 0)`

    `print ( "" )`
    `print ( "File upload initiated..." )`

    `while True:`
        `time.sleep(30)`

`except IoTHubError as iothub_error:`
    `print ( "Unexpected error %s from IoTHub" % iothub_error )`
    `return`
`except KeyboardInterrupt:`
    `print ( "IoTHubClient sample stopped" )`
`except:`
    `print ( "generic error" )`

if __name__ == '__main__': print ( "Simulating a file upload using the Azure IoT Hub Device SDK for Python" ) print ( " Protocol %s" % PROTOCOL ) print ( " Connection string=%s" % CONNECTION_STRING )

`iothub_file_upload_sample_run()`

Running Your Code

For running your code and connecting to Azure I keep running into errors. One thing I have learned is that you may need this dependency:
pip install iotedgehubdev OR pip install --upgrade iotedgehubdev

Next, in your terminal: docker login -u -p For us this equates to: docker login -u iotEdgeRegistryS2 -p yVEpOSo9rkyGR=ETcWEhhD2HY2e9=INL iotedgeregistrys2.azurecr.io

Last: In the VS Code explorer, right-click the deployment.template.json file and select Build and Push IoT Edge solution.

You will now see that you successfully pushed in the Container Registry in the Azure Account.

*Getting the code into its usual box was giving me trouble this week, but when using markdown 'code' option it highlighted everything that is code so although it is broken up oddly in this page, if it is highlighted, it is part of the program.

Clone this wiki locally