Skip to content

Instantly share code, notes, and snippets.

@balloob
Last active July 13, 2023 07:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balloob/daf310faa80112817d6826fbe5fc399d to your computer and use it in GitHub Desktop.
Save balloob/daf310faa80112817d6826fbe5fc399d to your computer and use it in GitHub Desktop.
ESP8266 sketch to control a Whynter ARC-110WD portable air conditioner and monitor temperature using MQTT, infrared transmitter, DHT22. For a list of possible IR codes see https://docs.google.com/spreadsheets/d/1dsr4Jh-nzC6xvSKGpLlPBF0NRwvlpyw-ozg8eZU813w/edit#gid=0
#define wifi_ssid "YOUR_WIFI_NAME"
#define wifi_password "YOUR_WIFI_PASSWORD"
#define mqtt_server "MQTT_SERVER"
#define mqtt_user "MQTT_USER"
#define mqtt_password "MQTT_PASS"
#define ac_topic "device/study_room/ac"
#define temperature_topic "device/study_room/temperature"
#define humidity_topic "device/study_room/humidity"
// Get ESP8266 going with Arduino IDE
// - https://github.com/esp8266/Arduino#installing-with-boards-manager
// Required libraries (sketch -> include library -> manage libraries)
// - PubSubClient by Nick 'O Leary
// - IRemoteESP8266
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <IRremoteESP8266.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 14
IRsend irsend(13); //an IR led is connected to GPIO pin 13
#include "config.h"
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if ((char)payload[0] == 'O') {
Serial.println("Sending off command");
irsend.sendWhynter(0xB400AAC, 32);
} else if ((char)payload[0] == 'F') {
Serial.println("Sending fan high command");
irsend.sendWhynter(0x8B400AA4, 32);
} else if ((char)payload[0] == 'C') {
Serial.println("Sending cool high - 19C command");
irsend.sendWhynter(0x86000AAD, 32);
}
}
WiFiClient espClient;
PubSubClient client(mqtt_server, 1883, callback, espClient);
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
long lastMsg = 0;
float temp = 0.0;
float hum = 0.0;
bool checkBound(float newValue, float prevValue, float maxDiff) {
return !isnan(newValue) &&
(newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
}
void setup() {
irsend.begin();
dht.begin();
Serial.begin(115200);
setup_wifi();
}
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
Serial.print("state=");
Serial.println(client.state());
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
Serial.print("state=");
Serial.println(client.state());
client.subscribe(ac_topic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
float newTemp = dht.readTemperature();
float newHum = dht.readHumidity();
if (checkBound(newTemp, temp, 0.5)) {
temp = newTemp;
Serial.print("New temperature:");
Serial.println(String(temp).c_str());
client.publish(temperature_topic, String(temp).c_str(), true);
}
if (checkBound(newHum, hum, 2)) {
hum = newHum;
Serial.print("New humidity:");
Serial.println(String(hum).c_str());
client.publish(humidity_topic, String(hum).c_str(), true);
}
}
}
@JonRob812
Copy link

How did you decode the IR commands? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment