SONOFF Dual Tutorial: Control Your Electrical Appliances Remotely Using MQTT and Ubidots

by UbiMaker in Circuits > Wireless

3935 Views, 21 Favorites, 0 Comments

SONOFF Dual Tutorial: Control Your Electrical Appliances Remotely Using MQTT and Ubidots

File1489607900373.png

This $9 Wi-Fi relay can control two appliances at the same time. Learn how to connect it to Ubidots and unleash its full potential!

In this guide you will learn how to control a couple of 110V appliances over Wi-Fi for $9, using Itead's SONOFF Dual. Compared with consumer-grade WiFi smart plugs in the market, 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.

Requirements and Setup

File1489609383501.png
File1489609007731.png
File1489609553329.png

In order to follow this Instructable, you'll need:


Hardware Setup

Disassemble the SONOFF Dual device, this is to access the SONOFF TTL pinout, which we'll need to program the onboard ESP8266. The SONOFF comes without two of its pin headers, so you will need to solder them before programming the unit.

After soldering, connect the board to the UartSBee following this table:

UartSBee ---> SONOFF Dual

VCC ---> VCC

TX ---> RX

RX ---> TX

GND ---> GND

Arduino IDE Setup

Screenshot-2015-11-03-12.27.18.png

In the Arduino IDE, click on Files -> Preferences and enter this URL into the Additional Boards Manager URLs field, to be able to access ESP8266's libraries for Arduino:

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

This field supports multiple URLs. Separate them with commas in case you already have other URLs typed.

  • Open the Boards Manager from Tools -> Board menu and install ESP8266 platform.
  • After installation, go to the Tools > Board menu and select the board: Generic ESP8266 Module.
  • Download the UbidotsESPMQTT library as a ZIP file in our GitHub account.
  • Back in your Arduino IDE, click on Sketch -> Include Library -> Add .ZIP Library.
  • Select the .ZIP file of UbidotsESPMQTT and then click on “Accept” or “Choose”.Close the Arduino IDE and open it again.

Coding Your SONOFF Dual

File1489613009887.png

This sample code will subscribe to a Ubidots variable that turns on or off both relays at the same time.

Before running the code, go to your Ubidots account, locate the "Devices" tab and create a Device called "SONOFF Dual" and a variable in it called "Relays". It should look like this image.

Make sure the Device API label is "sonoff-dual" and the variable API label is "relays". These are the unique identifiers used by the SONOFF to know to which variable to subscribe in the MQTT broker. You can edit the labels if needed.
You're now ready to flash your device with this code!

Control Your Appliances Remotely!

File1489613251617.png
File1489612948248.png

After flashing your devices, open up the serial monitor of the Arduino IDE, you should see something like in the pic, meaning the WiFi connection and MQTT subscription were successful.

Now go to the "Dashboards" tab and add a new Widget of the type "Control"--> "Switch". This switch will send a "1" or a "0" to the "Relays" variable, which is then read in the SONOFF's callback function to turn the relays on or off. You can now control your SONOFF Dual remotely from your dashboard!