The Edge Xpert MQTT Device Service provides an MQTT simulator. This is implemented with mqtt-scripts, a script runner based on Node.js for use in MQTT smart homes.
This is shipped with Edge Xpert and can be found in /usr/share/edgexpert/examples/device-services/mqtt/simulator/mqtt-scripts as the file room.js.
The MQTT script simulates a room sensor. It generates random values for the humidity and temperature of a room, whether a switch is turned ON or OFF, and the room number.
The device simulator supports the following behaviors:
- Publish room sensor data every 30 seconds
- Receive a request for readings and return the room number
- Receive a put request and change the room number accordingly
The following resources and commands are defined in the provided device profile (usr/share/edgexpert/examples/device-services/mqtt/simulator/simulator.profile.yml) and are detailed below:
||Read and Write|
|roomNumber||Reads and writes to
Run the Simulator
The MQTT room simulator can be run with the following command:
docker run -d --restart=always --name=mqtt-scripts --network=host -v /usr/share/edgexpert/examples/device-services/mqtt/simulator/mqtt-scripts:/scripts dersimn/mqtt-scripts --url mqtt://127.0.0.1 --dir /scripts
$ docker logs mqtt-scripts 2022-06-24 07:54:25.220 <info> mqtt-scripts 1.2.2 starting 2022-06-24 07:54:25.319 <info> mqtt connected mqtt://127.0.0.1 2022-06-24 07:54:25.830 <info> /scripts/room.js loading
If you wish to make any changes to the script, make a local copy of this script in your working directory and make changes to the copy. Replace the file path in the command with your own.
Connecting to the Simulator with Edge Xpert
Once both the Edge Xpert MQTT Device Service and the simulator are running, you can connect to the simulator simpling by adding a device to the device service.
When asked for a device name,
MQ_DEVICE should be used. This is the device name specified in the room.js script, and this must match the name used for the Edge Xpert device. If this has been changed in a local copy, use the new name.