flespi MQTT channel protocol by Gurtam serves as an MQTT client and is used to receive messages directly from the MQTT broker.
Channel configuration
When you create a new MQTT channel, you can configure it in one of the two ways:
- flespi MQTT broker — simplified configuration that requires a valid flespi token to connect and specific topic(s) to subscribe to:
- custom MQTT broker — standard configuration suitable for any MQTT broker; requires host:port and username/password to connect and specific topic(s) to subscribe to:
Message payload format
Expected MQTT message payload should be a valid JSON object or an array with objects. Each message should be identified by an ident value either placed into the message JSON payload or into the topic of the incoming message. In the first case, you can set another field to use as an ident in the channel configuration. In the second case, you can use a specific topic component as an ident value.
Other JSON object attributes like "timestamp", "position.latitude", "position.longitude" are optional. Check other protocols for suggested attribute names and values. The value of "timestamp" property (if present) should be a number with Unix time, or UTC time string in full ISO 8601 format (YYYY-MM-DDThh:mm:ss). You can now declare a custom naming scheme for the parameters in the MQTT message thus making it possible to adapt the naming notation to any format preferable for your project.
Using broker as a hub
To use flespi MQTT broker as a central hub between IoT devices, smart sensors, or custom platforms publishers and channel subscriber, follow these steps:
- Come up with a unique topic where you will publish messages, e.g. messages/[device].
- Create a flespi token with either no ACL or with MQTT ACL only and the possibility to publish to messages/+. Use this token to connect publishers to flespi MQTT broker.
- Create another flespi token with either no ACL or with MQTT ACL only and the possibility to subscribe to messages/+. Use this token to connect subscribers to flespi MQTT broker.
- You can test both tokens directly from the flespi MQTT Board tool: one page with the first token will publish messages and another page should subscribe to # and receive them.
- Then create a channel with MQTT protocol and configure it to connect to flespi MQTT broker: URI=mqtt.flespi.io:1883, username=2nd-token, topic=messages/+.
- Enable/start the channel and check in the Toolbox that you have no errors.
- Now you can publish messages from the first web-page or directly from your smart sensors, and the messages will appear in the channel and operate according to standard flespi telematics message pipeline — stored in the device, re-published, streamed, etc.
You can use just one token with no ACL for simplicity for both publisher and subscriber. Please look into the tokens ACL article for more information.
You may also use the MQTT channel to subscribe to messages from another flespi channel(s) — just subscribe to the correct topic, e.g. flespi/message/gw/channels/{channel_ids}/+. See flespi MQTT API for more information.
Bash example:
mosquitto_pub -h mqtt.flespi.io -p 1883 -u <YOUR_TOKEN_HERE> -m '{"ident": "ident","timestamp": 1702366832.965264,"message.type": "example","driver.message": "Hello, flespi!"}' -t messages/ident
channel.id
number
ID of channel that received a message
device.id
number
ID of device that received a message
device.name
string
Name of device that received a message
device.type.id
number
ID of device type of device that received a message
ident
string
Device unique identifier like serial number, IMEI, etc
peer
string
IP:port from which device connecting to the channel
protocol.id
number
ID of protocol
server.timestamp
number
seconds
Timestamp when server received a message
timestamp
number
seconds
Message timestamp