mirror of
https://github.com/mrcory/arduino-home-assistant.git
synced 2026-08-02 15:24:26 -04:00
add NodeMCU example
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
#include <Ethernet.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#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"
|
||||
|
||||
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();
|
||||
}
|
||||
@@ -22,7 +22,7 @@ HAMqtt mqtt(client, device);
|
||||
* - double
|
||||
* - float
|
||||
*/
|
||||
HASensor<double> temp("temp", 0, mqtt);
|
||||
HASensor<double> temp("temp", 0, mqtt); // you can use custom name in place of "temp"
|
||||
|
||||
void setup() {
|
||||
// you don't need to verify return status
|
||||
|
||||
Reference in New Issue
Block a user