Added basic syslog functionality
This commit is contained in:
@ -24,4 +24,4 @@ board = nodemcu
|
||||
upload_speed=921600
|
||||
|
||||
build_flags = -D PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY
|
||||
lib_deps = https://github.com/homieiot/homie-esp8266.git#develop
|
||||
lib_deps = https://github.com/homieiot/homie-esp8266.git#develop, Syslog
|
||||
|
76
src/main.cpp
76
src/main.cpp
@ -1,9 +1,15 @@
|
||||
#include <Homie.h>
|
||||
#include <Syslog.h>
|
||||
#include <WiFiUdp.h>
|
||||
WiFiUDP udpClient;
|
||||
Syslog syslog(udpClient, SYSLOG_PROTO_IETF);
|
||||
|
||||
const int PIN_RELAY = D1;
|
||||
int timer = 0;
|
||||
HomieNode powerNode("power", "switch");
|
||||
|
||||
|
||||
|
||||
bool powerOnHandler(const HomieRange& range, const String& value) {
|
||||
if (value != "true" && value != "false") return false;
|
||||
bool on = (value == "true");
|
||||
@ -21,7 +27,74 @@ void setupHandler() {
|
||||
powerNode.setProperty("on").send("false");
|
||||
}
|
||||
|
||||
void onHomieEvent(const HomieEvent& event) {
|
||||
switch(event.type) {
|
||||
case HomieEventType::STANDALONE_MODE:
|
||||
// Do whatever you want when standalone mode is started
|
||||
break;
|
||||
case HomieEventType::CONFIGURATION_MODE:
|
||||
// Do whatever you want when configuration mode is started
|
||||
break;
|
||||
case HomieEventType::NORMAL_MODE:
|
||||
// Do whatever you want when normal mode is started
|
||||
break;
|
||||
case HomieEventType::OTA_STARTED:
|
||||
// Do whatever you want when OTA is started
|
||||
break;
|
||||
case HomieEventType::OTA_PROGRESS:
|
||||
// Do whatever you want when OTA is in progress
|
||||
|
||||
// You can use event.sizeDone and event.sizeTotal
|
||||
break;
|
||||
case HomieEventType::OTA_FAILED:
|
||||
// Do whatever you want when OTA is failed
|
||||
break;
|
||||
case HomieEventType::OTA_SUCCESSFUL:
|
||||
// Do whatever you want when OTA is successful
|
||||
break;
|
||||
case HomieEventType::ABOUT_TO_RESET:
|
||||
// Do whatever you want when the device is about to reset
|
||||
break;
|
||||
case HomieEventType::WIFI_CONNECTED:
|
||||
// Do whatever you want when Wi-Fi is connected in normal mode
|
||||
syslog.deviceHostname(Homie.getConfiguration().name);
|
||||
syslog.appName(Interface::get().firmware.name);
|
||||
syslog.defaultPriority(LOG_ERR);
|
||||
syslog.log(LOG_INFO, "WiFi Connected, software version: %s", Interface::get().firmware.version);
|
||||
// You can use event.ip, event.gateway, event.mask
|
||||
break;
|
||||
case HomieEventType::WIFI_DISCONNECTED:
|
||||
// Do whatever you want when Wi-Fi is disconnected in normal mode
|
||||
|
||||
// You can use event.wifiReason
|
||||
break;
|
||||
case HomieEventType::MQTT_READY:
|
||||
// Do whatever you want when MQTT is connected in normal mode
|
||||
syslog.log(LOG_INFO, "MQTT connected");
|
||||
break;
|
||||
case HomieEventType::MQTT_DISCONNECTED:
|
||||
// Do whatever you want when MQTT is disconnected in normal mode
|
||||
syslog.log(LOG_INFO, "MQTT disconnected");
|
||||
// You can use event.mqttReason
|
||||
break;
|
||||
case HomieEventType::MQTT_PACKET_ACKNOWLEDGED:
|
||||
// Do whatever you want when an MQTT packet with QoS > 0 is acknowledged by the broker
|
||||
|
||||
// You can use event.packetId
|
||||
break;
|
||||
case HomieEventType::READY_TO_SLEEP:
|
||||
// After you've called `prepareToSleep()`, the event is triggered when MQTT is disconnected
|
||||
break;
|
||||
case HomieEventType::SENDING_STATISTICS:
|
||||
// Do whatever you want when statistics are sent in normal mode
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
syslog.server("192.168.1.100", 514);
|
||||
|
||||
|
||||
Serial.begin(115200);
|
||||
|
||||
Serial << endl << endl;
|
||||
@ -34,9 +107,10 @@ void setup() {
|
||||
powerNode.advertise("on").settable(powerOnHandler);
|
||||
|
||||
Homie.setSetupFunction(setupHandler);
|
||||
Homie.onEvent(onHomieEvent); // before Homie.setup()
|
||||
Homie.setup();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Homie.loop();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user