Use a ESP8266 to control your A/C via MQTT

We had some very (very!) hot days here in Sydney, up to 40 DegC over several days. After a day in the office, you open the door to your apartment and… 50 DegC 🙁 I looked for a solution to cool down home right after i left the office – with my phone – using MQTT 😉

Hardware

I am using three components for my project. The MCU, an adapter board and the IR-LED

NodeMCU

  • ESP8266
  • 4Mbyte ROM
  • On-board USB/UART

Grove Base Shield for NodeMCU

  • 5 Digital connectors
  • 2 I2C sockets
  • 1 Analog connectors (A0)
  • Power indicator LED

ShopWiki

Grove – Infrared Emitter

  • Voltage: 3.3-5V
  • Distance:10m

Shop | Wiki

 

Software

 

The Basics

The project is based on the Arduino core for ESP8266 and uses several additional libraries. I am using the platform.io IDE for development. The advantages are the fully integrated build and deployment process (via USB or over WiFi), the library manager and the code completion and linting. You can download my complete platform.io project here.

You’ll also need an MQTT server like RabbitMQ to send commands and receive the status. If you are familiar with Docker, you should read my previous post Dockerize RabbitMQ to get a server up and running in no time. If not: There are several binary pages on the RabbitMQ Website.

Basic structure:

  1. Connection to the WiFi
    • Managing OTA updates
  2. Connection to the MQTT Server
    • Subscribing to a command channel
    • Emitting current state to a status channel
  3. Emitting IR commands to the A/C

HeatpumpIR Library

The HeatpumpIR library is the heart of the project. Its designed to expose a unified interface for several different A/C manufacturers like Daikin, Samsung, Panasonic and other. The library is compatible to Arduino and ESP8266 MCUs.

The usage is really simple, have a look at the example code

This command will power on the A/C, set the mode cooling, the fan to auto, 24 degC and both swings to auto.

You can find a list of possible values here.

If you don’t have a Daikin A/C, have a look into the library and search for your brand. Just change the #include and you should be good to go.

The complete code

Depending on your IDE you have to search for the libraries by yourself. You’ll need PubSubClient, HeatpumpIR and Timer.

Again: It’s easier to install the Platform.io IDE and use my package 🙂

In both cases you have to modify several defines on top of the main.ino, here are the most relevant:

Name Description
IR_SEND_PIN The PIN where the IR LED is connected
WIFI_SSID Your WiFi network name
WIFI_PASS WiFi password
MQTT_HOST Mqtt hostname like mqtt.mydomain.com
MQTT_USER MQTT Username
MQTT_PASS MQTT Password

Example usage

Software uploaded, Hardware connected, WiFi and MQTT connection ok? Good! 🙂 Time to send the first command via MQTT. On MacOS i am using mqtt.fx to connect to my MQTT server. You can of course use another tool, it’s up to you to find and configure them 😉

The software is listening for commands on a specific topic configured via the constant MQTT_COMMAND_CHANNEL. The default configuration is “cmnd/daikin/#“. To set the power on the A/C, you just publish “1” to the topic “cmnd/daikin/power“. The MCU will now send the command via IR and post the current values of every setting (power, mode, fan etc.) as JSON to the topic “stat/daikin

{"power":1,"mode":3, "fan":2,"temp":23,"vswing":0,"hswing":0}

Command overview and value mapping

Command Descr → Value
cmnd/daikin/power POWER_OFF → 0
POWER_ON → 1
cmnd/daikin/mode MODE_AUTO → 1
MODE_HEAT → 2
MODE_COOL → 3
MODE_DRY → 4
MODE_FAN → 5
cmnd/daikin/fan FAN_AUTO → 0
FAN_1 → 1
FAN_2 → 2
FAN_3 → 3
FAN_4 → 4
FAN_5 → 5
cmnd/daikin/temp Numeric value
 ♦ VDIR_AUTO → 0
 ♦ HDIR_AUTO → 0

♦ The swing functionality is not implemented for now – feel free to do it by yourself 😉

Android App anyone?

I’m using a nice Android app called MQTT Dash


Also published on Medium.