Meshtastic networks in different locations beyond the reach of LoRa can be easily bridged together using MQTT. The simplest option is to connect your mesh to the official Meshtastic MQTT broker. This makes your devices appear on the world map, and provides a copy of your mesh traffic, translated into JSON. All you have to do to join the public MQTT server is to Enable MQTT and set Uplink and Downlink on the channels that you want to share over MQTT. The default device configuration using the public MQTT Server is encrypted.
You can also share channel settings with a remote network. If you use the default meshtastic MQTT server, packets are always encrypted. If you use a custom MQTT broker (ie set `mqtt.address`), the `mqtt.encryption_enabled` setting applies, which by default is false. You can also specify your own private MQTT broker and specify authentication for that broker to bridge several mesh networks together, via the internet (or just a local IP network).
Using or emitting packets directly in/from smart home control software such as Home Assistant or other consumers that can work with JSON messages.
When MQTT is enabled, the Meshtastic device simply uplinks and/or downlinks every raw protobuf packet that it sees to the MQTT broker. In addition, some packet types are serialized or deserialized from/to JSON messages for easier use in consumers. All packets are sent to the broker, whether they originate from another device on the mesh, or the gateway node itself.
Packets may be encrypted. If you use the default meshtastic MQTT server, packets are always encrypted. If you use a custom MQTT broker (ie set `mqtt.address`), the `mqtt.encryption_enabled` setting applies, which by default is false.
IMPORTANT: When MQTT is turned on, you are potentially broadcasting your entire mesh traffic onto the public internet. This includes messages and position information.
The payload is a raw protobuf. Looking at the MQTT traffic with a program like `mosquitto_sub` will tell you it's working, but you won't get much useful information out of it. For example:
Packets from the following [port numbers](/docs/development/firmware/portnum) are serialized to JSON and then forwarded to the `msh/2/json/CHANNELID/DEVICEID` topic: `TEXT_MESSAGE_APP`, `ENVIRONMENTAL_MEASUREMENT_APP`, `NODEINFO_APP` and `POSITION_APP`.
If the message received contains valid JSON in the payload, the JSON is deserialized and added as a JSON object rather than a string containing the serialized JSON.
**Sent messages** will be checked if the MQTT payload contains a valid JSON-encoded envelope:
```json
{
"sender": "SENDER",
"payload": {
"key":"value"
...
}
}
```
`sender` and `payload` fields are required for a valid envelope. If a valid MQTT message is found, the message is sent over the radio as a message of type `TEXT_MESSAGE_APP` with the serialized `payload` value in the message payload.
- Connect your gateway node to wifi, by setting the `network.wifi_ssid`, `network.wifi_psk` and `network.wifi_enabled` preferences.
- Alternatively use the RAK4631 with Ethernet Module RAK13800, by setting `network.eth_mode` and `network.eth_enabled`.
- Configure your broker settings: `mqtt.address`, `mqtt.username`, and `mqtt.password`. If all are left blank, the device will connect to the Meshtastic broker.
- Set `uplink_enabled` and `downlink_enabled` as appropriate for each channel. Most users will just have a single channel (at channel index 0). `meshtastic --ch-index 0 --ch-set uplink_enabled true`
The "mesh/crypt/CHANNELID/NODEID/PORTID" [topic](https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices) will be used for messages sent from/to a mesh.
Gateway nodes will forward any MeshPacket from a local mesh channel with uplink_enabled. The packet (encapsulated in a ServiceEnvelope) will remain encrypted with the key for the specified channel.
For any channels in the local node with downlink_enabled, the gateway node will forward packets from MQTT to the local mesh. It will do this by subscribing to mesh/crypt/CHANNELID/# and forwarding relevant packets.
If the channelid 'well known'/public it could be decrypted by a web service (if the web service was provided with the associated channel key), in which case it will be decrypted by a web service and appear at "mesh/clear/CHANNELID/NODEID/PORTID". Note: This is not in the initial deliverable.
A user ID string. This string is either a user ID if known or a nodeid to simply deliver the message to whoever the local user is of a particular device (i.e. person who might see the screen). FIXME, see what riot.im uses and perhaps use that convention? Or use the signal +phone number convention? Or the email address?
Any meshtastic node that has a direct connection to the internet (either via a helper app or installed WiFi/4G/satellite hardware) can function as a "Gateway node".
Gateway nodes (via code running in the phone) will contain two tables to whitelist particular traffic to either be delivered toward the internet, or down toward the mesh. Users that are developing custom apps will be able to customize these filters/subscriptions.
Since multiple gateway nodes might be connected to a single mesh, it is possible that duplicate messages will be published on any particular topic. Therefore, subscribers to these topics should
Node-RED is a free cross-platform programming tool for wiring together hardware, APIs, and online services developed originally by IBM for IOT. It is widely used for home automation by many non-professional programmers and runs well on Pi's. Node-red has many plug-in modules written by the community. I will use this platform as a practical example on how to interface with the MQTT features of Meshtastic. Everything can be done from GUI's without using command line.
Go to Channel Editor and set Uplink and Downlink enabled to True. Save. There is currently a bug for setting Uplink and Downlink Saving for the default channel. If you encounter this use the CLI commands:
Step two: if you don't want to depend on JSON decoding on the device, you can decode the protobuf messages off-device. To do that you will need to get the .proto files from https://github.com/meshtastic/protobufs. They function as a schema and are required for decoding in Node-RED. Save the files where the node-RED application can access them and note the file path of the "mqtt.proto" file.
Drag, drop, and wire the nodes like this. For this example, I ran node-RED on a Windows machine. Note that file paths might be specified differently on different platforms. MQTT server wild cards are usually the same. A "+" is a single level wildcard for a specific topic level. A "#" is a multiple level wildcard that can be used at the end of a topic filter. The debug messages shown are what happens when the inject button sends a JSON message with a topic designed to be picked up by the specified Meshtastic device and then having it rebroadcast the message.
[{"id":"10fe1b2e9cb3feb2","type":"decode","z":"23dbb1ee.bc2e8e","name":"decode Protobuf","protofile":"a0d4288141f6a629","protoType":"ServiceEnvelope","x":295.5,"y":285,"wires":[["d3e396cf4f0a9608","d08865b41a69d85d","6f592d47b6a2eac4"]]},{"id":"40c9ee66fe7a34cb","type":"function","z":"23dbb1ee.bc2e8e","name":"function get the message as string from TEXT_MESSAGE_APP","func":"msg.payload = msg.payload.packet.decoded.payload;\n\nlet bufferObj = Buffer.from(msg.payload, \"base64\");\nlet decodedString = bufferObj.toString(\"utf8\");\nmsg.payload = decodedString;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410.5,"y":450,"wires":[["553374591214eaca"]]},{"id":"553374591214eaca","type":"debug","z":"23dbb1ee.bc2e8e","name":"text message out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":762.5,"y":449,"wires":[]},{"id":"c6afbb9f1665b162","type":"debug","z":"23dbb1ee.bc2e8e","name":"channelId","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":785.5,"y":257,"wires":[]},{"id":"607ef387d5701985","type":"debug","z":"23dbb1ee.bc2e8e","name":"gatewayId","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":792.5,"y":293,"wires":[]},{"id":"d3e396cf4f0a9608","type":"debug","z":"23dbb1ee.bc2e8e","name":"entire payload","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":296.5,"y":247,"wires":[]},{"id":"2339b328bb9bb1d8","type":"comment","z":"23dbb1ee.bc2e8e","name":"Decode all cleartext text and position messages sent by Meshtastic devices into JSON without relying on JSON conversion on the device.","info":"","x":515.5,"y":214,"wires":[]},{"id":"408d796d997bb832","type":"function","z":"23dbb1ee.bc2e8e","name":"function get the nested payload as base64","func":"msg.payload = msg.payload.packet.decoded.payload;\n\nlet bufferObj = Buffer.from(msg.payload, \"base64\");\n//let decodedString = bufferObj.toString(\"hex\");\nmsg.payload = bufferObj;\nmsg.topic=\"\";\n//if you don't zero out the protubufTopic it will try to\n//decode it as part of the mqtt service envelope instead\n//of two-stage decoding\nmsg.protobufType=null;\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":349,"y":552,"wires":[["9435a3c605efedb4","1ed6f96c8214d7b3"]]},{"id":"61995c9f8e8266b3","type":"debug","z":"23dbb1ee.bc2e8e","name":"portnum","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":784.5,"y":330,"wires":[]},{"id":"9435a3c605efedb4","type":"debug","z":"23dbb1ee.bc2e8e","name":"nested payload","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":281.5,"y":603,"wires":[]},{"id":"b832775d386f7ac9","type":"mqtt in","z":"23dbb1ee.bc2e8e","name":"","topic":"msh/+/c/#","qos":"2","datatype":"buffer","broker":"37cadac381653b1e","nl":false,"rap":true,"rh":0,"inputs":0,"x":117.5,"y":286,"wires":[["10fe1b2e9cb3feb2"]]},{"id":"d08865b41a69d85d","type":"switch","z":"23dbb1ee.bc2e8e","name":"switch manual decoding nested message based on portum","property":"payload.packet.decoded.portnum","propertyType":"msg","rules":[{"t":"eq","v":"TEXT_MESSAGE_APP","vt":"str"},{"t":"eq","v":"POSITION_APP","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":281.5,"y":505,"wires":[["40c9ee66fe7a34cb"],["408d796d997bb832"]]},{"id":"8abb1bb458af2c4f","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"gatewayId","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1021.5,"y":288,"wires":[[]]},{"id":"1ced0be28eeef0d3","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"latitude","pt":"fl
Node-red can rapidly (minutes vs days) put together some pretty impressive output when paired with meshtastic. Here is the output of that flow geofencing and mapping via mqtt data.
Advanced use, such as encoding Position and sending it to a device via MQTT without using JSON can get a little complicated. An example of how it can be done is below.
[{"id":"32ca608d9e7c5236","type":"inject","z":"23dbb1ee.bc2e8e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":96.5,"y":1952,"wires":[["2b536512e8c7aef2"]]},{"id":"20bbd2d1408b8dc5","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"channelId_outbound","pt":"flow","to":"LongFast","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":772,"y":2027,"wires":[[]]},{"id":"c6cb373157be01d6","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"gatewayId_outbound","pt":"flow","to":"\"!55c7312c\"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":772,"y":2066,"wires":[[]]},{"id":"24199ec7eaf89c1a","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"portnum_outbound","pt":"flow","to":"3","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":774,"y":2106,"wires":[[]]},{"id":"de38ad5ef343623a","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"from_outbound","pt":"flow","to":"1439117612","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":781,"y":2146,"wires":[[]]},{"id":"d435e8abe0852f93","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"to_outbound","pt":"flow","to":"4294967295","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":2188,"wires":[[]]},{"id":"1f8d172708898860","type":"function","z":"23dbb1ee.bc2e8e","name":"Assemble Position protobuf","func":"msg.protobufType=null;\nmsg.payload =\n{\n \"packet\": {\n \"from\": flow.get(\"from_outbound\"),\n \"to\": flow.get(\"to_outbound\"), \n \"decoded\":{\n //how ENUMS are handled here\n //portnum is decoded as string but encoded as number\n //in the encode/decode node-red nodes based on protobuf.js\n \"portnum\": flow.get(\"portnum_outbound\"),\n \"payload\": msg.payload \n } \n },\n\n \"channelId\": flow.get(\"channelId_outbound\"),\n \"gatewayId\": flow.get(\"gatewayId_outbound\"),\n};\nreturn msg;\n//info on how to get json data into protobuf \"bytes\" field\n//https://github.com/protobufjs/protobuf.js/wiki/Changes-in-ProtoBuf.js-3.8","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1086,"y":2019,"wires":[["b8ccf1cfe8bf40a3"]]},{"id":"b8ccf1cfe8bf40a3","type":"encode","z":"23dbb1ee.bc2e8e","name":"","protofile":"a0d4288141f6a629","protoType":"ServiceEnvelope","x":1287,"y":2020,"wires":[["dbc78f035c9c2b56","a002c148f3a06bac"]]},{"id":"03a7e69ca6d471fe","type":"debug","z":"23dbb1ee.bc2e8e","name":"show hex string","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1319,"y":2180,"wires":[]},{"id":"dbc78f035c9c2b56","type":"function","z":"23dbb1ee.bc2e8e","name":"dump payload as hex string","func":"var hex=Buffer.from(msg.payload,\"hex\");\nmsg.payload=hex.toString(\"hex\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1096,"y":2178,"wires":[["03a7e69ca6d471fe"]]},{"id":"2b536512e8c7aef2","type":"function","z":"23dbb1ee.bc2e8e","name":"Inject lat lon alt","func":"msg.payload={\n \"latitudeI\": 399600000,\n \"longitudeI\": -862600000,\n \"altitude\": 100\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":277.5,"y":1953,"wires":[["9443a9a980e54c75"]]},{"id":"9443a9a980e54c75","type":"encode","z":"23dbb1ee.bc2e8e","name":"encode Position as protobuf","protofile":"dbab6472b07929a0","protoType":"Position","x":506,"y":1953,"wires":[["5c36d3a7f4dca14e"]]},{"id":"5c36d3a7f4dca14e","type":"change","z":"23dbb1ee.bc2e8e","name":"","rules":[{"t":"set","p":"nested_outbound","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":776,"y":1952,"wires":[["20bbd2d1408b8dc5","c6cb373157be01d6","24199ec7eaf89c1a","de38a