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.
- 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.
Connect the UARTbee pins to the SONOFF pins, following this pinout:
- 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.
Open the Boards Manager from Tools -> Board menu and install ESP8266 platform. Select your ESP8266 board from Tools > Board menu after installation.
Download the UbidotsMicroESP8266 library here.
Now, click on Sketch -> Include Library -> Add .ZIP Library.
Select the .ZIP file of UbidotsMicroESP8266 and then click on “Accept” or “Choose”.
Close the Arduino IDE and open it again.
Create a Ubidots Variable
Login in your Ubidots account.
Go to Sources and click on the orange plus symbol.
Set the name to the Data source.
Create four variables inside the Data source: Temperature, Humidity, Heat Index and Relay.
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:
- In your Ubidots account, go to Events and then click on “Add new Event”
Select the SONOFF data source you just created.
Select the Heat Index variable.
Set the trigger condition to “greater than 25”, or your desired Heat Index (the SONOFF sends the data in Degrees Celsius)
Select “Set a variable” trigger type, then select the SONOFF data source again, then the “Relay variable”
Set it to “1” so the Events sets the Relay variable to “1” when the condition is met.
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:
- Connect the UARTbee to PC USB.
Press SONOFF button and unconnect the USB at the same time.
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!