Hutchie

MQTT and a thermal printer

Thermal Printer saying owo

When I joined Edinburgh Hacklab in 2019, it was the first hackerspace I’d entered and I was amazed at all of the cool gadgets sprucing the place up. I have a particular interest in IoT and otherwise networked devices, so I was very curious how they got all their door locks, lights and various sensors to communicate with one another.

This article just writes about what I learned and experimented with: it is not a tutorial.


What is MQTT?

Messaging Protocols

Low-powered network devices often communicate using lightweight messaging protocols. One such protocol is MQTT. Different protocols have different capabilities, such as varying supported QoS levels. If you’re looking to use something like this in a production environment, it’s certainly worth a lot of research.

Protocols like Zigbee are used by medical devices to capture data (some smart-watches can use Zigbee to connect to external fitness monitors). Similar protocols are used to control sensors and lights decorating airplane runways and to record sensor readings from all across the ISS.

These protocols are also employed by smart home automation devices.

MQTT

MQTT stands for Message Queuing Telemetry Transport. It is a publish-subscribe protocol which defines two objects:

Eclipse Mosquitto is a common open source message broker (and what I started off using).

Publish & Subscribe

MQTT allows clients to publish messages to a broker along a URI, such as:

/bedroom/lights/ceiling/set on

Here, the message is on. Another client could subscribe to the same URI and the broker will relay messages along the URI to the client, allowing them to act upon incoming messages.

In this example, a device controlling the ceiling light will subscribe to the above URI and listen for another client sending the on message. The same for the off setting. We can use the same technique with ../brightness 50 or ../colour #FF5454 to control the brightness and colour of the light.

The controller may occasionally send status information along a URI such as:

/bedroom/lights/ceiling/temperature

Allowing other network devices can respond to dangerously high bulb temperatures, etc.


Thermal Printer?

I already owned a thermal printer (receipt printer) from Adafruit, one like this. It can be controlled via TTL Serial or USB connection. I connected the printer via TTL to an ESP8266 microcontroller (requiring the ESP8266WiFi library). The ESP8266 is an extremely cheap, Arduino C-compatible chip with WiFi built-in. Highly recommend. I also used the Adafruit_Thermal and PubSubClient libraries.

In my setup, the ESP8266 client is subscribed to receive messages along the /printer/thermal URI via the broker - a RPi3 on the local network running mosquitto. I wrote a secure, web-accessible interface (using Node.js) to write messages which would be relayed via the broker to the ESP8266 for printing.

I’m not going to go deep into the code, but the GitHub repo is available and free to explore here. The project has since been expanded on by BeardedTinker and René Lebherz, many thanks!


#mqtt #thermal printer #networking #esp8266