Last active
August 2, 2022 18:53
-
-
Save alexberry/56ebc81a625bb9f22964d34b45b01360 to your computer and use it in GitHub Desktop.
Westinghouse 6/ID6 Arduino Home Assistant
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
// https://github.com/dawidchyrzynski/arduino-home-assistant | |
#include <ArduinoHA.h> | |
// https://github.com/crankyoldgit/IRremoteESP8266 | |
#include <IRremoteESP8266.h> | |
#include <IRsend.h> | |
const uint16_t kIrLed = 4; | |
IRsend irsend(kIrLed); | |
#define BROKER_ADDR IPAddress(192,168,0,2) | |
#define BROKER_USERNAME "user" | |
#define BROKER_PASSWORD "password" | |
#define WIFI_SSID "wifi_ssid" | |
#define WIFI_PASSWORD "password123" | |
#define PATTERN_REPEAT 15 | |
WiFiClient client; | |
HADevice device; | |
HAMqtt mqtt(client, device); | |
HAFan fan("ventilation", HAFan::SpeedsFeature); | |
HASwitch fanlight("light", false); | |
void onStateChanged(bool state) { | |
Serial.print("State: "); | |
Serial.println(state); | |
if (state == 0 ) { | |
Serial.println("Fan Off"); | |
irsend.sendSymphony(0xC10, 12, PATTERN_REPEAT); | |
} | |
else { | |
Serial.println("Fan On"); | |
onSpeedChanged(fan.getSpeed()); | |
} | |
} | |
void onSpeedChanged(uint16_t speed) { | |
Serial.print("Speed percentage: "); | |
Serial.println(speed); | |
if (speed == 0) { | |
Serial.println("Speed 0"); | |
irsend.sendSymphony(0xC10, 12, PATTERN_REPEAT); | |
} | |
if (speed > 0 && speed <= 33) { | |
Serial.println("Speed 1"); | |
irsend.sendSymphony(0xC01, 12, PATTERN_REPEAT); | |
if (fan.getState() == 0) fan.setState(1); | |
} | |
if (speed >= 34 && speed <= 66 ) { | |
Serial.println("Speed 2"); | |
irsend.sendSymphony(0xC04, 12, PATTERN_REPEAT); | |
if (fan.getState() == 0) fan.setState(1); | |
} | |
if (speed >= 67) { | |
Serial.println("Speed 3"); | |
irsend.sendSymphony(0xC43, 12, PATTERN_REPEAT); | |
if (fan.getState() == 0) fan.setState(1); | |
} | |
} | |
void onSwitchStateChanged(bool state, HASwitch* s) | |
{ | |
irsend.sendSymphony(0xC08, 12, 3); | |
} | |
void setup() { | |
Serial.begin(9600); | |
irsend.begin(); | |
// Unique ID must be set! | |
byte mac[WL_MAC_ADDR_LENGTH]; | |
WiFi.macAddress(mac); | |
device.setUniqueId(mac, sizeof(mac)); | |
// connect to wifi | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(500); // waiting for the connection | |
} | |
Serial.println(); | |
Serial.println("Connected to the network"); | |
// set device's details (optional) | |
device.setName("BedroomFan"); | |
device.setSoftwareVersion("1.0.0"); | |
// configure fan (optional) | |
fan.setName("Bedroom"); | |
fan.setRetain(true); | |
fan.setSpeedRangeMin(1); | |
fan.setSpeedRangeMax(100); | |
// handle fan states | |
fan.onStateChanged(onStateChanged); | |
fan.onSpeedChanged(onSpeedChanged); | |
// set icon (optional) | |
fanlight.setIcon("mdi:lightbulb"); | |
fanlight.setName("Bedroom fan light"); | |
// handle switch state | |
fanlight.onStateChanged(onSwitchStateChanged); | |
mqtt.begin(BROKER_ADDR, BROKER_USERNAME, BROKER_PASSWORD); | |
} | |
void loop() { | |
mqtt.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment