How to control Sonoff Basic using Cloud4RPi and a smartphone

Cloud4RPi Team
Nov 27, 2017 · 4 min read

Sonoff is a device line for Smart Home developed by ITEAD. One of the most flexible and inexpensive devices from that line are Sonoff Basic. It is a Wi-Fi enabled switch based on a great chip, ESP8266. This article describes how to set up the Cloud4RPi service on a Sonoff Basic smart switch.

In the previous article, we explained how to flash the new MicroPython firmware on the Sonoff Basic or Sonoff Dual smart switch. In this article we are going to restore a part of the original Sonoff-enabled functions using Cloud4RPi.

We already have access to the Python REPL interface via the UART protocol. Since the ESP8266 is a Wi-Fi module, we can communicate with it wirelessly. Turn your MicroPython-enabled board on, access its command line and enter the following command to enable the WebREPL:

>>> import webrepl_setup

This command starts the configuration wizard where you can configure the WebREPL auto-start, set the password, and reboot once finished.

After rebooting, connect to your Wi-Fi network by executing the following commands (replace the Wi-Fi configuration with your data):

>>> from network import WLAN
>>> STA = WLAN(0);
>>> STA.connect('__YOUR_WIFI_NETWORK_NAME__', '__PASSWORD__')
>>> STA.ifconfig()

Wait a few seconds and check the STA.isconnected() output. If it outputs False, double-check the Wi-Fi credentials, reconnect, and check that the STA.isconnected() outputs True.

To get the ESP8266’s IP address in your network, execute the following command.

>>> STA.ifconfig()[0]

You can now connect to the ESP8266 via the WebREPL (download this HTML document and open it with your browser):

At the right-hand side of the WebREPL interface, you can see the file-manager fields allowing you to upload and download source code files to the ESP8266’s virtual file system.

Download the required files to your computer:

Open the file in a text editor (for instance, Visual Studio Code) and replace the following strings:

Save the file and upload the, and files to your ESP8266 via the WebREPL’s right-hand side panel.

You can use the command-line file uploader shipped with the WebREPL to upload files.

Reset the ESP8266. You can use the console for this:

>>> import machine
>>> machine.reset()

The file named is started automatically on boot.

If everything goes well, you can open the Cloud4RPi device page and see that the device is online.

Go to the Control Panels page and add a new control panel and add the Switchwidget and bind it to the LED variable.

Use the LED switch on the control panel to turn the Sonoff LED on.

Add a Text widget and bind it to the Button variable. Configure different colors for the “true” and “false” strings. You can now press the hardware button and see how the widget changes.

You can control the Sonoff Basic relays by adding a new variable bound to the hardware pin 12.

relay_pin = Pin(12, Pin.OUT)def on_relay(value):
return relay_pin.value()
# ...device.declare({
'Relay': {
'type': 'bool',
'value': False,
'bind': on_relay
# ...

We have connected the relay to our desktop light, watch the video in which we test it.

With Cloud4RPi, you can control your Raspberry Pi and other IoT devices remotely in real time. Visit our site and connect unlimited devices for free.


Cloud control panel for your IoT projects