Skip to content

Modbus TCP/IP Example

The Modbus TCP/IP Device Service example demonstrates onboarding the ModbusPal simulator.

Before running this example, ensure that ModbusPal is configured and running as described in the Modbus Simulator section.

Start the Modbus Device Service

To start the Modbus Device Service, enter the following command:

edgexpert up device-modbus

Device Onboarding

Once the Modbus Device Service has been started, Modbus devices can be onboarded to Edge Xpert using either the Edge Xpert Manager UI or through Edge Xpert REST API commands. Instructions on how to use both methods are described below.

Device Onboarding with the Edge Xpert Manager UI

Note

Please refer to the Edge Xpert Manager UI Device Management capability for full details.

  1. Ensure the Edge Xpert Manager UI is started, for example:

    edgexpert up xpert-manager
    
  2. Open a browser and go to localhost:9090. The default username and password is admin

  3. Using the device profile upload capability, upload the provided Modbus example device profile at /usr/share/edgexpert/examples/device-services/modbus/power-submeter/DENT.Mod.PS6037.profile.yaml

  4. Onboard the Modbus device using the device onboarding capability. The following values can be used to provision the example ModbusPal device:

Field Example Value Description Required
Name Power-Submeter-Device The unique name used to identify the device Required
Description Simulation of a power submeter device Any additional information about the device Optional
Label Modbus TCP Any additional information about the device Optional
Host 172.17.0.1 The IP address of the host running Edge Xpert. Required
Port 1502 The port used for communication across Modbus. 502 is common but typically requires root access, so 1502 used here Required
Unit Identifier 1 The Modbus station or slave identifier Required
Device Profile Network-Power-Meter The name of the device profile uploaded above Required
Device Service device-modbus The name of the appropriate device service Required
Auto Events Interval: 5s

OnChange: false

ResourceName: Values
Automated events to retrieve the data at specific frequencies

Note: If OnChange is set to true, values will only be pushed if a change in value has occurred during the specified interval. If set to false, values will be sent on the specified interval regardless of if there has been a change
Optional

Device Onboarding with the Edge Xpert REST API

Info

If Edge Xpert is running in secure mode, you will need to replace localhost in the URL with the IP address for the service. See CLI Service Ports for details.

  1. Upload the provided Modbus example device profile:

    curl http://localhost:59881/api/v2/deviceprofile/uploadfile -F "file=@/usr/share/edgexpert/examples/device-services/modbus/power-submeter/DENT.Mod.PS6037.profile.yaml"
    

  2. Onboard the device using similar values as explained in the above table:

    curl http://localhost:59881/api/v2/device -H "Content-Type:application/json" -X POST \
      -d '[
            {
              "apiVersion": "v2",
              "device": {
                "name": "Power-Submeter-Device",
                "description":"Power Submeter device",
                "labels":[
                  "power submeter",
                  "modbus TCP"
                ],
                "adminState": "UNLOCKED",
                "operatingState": "UP",
                "protocols": {
                  "modbus-tcp": {
                    "Address": "172.17.0.1",
                    "Port": "1502",
                    "UnitID": "1"
                  }
                },
                "serviceName": "device-modbus",
                "protocolName":"modbus-tcp",
                "profileName": "Network-Power-Meter",
                "autoEvents": [
                  {
                    "interval": "50s",
                    "onChange": false,
                    "sourceName": "Configuration"
                  },
                  {
                    "interval": "5s",
                    "onChange": false,
                    "sourceName": "Values"
                  }
                ]
              }
            }
          ]'
    

Device Data Flow

Once the device is onboarded to Edge Xpert, data flow between the Device Service and the connected device can be verified in a number of ways.

The Edge Xpert Manager UI supports Read and Write functionality for each device. Readings can also be viewed and queried within the Edge Xpert Manager UI Data Center.

Alternatively, you can read and write data with cURL commands or through REST API applications, such as Postman.

Info

If Edge Xpert is running in secure mode, you will need to replace localhost in the URL with the IP address for the service. See CLI Service Ports for details.

Both the GET and PUT requests can be made to the following cURL commands:

curl http://localhost:59882/api/v2/device/name/{deviceName}/{commandName}

Using the example above, a GET request to this device service could be:

curl http://localhost:59882/api/v2/device/name/Power-Submeter-Device/Configuration

For more information on this, see the Core Command Microservice. A list of available API commands can be found under EdgeXpert API.

Any data collected by the Device Service can then be processed like all other Edge Xpert sensor data. The data readings can be exported for data processing in a number of other applications and services. See the Supporting Services and Application Services documentation for more details.