Skip to content

Commit

Permalink
Improved MQTT setup and added support for digital switches as sensor …
Browse files Browse the repository at this point in the history
…input
  • Loading branch information
Manuel-Sensate committed Jul 21, 2020
1 parent ae8657a commit a329427
Show file tree
Hide file tree
Showing 21 changed files with 244 additions and 32 deletions.
10 changes: 5 additions & 5 deletions firmware-esp8266.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Expand Down
9 changes: 8 additions & 1 deletion src/communication/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;

Expand Down
23 changes: 20 additions & 3 deletions src/communication/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!
*/
/**************************************************************************/
Expand Down Expand Up @@ -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()+"\"}";

Expand Down
12 changes: 11 additions & 1 deletion src/controller/Bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/controller/Bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion src/input/Sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -68,6 +70,10 @@ String Sensor::getMqttUnit() {
return _calculation->getValueUnit();
}

bool Sensor::isBinary() {
return _binary;
}

long Sensor::getId() {
return _id;
}
Expand Down
5 changes: 4 additions & 1 deletion src/input/Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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);
Expand All @@ -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);
};
Expand Down
17 changes: 16 additions & 1 deletion src/input/SensorCalculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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";
Expand Down Expand Up @@ -165,7 +171,7 @@ SensorCalculationRawToPercent::SensorCalculationRawToPercent(float calcValue1, f
SensorCalculationRaw::SensorCalculationRaw(int portNumber) : SensorCalculation()
{
_valueType = "raw";
_valueUnit = "(raw))";
_valueUnit = "";
_portNumber = portNumber;
}

Expand Down Expand Up @@ -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");
}
4 changes: 4 additions & 0 deletions src/input/SensorCalculation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -148,6 +150,8 @@ class SensorCalculationRaw : public SensorCalculation {
public:
SensorCalculationRaw(int);
Data* calculate(Sensor* sensor, float, bool);
Data* calculate(Sensor* sensor, bool, bool);
};


#endif
74 changes: 74 additions & 0 deletions src/input/SensorDigitalSwitch.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
35 changes: 35 additions & 0 deletions src/input/SensorDigitalSwitch.h
Original file line number Diff line number Diff line change
@@ -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
Loading

0 comments on commit a329427

Please sign in to comment.