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

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

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.

Step 1: Connecting Via WebREPL

Picture of Connecting Via WebREPL

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.

Step 2: Connecting to Cloud4RPi

Picture of Connecting to Cloud4RPi

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.

Step 3: Setting Up Control Panel

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 
	}, 
	# ... 
})

Step 4: Final Result

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

Comments

About This Instructable

666views

30favorites

License:

Bio: Cloud control panel for IoT projects
More by Cloud4RPi:How to control ESP8266 based Sonoff Basic smart switch with a smartphoneHow to Control Beer Fermentation Temperature and Gravity From Your SmartphoneHow to Flash MicroPython Firmware on a ESP8266 Based Sonoff Smart Switch
Add instructable to: