IoT Projects, ESP8266, Makers

SONOFF Tutorial: A Wi-Fi Smart Switch for $5

In this guide you will learn how to control any 110-240V appliance for $5, using Itead’s SONOFF device. Compared with the $30 WiFi smart plugs out there, the SONOFF is a great alternative for making smart home and even industrial IoT projects at a larger scale. Moreover, it is based on the popular ESP8266 Wi-Fi chip, making it compatible with the Arduino environment and other resources like our ESP libraries at Ubidots.

The SONOFF comes with its own firmware and mobile app, but we think that its main value actually lies in its form-factor and low price. This is why we decided to do some hacking and unleash its full power!

Overview

sonoff_wifi_wireless_smart_switch.jpg

Be careful! Do not manipulate the SONOFF while it’s connected to the 110/240V AC lines

The SONOFF contains a relay, an ESP8266 WiFi chip and the required circuitry to power it with the AC line of a power outlet. It also comes in a nice package that makes it look more professional than an average DIY project at home.

Requirements

Setup

  1. Disassemble the SONOFF device, this is to access the SONOFF TTL pinout, which we’ll need to program the onboard ESP8266. You will need to solder four pins, these are shown in the picture below.
  2. Connect the UARTbee pins to the SONOFF pins, following this pinout:
SONOFF-PINOUT.jpg
UARTbee SONOFF
VC CVCC
TX RX
RX TX
GND GND

Go to the Arduino IDE, click on Files -> Preferences and enter this URL to be able to access ESP8266’s libraries for Arduino:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

into the Additional Boards Manager URLs field. This field supports multiple URLs, separating them with commas, in case you already have other URLs typed.

  1. Open the Boards Manager from Tools -> Board menu and install ESP8266 platform. Select your ESP8266 board from Tools > Board menu after installation.
  2. Download the UbidotsESPMQTT library here.
  3. Now, click on Sketch -> Include Library -> Add .ZIP Library.
  4. Select the .ZIP file of UbidotsESPMQTT and then click on “Accept” or “Choose”.
  5. Close the Arduino IDE and open it again.

Create a Ubidots Variable

  1. Ubidots account.
  2. Go to Sources and click on the orange plus symbol.
  3. Set the name of the data source and check the label
ezgif.com-video-to-gif (2).gif

4. Create a new variable for the relay and check the label

ezgif.com-video-to-gif (3).gif

Create a Button Widget

Now, to control the SONOFF device, you will need to create a button widget in you Ubidots account. This widget will set your RELAY variable to either “1” or “0”:

  1. Go to Dashboard and click on the orange plus icon.
  2. Click on Control -> Switchand select the Data source and Variable to control created before.
ezgif.com-video-to-gif (4).gif

Coding your SONOFF

Here is the code that turns on/off the SONOFF device. To use this code don’t forget to change the TOKEN with your Ubidots’ account token. Change WIFISSID and PASSWORD with your network credentials.

To upload the code into the SONOFF you will need to:

  1. Connect the UARTbee to your PC’s USB.
  2. Press SONOFF’s button and disconnect the USB at the same time.
  3. While still pushing the button, connect again the USB.

These steps are meant to bring the SONOFF into programming mode.

sonoff-connect.gif

When you’re ready, upload the code from the Arduino IDE:

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "..." // Your Ubidots TOKEN
#define WIFINAME "..." //Your SSID
#define WIFIPASS "..." // Your Wifi Pass
#define MQTTCLIENTNAME "..." // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name

#define RELAY 12
#define LED 13

Ubidots client(TOKEN, MQTTCLIENTNAME);

/****************************************
 * Auxiliar Functions
 ****************************************/

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i=0;i<length;i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  if ((char)payload[0] == '1') {
      digitalWrite(RELAY, HIGH); //On relay
      digitalWrite(LED, LOW); //On led
  }
  if ((char)payload[0] == '0') {
      digitalWrite(RELAY, LOW); //Off relay
      digitalWrite(LED, HIGH); //Off led
  }
}

/****************************************
 * Main Functions
 ****************************************/

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  pinMode(RELAY, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  client.begin(callback);
  client.ubidotsSubscribe("relay","sonoff"); //Insert the dataSource and Variable's Labels
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      Serial.println(RELAY);
      client.ubidotsSubscribe("relay","sonoff"); //Insert the dataSource and Variable's Labels
      }
  client.loop();
  }

Plug your Smart Switch and start controlling things

Now it’s time to make things “smart” by adding your SONOFF to them;control your lights, open or close your garage, etc.

Taking a step further…

Ubidots is a cloud service empowering thousands of engineers around the world to build IoT projects, both in education and the enterprise. To learn how Ubidots can help your business check out our features or leave us a message.

Author image

About Mateo Velez