May 19th, 2017

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

  1. OTA Upgrade by File Upload
  2. Choosing lighttpd
  3. Installing lighttpd on the Raspberry Pi
  4. OTA Upgrade by Web Server

  1. OTA Upgrade by File Upload
  2. The first step is to generate the binary file from the Arduino sketch. Start the Arduino IDE and load the Sonoff sketch ~/Arduino/Sonoff-MQTT-OTA-Arduino/sonoff.ino

    Click on 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 Click on the Firmware upgrade 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 Start upgrade 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 Main menu button. The firmware will have been updated.

    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 Configuration button in the initial main menu and then on the Reset Configuration 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.

  3. Choosing lighttpd
  4. 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.

  5. Installing lighttpd on the Raspberry Pi
  6. To install the lighttpd web server issue the typical install command.

    pi@domo:~ $ sudo apt-get install lighttpd

    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: If all goes well, you will see a web page that starts as follows

    Placeholder page

    The owner of this web site has not put up any web pages yet. Please come back later.

    You should replace this page with your own web pages as soon as possible.

    The page goes on to say

    However, if you display the content of /etc/lighttpd/lighttpd.conf you will see

    pi@domo:~ $ cat /etc/lighttpd/lighttpd.conf server.modules = ( "mod_access", "mod_alias", "mod_compress", "mod_redirect", # "mod_rewrite", ) server.document-root = "/var/www/html" ...
    that the document root is actually /var/www/html.

    Check by listing the content of the directory and you should see that it does contain the index file displayed by the web browser.

    pi@domo:~ $ ls /var/www/html index.lighttpd.html

    I decided to create a sub directory to contain the Sonoff firmware.

    pi@domo:~ $ $ sudo mkdir /var/www/html/bin pi@domo:~ $ ls /var/www/html bin index.lighttpd.html

    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 the user 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 file permissions.

    pi@domo:~ $ sudo usermod -a -G www-data pi pi@domo:~ $ sudo chown www-data:www-data /var/www pi@domo:~ $ sudo chmod 775 /var/www

    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 pi

    pi@domo:~ $ sudo chown -R pi:pi /var/www
    everything worked as I wanted. A question remains: is it necessary to make pi a member of the www-data group?

    That is all there is to enabling OTA. There is no need to install php or MySQL or anything else.

  7. OTA Upgrade by Web Server
  8. 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

    1. Start the Arduino IDE on the desktop computer.
    2. Load the Sonoff sketch, ~/Arduino/Sonoff-MQTT-OTA-Arduino/sonoff.ino. To do this click on Open in the File menu.
    3. Click on Verify/Compile in the Sketch menu or press on keyboard shortcut Ctrl R to compile the sketch to a binary file.
    4. 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.

    1. Start Filezilla on the desktop computer.
    2. Connect to the Raspberry Pi
    3. 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.
    4. Navigate to the /var/www/html/bin directory on right-hand panel (remote site which is the Raspberry Pi).
    5. Navigate to the /tmp/arduino_build_xxxxxx directory on the left-hand panel (local site which is the desktop computer.
    6. Double click on the firmware, sonoff.ino.bin in 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.

    1. 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
    2. Click on the Firmware upgrade button as shown in the left panel of the following image.
    3. Look at the Upgrade by web server section of the next page as shown in the right panel above.
    4. Ensure that the address of the firmware image file on the Raspberry Pi is correct in the OTA Url.
    5. Click on the Start upgrade button.

    Again, it will be necessary to reset the configuration for changes in the user_config.h settings to take effect. See the end of the first section above for the details.