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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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

  • Java’s Next Act: Native Speed for a Cloud-Native World
  • A Guide to Container Runtimes
  • Breaking Bottlenecks: Applying the Theory of Constraints to Software Development
  • Building Scalable and Resilient Data Pipelines With Apache Airflow
  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.3K 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, DZone MVB. 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
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!