SONOFF Tutorial: A Wi-Fi Room Temperature Controller for $10

Originate from Ubidots.

Author: Mateo Vélez

In our last blog post we learned how to use the SONOFF Smart Switch to control a relay from Ubidots. In this case, we’ll learn how to control an Air Conditioner with the SONOFF TH, a variation of the original one that includes temperature and humidity sensors. Using these readings, we’ll use Ubidots’ Event engine to turn on or off your air conditioner automatically, depending on the room temperature.


Compared to 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.



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:

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:
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 UbidotsMicroESP8266 library here.

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

7. Select the .ZIP file of UbidotsMicroESP8266 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 to the Data source.

4. Create four variables inside the Data source: Temperature, Humidity, Heat Index and Relay.

5. Copy the variable ID of all variables into the code snippet below.

Create Ubidots Event

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”:

We’ll need two Ubidots Events: one to turn on the relay and another one to turn it off:

1. In your Ubidots account, go to Events and then click on “Add new Event”

2. Select the SONOFF data source you just created.

3. Select the Heat Index variable.

4. Set the trigger condition to “greater than 25”, or your desired Heat Index (the SONOFF sends the data in Degrees Celsius)

5. Select “Set a variable” trigger type, then select the SONOFF data source again, then the “Relay variable”

6. Set it to “1” so the Events sets the Relay variable to “1” when the condition is met.

7. Repeat these steps to create a second event, but this time set the condition to “less than 20″and the value sent to the Relay to “0”.

Coding your SONOFF TH

Here is the code that turn on/off the SONOFF device, to use this code don’t forget to change TOKEN and ID with your Ubidots’ account token and the variable ID we just created. Change WIFISSID and PASSWORD with your network credentials.

To upload the code into the SONOFF you will need to put it into programming mode:

1. Connect the UARTbee to PC USB.

2. Press SONOFF button and unconnect the USB at the same time.

3. While pushing the button, connect the USB again.

When you finish uploading the code, disconnect the UARTbee and SONOFF, then connect the DHT11 sensor.

Your SONOFF is ready to connecto to a supply of 110-240V and to your air conditioner.

Putting it all together

We tested this project with a “SAMURAI Eole Crystal” fan. This machine required pushing a button to turn it on after plugging it to the AC outlet, so we soldered the ON/OFF button so it’s always ON:

We then re-assembled the unit, connected it to AC and done!

%d bloggers like this: