DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Upgrade Your Hobbyist Arduino Firmware To Work With STM32 For Wider Applications
  • How to Stream Sensor Data to Apache Pinot for Real Time Analysis
  • How To Check Tuya Library in Arduino IDE

Trending

  • DevOps Is Dead, Long Live Platform Engineering
  • How to Test a PATCH API Request With REST-Assured Java
  • Stop Writing Dialect-Specific SQL: A Unified Query Builder for Node.js
  • Building an Image Classification Pipeline With Apache Camel and Deep Java Library (DJL)
  1. DZone
  2. Data Engineering
  3. IoT
  4. Make a Wi-Fi Smart Switch for $5

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.

By 
Mateo Velez user avatar
Mateo Velez
·
Updated Nov. 08, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
48.6K Views

Join the DZone community and get the full member experience.

Join For Free

Wi-Fi smart switch

Here's how you can make a Wi-Fi smart switch for a few bucks.

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.

You may also like: Home Automation Using IoT

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!

SONOFF

Overview

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.

Requirements

  • UARTbee
  • SONOFF device
  • UbidotsESPMQTT

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:

UARTbe pins

UARTbee SONOFF
VCC VCC
TX RX
RX TX
GND GND

 

3. 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

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 the 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 to create a button widget in your 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

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 in 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. 

Enjoy!

Further Reading

Home Automation Using IoT

ESP8266 and Telegram Bot: Home Automation

arduino

Published at DZone with permission of Mateo Velez. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Upgrade Your Hobbyist Arduino Firmware To Work With STM32 For Wider Applications
  • How to Stream Sensor Data to Apache Pinot for Real Time Analysis
  • How To Check Tuya Library in Arduino IDE

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook