📜 ⬆️ ⬇️

ESP8266 as MQTT Broker for Mobile Application

As they say, laziness is the engine of progress. To make life easier, I am currently making myself a small device in the form of an ESP8266 module and an RS485 converter for communicating with gas analyzers using the Modbus protocol. In production, there is always a need to connect to devices to perform various kinds of diagnostics using the 485 interface, but each time it is inconvenient to carry a laptop with you, but the mobile phone is always in your pocket.

In the process of development, we got a library that allows you to connect directly from the phone to the module and exchange data through the client's MQTT application. Perhaps someone will also find this solution useful, because there is no need to have a third-party MQTT broker (whether it be a local Raspberry broker or an Internet broker) and an internet connection, which in my case is the most important thing.

Phone application

I chose IoTmanager as an application. The main feature is a very flexible configuration of widgets using HTML5 + CSS, all settings are made in devices, not in the application. Topics are sent in JSON format and contain header names, values ​​and display styles. Maybe someone will be uncomfortable, but I liked this approach.
')
The application can work on two MQTT libraries: Paho.js and MQTT.js. On the move, I managed to establish a WebSocket connection through the Paho library, and I stayed to work on it. If you select in the MQTT settings, the connection does not occur, I suspect that you need to dig in the WebSocketServer library.

I despised Arduino for a long time, but still gave up

I don’t want to make a discussion about the choice of environment, I’ll just say that for me an important role in choosing the Arduino IDE for writing the ESP8266 firmware was played by the presence of tons of ready-made libraries and documentation. Everything is simple and fast, the benefit of the project promises to be easy.

Offtopic
Believe it or not, even the rockets fly into space with microcontrollers programmed in the Arduino environment. The decisive role is often played by the speed of development - connected ready-made libraries and go.

Open source is our everything

Repository on github . The library is still damp, in some cases, the settings of the widgets cause IoTmanager'a reconnect, the reason for which I cannot find yet, perhaps the joint development will go faster.

The project contains two library implementations:

MQTTbroker.h is an attempt to implement a real broker with subscription controls. Ie when a message arrives in a topic, the broker goes through all clients and their subscriptions and searches for matches (including the masks / + /) and sends messages only to those whose subscriptions match the name of the topic, not forgetting about themselves.

MQTTbroker_lite.h works slightly faster due to the lack of automatic subscription processing logic. All incoming messages are redirected to the callback function of the main program, and already there, if necessary, we process them ourselves. For my particular case, this is exactly what is needed: one device - one plug-in client, I know what it is signed for and what it expects from me.

Work example

We take any module from ESP8266 (I have this NodeMcu) and load the example sketch from the library into it. I propose to disable the debugging port in the Arduino IDE tools, otherwise the library will send a bunch of debugging messages, then look at them. Open the serial port and observe the IP address.

On the phone, connect to the created Wi-Fi access point. In the IoTmanager application, we go into the connection settings: select the PAHO engine, drive in the module IP address, port 80, the topic prefix / IoTmanager and turn off SSL / TLS just below.

We press on the speedometer in the corner, after a small delay, it should connect and display the switches. If it does not connect, try to kill and re-launch the application. On the first switch I have an LED set up (see video).

Sometimes there is a delay in connecting. I think this is due to the fact that IoTmanager issues all initial messages at once, ESP hangs a little, processing them, and then it works without brakes.

Short description

Both parts of the library are very similar, I will give a description of the _lite version:

typedef void(*callback_t)(uint8_t num, Events_t event , String topic_name, uint8_t * payload, uint8_t length_payload); // callback'a     MQTTbroker_lite(WebSocketsServer * webSocket); //     WebSocket void setCallback(callback_t cb); //  callback'a void begin(void); //  ( ) void parsing(uint8_t num, uint8_t * payload, uint8_t length); //   WS  void publish(uint8_t num, String topic, uint8_t* payload, uint8_t length); //   num void disconnect(uint8_t num); //  bool clientIsConnected(uint8_t num); //    

Video demo


Thanks for attention. Join the development, the library is still damp.
For help, you can contact Telegram oWart

References:

1. Project on GitHub
2. Description of IoTmanager widgets
3. MQTT v.3.1.1 protocol specification

Source: https://habr.com/ru/post/326794/


All Articles