Permalink
Browse files

Add public access to the logger - close #248

1 parent 14994b3 commit 69dc5bc276027d70aac5216b59dfe43e84feece4 @marvinroger committed Dec 1, 2016
View
@@ -7,3 +7,6 @@ charset = utf-8
indent_style = space
indent_size = 2
trim_trailing_whitespace = true
+
+[keywords.txt]
+indent_style = tab
@@ -1,7 +1,7 @@
#include <Homie.h>
bool broadcastHandler(const String& level, const String& value) {
- Serial << "Received broadcast level " << level << ": " << value << endl;
+ Homie.getLogger() << "Received broadcast level " << level << ": " << value << endl;
return true;
}
@@ -15,7 +15,7 @@ void setupHandler() {
void loopHandler() {
if (millis() - lastTemperatureSent >= temperatureIntervalSetting.get() * 1000UL || lastTemperatureSent == 0) {
float temperature = 22; // Fake temperature here, for the example
- Serial << "Temperature: " << temperature << " °C" << endl;
+ Homie.getLogger() << "Temperature: " << temperature << " °C" << endl;
temperatureNode.setProperty("degrees").send(String(temperature));
lastTemperatureSent = millis();
}
@@ -11,7 +11,7 @@ void loopHandler() {
int doorValue = debouncer.read();
if (doorValue != lastDoorValue) {
- Serial << "Door is now " << (doorValue ? "open" : "close") << endl;
+ Homie.getLogger() << "Door is now " << (doorValue ? "open" : "close") << endl;
doorNode.setProperty("open").send(doorValue ? "true" : "false");
lastDoorValue = doorValue;
@@ -3,7 +3,7 @@
HomieNode lightNode("light", "switch");
bool globalInputHandler(const HomieNode& node, const String& property, const HomieRange& range, const String& value) {
- Serial << "Received on node " << node.getId() << ": " << property << " = " << value << endl;
+ Homie.getLogger() << "Received on node " << node.getId() << ": " << property << " = " << value << endl;
return true;
}
@@ -14,7 +14,7 @@ bool switchOnHandler(const HomieRange& range, const String& value) {
bool on = (value == "true");
digitalWrite(PIN_RELAY, on ? HIGH : LOW);
switchNode.setProperty("on").send(value);
- Serial << "Switch is " << (on ? "on" : "off") << endl;
+ Homie.getLogger() << "Switch is " << (on ? "on" : "off") << endl;
return true;
}
@@ -1,7 +1,7 @@
-/*
+/*
* Tested with "WiFi Smart Socket ESP8266 MQTT"
* and "Sonoff - WiFi Wireless Smart Switch ESP8266 MQTT"
- *
+ *
* The Relay could be toggeled with the physical pushbutton
*/
@@ -18,31 +18,21 @@ byte buttonPressHandled = 0;
HomieNode switchNode("switch", "switch");
bool switchOnHandler(HomieRange range, String value) {
- if (value == "true") {
- digitalWrite(PIN_RELAY, HIGH);
- switchNode.setProperty("on").send("true");
- Serial.println("Switch is on");
- } else if (value == "false") {
- digitalWrite(PIN_RELAY, LOW);
- switchNode.setProperty("on").send("false");
- Serial.println("Switch is off");
- } else {
- return false;
- }
+ if (value != "true" && value != "false") return false;
+
+ bool on = (value == "true");
+ digitalWrite(PIN_RELAY, on ? HIGH : LOW);
+ switchNode.setProperty("on").send(value);
+ Homie.getLogger() << "Switch is " << (on ? "on" : "off") << endl;
return true;
}
void toggleRelay() {
- if (digitalRead(PIN_RELAY) == LOW) {
- digitalWrite(PIN_RELAY, HIGH);
- switchNode.setProperty("on").send("true");
- Serial.println("Switch is on");
- } else {
- digitalWrite(PIN_RELAY, LOW);
- switchNode.setProperty("on").send("false");
- Serial.println("Switch is off");
- }
+ bool on = digitalRead(PIN_RELAY) == HIGH;
+ digitalWrite(PIN_RELAY, on ? LOW : HIGH);
+ switchNode.setProperty("on").send(on ? "false" : "true");
+ Homie.getLogger() << "Switch is " << (on ? "off" : "on") << endl;
}
void loopHandler() {
@@ -16,7 +16,7 @@ bool stripLedHandler(const HomieRange& range, const String& value) {
digitalWrite(LED_PINS[range.index - 1], on ? HIGH : LOW);
stripNode.setProperty("led").setRange(range).send(value); // Update the state of the led
- Serial << "Led " << range.index << " is " << value << endl;
+ Homie.getLogger() << "Led " << range.index << " is " << value << endl;
return true;
}
@@ -10,7 +10,7 @@ bool lightOnHandler(const HomieRange& range, const String& value) {
bool on = (value == "true");
digitalWrite(PIN_RELAY, on ? HIGH : LOW);
lightNode.setProperty("on").send(value);
- Serial << "Light is " << (on ? "on" : "off") << endl;
+ Homie.getLogger() << "Light is " << (on ? "on" : "off") << endl;
return true;
}
@@ -13,7 +13,7 @@ void setupHandler() {
void loopHandler() {
if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL * 1000UL || lastTemperatureSent == 0) {
float temperature = 22; // Fake temperature here, for the example
- Serial << "Temperature: " << temperature << " °C" << endl;
+ Homie.getLogger() << "Temperature: " << temperature << " °C" << endl;
temperatureNode.setProperty("degrees").send(String(temperature));
lastTemperatureSent = millis();
}
View
@@ -37,7 +37,8 @@ setIdle KEYWORD2
isConfigured KEYWORD2
isConnected KEYWORD2
getConfiguration KEYWORD2
-getMqttClient KEYWORD2
+getMqttClient KEYWORD2
+getLogger KEYWORD2
prepareToSleep KEYWORD2
# HomieNode
View
@@ -239,6 +239,10 @@ AsyncMqttClient& HomieClass::getMqttClient() {
return _mqttClient;
}
+Logger& HomieClass::getLogger() {
+ return _logger;
+}
+
void HomieClass::prepareToSleep() {
if (Interface::get().connected) {
Interface::get().flaggedForSleep = true;
View
@@ -56,6 +56,7 @@ class HomieClass {
bool isConnected() const;
const ConfigStruct& getConfiguration() const;
AsyncMqttClient& getMqttClient();
+ Logger& getLogger();
void prepareToSleep();
private:
@@ -3,15 +3,19 @@
#include "Arduino.h"
namespace HomieInternals {
+class HomieClass;
class Logger : public Print {
+ friend HomieClass;
+
public:
Logger();
- void setPrinter(Print* printer);
- void setLogging(bool enable);
virtual size_t write(uint8_t character);
virtual size_t write(const uint8_t* buffer, size_t size);
private:
+ void setPrinter(Print* printer);
+ void setLogging(bool enable);
+
bool _loggingEnabled;
Print* _printer;
};

0 comments on commit 69dc5bc

Please sign in to comment.