MQTT Simulator
The Edge Xpert MQTT Device Service provides an MQTT simulator. This is implemented with mqtt-scripts, a Node.js-based script runner 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.
Simulated Values
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:
Resource | Read/Write |
---|---|
switch |
Read only |
temperature |
Read only |
humidity |
Read only |
number |
Read and Write |
Command | Included Resources |
---|---|
room | Reads switch ,temperature , and humidity values |
roomNumber | Reads and writes to number |
Run the Simulator
Query the MQTT broker IP for the simulator's MQTT connection:
$ edgexpert ip | grep mqtt-broker
Name |IP
---------------------------------------------
mqtt-broker |172.20.0.3
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://172.20.0.3 --dir /scripts
Note
Replace mqtt://172.20.0.3
with the MQTT broker IP for your own Edge Xpert deployment. This can be retrieved through using the command edgexpert ip
After deploying Edge Xpert, we can inspect the broker connection of the simulator:
$ 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 want to make changes to the script, create a local copy of this script in your working directory and make changes to this 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 by simply 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, which must match the name of the Edge Xpert device. If this has been changed in a local copy, use the new name instead.