|
| 1 | +MQTT PV Support |
| 2 | +=============== |
| 3 | + |
| 4 | +MQTT is a broker-based protocol used in the Internet-of-Things (IoT) ecosystem. |
| 5 | +See https://mqtt.org for details. |
| 6 | +The `mqtt:...` PV support allows CS-Studio tools to read and write PVs via MQTT. |
| 7 | + |
| 8 | + |
| 9 | +Example Broker Setup and first steps |
| 10 | +------------------------------------ |
| 11 | + |
| 12 | +This example uses Eclipse Mosquitto on Linux. |
| 13 | +See https://mqtt.org for links to several other MQTT brokers and clients. |
| 14 | + |
| 15 | +Download: Get source release `mosquitto-....tar.gz` from https://mosquitto.org/download |
| 16 | + |
| 17 | +Unpack: `tar vzxf mosquitto-....tar.gz` |
| 18 | + |
| 19 | +Build: `make WITH_CJSON=no` |
| 20 | + |
| 21 | +Install: |
| 22 | + |
| 23 | +``` |
| 24 | +export LD_LIBRARY_PATH=`pwd`/lib |
| 25 | +# Optionally, add the following to a `bin` folder that's on your $PATH |
| 26 | +# src/mosquitto, client/mosquitto_sub, client/mosquitto_pub |
| 27 | +``` |
| 28 | + |
| 29 | +Run broker: |
| 30 | + |
| 31 | +``` |
| 32 | +# Allow remote access through firewall. |
| 33 | +# Depending on Linux release, similar to this |
| 34 | +sudo firewall-cmd --add-port=1883/tcp |
| 35 | +
|
| 36 | +# Create configuration file that allows remote access |
| 37 | +echo "listener 1883" >> mosquitto.conf |
| 38 | +echo "allow_anonymous true" >> mosquitto.conf |
| 39 | +
|
| 40 | +# Start broker with that configuration file |
| 41 | +src/mosquitto -c mosquitto.conf |
| 42 | +mosquitto version ... starting |
| 43 | +Config loaded from mosquitto.conf. |
| 44 | +... |
| 45 | +Opening ipv4 listen socket on port 1883. |
| 46 | +``` |
| 47 | + |
| 48 | +See https://mosquitto.org/documentation/authentication-methods |
| 49 | +for a more secure configuration. |
| 50 | + |
| 51 | +Subscribe to value updates: `client/mosquitto_sub -t sensors/temperature -q 1 ` |
| 52 | + |
| 53 | +Publish a value: `client/mosquitto_pub -t sensors/temperature -q 1 -m 42` |
| 54 | + |
| 55 | +By default, the broker will not persist messages. |
| 56 | +The subscribe command shown above will receive all newly |
| 57 | +published messages. If you close the `mosquitto_sub` and then restart it, |
| 58 | +it will show nothing until a new value is published. |
| 59 | + |
| 60 | +To persist data on the broker, each client that publishes or subscribes |
| 61 | +needs to connect with a unique client ID and an option to _not_ 'clean' the session, |
| 62 | +using options like `--id 8765 --disable-clean-session` for the `.._sub` and `.._pub` |
| 63 | +commands shown above. |
| 64 | + |
| 65 | + |
| 66 | +MQTT PV Configuration |
| 67 | +--------------------- |
| 68 | + |
| 69 | +By default, the MQTT PV will look for a broker on localhost |
| 70 | +and the default MQTT broker port 1883. |
| 71 | + |
| 72 | +To change this, add a variant of the following to your Phoebus settings: |
| 73 | + |
| 74 | +``` |
| 75 | +# MQTT Broker |
| 76 | +# All "mqtt://some/tag" PVs will use this broker |
| 77 | +#org.phoebus.pv.mqtt/mqtt_broker=tcp://localhost:1883 |
| 78 | +org.phoebus.pv.mqtt/mqtt_broker=tcp://my_host.site.org:1883 |
| 79 | +``` |
| 80 | + |
| 81 | +The MQTT PV will create a unique internal ID to read persisted messages, |
| 82 | +allowing the PV to start up with the last known value of an MQTT topic |
| 83 | +without need to wait for the next update. |
| 84 | + |
| 85 | + |
| 86 | +MQTT PV Syntax |
| 87 | +-------------- |
| 88 | + |
| 89 | +To interface with the example MQTT tag shown above, |
| 90 | +use the PV `mqtt://sensors/temperature`. |
| 91 | + |
| 92 | +The general format is `mqtt://` followed by the MQTT topic, |
| 93 | +for example `sensors/temperature`, |
| 94 | +and an optional `<VType>`. |
| 95 | + |
| 96 | +MQTT treats all tag data as text. By default, an MQTT PV expects |
| 97 | +the text to contain a number, but the optional `<VType>` will |
| 98 | +instruct the PV to parse the text in other ways. |
| 99 | + |
| 100 | +| `VType` | PV Value Parser | |
| 101 | +| ---------------- | ---------------------------------------------------------------------------- | |
| 102 | +| `<VDouble>` | This is the default, expecting the topic to parse as a floating point number | |
| 103 | +| `<VString>` | PV reads text as string | |
| 104 | +| `<VLong>` | Parse as long integer | |
| 105 | +| `<VDoubleArray>` | Parse as array of comma-separated floating point numbers | |
| 106 | +| `<VStringArray>` | Parse text as array of comma-separated strings | |
| 107 | + |
0 commit comments