Esp8266 control via the Internet using the MQTT protocol
Hello! This article will tell you in detail and show how literally in 20 minutes of free time, configure the remote control of the esp8266 module using an Android application using the MQTT protocol.
The idea of ​​remote control and monitoring has always excited the minds of people keen on electronics and programming. After all, the opportunity at any time to receive or send the necessary data, regardless of its location, provides ample opportunities. In my past articles ( Article 1 and Article 2 ) I tried to consider several accessible and relatively simple embodiments of remote control of microcontrollers via the Internet. However, time and the whole world do not stand still - progress continues its inexorable movement forward. During this short time, the esp8266 module has gained wide popularity due to its low price and integrated wi-fi has become one of the main components of the Smart Home. At the moment, MQTT is the most advanced and most popular data transfer protocol between individual devices within the “Smart Home” systems. It has several advantages over other protocols: - low traffic consumption; - the connection between the client and the server is always open; - does not load the Internet channel; - no delays in data transmission; - A convenient system of subscriptions to topics; All this makes it possible to monitor and control in real time. However, MQTT requires its own server, which serves as an intermediary between network clients. There are two ways to either create your own server or use third-party services.
The described control system consists of two main parts: the MQTT server (it is usually one) and clients, which can be quite a lot. In our case, the application on Android and the esp8266 module will act as clients. ')
The algorithm of the system is as follows. Clients connect to the server and immediately after connecting, each of them subscribes to and topics of interest. All communication between clients transits through the server, which redirects the data to other clients based on their subscriptions.
MQTT server.
In our case, we will use the extremely convenient service www.cloudmqtt.com which has a free tariff plan (Cute Cat), which will completely cover the needs for the implementation of a small, own "smart home" system. We will register on the site and get the necessary data to access the server. When configuring clients, you should use a regular Port (without SSL and TLS).
Android application.
Our application will act as a control panel for the microcontroller, and will also receive and display all received information from esp8266.
The application is called IoT MQTT Dashboard and is a ready mqtt client with a small number of very convenient widgets. More information about working with the application can be viewed on the video.
Esp8266.
The module is stitched in the Arduino programming environment, but I want to note that the module has problems with the firmware in the latest versions of Arduino, so I recommend using version 1.6.4. For example, an LED (5 pin) and a ds18b20 temperature sensor (2 pin) are connected to esp8266. Since it is necessary to receive data to control the LED, the esp must be subscribed to the corresponding “test / led” topic after connection, otherwise all sent data will pass by our microcontroller. You do not need a subscription to send temperature data, but when you transfer temperature values, you must specify the topic to which this data will go.
As a result, we get a handy tool for remote control and data monitoring, which is quite easy to learn and will even be a beginner.
Video with a demonstration of the control system
Detailed video tutorial on setting up the system
One application for managing esp8266 via the MQTT protocol
LED strip control via the Internet
If you have any questions about this material, I recommend watching the second part of the video, where the material is presented more clearly.
The archive contains a sketch and all the necessary libraries for flashing a microcontroller with a sketch from an example. I draw your attention that the library ESP8266WiFi.h is not included in this archive, it is installed via the Boards manager in the Arduino environment.