mirror of
https://github.com/mrcory/arduino-home-assistant.git
synced 2026-08-02 15:24:26 -04:00
* Add support for HVAC (#15) * remove HASwitch::triggerCallback method * fix typo in docs * HASwitch: simplify callback #define * work in progress: HVAC * replace local mqtt instances with singleton * device types optimization * cleanup * finished aux heating and away mode * wip: HVAC * finished HVAC implementation * HVAC implementation * add ifdefs for all HA components * wip: optimization * update readme * update examples * remove unused constructor from HAMqtt * update HAUtils * remove mqtt from constructors, fix compilation warnings * finished HVAC * add HVAC example, add changelog * update changelog, add documentation * Add support for icons in HASwitch and HASensor (#16) * add support for icon in HASwitch * add support for icon in HASensor * update changelog * add support for setting retain flag in HASwitch * Add support for text payload in HASensor (#17) * refactored HASensor * update changelog * fix sensor example * Add support for fans (HAFan) (#18) * finished basic implementation of the HAFan * add retain method to DeviceTypeSerializer * finished HAFan implementation * update HAHVAC * device types cleanup * add HAFan example * update changelog * update readme * update fan example * optimization, add support for ActionFeature in HVAC * Add support for MQTT LWT (#19) * add support for hostname in HAMqtt * add support for LWT and shared availability * minor fixes * added advanced availability example * update examples * update changelog * cleanup & bug fix * bump up version * minor improvements and fixes * update readme * add HAMqtt::onMessage method * Add support for covers (#28) * add HACovers * add cover example, update implementation * disable debug * update mqtt-events example * Add support for setting different prefix for non-discovery topics (#29) * add support for data prefix * cleanup * update comments and logs * update multi-state-button example * rename rexample from mqtt-events to mqtt-advanced * update changelog * update links in changelog * Home Assistant 2021.4.5 - MQTT upgrade (#33) * updae HAFan implementation * change lib version * update changelog * fix setSpeed method * update HAFan example * Separate uniqueId from name (#34) * rename name to uniqueId * remove legacy labels * update examples * rename isMyTopic to compareTopics * update changelog, rename name to uniqueId * Implement onBeforeStateChanged callback in HASwitch (#35) * add low latency mode to HASwitch * add onBeforeStateChanged callback to HASwitch * update readme * rename DEPRECATED macro * update advanced MQTT example
61 lines
1.5 KiB
Arduino
61 lines
1.5 KiB
Arduino
#include <ESP8266WiFi.h>
|
|
#include <ArduinoHA.h>
|
|
|
|
#define LED_PIN D0
|
|
#define BROKER_ADDR IPAddress(192,168,0,17)
|
|
#define WIFI_SSID "MyNetwork"
|
|
#define WIFI_PASSWORD "MyPassword"
|
|
|
|
WiFiClient client;
|
|
HADevice device;
|
|
HAMqtt mqtt(client, device);
|
|
HASwitch led("led", false); // "led" is unique ID of the switch. You should define your own ID.
|
|
|
|
void onBeforeSwitchStateChanged(bool state, HASwitch* s)
|
|
{
|
|
// this callback will be called before publishing new state to HA
|
|
// in some cases there may be delay before onStateChanged is called due to network latency
|
|
}
|
|
|
|
void onSwitchStateChanged(bool state, HASwitch* s)
|
|
{
|
|
digitalWrite(LED_PIN, (state ? HIGH : LOW));
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
Serial.println("Starting...");
|
|
|
|
// Unique ID must be set!
|
|
byte mac[WL_MAC_ADDR_LENGTH];
|
|
WiFi.macAddress(mac);
|
|
device.setUniqueId(mac, sizeof(mac));
|
|
|
|
pinMode(LED_PIN, OUTPUT);
|
|
digitalWrite(LED_PIN, LOW);
|
|
|
|
// 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("NodeMCU");
|
|
device.setSoftwareVersion("1.0.0");
|
|
|
|
// handle switch state
|
|
led.onBeforeStateChanged(onBeforeSwitchStateChanged); // optional
|
|
led.onStateChanged(onSwitchStateChanged);
|
|
led.setName("My LED"); // optional
|
|
|
|
mqtt.begin(BROKER_ADDR);
|
|
}
|
|
|
|
void loop() {
|
|
mqtt.loop();
|
|
}
|