Arduino framework library for controlling Midea home appliances using the UART protocol with communication tracking.
Control is possible with a custom dongle. You can make it yourself according to numerous instructions on the Internet, or you can buy a ready-made one in Tindie Shop, thereby supporting me and my work.
Primary Target: ESP8266 (NodeMCU v2)
Secondary Target: ESP32
Framework: Arduino
This library is optimized for ESP8266 but also supports ESP32. The comprehensive test framework ensures compatibility across both platforms.
A far from complete list of supported brands:
It's simple.
- Create appliance instance of
dudanov::midea::ac::AirConditioner. - Set serial stream interface and communication mode to
9600 8N1. - Add
setup()andloop()methods to the same-named global functions of the project. - Control device via
void control(const Control &control)with optional parameters. - You may optionally add your callback function for receive state changes notifications.
#include <Arduino.h>
#include <Appliance/AirConditioner/AirConditioner.h>
using namespace dudanov::midea::ac;
AirConditioner ac;
// Example how can change work mode easily
static inline void switchMode(Mode mode) {
Control control;
control.mode = mode;
ac.control(control);
}
// Example how can change mode and temp in same time
static inline void changeState(Mode mode, float targetTemp) {
Control control;
control.mode = mode;
control.targetTemp = targetTemp;
ac.control(control);
}
// Example how can change power state
static inline void setPowerState(bool state) {
ac.setPowerState(state);
}
// Example how can change mode to AUTO and set target temp to 25C
static inline void changeAuto25() {
Control control;
control.mode = Mode::MODE_AUTO;
control.targetTemp = 25.0f;
ac.control(control);
}
// Here you may get new properties states
void onStateChange() {
ac.getTargetTemp();
ac.getIndoorTemp();
ac.getMode();
ac.getPreset();
ac.getSwingMode();
ac.getFanMode();
// New v1.2.0: Monitor communication health
Serial.print("Successful frames: ");
Serial.println(ac.getFrameSuccess());
Serial.print("Failed frames: ");
Serial.println(ac.getFrameFailure());
Serial.print("Last success time: ");
Serial.println(ac.getLastSuccessTime());
}
void setup() {
Serial.begin(9600); // set serial baudrate to 9600 8N1
ac.setStream(&Serial); // set stream serial interface
ac.addOnStateCallback(onStateChange); // add callback
ac.setup();
}
void loop() {
ac.loop();
}The library now includes robust communication monitoring to help track the health of your connection:
// Get communication statistics
uint32_t successes = ac.getFrameSuccess(); // Number of successful frame exchanges
uint32_t failures = ac.getFrameFailure(); // Number of failed frame exchanges
uint32_t lastSuccess = ac.getLastSuccessTime(); // Timestamp of last successful communication
// Calculate success rate
float successRate = (successes + failures > 0) ?
(float)successes / (successes + failures) * 100.0f : 0.0f;
Serial.printf("Success rate: %.1f%% (%u/%u)\n",
successRate, successes, successes + failures);These metrics are automatically tracked in the background and help identify:
- Connection reliability issues
- When the last successful communication occurred
- Overall communication health over time
This library includes a comprehensive test framework for safe development and enhancement:
# Complete demo with automatic upload and monitoring
.\demo_complete.bat
# Quick test compilation and upload
.\quick_test.batIf you experience issues with PlatformIO's device monitor (common on Windows):
# Enhanced monitoring script (multiple fallback options)
.\monitor_device.bat
# PowerShell alternative
powershell -ExecutionPolicy Bypass -File monitor_device.ps1.\run_tests.bat# Build and upload demo to ESP8266
pio run -e demo_esp8266 -t upload
# Monitor serial output (if PlatformIO monitor works)
pio device monitor -e demo_esp8266
# Alternative monitoring with specific COM port
pio device monitor --port COM3 --baud 115200 --eol CRLFSee SERIAL_MONITOR_GUIDE.md for comprehensive troubleshooting if monitoring doesn't work.
For detailed testing information, see TESTING.md.
to the following people for their contributions to reverse engineering the UART protocol and source code in the following repositories:
If this project was useful to you, you can buy me a Cup of coffee :)