From d01c2f2903fb0ad1002d8c85225c403605326e23 Mon Sep 17 00:00:00 2001 From: Dawid Chyrzynski Date: Thu, 7 Jan 2021 23:28:00 +0100 Subject: [PATCH] add NodeMCU example --- README.md | 1 - arduino-home-assistant.ino | 42 ---------------- examples/binary-sensor/binary-sensor.ino | 3 ++ examples/led-switch/led-switch.ino | 2 +- .../mqtt-with-credentials.ino | 2 +- examples/nodemcu/nodemcu.ino | 49 +++++++++++++++++++ examples/sensor/sensor.ino | 2 +- 7 files changed, 55 insertions(+), 46 deletions(-) delete mode 100644 arduino-home-assistant.ino create mode 100644 examples/nodemcu/nodemcu.ino diff --git a/README.md b/README.md index b446d66..e8e4538 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ but I successfully use it on ESP8266/ESP8255 boards in my projects. * [LED switch](examples/led-switch/led-switch.ino) * [MQTT with credentials](examples/mqtt-with-credentials/mqtt-with-credentials.ino) * [Multi-state button](examples/multi-state-button/multi-state-button.ino) -* [RFID tag reader](examples/rfid-tag-reader/rfid-tag-reader.ino) * [Sensor (temperature, humidity, etc.)](examples/sensor/sensor.ino) * [NodeMCU Wi-Fi](examples/nodemcu/nodemcu.ino) diff --git a/arduino-home-assistant.ino b/arduino-home-assistant.ino deleted file mode 100644 index dd3e12f..0000000 --- a/arduino-home-assistant.ino +++ /dev/null @@ -1,42 +0,0 @@ -#include - -#include "src/ArduinoHA.h" - -byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; - -EthernetClient client; -HADevice device("unique-id"); -HAMqtt mqtt("client-id", client, device); -HATriggers triggers(mqtt); - -uint32_t lastTriggerAt = 0; - -void setup() { - Serial.begin(9600); - Ethernet.begin(mac); - - Serial.println("Got IP"); - Serial.println(Ethernet.localIP()); - - //device.setManufacturer("Arduino"); - //device.setModel("Arduino Uno"); - device.setName("My arduino"); - device.setSoftwareVersion("1.0.0"); - - // triggers setup - triggers.add("press", "s1"); - triggers.add("hold", "s1"); - - // switch setup - - mqtt.begin(IPAddress(192,168,0,17)); -} - -void loop() { - mqtt.loop(); - - if ((millis() - lastTriggerAt) >= 3000) { - triggers.trigger("press", "s1"); - lastTriggerAt = millis(); - } -} diff --git a/examples/binary-sensor/binary-sensor.ino b/examples/binary-sensor/binary-sensor.ino index 110fbe2..2e8f668 100644 --- a/examples/binary-sensor/binary-sensor.ino +++ b/examples/binary-sensor/binary-sensor.ino @@ -11,6 +11,9 @@ bool lastInputState = false; EthernetClient client; HADevice device(mac, sizeof(mac)); HAMqtt mqtt(client, device); + +// "input" may be anything you want to be displayed in HA panel +// "door" is device class (based on the class HA displays different icons in the panel) HABinarySensor sensor("input", "door", false, mqtt); void setup() { diff --git a/examples/led-switch/led-switch.ino b/examples/led-switch/led-switch.ino index 2567dcd..1636532 100644 --- a/examples/led-switch/led-switch.ino +++ b/examples/led-switch/led-switch.ino @@ -9,7 +9,7 @@ byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A}; EthernetClient client; HADevice device(mac, sizeof(mac)); HAMqtt mqtt(client, device); -HASwitch led("led", false, mqtt); +HASwitch led("led", false, mqtt); // you can use custom name in place of "led" void onSwitchStateChanged(bool state, HASwitch* s) { diff --git a/examples/mqtt-with-credentials/mqtt-with-credentials.ino b/examples/mqtt-with-credentials/mqtt-with-credentials.ino index 50c015c..363f70f 100644 --- a/examples/mqtt-with-credentials/mqtt-with-credentials.ino +++ b/examples/mqtt-with-credentials/mqtt-with-credentials.ino @@ -11,7 +11,7 @@ byte mac[] = {0x00, 0x10, 0xFA, 0x6E, 0x38, 0x4A}; EthernetClient client; HADevice device(mac, sizeof(mac)); HAMqtt mqtt(client, device); -HASwitch led("led", false, mqtt); +HASwitch led("led", false, mqtt); // you can use custom name in place of "led" void onSwitchStateChanged(bool state, HASwitch* s) { diff --git a/examples/nodemcu/nodemcu.ino b/examples/nodemcu/nodemcu.ino new file mode 100644 index 0000000..16528b3 --- /dev/null +++ b/examples/nodemcu/nodemcu.ino @@ -0,0 +1,49 @@ +#include +#include + +#define LED_PIN D0 +#define BROKER_ADDR IPAddress(192,168,0,17) +#define WIFI_SSID "MyNetwork" +#define WIFI_PASSWORD "MyPassword" + +byte mac[6]; +WiFiClient client; +HADevice device(mac, sizeof(mac)); +HAMqtt mqtt(client, device); +HASwitch led("led", false, mqtt); // you can use custom name in place of "led" + +void onSwitchStateChanged(bool state, HASwitch* s) +{ + digitalWrite(LED_PIN, (state ? HIGH : LOW)); +} + +void setup() { + Serial.begin(9600); + Serial.println("Starting..."); + + WiFi.macAddress(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.onStateChanged(onSwitchStateChanged); + + mqtt.begin(BROKER_ADDR); +} + +void loop() { + mqtt.loop(); +} diff --git a/examples/sensor/sensor.ino b/examples/sensor/sensor.ino index 11489d3..84abe7a 100644 --- a/examples/sensor/sensor.ino +++ b/examples/sensor/sensor.ino @@ -22,7 +22,7 @@ HAMqtt mqtt(client, device); * - double * - float */ -HASensor temp("temp", 0, mqtt); +HASensor temp("temp", 0, mqtt); // you can use custom name in place of "temp" void setup() { // you don't need to verify return status