-
Notifications
You must be signed in to change notification settings - Fork 3
IOT Team Sprint 2 Task 4: IOT Edge Module to upload log data
Install Docker CE (18.02.0+) on Windows, macOS or Linux.
Install Python (2.7/3.5/3.6/3.7/3.8) and Pip
Install iotedgehubdev by running below command in your terminal
pip install --upgrade iotedgehubdev
Note: Please install iotedgehubdev to root on Linux/macOS (Don't use '--user' option in the 'pip install' command).
To run the IOT Edge Dev Container:
- Create a local folder (you can use the the mkdir command shown below) to store your IoT Edge solution files. Once you create a folder, enter the docker command on the command line. This will create the files you need to work with IOT Edge.
mkdir c:\temp\iotedge
docker run -ti -v /var/run/docker.sock:/var/run/docker.sock -v c:/temp/iotedge:/home/iotedge microsoft/iotedgedev
sudo mkdir /home/iotedge
sudo docker run -ti -v /var/run/docker.sock:/var/run/docker.sock -v ~/iotedge:/home/iotedge microsoft/iotedgedev
mkdir ~/iotedge
docker run -ti -v /var/run/docker.sock:/var/run/docker.sock -v ~/iotedge:/home/iotedge microsoft/iotedgedev
-
Once you complete the previous step, enter the command below to initialize IoT Edge solution and setup Azure resources. This will create a sample module called "filtermodule" written in C#. We will need to change this to Python and customize it to collect log data from the Raspberry Pi.
iotedgedev init
-
Delete the C# file and create a Python file and add the following code to it. Please note that the Raspberry Pi has not yet been configured to collect actual data so this code is just a sample code that collects temperature data.
import smbus2 import bme280 import asyncio import time import json import RPi.GPIO as GPIO from azure.iot.device.aio import IoTHubDeviceClient port = 1 address = 0x77 bus = smbus2.SMBus(port) calibration_params = bme280.load_calibration_params(bus, address) GPIO.setmode(GPIO.BCM) GPIO.setup(24, GPIO.OUT) def get_temp(): data = bme280.sample(bus, address, calibration_params) return data.temperature def handle_twin(twin): print("Twin received", twin) if ('desired' in twin): desired = twin['desired'] if ('led' in desired): GPIO.output(24, desired['led']) async def main(): conn_str = "<Your device connection string goes here>" device_client = IoTHubDeviceClient.create_from_connection_string(conn_str) await device_client.connect() last_temp = "" while True: temp = "{0:0.1f}".format(get_temp()) print("Temperature", temp) if temp != last_temp: last_temp = temp; data = {} data['temperature'] = temp json_body = json.dumps(data) print("Sending message: ", json_body) await device_client.send_message(json_body) twin = await device_client.get_twin() handle_twin(twin) time.sleep(1) await device_client.disconnect() if __name__ == "__main__": asyncio.run(main())
-
Once you create a container registry in the the cloud account, update the container registry in the module.json file to the name of the server for the container registry. You can find it in the Access keys.
"repository": "<CONTAINER_REGISTRY_SERVER>"
-
Go to the deployment.template.json file. You will see two modules listed under "modules". Delete the filtermodule and change the name of the first module to "log_data". You will also need to update the image to the name of the container registry server.
"modules": { "log_data": { "version": "1.0", "type": "docker", "status": "running", "restartPolicy": "always", "settings": { "image": <CONTAINER_REGISTRY_SERVER>, "createOptions": {} } } DELETE: "filtermodule": { "version": "1.0", "type": "docker", "status": "running", "restartPolicy": "always", "settings": { "image": "${MODULES.filtermodule}", "createOptions": {} } }
-
Copy and paste the modified deployment.template.json file into the deployment.debug.template.json and deployment.amd63.json files.
-
Update the .env file to match your container registry's usernmame, password, and server.
-
Enter the command below to build the IoT Edge module images. This step will build user modules in deployment.template.json targeting amd64 platform.
sudo iotedgedev build
-
To run the simulator, enter the code below.
sudo iotedgedev start --setup --file config/deployment.amd64.json