The sonoff software that i programmed some time ago works realy well. However, the newest sonoffs have different flash chips on board that are not compatible with the old software. Since i had to make some adaptions in the software, i decided to give the whole project a facelift. I experimented with the async webserver but, although faster, i could not get it to function reliable so i sticked to the original esp8266 webserver. The result is that the sw is more memory efficient, more compact and better secured. From outside your network, the switch reacts on nothing without authentication. The differences relative to the old sw are:
It is now mandatory to secure the switch with a password.
The administrator password can only be changed in the AP mode.
The administrator can grant a user with limited privileges.
The authentication procedure has been changed.
The portnumber is not needed and is removed.
You now can connect an external pushbutton.
The OTA software update procedure is different.
Some cosmetic changes in the webpages.
A favicon icon has been added.
The switch name shows up in the browser tabs.
configurable maximum on-time in case of manual switch-on.
An informationpage with extended status information is added.
Some adaptions have been made to make it more mobile friendly.
from inside the network switching via a link can be done without authentication.
from outside the network switching is only possible via the website.
Temperature/humidity/pressure/light/motion/touch sensors can be connected.
It can work with any digital sensor (sound/rain/tilt and more)
For the rest the Sonoff works as it used to, meaning that we can control it completely via its webinterface, use it as thermostate, hygrostate, motionsensor, lightsensor rainsensor and more, that sends its sensor readings to domoticz. Or just use it as an internet controlled switch with timers to automate switching. All and all it has a lot of interesting features that makes it a real smart switch.
Another interesting possibility is to build the sonoff in a wall socket. When you have an obsolete wall socket laying around...
There are only a few components involved. The total costs are about 5 Euro.
1 x Sonoff
1 x 5 pins header)
1 x usb to serial converter 3.3v
1 x 4 dupont wires
The Sonoff has an ESP8266 onboard and by soldering a header i could establish an uart serial connection and reflash the ESP.
The Sonoff can be booted in flashmode by pushing the button while putting the USB plug into the PC.
I used the arduino IDE to program the software and to upload it to the Sonoff
After soldering the header the first thing was to flash nodeMCU firmware to the device. Connect the device to an usb-serial convertor (3,3v!!!)
and plug this in the pc while holding the button down. The device is in flashmode now. Open Arduino IDE, set the configuration to
flash size 1 MByte 64k SPIFFS, flash mode DOUT. Check the com port and click the upload button.
In the arduino IDE you can open a monitor on the serial port where you can see some debug information that the Sonoff spits out.
After flashing the sonoff will boot in the new software, which you could observe on the monitor. Next software updates can be OTA so flashing it this way is one-off.
The software has to be plug and play so that it can easely be installed in any network by anyone.
I used the library 'wifimanager' to develop my own configuration manager that allows to connect to the wifi network and configure some basic properties for the time, password and optional static IP.
So how does it work: When the device is powered on, it tries to connect to a network. When this doesn't succeed, it starts an accesspoint. The led is on contineously now.
You can connect your PC or Phone wifi to this AP and your browser will open the config portal automatically. Or you can open a website with http://192.168.4.1. The ESP serves a webpage that allowes us to configure the network
credentials and more.
When this is done, the Sonoff restarts, checks if there's a network connection and than starts in Station mode. The led flashes 3 times. Now we can switch it via its webpage and by using the button.
By connecting sensors and configure these via the webinterface, you can use the sonoff as a bodysensor, thermostate or hygrostate.