As part of the bigger JET story, I need to tie microcontroller nodes into MQTT, with the data flowing in both directions:
As shown above, the idea is to use Folie for this, but that’s still a way off and I don’t want to mess with it right now. So instead, I decided to create a little stand-alonebridge to perform this task and connect it to this:
It’s a simple Go application with no more than a few dozen lines of code. It
subscribes to the MQTT topic “bridge/out
”, sends each message it gets out
to the serial port (with a newline added), listens for lines coming from the
serial port, and publishes each one with MQTT topic “bridge/in
”.
There’s nothing Forth-specific in this code, although it is definitely meant to be used with a µC running Mecrisp Forth. At this stage, all the messages between MQTT and µC will be plain text and line-oriented.
Everything in this bridge is configurable:
$ bridge -h
Usage of bridge:
-m string
MQTT broker (default "tcp://127.0.0.1:1883")
-p string
serial port (default "/dev/cu.usbmodem322F2211")
-q quiet mode, don't show in/out messages
-t string
MQTT topic template (default "bridge/%")
For convenience during development, the bridge will also read lines from stdin and send them out to the attached µC:
$ bridge
1 2 + .< 1 2 + . 3 ok.
$
The bridge exits when stdin is closed.
No echo filtering, no throttling, no checks, but this is sufficient to control a µC via MQTT and pick up its responses:
- with the bridge in one window…
… we can send a command over MQTT:
$ mosquitto_pub -t bridge/out -m "11 22 + ."
… and see the result in a third window:
$ mosquitto_sub -t bridge/in 11 22 + . 33 ok.
… and in the bridge’s debug output:
$ bridge> 11 22 + .< 11 22 + . 33 ok.
There’s no support for sending multi-line commands with multiple word definitions to Mecrisp, but it should be good enough to let me move on to the next step: connecting a browser to a µC via MQTT - onwards!