-
Notifications
You must be signed in to change notification settings - Fork 3
IoT Edge module for uploading config every five minutes
### 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. Without a way of running it, I am not certain it will work. But for now, it is a jumping off point so we can go back and adjust the code later. You will need this dependency: pip import 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 = 'sample.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()`
For running your code and connecting to Azure I keep running into errors. One thing I have learned is that you will need this dependency:
pip install iotedgehubdev OR
pip install --upgrade iotedgehubdev
Next: iotedgehubdev setup -c "" Where = "HostName=bet-rg-feat-usw2-task2.azure-devices.net;DeviceId=san-edg-feat-usw2-task2;SharedAccessKey=Zui2YD413AcmSc7g+PFqDX2mpJcic64YpumUM69s0Oo="
In order for this to work, I had to run this on another person's laptop because mine still will not get past an Environment Error. Here is the command that works on someone else's laptop. You will need to look up your specific path to your deployment.template.json(you can find it below your .gitignore in your file explorer): iotedgehubdev start -d <path/to/deployment-manifest>
After running this command you should be able to see your module in the Azure Account!