We recently started exploring the ESP8266 boards and today’s project is going to be an interesting one based on this WiFi module. We are going to build an ESP8266 based WiFi Air condition Remote Controller. While this project can be adapted to fit any device that uses a remote control, we will use it for controlling an Air Conditioner.
Usually, everyone switches “OFF” all electrical appliances in their home, including Air conditioners when leaving the house, this means when you get back home, the temperature will not be regulated, and it will take the air conditioner some time to achieve your desired level of coolness. But what if you could automatically control your air conditioner from your mobile phone when you are a few blocks away from home? What if you could turn it OFF from wherever you are, if you discover you left it ON when leaving the house? What if it can be synced with your phone’s location and turned ON when you are almost home? What if it can be synced with the weather information of your phone? These and many more questions, inspired today’s project.
HOW IT WORKS
The air conditioner (AC), is normally controlled by a “remote control” which sends infrared signals to it when a button is pressed. The AC interprets the signals and converts them to instructions to either increase its coolness or reduce it, go OFF or come ON, etc. Thus to be able to control it using our own device, the device will have to be able to mimic and send the same signals sent by the remote to the AC, such that, by pressing the “ON” button (as an example) on the app, our WiFi remote control sends the same IR signal the remote would have sent if the “ON” button was pressed.
The device we are going to build is based on the ESP8266 and it’s wonderful features and use has been discussed in one of our past tutorials. The ESP8266 will be connected via MQTT to home bridge and through it, Siri can be used to give instructions to control the AC. Once the instruction (say “turn on” for instance) is received over MQTT, the NodeMCU matches it with corresponding IR signals and the signal is sent to the AC through an IR led. An OLED display is used to provide visual feedback about the current state to the user. For non-iPhone users, the device can also be connected to the Amazon Alexa.
Asides the control of the Air conditioner via Siri or Apple’s home kit, it is also equipped with control switches.
To make the experience a complete one, the following features were added to the project;
- Display NTP Clock when standby.
- Simple button control. Power, Temp up / down.
- Can control temperature, swing, fan speed and mode with HomeKit.
- Auto brightness according to ambient light.
- OTA Firmware upgrade.
- Control via HomeKit using HeaterCooler Service (iOS 11+)
- Works with Amazon Alexa using fan service
At the end of today’s tutorial, you would know the basics of building IoT solutions and Home automation. You would also learn how to intercept signals sent by IR based remote controls, ( please do this within lawful limits and do not apply to dangerous situations), create an IR transmitter which could be used to control all IR remotes based appliances in your home or office, and build devices that can be controlled via Apple’s Home kit.
The following components are needed for this tutorial;
- ESP8266 NodeMCU
- Raspberry Pi 4, or Raspberry Pi 3 or Raspberry Pi 2 with a WiFi dongle
- Keyboard, Monitor, and HDMI cable for the Pi or a LAN cable (for headless mode connection)
- 2A Power supply for the Raspberry Pi
- SSD1332 OLED Display (256 × 64)
- Push button
- IR LED
- 1k resistor (4)
- NPN Transistor BC338
- Jumper wires
- TSOP 1738
The LDR is not compulsory as it was used to dim or turn off the backlight and make the OLED screen easier to read.
As mentioned in the project description, the hardware part of the project itself is made up of the NodeMCU, an IR LED, some push-buttons and an OLED Display.
Connect the components as shown in the schematics below.
The push-buttons provide an alternative way of controlling the device when you are not carrying your mobile phone, and the OLED display provides diverse information including the current temperature being maintained by the AC.
Another view of the schematics is provided below to make it easier to follow.
PREPARING THE SYSTEM
Before we write the code for the project, we need to make some preliminary preparations.
- Prepare the IR signals that will be sent to the AC for each command
- Setup Communication with Homekit
1. Decoding The IR Signals
As mentioned above, this project works by mimicking the IR remote, sending out IR signals generated based on the commands from Homekit. Since the IR signals have to be the same as the one sent by the remote, we need to obtain the IR code for each button before anything else. To obtain the codes, we have to build a device which receives the IR signal from the remote control, decodes it and displays the encoded data so we can copy it for use later on.
To obtain the IR codes, we will use the cheap, efficient and popular TSOP1738 IR receiver, connected to Nodemcu, in the way that when a button on the remote is pressed, the IR receiver will pick it and through the decode sketch running on the NodeMCU, it will print the content of the signal on the serial monitor. This content will be later used to control the AC.
Connect the IR receiver to the NodeMCU as shown in the image below:
To determine when an IR signal is received and help us for debug, a LED was added in the schematics so that it only comes on when an IR signal is received.
Here is a pin map of the connection between the NodeMCU and the TSOP;
ESP8266 - TSOP1738 GND - GND 3.3V - VCC D5 - Signal
Today’s project is heavily dependent on the IRremote ESP8266 library, which can be downloaded from the link attached. The library is based on the standard Arduino IRremote library and comes with the decoded data (for few common keys), for quite a number of popular remote controls, which means that you could be lucky, and the codes for your remote will be already available in the library. For those that are not so lucky, along with the schematics above, we will use the example sketch, IRrecvDumpV2 which comes with the IRremote ESP8266 library to obtain the codes.
With the library installed, launch the Arduino IDE, go to examples and select the IRrecvDumpV2 example. Upload the code to your NodeMcu and press a button on your remote control. You should see an array of numbers show up in the serial monitor every time a button on the remote is pressed. Copy and keep the array for each key as they represent the decoded IR signals sent by the remote and we will use them to reproduce the IR signal for our Air conditioner. The code is well commented and should be easy to follow.
2. Connecting With Homekit
The second thing we need to do before writing the code is setting up the connection to Homekit. Connection to Homekit gives us the ability to control the device using Siri and the Homekit app. To achieve this, we will use the Homebridge-MQTT installed on a RaspberryPi which will serve as our server. The Homebridg-MQTT is essentially a plugin for homebridge that allows you to add and control devices via the MQTT protocol.
Boot up your RaspberryPi and open a terminal. We will start by updating the Pi to ensure we don’t run into any compatibility issue later on.
with this done, Install MOSQUITTO on the pi for MQTT. run;
once this is done, install the Homebridge-MQTT plugin using;
Next, we need to edit the config file. the config file is located inside the homebridge folder (home / .homebridge /run;
Add the platform section to the config file such that it now looks like this;
save the config file and exit.
Run HomeBridge, use MQTT LENS (can be downloaded from Chrome Webstore) and add Connection to our Raspberry Pi, creating an accessory that uses the heater-cooler service which is supported by any version of the IoS from IOS11. Set the publish to value as;
and set the payload as;
With all these done, we can now proceed to write the code for this project.
The code for today’s project will be developed using VScode and the platform.io IDE plugin. The code is quite bulky and may be difficult to follow for anyone not used to programming microcontrollers using these tools so we wrote a simple tutorial on using these tools, be sure to check it out.
As mentioned earlier, the code for today’s project is heavily reliant on the IRremoteESP8266 library, but it also utilizes the Wifi manager library, the Arduinojson library, the Arduino OTA library, and the NTP library. Each of these libraries made it easy to achieve different parts of the project. The IRremoteESP8266 library was used to send IR signals from the device to the Air conditioner, the Wifimanager library was used to make it easy to change the WiFi credentials, the Arduinojson was used to parse the data from homekit and the Arduino OTA library was used to enable over the air firmware update on the device. Other libraries used include U8g2 library which makes it easy to work with monochrome displays. It was used to display the graphics which formed the UI for the project. All the libraries used for this project can be downloaded and installed via the Arduino library manager.
The code for this project is quite bulky and may be too much to explain here but we have ensured it is well commented and attached in the zip file under the download section. The zip file contains a bunch of files including the graphics images used for the GUI, the libraries, and everything about the project. The code is located in the “src” folder. The “src folder also contain different codes most of which are header files for the main code-named main.cpp. All other files are important and need to be in the same project folder as the main.cpp as they contain all the information needed by the main file. For instance, main.h file, contains the MQTT declarations among other things while the UI.h file contains the char values of everything used for the UI.
For those who may be interested in creating their own UI for the project, check out this tutorial we created a while back. It might give you the necessary start point.
To make the project look pleasing to the eye, an enclosure was 3D printed. The design was done using Fusion 360 and all the design files are included n the zip file under the download section.
After printing, the enclosure was scrubbed and painted to make the print look better. The final enclosure is shown in the image below.
Ensure that the connections are as described and switch on your device. It should come up as shown in the picture below.
That’s it for this tutorial guys. As said in the beginning, the applications for this project are limitless. It can serve as a simple box that controls every IR remote controlled device in your house. Do let me know if you have questions and what cool ideas you will be implementing with all you have learned here.
Till next time.
A video of the device in action can be seen below:
Read more: ESP8266 WIFI AIR CONDITIONER REMOTE CONTROL