Creating an Azure IoT Edge custom module (RaspberryPi & Python)

Create a custom module after Install and Settings of Azure IoT Edge.

It's the very first, so I'll give it a try. (It feels like a waste of technology than Mr. T's FPGA + IoT Edge with L Chika) However, there are traps just by L-Chika. I'll explain in detail later, but the problem is that IoT Edge is running in a container.

See also the official documentation (https://docs.microsoft.com/ja-jp/azure/iot-edge/tutorial-python-module?view=iotedge-2018-06).

Preparation

・ IoT Hub ・ RaspberryPi (ARM32v7 or higher model) IoT Edge installed ・ Container registry ・ Docker ・ VS Code · Python extension for VS Code · Azure IoT Tools for VS Code ・ Python (3.7)

Connect the LED and resistor to the Raspberry Pi. led.png

Creating a project

    1. Start VS Code. Press F1 to open the command palette.
    1. Azure: Sign in to Azure with the Sign in command.
    1. Enter and run the Azure IoT Edge: New IoT Edge solution command in the command palette. led01.PNG
  1. Select the folder where you want to save the solution files. led02.PNG

  2. Enter the solution name. led03.PNG

  3. Select the Python module. led04.PNG

  4. Enter the module name. led05.PNG

  5. Enter the repository information. It will be [registry name] .azurecr.io/module name. led07.PNG

  6. Open the .env file in the project and check that the user name and password are set. If not set, copy the username and password from the container registry access key. led09.PNG contener05.PNG

  7. Click the target platform at the bottom of the screen and change the platform to "ARM32v7". led08.PNG

  8. Make sure that the language used at the bottom of the screen is Python 3.7. If it is different, select it again on the command palette. led10.PNG

  9. Open main.py and change it to the following:

main.py


import time
import RPi.GPIO as gpio

gpio.setmode(gpio.BCM)

gpio.setup(26,gpio.OUT)


try:
    while True:

        gpio.output(26,1)

        time.sleep(.5)

        gpio.output(26,0)

        time.sleep(.5)

except KeyboardInterrupt:
    gpio.cleanup()
  1. Open Dockerfile.arm32v7 and make the following changes.

Dockerfile.arm32v7


FROM arm32v7/python:3.7-slim-buster

WORKDIR /app

RUN apt update && apt install -y \
    build-essential

COPY requirements.txt ./
RUN pip install -r requirements.txt

RUN pip install RPi.GPIO

COPY . .

CMD [ "python3", "-u", "./main.py" ]
  1. Open deployment.template.json and change line 67 and beyond. This setting is the key. Normally, you cannot access the device hardware from within the container. You can access it from a container by using the privileged option, but you can't specify it on the command line like Docker. You can access it by adding an option in deployment.template.json. If you need to access Bluetooth or network other than GPIO of the device, you need to add such an option. (But why is this information not officially available? Overlooked?)

deployment.template.json



~ Omitted ~

"createOptions": {
  "NetworkingConfig": {
    "EndpointsConfig": {
      "host": {}
    }
  },
  "HostCOnfig": {
    "NetworkMode": "host",
    "Privileged": true
  }
}

~ Omitted ~

led13.PNG

  1. Right-click deployment.template.json and click Build and Push IoT Edge Solution. If there is no problem, the built solution file will be registered in the container registry. led14.PNG

  2. Expand Devices from the Auzre IoT Hub section, right-click the device you want to deploy and click "Place in One IoT Edge". led15.PNG

  3. Open the config folder and select "deployment.arm32v7.json". led16.PNG led17.PNG led18.PNG

  4. If there is no problem, the LED will start blinking after a while.

  5. Try changing the numbers in time.sleep on lines 14 and 18 of main.py.

  6. Increase the value of "version" on the 7th line in module.json and save it.

  7. If you proceed from step 15 again, the changes should be reflected on the device.

I was able to do remote deployment using IoT Edge.

What did you think.

Recommended Posts

Creating an Azure IoT Edge custom module (RaspberryPi & Python)
Creating an egg with python
Studying Python Part.1 Creating an environment
python3 How to install an external module
Note when creating an environment with python
[Azure] Hit Custom Vision Service with Python