Software apps and online services
The objective of this project is to demonstrate how to build an real Industrial Internet of Things application using the Microsoft IoT Edge to read the status of IO ports of a PLC (Programmable Logic Controller) using the protocol RS485 (Modbus).
The PLC choose of this project is the model CP-WS11/4DO4DI USB RS485 from an Brazillian company named ProxSys, this device has some logical inputs and a digital output conected to relay. This device supports communication using the protocol Modbus, a standart from industry to device-to-device connection.
Then we use a Raspberry Pi 2 B+, running Raspbian OS to work with the Azure IoT Edge, and sending the data to Azure IoT Hub.
- Config the Azure IoT Edge device
To do the configuration of Raspberry Pi to work as an Azure IoT Edge device, first you need to install the last version of Raspbian using this tutorial (https://www.raspberrypi.org/documentation/installation/installing-images/README.md)
Then you have to install the Azure IoT Edge module, this consists in a Docker application that will run two containers:
To install this containers you have to follow this tutorial from Microsoft that shows in detail how to install all packages (https://docs.microsoft.com/en-us/azure/iot-edge/how-to-install-iot-edge-linux-arm?WT.mc_id=AZ-MVP-5003638)
During this tutorial walk throw, you have to create your IoT Edge a new device into Azure and get the connection string provided from the registry process. To do that you have to log in Azure, and into your IoT Hub create an new IoT Edge device:
And then you can select your IoT Edge device to get your connection string, and use that into the /etc/iotedge/config.yaml file.
Once you had configured your IoT Edge device, we can go to the IoT Edge Modbus module, that is publish into this repository (https://github.com/Azure/iot-edge-modbus).
- Compiling the code, generating the container and pushing to Docker Hub
Before you clone the repository (https://github.com/Azure/iot-edge-modbus) it's important you have the Docker for Windows, Visual Studio Code and the Azure IoT Edge pluggin for Visual Studio Code installed.
The only change that I suggest, to make easy run this project is to modify the Dockerfile.arm32v7 on line 26, to use USER root insted of moduleuser. This change will prevent to have the error of no authorized access to the serial port in linux.
To compile the source code and generated the container just hit F1 and type iot edge, then select Azure IoT Edge: Build IoT Edge Module Image.
Select the module.json file that has the build process description.
And the desired platform: arm32v7
In the end of compile process, if you type the command
You will see that an new image was generated into your local docker machine
Then you can push this container to Azure Container Registry Service or like I did, to Docker Hub, using this commands:
docker tag <IMAGEID> <USERDOCKERHUB>/<IMAGENAME>
docker push <USERDOCKERHUB>/<IMAGENAME>
If you prefer you can use this docker image in my Docker Hub to run in your project.
Now the image containner is ready, we can run this IoT Edge module from Azure IoT Edge.
- Running Azure IoT Edge module and testing
First you need to find where Raspbian map your PLC USB device, to search that just run the command
You will see that the Raspbian map the USB device into the /dev/ttyACM0 port.
Go to Azure portal, select the IoT Hub service then select the IoT Edge and last select your IoT Edge device, click on the option Set Modules
Then select +Add and IoT Edge Module
On the next step, this screen will show up, you have to inform the name of the module, the image URI in Docker Hub or Azure Container Registry, the Container create options that is an JSON object that configure special parameters into the IoT Edge Device, and then the Module Twins Desired Properties, thats is another JSON object to configure the module IoT Edge Modbus that will run into your device.
This are suggest values for this fields:
Image URI: waltercoan/iotedgemodbus
Container Create Options:
Module Twin's desired properties:
It's important to note that this last JSON set what the module will read from PLC, we configure two operations OP01 and OP02, they read the UnitId 1 that is the device order identification, and the StartAddress 100002 and 100003 indicate that we want to get the status of the two switches connected to the input IO one and two from PLC. This address information can be found into the PLC manual.
On the next step you will configure the route that the messages from device will be route to IoT Hub. When you submit this last step, the IoT Edge will send and message to your device to publish this module.
If your connect again into your Raspberry Pi and run the command docker ps, you will see that the module is running into the device.
If you use the Visual Studio Code and the IoT Edge pluggin you can monitor the Device to Cloud messages and see that the device is receiving the status from de PLC.
This is an real application that applies Azure IoT Edge modules to send data to Azure IoT Hub from a PLC that don't have an Ethernet built-in capability.