Home

Lua application for management Sonoff WiFi Smart Switch,
there is no need cloud, broker or third party server.

The program was tested with the device Sonoff, which you see in the picture,
but it is likely that it will also work with older versions Sonoff modules or smart socket.

Sonoff module

Schematic of this Sonoff

Sonoff schema

The program is written in Lua and consists of four parts:

The application allows switching of appliances via a WiFi
network from a PC or mobile phone:

Device configuration

After loading the program into the device, Sonoff WiFi Smart Switch is set to default mode Access Point Only (APO).
Sonoff device appears as a WiFi access point named "APOxxxxxx" where xxxxxx is node.chipid () device.

Access point

Password to connect to this AP is "espxxxxxx" where xxxxxx is again node.chipid () device.
For example, if the device ID = 128163, then the SSID = "APO128163" and the password is "esp128163".
The device will appear after about 10 to 20 seconds in nearby WiFi networks. Be patient, it usually
takes a while. First, we login with password "espxxxxxx" to the access point and then put into a browser address 192.168.4.1
Then you should see the login web page:

Login web page

Just enter the passcode (default is "sonoff "), which protects access. With buttons is possible
to switch the relay ON and OFF.
The appearance of this page is in the following figure:

Operating web page

If you need Sonoff device connect to your router, then you have two options:


Access point for WiFi-Manager is named "SETxxxxxx" where xxxxxx is node.chipid () device.
Password to connect to this AP is "espxxxxxx" where xxxxxx is again node.chipid () device.
For example, if the device ID = 128163, then the SSID = "SET128163" and the password is "esp128163".
The device will appear after about 10 to 20 seconds in nearby WiFi networks. Be patient, it usually takes a while.
First, we login with password "espxxxxxx" to the access point and then put into a browser address 192.168.4.1
WiFi - Manager Mode APO looks like this:

Wifi manager

Passcode is a code for controlling relay. If we cancel the check for the Access Point mode,
the menu will also be the setting for the router:

Wifi manager full

All changes confirm the Submit button. Repeatedly pressing the REFRESH IP can you detect a router IP address assigned if,
however, we have entered the correct credentials. The settings have to end with EXIT.
Press EXIT goes Sonoff to normal operation mode. Depending on the setting either as an APO (address 192.168.4.1) or
connect to your router, which allocates IP address. If we not do any changes, you can also stop this mode by pressing
the button on the unit Sonoff. This button now works like a reset. The device is automatically switches to the basic - control mode.

Telnet WiFi "OTA" Lua can upload applications to the device Sonoff

ESPresso V.0.2.0.0 program allows recording applications directly to your device from a PC without serial line.
You need have only program ESPresso. Its operation is very simple. Sonoff equipment can be brought to telnet server
in two ways:

Access point for Telnet- Manager is named "TELxxxxxx" where xxxxxx is node.chipid () device.
Password to connect to this AP is "espxxxxxx" where xxxxxx is again node.chipid () device.
For example, if the device ID = 128163, then the SSID = "TEL128163" and the password is "esp128163".
The device will appear after about 10 to 20 seconds in nearby WiFi networks. Be patient, it usually takes a while.

Wifi manager

In ESPresso set the address 192.168.4.1 and port 2323 and select Connect. For this Sonoff program, do not use option Diet !
Then you can manipulate files, but warning, it is a little sawing a branch beneath you. Ending a telnet session is best done by
sending a command node.restart () from an application ESPresso. Reset but can also be made by pressing the button on the unit when
not transmitting data.

Login parameters

MODESSIDPASSWORDPASSCODEIP ADDRESS
Running with routerrouter SSIDrouter passwordsonoff *DHCP
Running as access pointAPOxxxxxxespxxxxxxsonoff *192.168.4.1
WiFi-Manager (as access point)SETxxxxxxespxxxxxx-----192.168.4.1
Telnet-Manager (as access point)TELxxxxxxespxxxxxx-----192.168.4.1:2323
Where xxxxxx is node.chipid () device
* Default passcode

Control and signaling

Sonoff buttonSonoff LEDAction
short pressON/OFFToggle the state of the relay ON/OFF
press > 3 secblinkingStart WiFi - Manager as AP IP 192.168.4.1
press > 6 secfast blinkingStart Telnet-Manager as AP IP 192.168.4.1:2323
w/o actionshort flashSonoff wait for IP address(not in APO mode)
In WiFi-Manager and Telnet-Manager, the button has only one function,
and that is the restart to basic mode.

Sonoff device can also be controlled from a Web page when logging in
using a supplement to passcode:
InputExampleAction
passcode.."res""sonoffres"restart with unchanged state of relay
passcode.."set""sonoffset"Start WiFi - Manager as AP IP 192.168.4.1
passcode.."tel""sonofftel"Start Telnet-Manager as AP IP 192.168.4.1:2323
passcode.."on""sonoffon"login and relay ON
passcode.."off""sonoffoff"login and relay OFF
Examples are given for the default passcode "sonoff"

Connecting to a serial line and flash Lua firmware

Sonoff to serial

I used this integer Lua firmware from https://nodemcu-build.com:

nodemcu-master-11-modules-2016-12-06-16-22-27-integer.bin NodeMCU custom build by frightanic.com
branch: master
commit: ec265a6c21db22640795f190bdcb8a4f014cdced
SSL: false
modules: bit,file,gpio,net,node,pwm,rtcmem,sntp,tmr,uart,wifi
build built on: 2016-12-06 16:21
powered by Lua 5.1.4 on SDK 1.5.4.1(39cb9a32)

I do flash Lua firmware with this classic flasher :

Settings of flasher for Sonoff 1Mbyte memory

Flasher settings Flasher settings

The necessary programs :

Dec / 2016

UP