From a329427305a4dda9432d7405f5216d8bc5509722 Mon Sep 17 00:00:00 2001 From: Manuel-Sensate Date: Tue, 21 Jul 2020 10:21:55 +0200 Subject: [PATCH] Improved MQTT setup and added support for digital switches as sensor input --- firmware-esp8266.ino | 10 ++-- src/communication/Data.cpp | 9 +++- src/communication/MQTT.cpp | 23 +++++++-- src/controller/Bridge.cpp | 12 ++++- src/controller/Bridge.h | 2 + src/input/Sensor.cpp | 8 ++- src/input/Sensor.h | 5 +- src/input/SensorCalculation.cpp | 17 +++++- src/input/SensorCalculation.h | 4 ++ src/input/SensorDigitalSwitch.cpp | 74 +++++++++++++++++++++++++++ src/input/SensorDigitalSwitch.h | 35 +++++++++++++ src/input/analog/SensorAnalogue.cpp | 7 +-- src/input/i2c/Ads1x15.cpp | 3 +- src/input/i2c/SensorBH1750.cpp | 3 +- src/input/i2c/SensorBME680.cpp | 3 +- src/input/i2c/SensorBMx280.cpp | 3 +- src/input/i2c/SensorMax44009.cpp | 3 +- src/input/onewire/SensorDHT.cpp | 3 +- src/input/onewire/SensorDallas.cpp | 3 +- src/output/display/DisplayOLED128.cpp | 43 +++++++++++++--- src/output/display/DisplayOLED128.h | 6 ++- 21 files changed, 244 insertions(+), 32 deletions(-) create mode 100644 src/input/SensorDigitalSwitch.cpp create mode 100644 src/input/SensorDigitalSwitch.h diff --git a/firmware-esp8266.ino b/firmware-esp8266.ino index 46e3b9a..d856db8 100644 --- a/firmware-esp8266.ino +++ b/firmware-esp8266.ino @@ -11,7 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY - v33 - Improved MQTT Setup Routine + v33 - Added Digital Sensor Switch Support, Improved MQTT Setup Routine v32 - Added MQTT Support! v31 - Fixed an issue with DHT11 Sensors v30 - Added support for SSD1306 I2C Displays @@ -30,11 +30,11 @@ Display* display = NULL; int currentVersion = 33; boolean printMemory = false; -// String board = "Generic"; -// char firmwareType[] = "ESP8266"; +String board = "Generic"; +char firmwareType[] = "ESP8266"; -String board = "NodeMCU"; -char firmwareType[] = "ESP8266-NodeMCU"; +// String board = "NodeMCU"; +// char firmwareType[] = "ESP8266-NodeMCU"; // String board = "ESP12s"; // char firmwareType[] = "ESP8266-ESP12s"; diff --git a/src/communication/Data.cpp b/src/communication/Data.cpp index 6c69c5d..a76b98c 100644 --- a/src/communication/Data.cpp +++ b/src/communication/Data.cpp @@ -12,6 +12,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -49,7 +50,13 @@ String Data::getRequestString() { else if(_type==2) returnString = returnString + String(_valueInt); else if(_type==3) - returnString = returnString + String(_valueBoolean); + { + if(_valueBoolean) + returnString = returnString + "true"; + else + returnString = returnString + "false"; + + } return returnString+","+_unit; diff --git a/src/communication/MQTT.cpp b/src/communication/MQTT.cpp index f16808e..3c104ca 100644 --- a/src/communication/MQTT.cpp +++ b/src/communication/MQTT.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v32 - Added MQTT Support! */ /**************************************************************************/ @@ -90,15 +91,31 @@ bool MQTT::connect() void MQTT::publishForAutoDiscovery(Sensor* sensor) { - String pTopic = "homeassistant/sensor/"+clientId+"/"+String(sensor->getId())+"/config"; + String pTopic; + + if(sensor->isBinary()) + { + pTopic = "homeassistant/binary_sensor/"+clientId+"/"+String(sensor->getId())+"/config"; + } + else + pTopic = "homeassistant/sensor/"+clientId+"/"+String(sensor->getId())+"/config"; + String category = sensor->getCategory(); String pPayload; if(category==NULL) category = "Unnamed"; - if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="") - pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}"; + if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="" || sensor->getMqttClass()=="raw") + { + pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\""; + if(sensor->isBinary()) + pPayload = pPayload + ", \"payload_on\": \"1\", \"payload_off\": \"0\"}"; + else + { + pPayload = pPayload + ", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}"; + } + } else pPayload = "{\"name\": \""+sensor->getName()+"\", \"device_class\": \""+sensor->getMqttClass()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}"; diff --git a/src/controller/Bridge.cpp b/src/controller/Bridge.cpp index 40d45b7..e983e7c 100644 --- a/src/controller/Bridge.cpp +++ b/src/controller/Bridge.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -707,7 +708,7 @@ void configurePort(int portNumber, JsonObject& portConfig) { Serial.println("Configure Onboard Port:" + port); - // portConfig.prettyPrintTo(Serial); + portConfig.prettyPrintTo(Serial); SensorCalculation* calc = NULL; @@ -777,6 +778,15 @@ void configurePort(int portNumber, JsonObject& portConfig) { addSensor(new SensorAnalogue (portConfig["id"], portConfig["c"], portConfig["sn"], portConfig["n"], 0, refreshInterval, postDataInterval, portConfig["s"]["svt"], calc)); } } + else + { + uint8_t intPort = translateGPIOPort(port); + if(intPort!=-1) + { + Serial.println("Setting up Digital Switch at Port: " + port); + addSensor(new SensorDigitalSwitch(portConfig["id"], portConfig["c"], portConfig["sn"], portConfig["n"], intPort, refreshInterval, postDataInterval, calc)); + } + } } void addSensor(Sensor *sensor) diff --git a/src/controller/Bridge.h b/src/controller/Bridge.h index b10cf1e..f3c95f2 100644 --- a/src/controller/Bridge.h +++ b/src/controller/Bridge.h @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -28,6 +29,7 @@ #include "../communication/MQTT.h" #include "../input/Sensor.h" #include "../input/analog/SensorAnalogue.h" +#include "../input/SensorDigitalSwitch.h" #include "../input/i2c/Ads1x15.h" #include "../input/i2c/SensorBMx280.h" #include "../input/i2c/SensorBME680.h" diff --git a/src/input/Sensor.cpp b/src/input/Sensor.cpp index 77450cd..281e40c 100644 --- a/src/input/Sensor.cpp +++ b/src/input/Sensor.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -20,7 +21,7 @@ extern unsigned long nextSensorDue; -Sensor::Sensor (long id, String category, String shortName, String name, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) { +Sensor::Sensor (long id, String category, String shortName, String name, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation, bool binary) { lastTick = -1; lastPost = -1; _refreshInterval = refreshInterval; @@ -31,6 +32,7 @@ Sensor::Sensor (long id, String category, String shortName, String name, int ref _id = id; _calculation = calculation; _smartValueThreshold = smartValueThreshold; + _binary = binary; } int Sensor::getRefreshInterval() { @@ -68,6 +70,10 @@ String Sensor::getMqttUnit() { return _calculation->getValueUnit(); } +bool Sensor::isBinary() { + return _binary; +} + long Sensor::getId() { return _id; } diff --git a/src/input/Sensor.h b/src/input/Sensor.h index 99667a3..02b7cdc 100644 --- a/src/input/Sensor.h +++ b/src/input/Sensor.h @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -38,10 +39,11 @@ class Sensor { String _shortName; long _id; SensorCalculation* _calculation; + bool _binary; virtual void preCycle(int); virtual Data* read(bool); public: - Sensor (long, String, String, String, int, int, float, SensorCalculation*); + Sensor (long, String, String, String, int, int, float, SensorCalculation*, bool binary); int getRefreshInterval(void); int getPostDataInterval(void); long getId(void); @@ -50,6 +52,7 @@ class Sensor { String getCategory(void); String getMqttClass(void); String getMqttUnit(void); + bool isBinary(); Data* trySensorRead(unsigned long, int); Data* forceSensorRead(unsigned long, int); }; diff --git a/src/input/SensorCalculation.cpp b/src/input/SensorCalculation.cpp index 6469c79..b20316b 100644 --- a/src/input/SensorCalculation.cpp +++ b/src/input/SensorCalculation.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -35,6 +36,11 @@ String SensorCalculation::getValueUnit() return _valueUnit; } +Data* SensorCalculation::calculate(Sensor* sensor, bool boolValue, bool postData) +{ + return NULL; +} + SensorCalculationApproxQuad::SensorCalculationApproxQuad(double calcValue1, double calcValue2, double calcValue3, double calcValue4, int portNumber) : SensorCalculation() { _valueType = "temperature"; @@ -165,7 +171,7 @@ SensorCalculationRawToPercent::SensorCalculationRawToPercent(float calcValue1, f SensorCalculationRaw::SensorCalculationRaw(int portNumber) : SensorCalculation() { _valueType = "raw"; - _valueUnit = "(raw))"; + _valueUnit = ""; _portNumber = portNumber; } @@ -353,4 +359,13 @@ Data* SensorCalculationRaw::calculate(Sensor* sensor, float rawValue, bool postD if(!postData) return NULL; return new Data (sensor, rawValue, "UNKNOWN"); +} + +Data* SensorCalculationRaw::calculate(Sensor* sensor, bool boolValue, bool postData) +{ + if(display!=NULL && _portNumber>=0) + display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), boolValue, "ON", "OFF"); + if(!postData) + return NULL; + return new Data (sensor, boolValue, "NONE"); } \ No newline at end of file diff --git a/src/input/SensorCalculation.h b/src/input/SensorCalculation.h index 4c4ddad..e32152a 100644 --- a/src/input/SensorCalculation.h +++ b/src/input/SensorCalculation.h @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -38,6 +39,7 @@ class SensorCalculation { String getValueUnit(); SensorCalculation(); virtual Data* calculate(Sensor* sensor, float, bool); + virtual Data* calculate(Sensor* sensor, bool, bool); }; class SensorCalculationApproxQuad : public SensorCalculation { @@ -148,6 +150,8 @@ class SensorCalculationRaw : public SensorCalculation { public: SensorCalculationRaw(int); Data* calculate(Sensor* sensor, float, bool); + Data* calculate(Sensor* sensor, bool, bool); }; + #endif \ No newline at end of file diff --git a/src/input/SensorDigitalSwitch.cpp b/src/input/SensorDigitalSwitch.cpp new file mode 100644 index 0000000..bff7275 --- /dev/null +++ b/src/input/SensorDigitalSwitch.cpp @@ -0,0 +1,74 @@ +/**************************************************************************/ +/*! + @file SensorDigitalSwitch.cpp + @author M. Fegerl (Sensate Digital Solutions GmbH) + @license GPL (see LICENSE file) + The Sensate ESP8266 firmware is used to connect ESP8266 based hardware + with the Sensate Cloud and the Sensate apps. + + ----> https://www.sensate.io + + SOURCE: https://github.com/sensate-io/firmware-esp8266.git + + @section HISTORY + v33 - Added Digital Sensor Switch +*/ +/**************************************************************************/ + +#include "SensorDigitalSwitch.h" + +extern boolean isResetting; +extern int powerMode; + +SensorDigitalSwitch::SensorDigitalSwitch (long id, String category, String shortName, String name, uint8_t port, int refreshInterval, int postDataInterval, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, 0.5, calculation, true) { + + pinMode(port, INPUT); + _port = port; + +} + +void SensorDigitalSwitch::preCycle(int cycleId) +{ +} + +Data* SensorDigitalSwitch::read(bool shouldPostData) +{ + if(!isResetting) + { + bool portState = digitalRead(_port); + if(lastPostedValue!=portState) + shouldPostData = true; + + Data *data = _calculation->calculate(this, portState, shouldPostData); + + if(shouldPostData) + lastPostedValue = portState; + + return data; + } + + return NULL; + +} + +boolean SensorDigitalSwitch::smartSensorCheck(float currentRawValue, float threshhold, boolean shouldPostData) +{ + if(powerMode == 3) + { + if(!shouldPostData) + { + if(!isnan(lastPostedValue)) + { + if(lastPostedValue-currentRawValue>threshhold|| lastPostedValue-currentRawValue<-threshhold) + { + shouldPostData = true; + } + } + } + + if(shouldPostData) + lastPostedValue = currentRawValue; + } + + return shouldPostData; +} \ No newline at end of file diff --git a/src/input/SensorDigitalSwitch.h b/src/input/SensorDigitalSwitch.h new file mode 100644 index 0000000..846392e --- /dev/null +++ b/src/input/SensorDigitalSwitch.h @@ -0,0 +1,35 @@ +/**************************************************************************/ +/*! + @file SensorDigitalSwitch.h + @author M. Fegerl (Sensate Digital Solutions GmbH) + @license GPL (see LICENSE file) + The Sensate ESP8266 firmware is used to connect ESP8266 based hardware + with the Sensate Cloud and the Sensate apps. + + ----> https://www.sensate.io + + SOURCE: https://github.com/sensate-io/firmware-esp8266.git + + @section HISTORY + v33 - Added Digital Sensor Switch +*/ +/**************************************************************************/ + +#ifndef _SensorDigitalSwitch_h_ +#define _SensorDigitalSwitch_h_ + +#include "Sensor.h" + +class SensorDigitalSwitch : public Sensor { + private: + bool lastPostedValue = 0; + uint8_t _port; + protected: + Data* read(bool); + void preCycle(int); + boolean smartSensorCheck(float, float, boolean); + public: + SensorDigitalSwitch (long, String, String, String, uint8_t, int, int, SensorCalculation*); +}; + +#endif \ No newline at end of file diff --git a/src/input/analog/SensorAnalogue.cpp b/src/input/analog/SensorAnalogue.cpp index 7945216..8509f71 100644 --- a/src/input/analog/SensorAnalogue.cpp +++ b/src/input/analog/SensorAnalogue.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v29 - First Public Release v32 - Added MQTT Support! */ @@ -21,11 +22,11 @@ extern boolean isResetting; extern int powerMode; -SensorAnalogue::SensorAnalogue (long id, String category, String shortName, String name, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorAnalogue::SensorAnalogue (long id, String category, String shortName, String name, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { } -SensorAnalogue::SensorAnalogue (long id, String category, String shortName, String name, int rSplit, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorAnalogue::SensorAnalogue (long id, String category, String shortName, String name, int rSplit, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { _rSplit = rSplit; @@ -54,7 +55,7 @@ Data* SensorAnalogue::read(bool shouldPostData) { double rT = ((double) adc)*_rSplit/(1024-adc); shouldPostData = smartSensorCheck(rT, _smartValueThreshold, shouldPostData); - return _calculation->calculate(this, rT, shouldPostData); + return _calculation->calculate(this, (float)rT, shouldPostData); } else { diff --git a/src/input/i2c/Ads1x15.cpp b/src/input/i2c/Ads1x15.cpp index 72c2a95..729951f 100644 --- a/src/input/i2c/Ads1x15.cpp +++ b/src/input/i2c/Ads1x15.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -29,7 +30,7 @@ Adafruit_ADS1015* Ads1x15::ads1x15_4B = NULL; extern int powerMode; -Ads1x15::Ads1x15 (long id, String category, String shortName, String name, String type, String addressString, int channel, int preResistor, int postResistor, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +Ads1x15::Ads1x15 (long id, String category, String shortName, String name, String type, String addressString, int channel, int preResistor, int postResistor, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { _channel = channel; numberOfSamples = 10; diff --git a/src/input/i2c/SensorBH1750.cpp b/src/input/i2c/SensorBH1750.cpp index 0c5168e..6d46548 100644 --- a/src/input/i2c/SensorBH1750.cpp +++ b/src/input/i2c/SensorBH1750.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -23,7 +24,7 @@ extern int powerMode; BH1750* SensorBH1750::bh1750; -SensorBH1750::SensorBH1750 (long id, String category, String shortName, String name, String PortSDA, String PortSCL, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorBH1750::SensorBH1750 (long id, String category, String shortName, String name, String PortSDA, String PortSCL, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { if(bh1750==NULL) { diff --git a/src/input/i2c/SensorBME680.cpp b/src/input/i2c/SensorBME680.cpp index 9842dd8..5ec47d0 100644 --- a/src/input/i2c/SensorBME680.cpp +++ b/src/input/i2c/SensorBME680.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -29,7 +30,7 @@ int SensorBME680::lastReadCycleId = 0; #define BME_MOSI 11 #define BME_CS 10 -SensorBME680::SensorBME680 (long id, String category, String shortName, String name, String PortSDA, String PortSCL, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorBME680::SensorBME680 (long id, String category, String shortName, String name, String PortSDA, String PortSCL, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { if(bme == NULL) { diff --git a/src/input/i2c/SensorBMx280.cpp b/src/input/i2c/SensorBMx280.cpp index 2a1aa9a..173bf31 100644 --- a/src/input/i2c/SensorBMx280.cpp +++ b/src/input/i2c/SensorBMx280.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -25,7 +26,7 @@ extern int powerMode; BME280I2C* SensorBMx280::bme76 = NULL; BME280I2C* SensorBMx280::bme77 = NULL; -SensorBMx280::SensorBMx280 (long id, String category, String shortName, String name, String i2cAdress, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorBMx280::SensorBMx280 (long id, String category, String shortName, String name, String i2cAdress, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); BME280::PresUnit presUnit(BME280::PresUnit_hPa); diff --git a/src/input/i2c/SensorMax44009.cpp b/src/input/i2c/SensorMax44009.cpp index cb1fa4c..da75536 100644 --- a/src/input/i2c/SensorMax44009.cpp +++ b/src/input/i2c/SensorMax44009.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -23,7 +24,7 @@ extern int powerMode; MAX44009* SensorMax44009::max44009; -SensorMax44009::SensorMax44009 (long id, String category, String shortName, String name, String PortSDA, String PortSCL, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorMax44009::SensorMax44009 (long id, String category, String shortName, String name, String PortSDA, String PortSCL, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { int i=0; failedInit = false; diff --git a/src/input/onewire/SensorDHT.cpp b/src/input/onewire/SensorDHT.cpp index 2033e0d..bbee1f3 100644 --- a/src/input/onewire/SensorDHT.cpp +++ b/src/input/onewire/SensorDHT.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v31 - Fixed an issue with DHT11 Sensors v29 - First Public Release @@ -58,7 +59,7 @@ DHT_Unified* SensorDHT::dht16 = NULL; extern boolean isResetting; extern int powerMode; -SensorDHT::SensorDHT (long id, String category, String shortName, String name, String dhtType, uint8_t port, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorDHT::SensorDHT (long id, String category, String shortName, String name, String dhtType, uint8_t port, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { uint8_t type; diff --git a/src/input/onewire/SensorDallas.cpp b/src/input/onewire/SensorDallas.cpp index 4312da9..24d94e8 100644 --- a/src/input/onewire/SensorDallas.cpp +++ b/src/input/onewire/SensorDallas.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Changes for Digital Sensor Switch Support v32 - Added MQTT Support! v29 - First Public Release */ @@ -73,7 +74,7 @@ DallasTemperature* SensorDallas::dallasTemperature16 = NULL; extern boolean isResetting; extern int powerMode; -SensorDallas::SensorDallas (long id, String category, String shortName, String name, uint8_t port, int channel, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation) { +SensorDallas::SensorDallas (long id, String category, String shortName, String name, uint8_t port, int channel, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) : Sensor (id, category, shortName, name, refreshInterval, postDataInterval, smartValueThreshold, calculation, false) { _channel = channel; diff --git a/src/output/display/DisplayOLED128.cpp b/src/output/display/DisplayOLED128.cpp index befb313..d72237c 100644 --- a/src/output/display/DisplayOLED128.cpp +++ b/src/output/display/DisplayOLED128.cpp @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v30 - Added Support for SSD1306 Displays v29 - First Public Release */ @@ -176,25 +177,53 @@ void Display::drawValue(int position, String name, String shortName, float value firstSensorData=false; } + String valueString = String(value) + " " + unit; + + switch(displayMode) + { + case 1: + drawValueQuad(position, name, shortName, valueString); + break; + default: + drawValueClassic(position, name, shortName, valueString); + break; + } + +} + +void Display::drawValue(int position, String name, String shortName, bool value, String onString, String offString) { + + if(firstSensorData) + { + clear(true); + firstSensorData=false; + } + + String boolString; + if(value) + boolString = onString; + else + boolString = offString; + switch(displayMode) { case 1: - drawValueQuad(position, name, shortName, value, unit); + drawValueQuad(position, name, shortName, boolString); break; default: - drawValueClassic(position, name, shortName, value, unit); + drawValueClassic(position, name, shortName, boolString); break; } } -void Display::drawValueClassic(int position, String name, String shortName, float value, String unit) { +void Display::drawValueClassic(int position, String name, String shortName, String valueString) { if(!isResetting && displayEnabled) { - String text = name+": "+value + " "+unit; + String text = name+": "+valueString; if(display->getStringWidth(text)>=128) - text = shortName+": "+value + " "+unit; + text = shortName+": "+valueString; display->setColor(BLACK); display->fillRect(0, position*16, 128, 16); @@ -205,7 +234,7 @@ void Display::drawValueClassic(int position, String name, String shortName, floa } -void Display::drawValueQuad(int position, String name, String shortName, float value, String unit) { +void Display::drawValueQuad(int position, String name, String shortName, String valueString) { if(!isResetting && displayEnabled) { @@ -246,7 +275,7 @@ void Display::drawValueQuad(int position, String name, String shortName, float v display->drawString(x, y, label); - String text = String(value)+" "+unit; + String text = valueString; x = x+(w/2)-(display->getStringWidth(text)/2); display->drawString(x,y+16,text); diff --git a/src/output/display/DisplayOLED128.h b/src/output/display/DisplayOLED128.h index ddc6195..c9d4865 100644 --- a/src/output/display/DisplayOLED128.h +++ b/src/output/display/DisplayOLED128.h @@ -11,6 +11,7 @@ SOURCE: https://github.com/sensate-io/firmware-esp8266.git @section HISTORY + v33 - Added Digital Sensor Switch Support v30 - Added Support for SSD1306 Displays v29 - First Public Release */ @@ -127,8 +128,9 @@ class Display { void drawConnected(bool update); void flip(int rotation); void drawValue(int position, String name, String shortName, float value, String unit); - void drawValueClassic(int position, String name, String shortName, float value, String uit); - void drawValueQuad(int position, String name, String shortName, float value, String unit); + void drawValue(int position, String name, String shortName, bool value, String onString, String offString); + void drawValueClassic(int position, String name, String shortName, String valueString); + void drawValueQuad(int position, String name, String shortName, String valueString); int getType(); };