One of the very useful features of the Sonoff-MQTT-OTA-Arduino firmware for Sonoff switches by Theo Arends is its ability to be updated over the air (OTA). It is built in, ready to be used: just go to the Sonoff switch web site, select Update firmware, and "pull" (download) the new version of the firmware from the desktop computer or from a web server on the local area network.
There is nothing to set up for upgrading the firmware from a file on the desktop computer. If there is already a Web server running on a computer connected to the local area network, then all that needs to be done is to copy the new Sonoff firmware binary to a directory somewhere in the local path of the web server.
The next section shows how to upgrade the switch firmware by uploading a file from the desktop computer. The rest of the post is about installing a Web server on the Raspberry Pi hosting the home automation system in order to upgrade the firmware by using a web server.
Table of Contents
- OTA Upgrade by File Upload
- Choosing lighttpd
- Installing lighttpd on the Raspberry Pi
The first step is to generate the binary file from the Arduino sketch.
Start the Arduino IDE and load the Sonoff sketch
Verify/Compile in the Sketch menu or Ctrl R to compile the sketch to a
binary file. Once the compilation is done something like this
Archiving built core (caching) in: /tmp/arduino_cache_935355/core/core_esp8266_esp826...will be displayed in the message window at the bottom of the Arduino IDE.
Even if compilation is completed, do not close the IDE. As hinted in the message, the output is stored a temporary directory that will be erased when the IDE is closed.
Open Sonoff Web site with a navigator on the desktop computer. In the case
presented here, its address is
192.168.0.12. Click on the button as shown in the left
panel of the following image.
Look at the Upgrade by file upload section of the next page as
shown in the middle panel above. Click on the Browse...
button. Then use the desktop computer file navigator to select the binary image
of the sketch. It will be named
sonoff.ino.bin and will be found
in the temporary directory and its name will begin with
arduino_build_ completed with a six-digit number such as
/tmp/arduino_build_278396. Once the image file has been selected,
click on the button of the
Upgrade by file upload section.
The Sonoff will show a new page indicating that it is fetching the
file (see top right panel above). When the operation is completed,
a new page will be displayed in the web browser (bottom right panel)
saying that the file has been uploaded and that the firmware is being
upgraded. In a
few seconds you can click on the
button. The firmware will have been
If you changed a setting in the
user_config_.h file of the
Sonoff sketch, it will probably not be reflected in the configuration of the
switch after the OTA upgrade. This is because, Theo Arends' firmware keeps
the configuration data in the ESP8266 (spiffs) file system which is not
updated when the firmware is upgraded. That way, things like the Domoticz index number of the switch is not lost. The
configuration data can be reset to the default values defined in the
upgraded sketch. Click on the
button in the initial main menu and then on the button to achieve that goal. You will probably need to
update the Domoticz index number, the MQTT topic,
the Friendly Name field and so on.
As mentioned above, when the Arduino IDE is closed, the two temporary directories it created will be erased. You may want to copy the binary file elsewhere for future use. Another possibility is to upload it to a Web server on the Raspberry Pi already hosting the home automation server and to then upgrade by web server. The rest of this post looks at this option.
I am sure there is a debate about which web server is best. I am equally certain that there is a branch of that debate that concentrates on which server is best for a Raspberry Pi. At least three well-known contenders, Apache, lighttpd, Nginx, can be installed on the Raspberry Pi. There is probably a passionate discussion about which server to use with the Raspberry Pi 3 as opposed to an older version of the single-board computer. I have no wish to be involved in this discussion. I am sure that any one of these three servers could be used.
I will describe a minimalist setup of lighttpd on any Raspberry Pi already running a home automation system just because that was the last HTML server I installed on a Raspberry Pi. I found the installation simple and it works.
To install the lighttpd web server issue the typical install command.
This will download the web server and also pull in any other packages (called dependencies) that are required. The server will be automatically started and set to start by default after a reboot.
Start a web browser on a desktop computer connected to the local
area network and point it to the newly installed web server. In my case it
is at the following IP address:
192.168.0.22. If all goes well,
you will see a web page that starts as follows
The page goes on to say
- Configuration files can be found in
/etc/lighttpd. Please read
DocumentRoot, which is the directory under which all your HTML files should exist, is set to
However, if you display the content of
/etc/lighttpd/lighttpd.conf you will see
Check by listing the content of the directory and you should see that it does contain the index file displayed by the web browser.
I decided to create a sub directory to contain the Sonoff firmware.
I want to use Filezilla to copy the newer versions
of the firmware to that directory as explained in a former post:
Workflow when Working with a Headless Raspberry Pi. I found that
I had to change ownership of the directories to do this. First, I added
pi to the
www-data group and made sure that
group owned all the directories and files in the HTTP server root directory
as suggested in most guides to installing web servers. I also changed
Unfortunately, that did not work as I wanted. I could not copy files
over from my desktop using Filezilla. And working in
the HTTP server directory, I had to be user
www-data (using the
sudo -u www-data... preamble). After I changed
ownership to user
pia member of the
That is all there is to enabling OTA. There is no need to install
MySQL or anything else.
This procedure is quite similar to what was shown for upgrading by file upload. First, compile the Sonoff sketch in the Arduino IDE to create a firmware image file. Second, copy the later to the Web server document directory. Third, use the Web site of a Sonoff switch to upgrade its own firmware.
The details of the first step are
- Start the Arduino IDE on the desktop computer.
- Load the Sonoff sketch,
~/Arduino/Sonoff-MQTT-OTA-Arduino/sonoff.ino. To do this click on
- Click on
Sketchmenu or press on keyboard shortcut Ctrl R to compile the sketch to a binary file.
- Keep the Arduino IDE until the next step is completed.
Here is how I get the compiled binary file over to the Raspberry Pi running both the Domoticz and lighttpd servers.
- Start Filezilla on the desktop computer.
- Connect to the Raspberry Pi
- Disable synchronized browsing if enabled. Pressing on the second to last button on the speed bar (with a green and yellow arrows) toggles this function.
- Navigate to the
/var/www/html/bindirectory on right-hand panel (remote site which is the Raspberry Pi).
- Navigate to the
/tmp/arduino_build_xxxxxxdirectory on the left-hand panel (local site which is the desktop computer.
- Double click on the firmware,
sonoff.ino.binin the left panel, to copy it over to the remote computer.
The Arduino IDE can be closed at this point. The last step is almost the same as before.
- Using a web browser on the desktop computer, open the Web site on the
desired Sonoff switch. Again, in this example, the IP address of the switch is
- Click on the
shown in the left panel of the following image.
- Look at the Upgrade by web server section of the next page as shown in the right panel above.
- Ensure that the address of the firmware image file on the Raspberry Pi is correct in the OTA Url.
- Click on the button.
Again, it will be necessary to reset the configuration for changes in
user_config.h settings to take effect. See the end of the
first section above for the details.