October 31, 2017
Update: November 22, 2017

Updating to Sonoff-Tasmota by Theo Arends has come around a lot faster than projected for three reasons. First, a package from ITEAD containing some Sonoff Basic and POW switches arrived and, of course, the firmware on these has to be replaced if they are to be used with Domoticz. Second, I was eager to try out the "experimental" method of uploading the new firmware that does not involve soldering a header. Third, Andreas Spiess posted a YouTube video informing us that a new revision of the ESP8266 SDK preventing a key reinstallation attack (KRACK) is available.

Updating to version 2.4.0-rc2 of esp8266 in Arduino and replacing Sonoff-MQTT-OTA-Arduino with Sonoff-Tasmota was quickly done without any real problem. Details follow below.

I did manage to use SonOTA by Mirko Vogt to update the firmware of an unmodified Sonoff Basic switch using the original OTA upgrade mechanism provided by the ITEAD firmware. Very nice: there was no need to solder a header on the board and no need for a serial programmer. However, I failed when I tried to do the same with a Sonoff POW switch. This was probably because of a false move on my part. I tried to do cut and paste from the terminal window using CtrlC (old habits!) and thus interrupted the process. I could not recover from that situation and had to solder a header and upload the firmware in the usual method. I plan on using this OTA approach again and may report on it in the future.

Table of Contents

  1. Updating the ESP8266 Arduino Core
  2. Prerequisites for Sonoff-Tasmota
  3. Updating Sonoff Switches
  4. Updating Sonoff Switches with Local Controls
  5. Concluding Remarks

  1. Updating the ESP8266 Arduino Core
  2. On October 16, 2017 Espressif released patches to counter the Key Reinstallation Attack (KRACK) vulnerability of the WPA2 WiFi security protocol. And it appears that on October 30, 2017, Ivan Grokhotkov (Igrr) incorporated these changes in the latest release candidate for version 2.4.0 of the ESP8266 Arduino Core Release. On the 31st, it seemed like a good time to update the ESP8266 Arduino Core in the Arduino IDE.

    Since I was using the latest stable version of the Core, I had to add an additional URL to the list of Board Manager URLS.

    You can find instructions for adding the URL on the Read the docs site for release candidate 2. I decided to keep the older URL also. Note that the instructions still give the URL for the stable 2.3 version; the URL for the release candidate is https://github.com/esp8266/Arduino/releases/download/2.4.0-rc2/package_esp8266com_index.json As the instructions say, add the new URL and then put a comma to separate it from the old one.

    If you press on the button to the right of the text box, it will be easier to add URLs, one per line (no separating comma) as seen on the following screen shot.

    The next step is to go to the boards manager: Tools/Board/Boards Manager....

    I put esp in the filter box to quickly find esp8266 by the ESP8266 Community. As can be seen, version 2.3.0 was installed. The the Update and Remove buttons are made visible by clicking on the entry. I elected to remove this older version

    and, after, to install the newest version 2.4.0-rc2.

    Presumably, one could have chosen to update to the newer version; I am not sure what would be the difference.

    To use the patched version of the Espressif SDK in a deployed device, its sketch would need to be recompiled with the new version of the ESP8266 Arduino Core and the binary file would have to be uploaded to the device. I only have one device running my own code and I will soon do that. In the meantime, I decided to switch to Theo Arends latest version of his replacement Sonoff firmware for the Sonoff Basic switches I have around the house.

    It is important to note that the WiFi router as well as all devices connected to it must also be updated to remove the KRACK vulnerability. If you are in a similar situation to mine that will not be easy. We have older Android tablets and telephones with older versions of the OS that are not being updated. Some of the manufacturers have gone out of business some time ago.

    Friends and acquaintances sometimes come with their own devices and we have in the past given them the password to our WiFi network. It will be impossible to control if they have updated their devices in the future. As a precaution, I changed the SSID and password of our wireless network and activated a guest network using the old SSID. That way, our guests will still be able to log on, in a way that is similar to logging on to public WiFi networks. As a result it will be simple matter to change the password on a regular basis and guests will no longer have access to our main network.

    As for the router, it is rented from an ISP and I will have to hope that it will go about updating the firmware in a timely fashion.

  3. Prerequisites for Sonoff-Tasmota
  4. The first step is to get the project from GitHub. This time around, I used git to create a clone in the Arduino directory where I store sketches.

    michel@hp:~$ cd Arduino michel@hp:~/Arduino$ git clone https://github.com/arendst/Sonoff-Tasmota.git

    Instead, you can download a zip file and extract it to a separate directory in your Arduino sketch directory.

    Sonoff-Tasmota requires more "tweaking" of the Arduino IDE and some libraries than the older Sonoff-MQTT-OTA-Arduino. I more or less followed the instructions contained in the Prerequisites page of the Wiki.

    1. I copied two files, platform.txt and board.txt from /home/michel/Arduino/Sonoff-Tasmota/arduino/version 2.4.0-rc2/ to /home/michel/.arduino15/packages/esp8266/hardware/esp8266/2.4.0-rc2/. Of course, your home directory will probably not be /home/michel but something like /home/your_name_or_something. And the hidden directory /home/michel/.arduino15 is where the binary Arduino files are stored. They could be somewhere else, especially if you installed the IDE for use by everyone on the computer. Note that I took care to save the original files under different names; you never know.
    2. I did not copy eagle.flash.1m0.ld from /home/michel/Arduino/Sonoff-Tasmota/ to /home/michel/.arduino15/packages/esp8266/hardware/esp8266/2.4.0-rc2/tools/sdk/ld/. The original eagle.flash.1m0.ld ended with the line
           INCLUDE "../ld/eagle.app.v6.common.ld"
      which existed (I suppose it could have been written as INCLUDE "eagle.app.v6.common.ld") whereas the link in the Tasmota directory,
           INCLUDE "esp8266.flash.common.ld"
      pointed to a non-existing file.
    3. The library pubsubclient was already installed and the define MQTT_MAX_PACKET_SIZE was already set to 512 (bytes) so there was nothing more that needed doing.
    4. I did install version 5.8.3 of the ArduinoJson library via the library manager in the Arduino IDE (Sketch/Include Library/Manage Libraries...).
    5. As I do not use them, I did not install the NeoPixelBus, OneWire and IRremoteESP8266 libraries.

  5. Updating Sonoff Switches
  6. The user_config.h in the Sonoff-Tasmota sketch needs to be edited and then the sketch can be compiled and uploaded to the Sonoff devices.

    1. I started the Arduino IDE.
    2. Then I opened the project: /home/michel/Arduino/Sonoff-Tasmota/sonoff/sonoff.ino In the IDE click on the File menu and then choose Open.
    3. Next, I selected the user_config.h tab. There are many files in the project, it may be easier to click on the down arrow at the right edge of the tabs. Then select the file in the drop-down list.
    4. I made sure that USE_WS2812 USE_DS18x20 USE_IR_REMOTE are commented out. For example, I changed
      #define USE_IR_REMOTE
      //MD #define USE_IR_REMOTE
    5. Then, in the Tools menu of the IDE, I selected the correct board (generic), flash mode (DOUT), flash size (1M (no SPIFFS)) upload speed (115200) and port. Other options were acceptable.

    6. I compiled the sketch by pressing the keyboard combination CtrlR. Or the menu can be used: Sketch/Verify/Compile.
    7. Without closing the Arduino IDE, I checked that there was a newly created sonoff.bin.ino file in a directory called /tmp/arduino_build_xxxxxx where xxxxxx will be different for each distinct build. That is the file that needs to be flashed or uploaded OTA to each device.
    8. I used FileZilla to copy the file to the appropriate directory on the Raspberry Pi hosting my home automation system and I logged onto each device running Theo Arends firmware and updated its firmware. All this is explained in the previous post Over the Air Sonoff Flashing.

    For most of the Sonoff Basic switches around home, that was all that was needed. The new version picked up the old configuration and everything worked as before. There were two exceptions.

    It appears that newer versions of ESP8285 and ESP8286 based devices, including the Sonoff, contain flash memory with DOUT acces mode. I followed Theo Arends July 14th tip about using this mode when I flashed these newly acquired switches. I think that all the older Sonoff switches that I updated had been flashed with DIO access mode which is slightly faster than DOUT mode.

    Espressif suggests to choose the fastest option for flash_mode that works with your device. This is certainly possible when updating a device with a serial link. However, it is probably not a good idea when flashing over the air. It may very well force retrieval of the device to connect it with a serial cable to a desktop computer to revive a DOUT mode device that was flashed in DIO, QIO or other access mode.

    It looks like I will now have some switches that can handle DOUT flash access mode only and older devices that use both DIO and DOUT modes. Since I do not want to keep records about which switch is in which mode, I decided to use DOUT mode for all the switches. Memory access is slower, but it has not resulted in noticeable difference in performance, probably because slow flash memory is paged into fast RAM memory.

    Update: November 22, 2017

  7. Updating Sonoff Switches with Local Controls
  8. Two bedside lamps are plugged into Sonoff switches and are also locally controlled with four push buttons set up as parallel pairs connected to GPIO 14 on the fifth header pin of the Sonoff. This was all explained in Local Control of Sonoff Switches. Sonoff-Tasmota did not pick up the correct settings for these switches.

    1. Click on the Configuration in the main menu (left panel above).
    2. Click on Configure Module in the configuration menu (centre panel above).
    3. Set the GMIO14 Sensor to 09 Switch 1.
    4. Click on the Save button. Once the device restarts, the lamp will follow the state of the push button, which is not what is wanted. The push button should toggle the lamp on or off just as does the tactile button on the Sonoff does.

    The mode of the button connected to GPIO 14 needs to be changed. I could not find a way of doing this in the module configuration page, but it can be done in the console.

    Return to the main menu and select Console as shown on the left. In the Enter command box enter switchmode 3 and press the Enter button. The result will be as shown above on the right. That is all that is required. Of course I had to repeat the steps described in this section for the other bedside lamp.

  9. Concluding Remarks
  10. As I mentioned in the introduction, updating to Sonoff-Tasmota is really very simple if Sonoff-MQTT-OTA-Arduino is already functioning properly. This can be done over the air, without disturbing the devices in a short time. For most basic Sonoff switches, that is all that will be needed. Some relatively simple adjustments may have to be done for more complicated situation as explained in the last section.

    Sonoff-Tasmota represents a nice evolution in Theo Arends project to create a one size fits all firmware for Sonoff devices. I was able to load exactly the same sketch on a POW switch and again the final settings of the parameters was done with the Web interface on the switch.