Over a million developers have joined DZone.

Make a Wi-Fi Smart Switch for $5

DZone 's Guide to

Make a Wi-Fi Smart Switch for $5

With $5 and a bit of C, you can create your own smart switch to control your lights, your garage door, or plenty of other devices.

· IoT Zone ·
Free Resource

In this guide, you will learn how to control any 110-240V appliance for $5, using Itead's SONOFF device. Compared with the $30 Wi-Fi 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!

Image title


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

The SONOFF contains a relay, an ESP8266 Wi-Fi 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.



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:

Image title



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


You'll want to enter that into the Additional Boards Manager URLs field. This field supports multiple URLs, separating them with commas, in case you already have other URLs typed.

4. Open the Boards Manager from Tools -> Board menu and install ESP8266 platform. Select your ESP8266 board from Tools > Board menu after installation.

5. Download the UbidotsESPMQTT library here.

6. Now, click on Sketch -> Include Library -> Add .ZIP Library.

7. Select the .ZIP file of UbidotsESPMQTT and then click on  “Accept” or “Choose”.

8. Close the Arduino IDE and open it again.

Create a Ubidots Variable

1. Login in your 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: 

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

Create a Button Widget

Now, to control the SONOFF device, you will need 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 -> Switch and select the Data source and Variable to control created before.

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 to the USB.

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

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


 * Auxiliar Functions

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

  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:
  client.wifiConnection(WIFINAME, WIFIPASS);
  pinMode(RELAY, OUTPUT);
  pinMode(LED, OUTPUT);
  digitalWrite(LED, HIGH);
  client.ubidotsSubscribe("relay","sonoff"); //Insert the dataSource and Variable's Labels

void loop() {
  // put your main code here, to run repeatedly:
      client.ubidotsSubscribe("relay","sonoff"); //Insert the dataSource and Variable's Labels

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. 

wi-fi ,sonoff ,smart switch ,iot

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}