Introduction: Control Sonoff From Raspberry Pi

This tutorial describes how to control a Sonoff switch from a Raspberry Pi. I plan to use Sonoff with my LiV Pi device to turn on an AC unit when CO2 levels exceed a defined threshold. The code should work on any Python-enabled platform able to send HTTP requests. I hope this tutorial will save time to someone setting up his Sonoff switch.

Sonoff is a WiFi wireless switch that can connect to a wide range of home appliances. Out of the box, Sonoff transmits data to a cloud platform through the home WiFi router, which enables users to remotely control the connected appliances, via a mobile application called eWeLink.

Sonoff can also be used with an open source firmware called ESP Easy. ESP Easy allows users to fully configure Sonoff (IP address, DNS, gateways, sensors, rules, etc...) using a web interface. Sonoff with ESP Easy is becoming a very popular device for DIY home automation applications.

A few Sonoff links:

https://www.itead.cc/wiki/Sonoff

http://sonoff.itead.cc/en/products/sonoff/sonoff-basic

A few ESP Easy links:

https://www.letscontrolit.com/

https://www.letscontrolit.com/wiki/index.php/ESPEasy

Step 1: Install ESP Easy on Sonoff

Once you got your Sonoff switch, you have to install ESP Easy on it.

In order to do this, you need to open Sonoff's case, solder a 5 pin connector on the PCB and install the ESP Easy firmware. You will also need a USB-TTL converter to flash the firmware.

Since there are quite a few good tutorials out there on how to install ESP Easy on Sonoff, I will not provide ESP Easy installation instructions in this tutorial.

Here are a couple of good links on flashing Sonoff with ESP Easy:

https://www.youtube.com/watch?v=fN_QKOWvG1s

https://www.letscontrolit.com/wiki/index.php/Tutorial_ESPEasy_Firmware_Upload


I recommend this very detailed tutorial:

https://rutg3r.com/sonoff-firmware-tutorial-to-esp-easy/

Step 2: Configure Sonoff

After you installed ESP Easy on Sonoff, you can access Sonoff using its web interface by pointing your web browser to Sonoff's IP address.

You can now set up Sonoff's IP address and the rules.

In my case, I am using a fixed IP address. My settings are:

IP address: 192.168.1.42

Gateway: 192.168.1.1

Mask: 255.255.255.0

I also created a set of rules (please refer to the attached photo).

My rules are:


On T0 do

gpio,12,0

EndOn


On T1 do

gpio,12,1

EndOn


The T1 rule means that on event T1, Sonoff will turn on its switch. The T0 rule means that on event T0, Sonoff will turn off its switch.

Step 3: Check Sonoff Rules

Now we can check if Sonoff was properly set by sending HPPT requests to Sonoff's IP using the web browser.

My Sonoff has IP address 192.168.1.42, you might need to change the instructions according to your settings.

Copy http://192.168.1.42/control?cmd=event,T1 in the browser address line and press Enter. You should hear the Sonoff relay clicking (the switch is now on) and see an OK confirmation in the browser.

Copy http://192.168.1.42/control?cmd=event,T0 in the browser address line and press Enter. You should hear the Sonoff relay clicking (the switch is now off) and see an OK confirmation in the browser.

Step 4: Python Code

Create a file called "test.py" on your Raspberry Pi and copy the following code into it.

You can run the code with "sudo python test.py".

The code on your Raspberry Pi turns on and off Sonoff every 5 seconds by sending HTTP requests alternating the T1 and T0 events that you created on Sonoff before.

#!/usr/bin/env python
""" Sonoff http POST example """

import time
import requests

url_switch_on  = 'http://192.168.1.42/control?cmd=event,T1'
url_switch_off  = 'http://192.168.1.42/control?cmd=event,T0'
sonoff_url        = 'NOT_INIT'
loop_time        = 5

def main():
  loop_counter = 0
  while (True):
    try:
      if loop_counter%2 == 0:
          sonoff_url = url_switch_on
      else:
          sonoff_url = url_switch_off
                    
      r = requests.post(sonoff_url)
      
      if r.status_code == 200:
        print("Sonoff return code: SUCCESS\n")
      else:
        print("Sonoff return code: FAILED\n")
    
    except Exception:
      print("UNABLE TO SEND COMMAND TO SONOFF\n")          
    
    loop_counter =loop_counter + 1
    time.sleep(loop_time)

if __name__ == "__main__":
  main()