sONOFF Local Server - mpalitto/HomeHacks GitHub Wiki
The PROBLEM
The sONOFF relays during setup time get the address of the cloud server to connect to from the APP which is used for the setup.
When the sONOFF powers up, it connects to the WIFI network and then it will attempt to connect to the cloud server.
If not connection to the cloud server is successful, the relay cannot be operated using WIFI.
a SOLUTION
- have all sONOFF connect to custom WIFI AP which is implemented by using a low cost Linux board and a WIFI usb adapter.
- use IPTABLES to redirect received traffic (from sONOFF devices) to local address and port.
- have a local server running on the Linux board to serve the sONOFF and give a local interface to switch the relay.
- In order to keep the cellphone APP functionality in the case of available internet connection, we can simulate the sONOFF client and connect to the cloud server. This will allow the cloud server to send commands which we can then forward to the sONOFF of interest, when sONOFF sends status to server (local) we forward it to the cloud server.
SERVER IMPLEMENTATION
ANALISYS:
I have started from this blog page which explains the protocol between the cloud server and the sONOFF device
The sONOFF protocol identifies 4 phases:
- DISPATCH
- WEBSOCKET
- REGISTRATION
- COMMANDS
PHASE 1: DISPATCH sONOFF send a POST HTTPS request to eu-disp.coolkit.cc from which receives the IP and PORT for the WebSocket (WS) server.
PHASE 2: Establish WS connection according to the websocket standard.
PHASE 3: REGISTRATION Once the WS is established, a sequence of WS messages are exchanged to register the sONOFF device with the server (see above link for details)
PHASE 4: COMMANDS in this state the sONOFF device is waiting for commands from server, and sending periodic state updates.
CONSIDERATIONs: Since we want to use the same LINUX computer for DISPATCH Server& and for WS server, we need to necessarily use 2 different ports for the 2 servers.
DISPATCH uses port 443 or 8080 depending on the sONOFF version.
We can then use 8443 for the local DISPATCH server, thus using IPTABLES we need to redirect ports 443 and 8080 to local port 8443.
Our local DISPATCH Server will then provide the sONOFF devices the IP address of the local Linux computer and the PORT (for example 8888) where we have our WS Server listening.
For each sONOFF connection we will need to store the deviceID. On 1st use of each deviceID we can associate a device name that can be used for easier reference.
The association can be stored in a file.
For each sONOFF a status must be maintained in memory.
Each sONOFF have settings that can be changed like the PowerON state and timers.
These information will need to be stored in a file.
A regular socket will be used to get commands from command-line destined to the sONOFF devices.