Private SONOFF Server - Sonoff Cloud based on node.js - Supporting Sonoff Duel
The idea was to have a private, locally/remote hosted server to manage all of your own Sonoff devices.
Does not require flashing and without firmware upgrade.
It is done by connecting to the device (via wifi, when it is in AP mode)
and telling him that from now on, his server is in a new IP address (the private sonoff server, instead of the sonoff cloud servers).
A lot of this code is based on the findings and code from these sources:
openssl req -x509 -newkey rsa:2048 -keyout certs/keytmp.pem -out certs/cert.pem -days 365
openssl rsa -in certs/keytmp.pem -out certs/key.pem
npm install
Change the config.json to fit your environment and desired settings.
The “server” is the device, which should stay in contact with the SONOFF devices. In my case it was the CentOS based home server on an old laptop.
To work in dev mode (with nodemon) - run:
npm run dev
Also, there is a .dotenv support, instead of editing the configuration directly via the config.json,
It is possible to add a .env file, .env-example supplied.
There are two ways of setting up the devices.
Either manually by doing a POST request (by wget or postman) while connected to the device wifi (ITEAD-1000xxxxx )
Or with the sonoff.setupdevice.js script.
sudo route change 0.0.0.0 mask 0.0.0.0 10.10.7.1
(was not needed in my case).Do a postman POST request:
Go to headers: add the following:
Go into body, set it to raw, paste in this:
{
"version": 4,
"ssid": "mamaliga",
"password": "JC268626",
"serverName": "192.168.2.112",
"port": 8080
}
{
"error": 0
}
(thanks @andrewerrington)
sudo route change 0.0.0.0 mask 0.0.0.0 10.10.7.1
wget -O- 10.10.7.1/device
wget -O- --post-data='{"version":4,"ssid":"yourSSID","password":"yourSSID_PASSWORD","serverName":"n.n.n.n","port":1081}' --header=Content-Type:application/json "http://10.10.7.1/ap"
The device will automatically drop out of AP mode and tries to connect to WiFi and server.
Start sonoff.setupdevice.js on a computer you like. It will connect to the SONOFF device, so you will lose internet connection. When the scripts runs, you must long-click the black button on the device, and it will be configured to use the “server” as its cloud. Which now runs in your own network.
To run this on a linux device, the network manager must be installed. On an raspberry pi I would suggest to do the setup process manually with wget.
Start app.js by any of this methods.
npm run start
node app.js
IF you are using PM2 (Node Process manager) (the —name can be anything you want):
pm2 start app.js --name "sonoff_server" --update-env
This Server keeps the connection to the sonoff devices, and must run permanently.
All routes can be accessed from the browser (either by http or https, make sure to add the port):
example, if you are using the default ports: