"MQTT-Lite" protocol to send commands from host to microcontroller through serial port?

The PC is sending simple text commands to the microcontroller board via USB (virtual serial port). I would like to go a notch above using Serial.read(), e.g. have some data integrity assurance. This wheel was most likely reinvented a million times. Are there any good libraries doing it?

If there is a good MQTT library, which works over serial port, it would fit the bill, but it doesn’t have to be such high level.

If the Serial is going via USB, you already have the CRC of the USB packets ensuring basic data integrety (polynomial x^(16) + x^(15) + x^(2) + x^(0) for data packets). If you want to protect whole chunks of data, you can add your own packet format (for example, length, value, CRC). CRC libraries are widely available and cheap to compute, plus you can choose the “strength” of the code via its length, as in, CRC8, CRC16, CRC32…

On the topic of leightweight MQTT over serial libraries / projects: There is e.g. Using TTY2MQTT to bridge between serial communication and MQTT | METACODES.PRO. This project has additionall CRC checks though, so you’ll be relying on USB (which is fine enough in my opinion for these short packets).

Projects like https://github.com/vortex314/serial2mqtt do seem to have a CRC in their protocol messages built in.

A more generic approach to network connectivity via seral is “PPPoS”, Point-to-Point Protocol (over Serial). Basically, you can configure a Linux system such that it will listen to PPPoS packets on a serial port and respond appropriately, giving the connected serial the possibility to send arbitrary packets. Then libraries like https://github.com/levkovigor/ppposclient give you the possibility to instantiate a standard PubSubClient with the PPPOSClient object that works via serial. PPP packets, being based on HDLC, also have a checksum, which is a CRC16 (x^16 + x^12 + x^5 + 1).

In short, solutions to this problem already exist manyfold.

1 Like

Thank you for the links.

I use GitHub - min-protocol/min: The MIN protocol specification and reference implementation to send command from PC to microcontroller via serial port. Not MQTT, though.

1 Like