Provide ESP8266 based itead Sonoff with OTA and MQTT firmware

Mukntp
4 min readFeb 10, 2021

ESP8266, MQTT, OTA

Because of the previous article that I have written about Custom Sonoff based on ESP8266 compatibility with SmartThings and IFTTT. This article will combine the previous articles to be a Web Application for controlling Sonoff via the OTA and MQTT Protocol.

Custom Sonoff based on ESP8266 compatibility with SmartThings and IFTTT

What is OTA?

OTA (over-the-air) programming refers to various methods of distributing new software, configuration settings, and even updating encryption keys to devices like mobile phones, set-top boxes, electric cars, or secure voice communication equipment (encrypted 2-way radios).

One important feature of OTA is that one central location can send an update to all the users, who are unable to refuse, defeat, or alter that update, and that the update applies immediately to everyone on the channel. An Over-The-Air (OTA) update is a mechanism for remotely updating internet-connected hardware with new settings, software, and/or firmware.

What is MQTT?

MQTT (Message Queuing Telemetry Transport) is an OASIS standard messaging protocol for the Internet of Things (IoT). It is designed as an extremely lightweight publish-subscribe network protocol that transports messages between devices. The protocol usually runs over TCP/IP and ideal for connecting the remote device with a small code footprint and minimal network bandwidth.

Basic concept:
- Publish/Subscribe
- Messages
- Topics
- Broker

MQTT allows you to send commands to control outputs, read and publish data from sensor nodes and much more which publish to a broker. Clients connect to this broker, which then mediates communication between the two devices. Each device can subscribe, or register, to particular topics. When another client publishes a message on a subscribed topic, the broker forwards the message to any client that has subscribed.

How to custom Sonoff with MQTT firmware?

First, have to flash the firmware through UART pins are exposed on Sonoff switches so can solder a 4-pin header and connect a USB to TTL. In order to enter programming mode, you have to hold down the push button while connecting the adapter to the computer (and powering up the ESP8266).

OTA firmware: Tasmota vs ESPurna vs ESPEasy

at least have 3 good alternatives to provide firmware for re-flash ESP8266 but the firmware that has been chosen which more suited for use-case is Tasmota. There various ways to program the ESP8266 on the Sonoff board, which are described on this Tasmota wiki page. I have chosen to use PlatformIO IDE for Atom.

Here’s how it works:

  • Download PlatformIO IDE for Atom for operating systems, and install it.
  • Select Open Project, and browse for Tasmota/code directory to load the project.
  • Enable Developer mode in the IDE by going to the top menu to select PlatformIO->Settings->PlatformIO IDE, and checking “Use development version of PlatformIO
  • Go to “user_config.h” enter Sonoff firmware settings and change where it’s called “STA_SSID1”, which is your WiFi access point which esp8266 on the Sonoff for connective.
  • Compile and upload firmware into Sonoff then go to “platformio.ini” click “PlatformIO” and click “upload”.
  • After that will get the local IP address which connected with a WiFi access point of Sonoff where name begins as sonoff-xxx-module via Tasmota Web GUI (graphical user interface).
  1. Toggle: turn on-off remotely
  2. Configuration settings:
    - Module type: GPIO setting contains the button, switch, relay, and Led
    - WiFi: can configure new WiFi SSID and password. There is an option to set up two SSIDs. When one of them is offline, ESP8266 will try to connect to the other. (The WiFi section is used to connect to up to 3 wireless routers)
    - MQTT (Mosquito): configure an MQTT broker IP and MQTT broker port
  3. Information: used to calibrate the power monitoring device. (current status)
  4. Firmware Upgrade: replace the lite version with the newest Tasmota-basic.

--

--

Mukntp

👨🏻‍🎓B.Embedded System Engineering BUU 👨🏻‍💻M.Electrical Engineering CU