Introduction: IDC2018IOT Tell Me When to Turn Off the AC
Many of us, especially at summer time, use the AC almost nonstop, when in reality at certain times of the day we can just open a window and enjoy a nice breeze. Also, we personally noticed that we sometimes even just forget to turn the AC off when leaving the room, wasting energy and money.
The solution we will build will compare the inside temperature with the outside, and when they are close enough, will notify us via Facebook Messanger that it's time to open a window and give the AC some rest.
Also, we will make another mechanism to notify us when we forgot the AC on and left the room.
Step 1: A Bit More Details
We collect data from 4 different sensors:
- Two DHT sensors collect the temperature inside the house and outside the house.
- One PIR sensor detects movement in the room.
- One Electret microphone is used to detect the wind coming out from the AC vent, a simple and reliable way to determine if the AC is on.
The data coming from the sensors will get processed and sent to the Blynk where it will be displayed in an interface we will create. Also, we will trigger IFTTT events in order to notify the user when he can open a window instead of the AC, and when he forgot the AC on and left the room for a predefined period of time.
The Blynk interface will also give us a way to change relevant settings according to user preference, as we will discuss in more details later.
- WiFi module - ESP8266
- PIR sensor.
- DHT11/DHT22 temperature sensors x2.
- 10k/4.7k resistors (DHT11 - 4.7k, DHT22 - 10k, PIR - 10k).
- Electret microphone.
- Long cables (Telephone wire will do a great job).
The complete code of the project is attached at the end with comments throughout the code.
Logically, it has a few different layers of functionality:
- The data from the sensors is read in intervals of 3 seconds as it shows to be more accurate and there is no need for more than that.
- One part of the code is keeping track of the AC state by the values coming from the electret microphone which is placed over the opening of the AC.
- Another part is keeping track of the reading coming from the temperature sensors, and the difference the use defined as acceptable to turn of the AC and open a window instead. We look for the moment when the temperatures get close enough.
- A third part is keeping track of movement in the room. If it detects no major movement (the way to check major will be explained soon) for a timeframe defined by the user, and the AC state is ON, a notification will be sent to the user.
- The notifications are handled through triggering IFTTT Webhooks which send predefined messages to the user through Facebook Messenger
- The last part worth noting is the part which handles the Blynk interface, both by getting the changes the user makes to variables and on the other way - pushing data to the Blynk interface for the user to see.
Step 2: In a Lot More Details - Sensors
First, we need to make sure both of our DHT sensors read the same temperature when placed in the same place. For that, we made a simple sketch attached at the end of this section (CompareSensors.ino). Connect both sensors, and make sure you change the type of DHT sensors in the sketch according to the ones you have (the default is one DHT11 and one DHT22, so you can see how both are dealt with in the code). Open the serial monitor and let them work for a while, especially if you use DHT11 sensors, as they tend to take longer to adjust themselves to temperature changes.
Note the difference between the sensors, and insert it later in the main code in the "offset" variable.
One DHT sensor should be placed on the outside wall of the house, so connect it to some long cables, long enough to reach your ESP8266 inside the room, and place it outside (can be done easily through the window). The other DHT sensor should be placed on the breadboard, inside the room in which we use the AC.
The electret microphone should also be connected to long enough cables and placed in a location where the wind coming out of the AC will hit it.
Finally, the PIR sensor should be placed in a location facing the center of the room so it will capture every movement in the room. Note that the sensor has two little knobs, one controlling the delay (how long the HIGH signal of detecting a movement is kept HIGH), and the other one controls the sensitivity (see picture).
You might need to play around with it until you get reading which you are satisfied with. For us, the best result was delay all the way to the left (lowest value) and sensitivity right in the middle. The code provides serial prints which include readings from all the sensors that will make debugging such problems much easier.
Connecting the sensors:
The pin numbers we used are as follows (and can be changed in the main code):
Outside DHT sensor - D2.
Inside DHT sensor - D3.
Electret - A0 (analog pin).
PIR - D5.
The schematics for connecting each of them can be easily found using google image search with something along the lines of "PIR resistor Arduino schematic" (we wouldn't want to copy them here and cross any copyright lines :) ).
We also attached a picture of our breadboard, it's probably hard to really follow the connections, but it can give a good feeling for it.
As you probably know, things rarely if ever work the first time we connect them.
That's why we made a function which prints the readings from the sensors in an easy to read way, so you can debug your way for them to work. If you don't want the code to try to connect to Blynk while debugging, just comment out "Blynk.begin(auth, ssid, pass);" from the setup part of the code, run it, and open serial monitor to see the prints. We also attached a picture of the prints.
Step 3: In a Lot More Details - IFTTT Sequence
So we want to get notified in two scenarios:
1. The outside temp is close enough to the one we have inside with the AC working.
2. We have left the room for an extended period of time and the AC is still working.
IFTTT enables us to connect many different services which usually don't interact, in a very simple way. In our case, it lets us send notifications very easily through many services. We chose Facebook Messanger, but after getting it to work with Facebook Messanger you will be able to easily change it to any other service of your choosing.
On the IFTTT website click on your username (top right corner) and then "New Applet" choose "Webhooks" as the trigger (the "this"), and choose "Receive a web request". Set an event name (e.g. empty_room).
For the triggered service, the action (the "that"), choose Facebook Messenger > Send message, and type in the message you want to receive when this event happens (e.g. "Hi, seems like you forgot the AC on :\ ).
While we are here, you should also find your secret key which you will need to insert in the appropriate place in the code.
To find your secret key go to https://ifttt.com/services/maker_webhooks/settings There you will find a URL with your key in the following format: https://maker.ifttt.com/use/<your secret key>
Step 4: In a Lot More Details - Blynk
We also want an interface which will have the following features:
1. Ability to set how long should the room be empty with the AC working before we get notified
2. Ability to choose how close should the outside temperature be to the inside.
3. A display for the readings from the temperature sensors
4. A led telling us the state of the AC (on/off).
5. And most importantly, a display to show how much $$$ and energy we saved.
How to create the Blynk interface:
If you don't yet have the Blynk app, download it to your phone. When you open the app and create a new project, make sure to choose the appropriate device (e.g. ESP8266).
You will get an email with an authentication token, which you will insert in the code in the appropriate place (you can also resend it to yourself from the settings later if you lose it).
The place new widgets on your screen, click the + sign at the top. Choose the widgets, and then click on a widget to enter its settings. We have added pictures of the settings for all the widgets we used, for your reference.
After you finish with the app, and when you eventually want to use it, simply click on the "play" icon on the top right corner to run the Blynk app. You will be able to also see when your ESP8266 connects.
Note - the "update" button is used to fetch the temperature and state of the AC for us to see in the app. It is not required when changing settings (such as the temperature difference), as they get pushed automatically.
Step 5: The Code
We made lots of effort to document every part of the code in a way that would make understanding it as easy as possible.
Parts in the code that you must change before using it (as the auth key for Blynk, your wifi SSID and password, etc...) are followed by the comment //*change* so you can easily look for them.
You will need to have the libraries used in the code, you can install them through the Arduino IDE by clicking Sketch>Include Libraries>Manage Libraries. There you can search for the library name and install it. Also, make sure you put the generic8266_ifttt.h file in the same location as the ACsaver.ino.
One part of the code we will explain here as we didn't want to clutter the code, is how we decide when to change the state of the AC from on to off, and the state of the room from empty to not empty.
We read from the sensors every 3 seconds, but as sensors are not 100% accurate, we don't want a single read to change the state we believe is in the room now. To solve this, what the code does, is we have a counter that we ++ when we get a reading in favor of "AC is on", and -- otherwise. Then, when we get to the value defined in SWITCHAFTER (default to 4), we change the state to "AC is on", when we get to -SWITCHAFTER (negative the same value), we change the state to "AC is off".
The impact on the time it takes to switch is negligible, and we find it to be very reliable in detecting only correct changes.
Step 6: Putting It All Together
Ok, so all of the sensors are in place and working properly. The Blynk interface is set (with the correct virtual pins!). And the IFTTT events are waiting for our trigger.
You've inserted the IFTTT secret key in the code,the auth key from Blynk, the SSID of your WiFi and the password, and you even checked that the DHT sensors are calibrated and if not, changed the offset accordingly (for example, our outside DHT read temperatures higher by 1 degree Celsius that what he should have, so we used offset = -1).
Make sure your WiFi is up, start your Blynk app, and load the code on to your ESP8266.
That's it. If everything was done correctly, you can play around now and see it in action.
And if you just want to see it in action without the trouble of putting it all together... Well... Scroll up and watch the video. (Watch with subtitles! No voice over)
Step 7: Thoughts
We had two main challenge here.
First of all, how do we know the AC is on? We tried to use an IR receiver that will "listen" to the communication between the AC and the remote. It appeared to be too complicated, as the data was very messy and wasn't consistent enough to understand "ok, this is an ON signal". So we looked for other ways. One idea was to use a small propeller which will generate small current when moves from the wind of the AC, another idea we tried was to have an accelerometer measure the angle of the rotating wings on the vents and detect their movement from the OFF position.
Eventually, we realized the simplest way to do it is with the electret microphone, which very reliably detects the wind coming out of the AC
Getting the DHT sensors to work was a breeze ;), but only later we realized one of them was a bit off from real temperature. The PIR sensor required some adjustments as well, as described previously.
The second challenge was making the whole solution simple, and reliable. In a sense that it should be annoying to use, it should just be there and nudge when you need. Otherwise, we ourselves would probably stop using it.
So we put some thought into what should be in the Blynk interface and tried to make the code as reliable as can be, by taking care of every edge case we could come up with.
Another challenge, which we failed to solve by the time writing this instructable, was to add an IR blaster that will allow us to turn off the AC from the Blynk interface. What's the point in knowing you forgot the AC on without the possibility to turn in off? (well... you could ask someone if they're at home).
Unfortunately, we had some difficulties replaying the signals we recorded from the remote control, back to the AC with the ESP8266. We managed to control the AC by an Arduino Uno, following this instructable:
We will try again soon, and update the instructable with our findings, and hopefully instructions on how to add that capability.
Another limitation we see is the fact that we need to connect a sensor outside the window, which might not be possible in certain situations, and also means a long cable need to go outside. A solution may be to fetch weather data of your location from the internet. Also, the electret sensor which runs from the AC can be replaced by the IR receiver we described above, for models of AC with more known or easy to decode IR codes.
The project can be extended in many ways. As said above, we will try to find a way to include IR control over the AC, which then opens a whole new world of opportunities to turn on and off the AC from anywhere in the world, or set on and off times through the Blynk app, as another example. After figuring out the technical IR difficulties, adding the code is fairly simple and straightforward, and shouldn't take long.
If we really want to dream big... The project can be turned into a complete module which makes any AC a smart AC. And it doesn't need much more than we did. Just more code, more utilization of the IR, and if we want it to be mass produced, maybe make sure to fetch weather data by location, then we can put the whole thing in a tiny little box.
Really, all we need is a temperature sensor for the inside temperature, a PIR sensor to detect movement, and IR LED as a blaster, and an IR receiver to "listen" to the communication between the AC and the remote we use.
Blynk provides all the capabilities we need to control the magic box, in a very simple and reliable way.
Making such a full project will take some time, especially from the viewpoint of making it versatile enough to configure itself and automatically detect and understand most ACs.
But making it for yourself, well, if you do it in your spare time, we approximate shouldn't take more than a week or two. Depends on how much spare time you have... The main challenge here would be to save all the different signals the AC remote can send, and making sense of them. (Although just replaying them should be even easier).