DEV Community

Fernando Tricas García
Fernando Tricas García

Posted on • Updated on

Managing my sonoff device from command-line

Some time ago I bought a Sonoff Smart Remote Control Wi-Fi Switch Smart Home Automation device (too much names for the thing!). I plugged it to a lamp in our livingroom, installed the app, configured the device, and started using it.
It works more or less well (sometimes is not very fast) and in these months the app evolved and become more useful (or, perhaps, my knowledge about it).

Alt Text

Anyway, I was not very happy with the idea of using the mobile app: yo need to install it, configure, maybe you do not have your mobile at hand but you have a computer ....
At that time I was not able to find alternatives (well, yes, you could flash de device and so on, but then... Why not to make your own device with the ESP?).

Recently I've researched a bit more and I found the project pysonofflanr3 which provides a command line interface to manage the device.

For example, if you type:

pysonofflanr3 discover
Enter fullscreen mode Exit fullscreen mode

You'll get a list of devices available in your network:

2020-12-01 17:49:31,274 - info: Attempting to discover Sonoff LAN Mode devices on the local network, please wait...
2020-12-01 17:49:31,441 - info: Found Sonoff LAN Mode device 10005f4eae at socket 192.168.1.33:8081
Enter fullscreen mode Exit fullscreen mode

Then, with this IP and some other parameters, you can switch on and off the device, look at its state, and so on:

On:

pysonofflanr3 --host myIp --api_key myAPIkey on
Enter fullscreen mode Exit fullscreen mode

Off:

pysonofflanr3 --host myIp --api_key myAPIkey on
Enter fullscreen mode Exit fullscreen mode

My Raspberry Pi terminal output.

Alt Text

You can obtain your parameters, for example, setting the device in pairing mode and then, you can connect to the access point of the device, and pointing your browser to http://10.10.7.1/device
You'll get some parameters including the API key.

The next step is to integrate this actions in another programs. Maybe for another post.

Top comments (0)