Close
0%
0%

ESP8266 MQTT Infrared AirCon Control

An amateur build of a monitoring and control system for the aircon in my baby's room, using an ESP8266, IR Led, JSON and MQTT protocol.

Similar projects worth following
My wife believes temperature is a major factors in how well a baby sleeps, and doesn't like to open the door to adjust the temperature in case she wakes the baby. Having just taken up electronics as a hobby, I set myself a stretch target of building a system to give my wife greater control over the temperature in the room.

To succeed the system must:
+ Report temperature to her phone in near-real time
+ Provide facility to control the AirCon from her phone, (outside the baby's room)

I have built a prototype that meets the success criteria, but there is lots of room for improvement and refinement.

My project consists of 3 parts (see diagram 1):
1. ESP8266, with an temp sensor and IR LED to interface with the AirCon
2. RPI running Mosquito MQTT broker
3. Generic MQTT client on a phone (UPDATE - now custom app sending JSON)

Essentially the ESP communicates via MQTT to a client on a phone, and controls the Mitsubishi Heavy Industries AirCon via IR.

How it Works

Essentially the ESP8266 has a temp sensor that reads the temp in the baby's room periodically, this reading is reported via MQTT to Mosquito. The phone has a generic client I got from the Google store configured to subscribe to these messages. The Client also is configured to be able to send MQTT messages to control the aircon. The ESP subscribes to the control messages, and sends appropriate IR signals to the Aircon to make the adjustments.

Android MQTT Client

Air conditioners use HVAC signal, which means they are state based not event based. The entire state of all settings are sent each time any commands are sent. Unfortunately the available MQTT clients do not deal well with HVAC - controls are bound to one value and thus event based. This means until I have enough time to write myself a HVAC MQTT client for Android, I will have to maintain state somewhere else in the system. At the moment the ESP maintains this state, in a somewhat clunky manner. Settings are send one at a time, but applied as a group once the "go" command is sent. This is not great form a user-ability perspective, and order of commands is important.

Mosquito MQTT Broker

I am using a completely vanilla Mosquitto install on a Raspberry Pi running Ubuntu Mate (https://mosquitto.org/). I literally have not configured it all beyond installing it. Note I have not implemented SSL/TSL and am not exposing it to the internet.

ESP8266

The circuit is based on a nodeMCU ESP-12e, it is a simple setup with a DHT22 sensor and an IR led with resistors. It sits on top of the Air Conditioner head unit, and the IR led is taped next to the IR receiver, and the Temp sensor is taped to the wall, at Baby level next to the cot. Previously I had an elabrate setup with a 12v battery, and a switching transistor, however I got better results with the taped IR led. The setup runs off a small usb powerbank that I know is pretty inefficient, I have orders some battery clips and voltage regulators and would like to improve the power usage.

The code is relatively simple, it establishes a connection to my home WiFi, connects to the MQTT broker and subscribes to the relevant topics. Periodic the temperature is taken and a MQTT message is sent.

The air con unit uses a 19 Byte HVAC IR signal (see the attached files IR Map), so the ESP maintains a 19 value byte array that represents a valid frame. When adjustments to settings are received the relevant Bytes are updated and stored. When a "go" command is received the whole frame is sent to the air con via the IR led.

This is a little clunky, but it does work. I could improve the way it is handled, and may do so, but I'd prefer to improve the client so it can send HVAC frames to the ESP. This is definitely and area for improvement.

I also plan on using the chips deep sleep to wake up periodically, however I will need to research how to store state data to survive the reset, possible in RTC memory.

Decoding the Mitsubishi Heavy Industries IR signal

Initially I searched the net, to try to find someone who had already decoded the MHI IR signal, I even tried the Mitsubishi electric codes, but alas nothing worked. So I bit the bullet and bought AnalysIR (maker edition) and set about decoding the signal myself. A quick circuit with an arduino nano and an IR receiver and I was in business. I followed the process documented on the AnalysIR website, where a different button on the remote is pressed one at a time, and the changes between each frame highlighted. Through this process it was quite easy to decode the IR signal, and get it into hex values I could manipulate.

Next Steps

  • Write HVAC capable MQTT Android client / or Raspberry PI hosted HTML5 client.
  • Redesign ESP8266 circuit to save power, and include sleep and store data that survives
  • Separate Temperature sensor into separate device

Credits

Upfront I want to say that I haven't really broken new ground, I've essentially stitched together a bunch of different blogs, youtube videos, tutorials, etc....

Read more »

MQTT_ESP8266_MHIv1.ino

Copy of the source code I use to run the ESP8266 circuit

ino - 14.37 kB - 09/03/2016 at 14:07

Download

ACIR.xlsx

Working file for decoding the IR signal - contains extracts from AnalysIR, Map of IR codes, and sheets of unit tests.

sheet - 48.24 kB - 09/03/2016 at 14:03

Download

MitsubishiHEavyIndustriesAirCon_IR_Map.png

Map of the decoded IR signal for a Mitsubishi Heavy Industries Air con (DXK series).

Portable Network Graphics (PNG) - 59.15 kB - 09/03/2016 at 14:02

Preview
Download

  • 1 × NodeMCU v1.0 ESP8266 development board
  • 1 × DHT22 (AM2302) Tempersure Sensor.
  • 1 × IR Led (TSAL6100)
  • 1 × Resistor 1k
  • 1 × Resistor 10k

  • Battery testing... and failing

    Shoebox12/02/2017 at 10:08 0 comments

    This weekend, I decided to test the battery life of my prototypes.

    The program is currently processing for 30 seconds, then sleeping for 1 minute

    • ESP01  LiPo 18650 - Claim 3000mAh, but probably 1100mAh -  16 hr 40 mins
    • Wemos D1 - LiPo 18650 - Claim 3000mAh, but probably 1100mAh -  34 hr 25 mins

    Those times are fairly disappointing. I don't want to be changing batteries every day.

    I might go down the static IP route to speed up the connection, and then play with the awake/sleep ratio. I should be able to decrease awake time, and increase sleep time.

  • ESP sleep state problem and resolution

    Shoebox12/01/2017 at 12:24 0 comments

    A while back, to extend the battery life on the esp8266, I added the sleep function, so every few minutes the ESP wakes up, check the retained message on the MQTT queue, processes and goes back to sleep. The lag of a couple of minute is an okay trade off for extended battery life,

    However there was a problem. The every time the esp wakes up a new wifi and MQTT connection is established, and the retained message was reprocessed. So 2 weekends ago I finally had time to read up on MQTT. Turns out it is a very simple fix, writing back a 0 byte MQTT message with retain flag on to the queue clears the queue.

    So I modified my code to, clear the queue before the ESP goes to sleep, and the state problem was resolved.

    I also soldered up a new prototype, this one is a hat for Wemos D1. IT works great, and the serial interface is a lot easier to debug than the esp-01 board I made. Great for tracking down bugs.

    Now on to test the battery life.

  • Github repo link ConAir Added

    Shoebox11/03/2017 at 12:06 0 comments

    Finally I have checked in the andorid app and arduino code into github and shared. Sorry about the delay... here is the sad sad story...

    Two weeks ago, I converted my code over to use JSON, and wrote an android app to send the JSON messages over MQTT. I got it working, I was very happy and thought, I will clean up the code the create a github repo and share. This is a good idea, however in the wrong order... always check into source control before you clean up the code. I made a mistake, broke my code and spend many hours last weekend trying to fix it. I hope you can learn from my mistake.

    Now, I'm off to clean up the code.

  • Iteration 3 - Android app sending Json to ESP-01 via MQTT

    Shoebox10/21/2017 at 13:23 0 comments

    Quick update on the project...

    Aircons are HVAC systems and receive the full state in every message - i.e. if you press temp down 1 degree, the remote actual sends the temp setting, the fan setting, the swing setting etc every time.  Previously I had to use a work around because the free android MQTT clients I found, could not send settings from multiple controls in a single message. I've written my own now,  am  iterating to remove the unneeded complexity.

    I've developed a basic MQTT android app that can send the full state. I've started using JSON payloads in the MQTT messages, and I'm rewriting the firmware on the ESP-01 as well.

    I'll write this up more completely another time and upload the code to GitHub, but for now here is a couple of screenshots,

    And the latest iteration of the device... I've changed the power source to a 18650 instead of 2 AA... after losing 3 hours on a bug that was actually a power issue...   I've also broken out the reset pin so I can add back the ESP Deep Sleep function to extend battery life.

  • ESP01 version - board pics

    Shoebox09/04/2017 at 09:44 0 comments

    Here are a couple of pics of an ESP 01 version I made on the weekend.

    This version uses a switching transistor to drive the LED, but can't use the ESP sleep function the previous one uses as it  doesn't expose the right pins, although there are work-arounds I will explore in the future.

  • Adapted my code to run on the ESP01 module

    Shoebox09/01/2017 at 14:08 1 comment

    After stumbling and bumbling I've revised my code to run on the ESP 01. I'll be trying to build some boards over the next few months and controlling all 6 AC units in my house.

  • ESP8266 RTC memory

    Shoebox09/06/2016 at 13:51 1 comment

    Quick update, I've revised my new version of the code to use RTC clock memory to save state in preparation for implementing deep sleep. This is hopefully the first step towards increasing battery life. Before I implement this, I need to come up with a mechanism to store and forward MQTT messages to the ESP when it wakes up, I maybe able to trick it with the MQTT retain flag, but need to investigate delivery order, and mitigate the risk of firing off IR commands after every deep sleep, even when no changes have been received. I think it is do-able.

    I used Andreas Spiess Youtube video as a guide on how to use RTC memory. Check it out -

View all 7 project logs

Enjoy this project?

Share

Discussions

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates