How to Control ESP8266 Based Sonoff Basic Smart Switch With a Smartphone

by Cloud4RPi in Circuits > Microcontrollers

6364 Views, 42 Favorites, 0 Comments

How to Control ESP8266 Based Sonoff Basic Smart Switch With a Smartphone

1.jpeg

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 instructable, we explained how to flash the new MicroPythonfirmware 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.

Connecting Via WebREPL

2.png

Earlier we accessed 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.active(1)
>>> 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]
'192.168.1.108'

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.

Connecting to Cloud4RPi

3.png

Download the required files to your computer:

  • simple.py: The MQTT Library for MicroPython. Save this file as mqtt.py when downloading.
  • cloud4rpi.py: The Cloud4RPi client library for MicroPython.
  • main.py: Sample code.

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

  • __SSID__ with your Wi-Fi network name.
  • __PWD__ with your Wi-Fi network password. If you have an open network, remove the '__PWD__' element without removing the trailing comma so that the WIFI_SSID_PASSWORD variable becomes a tuple with one element.
  • __YOUR_DEVICE_TOKEN__ with the token displayed at the top of the device page on cloud4rpi.io. If you do not have a token, open the Devices page, create a device using the New Device button in the top right corner, and use its token.
  • Change the LED_PIN to 13 and the BUTTON_PIN to 0.

Save the file main.py and upload the mqtt.py, cloud4rpi.py and main.py 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 main.py is started automatically on boot.

If everything goes well, you can see the device is on the Cloud4RPi device page.

Setting Up Control Panel

Sonoff Basic Control Panel Configuration

Go to the Control Panels page and add a new control panel and add the Switch widget 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): 
	relay_pin.value(value) 
	return relay_pin.value()
# ...

device.declare({ 
	'Relay': { 
		'type': 'bool', 
		'value': False, 
		'bind': on_relay 
	}, 
	# ... 
})

Final Result

Controlling Sonoff Basic using Cloud4RPi on a smartphone

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