13 February, 2019

Communicating with ESP8266 over the air via MQTT

A custom firmware enabling to control the hardware over its serial port to exchange data between any two points.

MQTT’s simplicity is welcoming hundreds of tech-savvy enthusiasts to utilize the protocol in their projects and get quick results. Today we’ll be talking about one of such projects created by one of flespi MQTT broker users, an Austrian industrial engineer Reinhard Dämon. You can contact Reinhard (reinharddaemon@gmail.com) at any time in case of questions or suggestions.

The project is a perfect illustration of an engineering approach to life — if you can’t find what you need, invent it yourself.

Concept

The project represents a firmware for a cheap and simple ESP8266 ESP-01 hardware that allows the user to control it over its serial port

Logic

A typical application situation would be as follows:

  1. Connect to your Wi-Fi (SSID and password) and to Flespi MQTT broker (token):
    "w:con:myWLAN_93,myPASS_39,lkadsfajsdlkj42314jklj21354lkerwjXXXX<CR><LF>"

  2. Wait and check whether already connected:
    "r:con:<CR><LF>"
    (if connected, ESP8266 responds "r:con:+<CR><LF>", if not: "r:con:-<CR><LF>")

  3. When connected, publish a message, e.g.:
    “w:pub:top/1,1,false,abc<CR><LF>"
    (this will publish the payload "abc" to topic "top/1" with QoS1 and no retaining)

  4. Subscribe to a topic:
    "w:sub:top/1,1<CR><LF>"
    (to subscribe to topic "top/1" with QoS1)

  5. Get the number of topic/payload pairs in the FIFO buffer:
    "r:ele:<CR><LF>"

  6. If there is data available in the FIFO buffer, read it:
    "r:rcv:<CR><LF>"

  7. Then you can determine any logic based on the received data (see use case below).

Any kind of data can be transmitted from anywhere in the world to anywhere else, provided that Wi-Fi and the Internet connection are available.

You can easily observe the communication process. Go to MQTT Board. Enter your Flespi token as a username and connect.

You can enter # into the topic field of the subscription section and subscribe in order to
watch all traffic or just enter the topic/subtopic you are interested in.

Requirements

One of the key advantages of the proposed solution is that it can work with a variety of hardware. It can apply to any uC, processor, personal computer, terminal, etc. that has a serial port (or can provide a virtual COM port from a physical USB port). Only GND, 3.3V, RX, and TX pins are used.

Applicability

The areas of application for the given solution are not limited to any particular domain since MQTT is simply a communication protocol that can ensure reliable data exchange between any two or more parties.

Any kind of remote control or telemetry can be done. The user is free to publish anything to any desired topic and subscribe to any topic, as well.

Affordability

One of the key advantages of the proposed solution is that it’s very cheap and all-purpose making it suitable for hobbyists, amateurs, and professionals alike.

Just flash the provided firmware to the ESP8266 ESP-01 module, connect it to your uC, processor, personal computer, terminal, etc., and communicate to the module via plain-text commands.

The solution is easily scalable to serve businesses — it’s a matter of uploading the same firmware on all ESP8266 ESP-01 modules used in the project. Companies will use bulk programming systems, while hobbyists may stick to Arduino IDE.

Simple use case

Here’s an example of a real-life project that relies on the discussed solution.

Sender:
An appliance which senses the AC level of some electrical machine (e.g. washing machine, clothes dryer, etc.) that runs a program (washing, drying, etc.) for some time and stops thereafter.
While running a lot of current is consumed (publishing "running" via flespi) and when finished nearly no current is consumed (publishing "done" via flespi).

Receiver:

An appliance listening to a particular topic and receiving either "running" or "done". These values are then interpreted as corresponding LED lights: red light = “running”, green light = “done”.

***

As already mentioned, the PUB/SUB principle behind the MQTT protocol provides fast, lightweight, and secure M2M communication independent on the hardware type. Therefore, any applications requiring device-to-device interaction can benefit from the solution.