ESP Haier: Haier Air Conditioner + ESP Home + Wemos D1 mini


I flashed with the haier.h version and I have no more control or temperature I no longer have the original file! help please

problem solved :hugs: known problem

I’m looking for how to activate silent mode! cordially

What was the problem?

@Knol010 I also have a new haier tundra unit with the esp32 unit. I found the correct jst connector:
JST SM04B-GHS-TB

After soldering the male connector on a smd breakout board and connecting it to a esp8266 I can confirm that it works with the Haierv3.h! This leaves the original cable intact.
And remember to update esphome to the latest version, I was running an older version that did not support baud_rate: 0 for the logger.

1 Like

Finally thanks. I’m going to order them!

Hi everybody,

Don’t know if this information will help anybody. But I was able to make a build that is working on ESP32-for-Haier (the one that is on this photos ESP Haier: Haier Air Conditioner + ESP Home + Wemos D1 mini - #103 by Knol010 ) Processor used on this board is ESP32­SOLO­1 which unlike full-scale ESP32 has only one core so normal builds for ESP32 will not work you need to make special single core builds.
This configuration for ESPHome worked for me:

esphome:
  name: haier
  platform: ESP32
  board: esp32dev
  includes:
    - Haierv2.h
  platformio_options:
    platform: [email protected]
    platform_packages: tasmota/framework-arduinoespressif32 @ 3.10006.210420

To Flash it you will need to use these pins:


The port that is used for communication with AC is UART2. I wasn’t able to make it to communicate over the Serial component (by using hardware_uart: UART2 option) but I succeeded by using my custom code based on UARTDevice ESPHome component and this version 2 protocol from albetaCOM https://github.com/albetaCOM/esp-haier/blob/e2524992f9d11af9965d4c6dbb3e2a81981ceae4/Haierv2.h .
I am not a big specialist in ESPHome. Can somebody help me with figuring out why hardware_uart option didn’t work in my case?

2 Likes

Was anyone able to extract binary from “Esp32-for-haier” module?

I have mix of hOn and smartAir2 aiconditioners, i have tested that the hON module (Esp32-for-haier") works with the aircon originaly for smartAir2, but it does not work other way, so I would like to program my own ESP32 (single core) to work with hOn if possible.

Would this aliexpress.com/item/1005003107648348.html or this 10 Sets SH 1.0mm JST 1.25mm ZH 1.5mm PH 2.0 XH 2.54mm 2Pin /3/4/5/6/7/8/10P Male & Female Plug Connector with 100mm length Wire|Connectors| - AliExpress JST 1.25mm pitch 4mm work with the original connector?

I ordered this:

I have these:

image

1 Like

Not the correct ones :see_no_evil:

I ordered this now also. Is it possible to solder it without the Breadbord? I don’t think it’s gonna fit with the breadbord and the esp chip?


Hi I have an Haier AC50S2SG1FA. And dont know which wifi module is for my AC. Also, can I make my own with ESP32. On the photo is the connection to my wifi module. CN4
Thank you

1 Like

I did successfully flashed and connected my nodemcu to my Haier ac but there was a little trick RX and tx if connected on the same as on the PCB motherboard I couldn’t access my AC. Then I switched them around and now it works hopefully some can use this.

1 Like

Hi, is here someone with as50s2f1fa aircon with the new wifi module (black box, hON app, version 4? ) and can provide serial number? I want to try new haier module with old aircon, but I need correct serial number for that.

I managed to get as35s2f1fa-wh working with the new module instead of the old one with usb connector (that is not using actual usb), but for that i had also the new unit with correct SN to identify it.

@Nikola_Jovanoski Can you please send me your SN? Maybe even AC50S2SG1FA would work for, I just need to try in the app.

Update: nevermind, I don’t think anymore it is possible to use newer module with as50s2f1fa, it works with as35s2f1fa thought, maybe it would be possible using the “jumpers” on the board, but i don’t want to mess with that.

I changed from internet provider yesterday and now my AC units doesn’t work anymore. The ESP’s are online and I can see the logfiles. I don’t know the solution. Can someone help me in the right direction?

Maybe you new router block the ESP messages or change the IP for device?

Try remove the esphome integration and you add it again.

1 Like

I did it yesterday and the problem is solved!
Thanks anyway.

Hello,
I had some issues with this, such as changes through home assistant not respecting the current display or purify/health state when there is a change, so i went ahead and modified it to work for me, also cleaned up a few things that didnt seem to be working. Biggest change was, instead of setting each bit individually and trying to remember them all, we use the last received AC status and only modify the bits we need. This fixed purify mode for me and the display, and in my quick testing everything seems to work with this new code. This may fix other issues as well since we just basically echo back the control bits the ac tells us except for the bits we changed to control instead of trying to know what every bit does.

Next big question is, how do I add options for the items not supported by “climate” entities in home assistant in the yaml? Some switches and status for health/purify mode, display on/off, etc would be nice to have in home assistant. We already have the functions to get the status of those Items, but whats the best way to add entities for those in the yaml?

Here is my attached HaierV4.h, once i figure out how to implement custom switches for missing features in the yaml file, adding the missing features such as turbo mode, quiet mode, timers, etc should be easy since we already know how to get them from the AC unit.

/**
* Create by Miguel Ángel López on 20/07/19
* Modified by Alba Prades on 21/07/20
* Modified by Alba Prades on 13/08/20 
* Modified by Alba Prades on 25/08/20: Added fan, dry and swing features
*      Added modes
* Modified by Carlo Pinasco on 25/08/21: Workaround to fix Purify problem and fix deprecated warning messages.
* Modified By IvoH on 1/31/22, read status from AC for every control command, cleanup not working options. 
**/
#define DEBUG

#ifndef HAIER_ESP_HAIER_H
#define HAIER_ESP_HAIER_H

#include "esphome.h"
#include <string>

using namespace esphome;
using namespace esphome::climate;

// Updated read offset

#define MODE_OFFSET 			14
#define MODE_MSK				0xF0
	#define MODE_AUTO       	0x00
	#define MODE_DRY			0x40
	#define MODE_COOL			0x20
	#define MODE_HEAT			0x80
	#define MODE_FAN			0xC0
#define FAN_MSK					0x0F
	#define FAN_LOW	    		0x03
	#define FAN_MID		  		0x02
	#define FAN_HIGH	     	0x01
	#define FAN_AUTO	   		0x05
	
#define HORIZONTAL_SWING_OFFSET		19
	#define HORIZONTAL_SWING_CENTER 		0x00
	#define HORIZONTAL_SWING_MAX_LEFT 		0x03
	#define HORIZONTAL_SWING_LEFT 			0x04
	#define HORIZONTAL_SWING_MAX_RIGHT 		0x06
	#define HORIZONTAL_SWING_RIGHT 			0x05
	#define HORIZONTAL_SWING_AUTO 			0x07
	
#define VERTICAL_SWING_OFFSET			13
	#define VERTICAL_SWING_MAX_UP			0x02
	#define VERTICAL_SWING_UP				0x04
	#define VERTICAL_SWING_CENTER				0x06
	#define VERTICAL_SWING_DOWN				0x08
	#define VERTICAL_SWING_HEALTH_UP			0x01
	#define VERTICAL_SWING_HEALTH_DOWN		0x03
	#define VERTICAL_SWING_AUTO 				0x0C

#define TEMPERATURE_OFFSET   	22

#define STATUS_DATA_OFFSET			17 // Purify/Quiet mode/OnOff/...
	#define POWER_BIT				(0)	
	#define PURIFY_BIT_STATUS				(1)	
	#define QUIET_BIT				(3)	
	#define AUTO_FAN_MAX_BIT		(4)

#define PURIFY_DATA_OFFSET 21 //multiple bits change when purify is enabled, some are here
	#define PURIFY_BIT_CONTROL_1	(2)
	#define PURIFY_BIT_CONTROL_2	(3)

#define DISPLAY_STATUS_OFFSET 16 //display status is here
	#define DISPLAY_BIT				(1)	//if the display is on or off

#define SET_POINT_OFFSET 		12	

// Another byte
	#define SWING        		27
	#define SWING_OFF          	0
	#define SWING_VERTICAL     	1
	#define SWING_HORIZONTAL   	2
	#define SWING_BOTH

	#define LOCK        		28
	#define LOCK_ON     		80
	#define LOCK_OFF    		00

// Updated read offset


#define COMMAND_OFFSET			9
#define RESPONSE_POLL			2
#define CONTROL_COMMAND_OFFSET  11 //where our commands start in the control message

#define CRC_OFFSET(message)		(2 + message[2])

// Control commands
#define CTR_POWER_OFFSET		13
#define CTR_POWER_ON		0x01
#define CTR_POWER_OFF		0x00
	
#define POLY 0xa001


// temperatures supported by AC system
#define MIN_SET_TEMPERATURE 16
#define MAX_SET_TEMPERATURE 30

//if internal temperature is outside of those boundaries, message will be discarded
#define MIN_VALID_INTERNAL_TEMP 10
#define MAX_VALID_INTERNAL_TEMP 50

class Haier : public Climate, public PollingComponent {

private:

    byte lastCRC;
    byte status[47];
	
	byte initialization_1[13] = {0xFF,0xFF,0x0A,0x0,0x0,0x0,0x0,0x0,0x00,0x61,0x00,0x07,0x72};
	byte initialization_2[13] = {0xFF,0xFF,0x08,0x40,0x0,0x0,0x0,0x0,0x0,0x70,0xB8,0x86,0x41};
 	byte poll[15] = {0xFF,0xFF,0x0A,0x40,0x00,0x00,0x00,0x00,0x00,0x01,0x4D,0x01,0x99,0xB3,0xB4};
	byte control_command[25] = {0xFF,0xFF,0x14,0x40,0x00,0x00,0x00,0x00,0x00,0x01,0x60};
	
	bool first_status_received = false;
	byte climate_mode_fan_speed = FAN_AUTO;
	byte fan_mode_fan_speed = FAN_HIGH;

	// Some vars for debuging purposes	
	byte previous_status[47];
	bool previous_status_init = false;
	
	
	// Functions

	void SetHvacModeControl(byte mode)
	{
		control_command[MODE_OFFSET] &= ~MODE_MSK;
		control_command[MODE_OFFSET] |= mode;
	}	
	
	byte GetHvacModeStatus()
	{
		return status[MODE_OFFSET] & MODE_MSK;
	}	
	
	void SetTemperatureSetpointControl(byte temp)
	{ 
		control_command[SET_POINT_OFFSET] = temp;
	}	
	
	byte GetTemperatureSetpointStatus()
	{
		return status[SET_POINT_OFFSET];
	}	
	
	void SetFanSpeedControl(byte fan_mode)
	{
		control_command[MODE_OFFSET] &= ~FAN_MSK;
		control_command[MODE_OFFSET] |= fan_mode;
	}
	
	byte GetFanSpeedStatus()
	{
		return status[MODE_OFFSET] & FAN_MSK;
	}		
	
	void SetHorizontalSwingControl(byte swing_mode)
	{
		control_command[HORIZONTAL_SWING_OFFSET] = swing_mode;
	}
	
	byte GetHorizontalSwingStatus()
	{
		return status[HORIZONTAL_SWING_OFFSET];
	}	
	
	void SetVerticalSwingControl(byte swing_mode)
	{
		control_command[VERTICAL_SWING_OFFSET] = swing_mode;
	}
	
	byte GetVerticalSwingStatus()
	{
		return status[VERTICAL_SWING_OFFSET];
	}	
	
	void SetQuietModeControl(bool quiet_mode)
	{
		byte tmp;
		byte msk;
		
		msk = (0x01 << QUIET_BIT);		
		
		if(quiet_mode == true){
			control_command[STATUS_DATA_OFFSET] |= msk;
		}
		else{
			msk = ~msk;
			control_command[STATUS_DATA_OFFSET] &= msk;
		}
	}
	
	bool GetQuietModeStatus( void )
	{
		bool ret = false;		
		byte tmp;
		byte msk;
		
		msk = (0x01 << QUIET_BIT);
		tmp = status[STATUS_DATA_OFFSET] & msk;
		
		if(tmp != 0) ret = true;
		
		return ret;
	}
	
	
void SetPurifyControl(bool purify_mode)
	{
		byte tmp;
		byte msk;
		
		msk = (0x01 << PURIFY_BIT_STATUS);		
		
		if(purify_mode == true){
			control_command[STATUS_DATA_OFFSET] |= msk;
			msk = (0x01 << PURIFY_BIT_CONTROL_1);	
			control_command[PURIFY_DATA_OFFSET] |= msk;
			msk = (0x01 << PURIFY_BIT_CONTROL_2);	
			control_command[PURIFY_DATA_OFFSET] |= msk;
		}
		else{
			msk = ~msk;
			control_command[STATUS_DATA_OFFSET] &= msk;

			msk = (0x01 << PURIFY_BIT_CONTROL_1);
			msk = ~msk;
			control_command[PURIFY_DATA_OFFSET] &= msk;
			msk = (0x01 << PURIFY_BIT_CONTROL_2);
			msk = ~msk;
			control_command[PURIFY_DATA_OFFSET] &= msk;
		}
	}
	bool GetPurifyStatus( void )
	{
		bool ret = false;		
		byte tmp;
		byte msk;
		
		msk = (0x01 << PURIFY_BIT_STATUS);
		tmp = status[STATUS_DATA_OFFSET] & msk;
		
		if(tmp != 0) ret = true;
		
		return ret;
	}
	
		void SetDisplayStatus(bool display_on)
	{
		byte tmp;
		byte msk;
		
		msk = (0x01 << DISPLAY_BIT);		
		
		if(display_on == true){
			control_command[DISPLAY_STATUS_OFFSET] |= msk;
		}
		else{
			msk = ~msk;
			control_command[DISPLAY_STATUS_OFFSET] &= msk;
		}
	}

	bool GetDisplayStatus( void )
	{
		bool ret = false;		
		byte tmp;
		byte msk;
		
		msk = (0x01 << DISPLAY_BIT);
		tmp = status[DISPLAY_STATUS_OFFSET] & msk;
		
		if(tmp != 0) ret = true;
		
		return ret;
	}
	
	
	void SetPowerControl(bool power_mode)
	{
		byte tmp;
		byte msk;
		
		msk = (0x01 << POWER_BIT);		
		
		if(power_mode == true){
			control_command[STATUS_DATA_OFFSET] |= msk;
		}
		else{
			msk = ~msk;
			control_command[STATUS_DATA_OFFSET] &= msk;
		}
	}
	
	bool GetPowerStatus( void )
	{
		bool ret = false;		
		byte tmp;
		byte msk;
		
		msk = (0x01 << POWER_BIT);
		tmp = status[STATUS_DATA_OFFSET] & msk;
		
		if(tmp != 0) ret = true;
		
		return ret;
	}
	
	
	bool GetFastModeStatus( void )
	{
		bool ret = false;		
		byte tmp;
		byte msk;
		
		msk = (0x01 << AUTO_FAN_MAX_BIT);
		tmp = status[STATUS_DATA_OFFSET] & msk;
		
		if(tmp != 0) ret = true;
		
		return ret;
	}
	
	void SetFastModeControl(bool fast_mode)
	{
		byte tmp;
		byte msk;
		
		msk = (0x01 << AUTO_FAN_MAX_BIT);		
		
		if(fast_mode == true){
			control_command[STATUS_DATA_OFFSET] |= msk;
		}
		else{
			msk = ~msk;
			control_command[STATUS_DATA_OFFSET] &= msk;
		}
	}
	
	
	void CompareStatusByte()
	{
		int i;
		
		if(previous_status_init == false){
			for (i=0;i<sizeof(status);i++){
				previous_status[i] = status[i];
			}
			previous_status_init = true;
		}
		
		for (i=0;i<sizeof(status);i++)
		{
			if(status[i] != previous_status[i]){
				ESP_LOGD("Debug", "Status byte %d: 0x%X --> 0x%X ", i, previous_status[i],status[i]);
			}
			previous_status[i] = status[i];
		}
	}


public:

    Haier() : PollingComponent(5 * 1000) {
        lastCRC = 0;
    }


    
    void setup() override {
        
        Serial.begin(9600);
		Serial.write(initialization_1, sizeof(initialization_1));
        auto raw = getHex(initialization_1, sizeof(initialization_1));
        ESP_LOGD("Haier", "initialization_1: %s ", raw.c_str());
		Serial.write(initialization_2, sizeof(initialization_2));
        raw = getHex(initialization_2, sizeof(initialization_2));
        ESP_LOGD("Haier", "initialization_2: %s ", raw.c_str());
    }

    void loop() override  {
		byte data[47];
        if (Serial.available() > 0) {
			if (Serial.read() != 255) return;
			if (Serial.read() != 255) return;
			
			data[0] = 255;
			data[1] = 255;

            Serial.readBytes(data+2, sizeof(data)-2);
			
			// If is a status response
			if (data[COMMAND_OFFSET] == RESPONSE_POLL) {
				// Update the status frame
				memcpy(status, data, sizeof(status));
				parseStatus();
			}
		}
    }

    void update() override {
        
        Serial.write(poll, sizeof(poll));
        auto raw = getHex(poll, sizeof(poll));
        ESP_LOGD("Haier", "POLL: %s ", raw.c_str());
    }

protected:
    ClimateTraits traits() override {
        auto traits = climate::ClimateTraits();
        traits.set_supported_modes(
        {
            climate::CLIMATE_MODE_OFF,
            climate::CLIMATE_MODE_COOL,
            climate::CLIMATE_MODE_HEAT,
            climate::CLIMATE_MODE_FAN_ONLY,
            climate::CLIMATE_MODE_DRY,
            climate::CLIMATE_MODE_AUTO
        });

        traits.set_supported_fan_modes(
        {
            climate::CLIMATE_FAN_AUTO,
            climate::CLIMATE_FAN_LOW,
            climate::CLIMATE_FAN_MEDIUM,
            climate::CLIMATE_FAN_HIGH,
        });

        traits.set_supported_swing_modes(
        {
            climate::CLIMATE_SWING_OFF,
            climate::CLIMATE_SWING_BOTH,
            climate::CLIMATE_SWING_VERTICAL,
            climate::CLIMATE_SWING_HORIZONTAL
        });

        traits.set_visual_min_temperature(MIN_SET_TEMPERATURE);
        traits.set_visual_max_temperature(MAX_SET_TEMPERATURE);
        traits.set_visual_temperature_step(1.0f);
        traits.set_supports_current_temperature(true);
        return traits;
    }

public:

    void parseStatus() {


        auto raw = getHex(status, sizeof(status));
        ESP_LOGD("Haier", "Readed message ALBA: %s ", raw.c_str());

        byte check = getChecksum(status, sizeof(status));

        if (check != status[CRC_OFFSET(status)]) {
            ESP_LOGW("Haier", "Invalid checksum (%d vs %d)", check, status[CRC_OFFSET(status)]);
            return;
        }

        lastCRC = check;
		//update all statuses in the control part of the message from what we recieve from the status message
		
		for (int i=CONTROL_COMMAND_OFFSET;i<(sizeof(control_command)-3);i++)
		{
			control_command[i] = status[i];
		}

		if(GetHvacModeStatus() == MODE_FAN){
			fan_mode_fan_speed = GetFanSpeedStatus();
		}
		else{
			climate_mode_fan_speed = GetFanSpeedStatus();
		}

        current_temperature = status[TEMPERATURE_OFFSET]/2;
        target_temperature = status[SET_POINT_OFFSET] + 16;

        if(current_temperature < MIN_VALID_INTERNAL_TEMP || current_temperature > MAX_VALID_INTERNAL_TEMP 
            || target_temperature < MIN_SET_TEMPERATURE || target_temperature > MAX_SET_TEMPERATURE){
            ESP_LOGW("Haier", "Invalid temperatures");
            return;
        }

		first_status_received = true;

		#ifdef DEBUG
		ESP_LOGD("Debug", "HVAC Mode = 0x%X", GetHvacModeStatus());
		ESP_LOGD("Debug", "Display status = 0x%X", GetDisplayStatus());
		ESP_LOGD("Debug", "Power Status = 0x%X", GetPowerStatus());
		ESP_LOGD("Debug", "Purify status = 0x%X", GetPurifyStatus());
		ESP_LOGD("Debug", "Quiet mode Status = 0x%X", GetQuietModeStatus());
		ESP_LOGD("Debug", "Fast mode Status = 0x%X", GetFastModeStatus());
		ESP_LOGD("Debug", "Fan speed Status = 0x%X", GetFanSpeedStatus());
		ESP_LOGD("Debug", "Horizontal Swing Status = 0x%X", GetHorizontalSwingStatus());
		ESP_LOGD("Debug", "Vertical Swing Status = 0x%X", GetVerticalSwingStatus());
		ESP_LOGD("Debug", "Set Point Status = 0x%X", GetTemperatureSetpointStatus());
		#endif

		CompareStatusByte();
		
		
		// Update home assistant component
		
        if (GetPowerStatus() == false) {
            mode = CLIMATE_MODE_OFF;
		} else {
			// Check current hvac mode
            switch (GetHvacModeStatus()) {
                case MODE_COOL:
                    mode = CLIMATE_MODE_COOL;
                    break;
                case MODE_HEAT:
                    mode = CLIMATE_MODE_HEAT;
                    break;
                case MODE_DRY:
				    mode = CLIMATE_MODE_DRY;
					break;
				case MODE_FAN:
                    mode = CLIMATE_MODE_FAN_ONLY;
                    break;
                case MODE_AUTO:
                default:
                    mode = CLIMATE_MODE_AUTO;
            }
					
			// Get fan speed
			// If "quiet mode" is set we will read it as "fan low"
			if ( GetQuietModeStatus() == true) {
                fan_mode = CLIMATE_FAN_LOW;
            }
			// If we detect that fast mode is on the we read it as "fan high"
			else if( GetFastModeStatus() == true) {
				fan_mode = CLIMATE_FAN_HIGH;
			}			
			else {				
				// No quiet or fast so we read the actual fan speed.
                switch (GetFanSpeedStatus()) {
                    case FAN_AUTO:
                        fan_mode = CLIMATE_FAN_AUTO;
                        break;
                    case FAN_MID:
                        fan_mode = CLIMATE_FAN_MEDIUM;
                        break;
					case FAN_LOW:
						fan_mode = CLIMATE_FAN_LOW;
                        break;
                    case FAN_HIGH:
                        fan_mode = CLIMATE_FAN_HIGH;
                        break;
                    default:
                        fan_mode = CLIMATE_FAN_AUTO;
						
                }
            }				


			// Check the status of the swings (vertical and horizontal and translate according component configuration
			if( (GetHorizontalSwingStatus() == HORIZONTAL_SWING_AUTO) && (GetVerticalSwingStatus() == VERTICAL_SWING_AUTO) ){
				swing_mode = CLIMATE_SWING_BOTH;				
			}
			else if(GetHorizontalSwingStatus() == HORIZONTAL_SWING_AUTO){
				swing_mode = CLIMATE_SWING_HORIZONTAL;
			}
			else if(GetVerticalSwingStatus() == VERTICAL_SWING_AUTO){
				swing_mode = CLIMATE_SWING_VERTICAL;
			}
			else{
				swing_mode = CLIMATE_SWING_OFF;
			}
		}

        this->publish_state();

    }


    void control(const ClimateCall &call) override {
		
        ClimateMode new_mode;
		bool new_control_cmd = false;
		
		
		ESP_LOGD("Control", "Control call");
		
		if(first_status_received == false){
			ESP_LOGD("Control", "No action, first poll answer not received");
			return;
		}

        if (call.get_mode().has_value()) {
            // User requested mode change
            new_mode = *call.get_mode();
        
			ESP_LOGD("Control", "*call.get_mode() = %d", new_mode);
			
            switch (new_mode) {
                case CLIMATE_MODE_OFF:
					SetPowerControl(false);
					sendData(control_command, sizeof(control_command)); 
                    break;
					
                case CLIMATE_MODE_AUTO:
					SetPowerControl(true);
					SetHvacModeControl(MODE_AUTO);
					SetFanSpeedControl(climate_mode_fan_speed);	
					sendData(control_command, sizeof(control_command));
                    break;
					
                case CLIMATE_MODE_HEAT:	
					SetPowerControl(true);
					SetHvacModeControl(MODE_HEAT);
					SetFanSpeedControl(climate_mode_fan_speed);	
					sendData(control_command, sizeof(control_command));
                    break;
					
                case CLIMATE_MODE_DRY:		
					SetPowerControl(true);
					SetHvacModeControl(MODE_DRY);
					SetFanSpeedControl(climate_mode_fan_speed);				
					sendData(control_command, sizeof(control_command));					
                    break;
					
                case CLIMATE_MODE_FAN_ONLY:				
					SetPowerControl(true);
					SetHvacModeControl(MODE_FAN);
					SetFanSpeedControl(fan_mode_fan_speed); //pick a speed, auto doesnt work in fan only mode				
					sendData(control_command, sizeof(control_command));
                    break;

                case CLIMATE_MODE_COOL:
					SetPowerControl(true);
					SetHvacModeControl(MODE_COOL);
					SetFanSpeedControl(climate_mode_fan_speed);	
					sendData(control_command, sizeof(control_command));
                    break;
            }
            // Publish updated state
            mode = new_mode;
            this->publish_state();
		}
		
		        //Set fan speed
        if (call.get_fan_mode().has_value()) {
            switch(call.get_fan_mode().value()) {
                case CLIMATE_FAN_LOW:
					SetFanSpeedControl(FAN_LOW);
                    break;
                case CLIMATE_FAN_MEDIUM:
					SetFanSpeedControl(FAN_MID);
                    break;
                case CLIMATE_FAN_HIGH:
					SetFanSpeedControl(FAN_HIGH);
                    break;
                case CLIMATE_FAN_AUTO:
                    SetFanSpeedControl(FAN_AUTO);
                    break;
			}
			sendData(control_command, sizeof(control_command)); 
		}

        //Set swing mode
        if (call.get_swing_mode().has_value()){
            switch(call.get_swing_mode().value()) {
                case CLIMATE_SWING_OFF:
					// When not auto we decide to set it to the center
					SetHorizontalSwingControl(HORIZONTAL_SWING_CENTER);
					// When not auto we decide to set it to the center
					SetVerticalSwingControl(VERTICAL_SWING_CENTER);
                    break;
                case CLIMATE_SWING_VERTICAL:
					// When not auto we decide to set it to the center
                    SetHorizontalSwingControl(HORIZONTAL_SWING_CENTER);
					SetVerticalSwingControl(VERTICAL_SWING_AUTO);
                    break;
                case CLIMATE_SWING_HORIZONTAL:
                    SetHorizontalSwingControl(HORIZONTAL_SWING_AUTO);
					// When not auto we decide to set it to the center
					SetVerticalSwingControl(VERTICAL_SWING_CENTER);
                    break;
                case CLIMATE_SWING_BOTH:
                    SetHorizontalSwingControl(HORIZONTAL_SWING_AUTO);
					SetVerticalSwingControl(VERTICAL_SWING_AUTO);
                    break;
			}
			sendData(control_command, sizeof(control_command)); 
        }
		
		
		if (call.get_target_temperature().has_value()) {
		    float temp = *call.get_target_temperature();
			ESP_LOGD("Control", "*call.get_target_temperature() = %f", temp);
			control_command[SET_POINT_OFFSET] = (unsigned int) temp - 16;
			sendData(control_command, sizeof(control_command));			
			target_temperature = temp;
            this->publish_state();
		}
		
		
   }


    void sendData(byte * message, byte size) {
        byte crc_offset = CRC_OFFSET(message);
        byte crc = getChecksum(message, size);
        word crc_16 = crc16(0, &(message[2]), crc_offset-2);
        
        // Updates the crc
        message[crc_offset] = crc;
        message[crc_offset+1] = (crc_16>>8)&0xFF;
        message[crc_offset+2] = crc_16&0xFF;

        Serial.write(message, size);
        auto raw = getHex(message, size);
        ESP_LOGD("Haier", "Message sent: %s  - CRC: %X - CRC16: %X", raw.c_str(), crc, crc_16);

    }

    String getHex(byte * message, byte size) {


        String raw;

        for (int i=0; i < size; i++){
			raw += " " + String(message[i]);

        }
        raw.toUpperCase();

        return raw;


    }

    byte getChecksum(const byte * message, size_t size) {
		byte position = CRC_OFFSET(message);
        byte crc = 0;
        
        if (size < ( position)) {
        	ESP_LOGE("Control", "frame format error (size = %d vs length = %d)", size, message[2]);
        	return 0;
        }

        for (int i = 2; i < position; i++)
            crc += message[i];

        return crc;
    }


    unsigned crc16(unsigned crc, unsigned char *buf, size_t len)
    { 
        while (len--) {
            crc ^= *buf++;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
            crc = crc & 1 ? (crc >> 1) ^ POLY : crc >> 1;
        }
        return crc;
    }


};


#endif //HAIER_ESP_HAIER_H

Hi all,

To save anyone else having the trouble I’ve had most of today - figured I’d register an account and post.

I’m using 3 Haier AC units all running R_1.0.00/e_2.3.12 firmware, originally connected to SmartAir2.

I was having strange issues where my ESP8266 was sending all of the right commands and my ESPHome logs showed polling messages, but for some reason I’d never be able to use the integration as I would hit an error “No action, first poll answer not received” when trying to adjust the state of the A/C.

I spent some time debugging, and realised while sniffing with a UART that upon connecting the RX pin to my ESP8266, I was triggering the over-voltage snap-back circuit, which was grounding out the RX pin.

Basically, I could see response messages from my air-conditioner when only the UART was connected to the serial interface, but as soon as the ESP was connected too - dead silence.

Investigating Instalator’s circuit schematics I could see he was running RX through a series of voltage drop resistors. This wasn’t enough to resolve the issue for me, and I ended up testing with a SN74ahct125n level shifter - immediately started to work, but these are large quad channel units I had laying around and I needed two - one for RX and one for TX.

I’ve since ordered a sparkfun logic level converter with the BSS138 mosfets on board which are bi-directional. A single one of these boards should sort out my issues once and for all.

TL;DR: if you’re having issues with this integration not receiving response messages and you’ve tried a few different Haier.h file versions, you likely need a level shifter like I did.

Hi Everyone, Ive been working on this a little more and ended up rewriting a huge part of the current integration. I now have purify, self clean, steri clean, sleep, boost, quiet, display controls, and remote lock all working. Ive also worked to further the reverse engineering the communication messages.

Testing and feedback welcome. Timers and wifi light control are the main things i know are missing as of now.

4 Likes