This is the fourth article in Edgex series. This time, we connect an MQTT virtual device from scratch, experience a complete access process, and use the relevant service API to call.

1. Overall structure

Js script for MQTT Device; Mosquitos to act as proxies, similar to MQ brokers; The client uses the built-in device-MQTT-Go module of EdgeX, of course, we can also customize development based on the SDK of EdgeX. MQTT Device Simulation publishes the DataTopic and ResponseTopic and accepts requests from CommandTopic.

1. Prepare device configuration files

Create directories and files.

- custom-config 
    |- profiles 
        |- my.custom.device.profile.yml 
    |- devices 
        |- my.custom.device.config.toml
Copy the code

Edit profile file: vim my. Custom. Device. Profile. Yml, content is as follows

name: "my-custom-device-profile"
manufacturer: "iot"
model: "MQTT-DEVICE"
description: "Test device profile"
labels:
  - "mqtt"
  - "test"
deviceResources:
  -
    name: randnum
    isHidden: true
    description: "device random number"
    properties:
      valueType: "Float32"
      readWrite: "R"
  -
    name: ping
    isHidden: true
    description: "device awake"
    properties:
      valueType: "String"
      readWrite: "R"
  -
    name: message
    isHidden: false
    description: "device message"
    properties:
      valueType: "String"
      readWrite: "RW"

deviceCommands:
  -
    name: values
    readWrite: "R"
    isHidden: false
    resourceOperations:
        - { deviceResource: "randnum" }
        - { deviceResource: "ping" }
        - { deviceResource: "message" }
Copy the code

Edit the config file: vim my. Custom. Device. Config. Toml, content is as follows

# Pre-define Devices
[[DeviceList]]
  Name = "my-custom-device"
  ProfileName = "my-custom-device-profile"
  Description = "MQTT device is created for test purpose"
  Labels = [ "MQTT", "test" ]
  [DeviceList.Protocols]
    [DeviceList.Protocols.mqtt]
       CommandTopic = "CommandTopic"
    [[DeviceList.AutoEvents]]
       Interval = "30s"
       OnChange = false
       SourceName = "message"
Copy the code

CommandTopic here is used to handle GET/SET requests.

Docker-compose file

  1. Enter the Edgex-Compose project that we cloned earlier
  2. cd ./compose-builder
  3. performmake gen ds-mqtt mqtt-broker no-secty ds-virtual uiGenerate the docker-compose file, heremqtt-brokerThat ismosquitto.ds-mqttThat isdevice-mqtt-go
  4. Check the generated filescat docker-compose.ymlBefore the port mapping is deleted127.0.0.1
  5. adddevice-mqttEnvironment variables and data volumes
  6. Start theEdgeX Foundry
device-mqtt:
    ...
    environment:
        ...
        DEVICE_DEVICESDIR: /custom-config/devices
        DEVICE_PROFILESDIR: /custom-config/profiles
    ...
    volumes:
        - /usr/local/cq/tool/custom-config:/custom-config
Copy the code
  1. indocker-compose.ymlFile directory executiondocker-compose up -d
  2. docker psCheck that all services are started

Note: the export service in the red line can be ignored for now.

3. Run the MQTT device simulator

  1. createmock-device.jsfile
  2. vim mock-device.js
function getRandomFloat(min, max) { return Math.random() * (max - min) + min; } const deviceName = "my-custom-device"; let message = "test-message"; // DataSender sends async value to MQTT broker every 15 seconds schedule('*/15 * * * * *', ()=>{ let body = { "name": DeviceName, "CMD ": "randnum", "randnum": getRandomFloat(25,29).tofixed (1)}; publish( 'DataTopic', JSON.stringify(body)); }); // CommandHandler receives commands and sends response to MQTT broker // 1. Receive the reading request, then return the response // 2. Receive the set request, then change the device value subscribe( "CommandTopic" , (topic, val) => { var data = val; if (data.method == "set") { message = data[data.cmd] }else{ switch(data.cmd) { case "ping": data.ping = "pong"; break; case "message": data.message = message; break; Case "randnum": data. Randnum = 12.123; break; } } publish( "ResponseTopic", JSON.stringify(data)); });Copy the code
  1. Running equipment service
docker run -d --restart=always --name=mqtt-scripts \ -v /usr/local/cq/tool/mqtt-scripts:/scripts \ dersimn/mqtt-scripts - the url MQTT: / / 172.17.0.1 - dir/scriptsCopy the code

Note Replace /usr/local/cq/tool/mqtt-scripts with the host file path. After running, use docker PS to check whether the service is started.

4. Access the UI to view the device

Enter ${EdgexIp} : 4000

You see an associated device, and we click to go in

commandDefined in the representation modelget/setMethod,

auto eventsIndicates a scheduled task, that is, message resources will be executed for 30 seconds. If OnChange is false, data will still be reported if no changes are made.

Then we check to see if DataCentor has any data coming in

In addition, we can call related services through HTTP to obtain data, for example, we call device services to view all service informationWe can see that the service is already registered.

The next article covers exporting data.

5. Reference materials

Github.com/edgexfoundr… Docs.edgexfoundry.org/2.0/example…