Skip to content

Commit a329427

Browse files
Improved MQTT setup and added support for digital switches as sensor input
1 parent ae8657a commit a329427

21 files changed

+244
-32
lines changed

firmware-esp8266.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14-
v33 - Improved MQTT Setup Routine
14+
v33 - Added Digital Sensor Switch Support, Improved MQTT Setup Routine
1515
v32 - Added MQTT Support!
1616
v31 - Fixed an issue with DHT11 Sensors
1717
v30 - Added support for SSD1306 I2C Displays
@@ -30,11 +30,11 @@ Display* display = NULL;
3030
int currentVersion = 33;
3131
boolean printMemory = false;
3232

33-
// String board = "Generic";
34-
// char firmwareType[] = "ESP8266";
33+
String board = "Generic";
34+
char firmwareType[] = "ESP8266";
3535

36-
String board = "NodeMCU";
37-
char firmwareType[] = "ESP8266-NodeMCU";
36+
// String board = "NodeMCU";
37+
// char firmwareType[] = "ESP8266-NodeMCU";
3838

3939
// String board = "ESP12s";
4040
// char firmwareType[] = "ESP8266-ESP12s";

src/communication/Data.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1313
1414
@section HISTORY
15+
v33 - Added Digital Sensor Switch Support
1516
v32 - Added MQTT Support!
1617
v29 - First Public Release
1718
*/
@@ -49,7 +50,13 @@ String Data::getRequestString() {
4950
else if(_type==2)
5051
returnString = returnString + String(_valueInt);
5152
else if(_type==3)
52-
returnString = returnString + String(_valueBoolean);
53+
{
54+
if(_valueBoolean)
55+
returnString = returnString + "true";
56+
else
57+
returnString = returnString + "false";
58+
59+
}
5360

5461
return returnString+","+_unit;
5562

src/communication/MQTT.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Added Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
*/
1617
/**************************************************************************/
@@ -90,15 +91,31 @@ bool MQTT::connect()
9091

9192
void MQTT::publishForAutoDiscovery(Sensor* sensor)
9293
{
93-
String pTopic = "homeassistant/sensor/"+clientId+"/"+String(sensor->getId())+"/config";
94+
String pTopic;
95+
96+
if(sensor->isBinary())
97+
{
98+
pTopic = "homeassistant/binary_sensor/"+clientId+"/"+String(sensor->getId())+"/config";
99+
}
100+
else
101+
pTopic = "homeassistant/sensor/"+clientId+"/"+String(sensor->getId())+"/config";
102+
94103
String category = sensor->getCategory();
95104
String pPayload;
96105

97106
if(category==NULL)
98107
category = "Unnamed";
99108

100-
if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="")
101-
pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}";
109+
if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="" || sensor->getMqttClass()=="raw")
110+
{
111+
pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\"";
112+
if(sensor->isBinary())
113+
pPayload = pPayload + ", \"payload_on\": \"1\", \"payload_off\": \"0\"}";
114+
else
115+
{
116+
pPayload = pPayload + ", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}";
117+
}
118+
}
102119
else
103120
pPayload = "{\"name\": \""+sensor->getName()+"\", \"device_class\": \""+sensor->getMqttClass()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\", \"unit_of_measurement\": \""+sensor->getMqttUnit()+"\"}";
104121

src/controller/Bridge.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Added Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
v29 - First Public Release
1617
*/
@@ -707,7 +708,7 @@ void configurePort(int portNumber, JsonObject& portConfig) {
707708

708709
Serial.println("Configure Onboard Port:" + port);
709710

710-
// portConfig.prettyPrintTo(Serial);
711+
portConfig.prettyPrintTo(Serial);
711712

712713
SensorCalculation* calc = NULL;
713714

@@ -777,6 +778,15 @@ void configurePort(int portNumber, JsonObject& portConfig) {
777778
addSensor(new SensorAnalogue (portConfig["id"], portConfig["c"], portConfig["sn"], portConfig["n"], 0, refreshInterval, postDataInterval, portConfig["s"]["svt"], calc));
778779
}
779780
}
781+
else
782+
{
783+
uint8_t intPort = translateGPIOPort(port);
784+
if(intPort!=-1)
785+
{
786+
Serial.println("Setting up Digital Switch at Port: " + port);
787+
addSensor(new SensorDigitalSwitch(portConfig["id"], portConfig["c"], portConfig["sn"], portConfig["n"], intPort, refreshInterval, postDataInterval, calc));
788+
}
789+
}
780790
}
781791

782792
void addSensor(Sensor *sensor)

src/controller/Bridge.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Added Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
v29 - First Public Release
1617
*/
@@ -28,6 +29,7 @@
2829
#include "../communication/MQTT.h"
2930
#include "../input/Sensor.h"
3031
#include "../input/analog/SensorAnalogue.h"
32+
#include "../input/SensorDigitalSwitch.h"
3133
#include "../input/i2c/Ads1x15.h"
3234
#include "../input/i2c/SensorBMx280.h"
3335
#include "../input/i2c/SensorBME680.h"

src/input/Sensor.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Changes for Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
v29 - First Public Release
1617
*/
@@ -20,7 +21,7 @@
2021

2122
extern unsigned long nextSensorDue;
2223

23-
Sensor::Sensor (long id, String category, String shortName, String name, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation) {
24+
Sensor::Sensor (long id, String category, String shortName, String name, int refreshInterval, int postDataInterval, float smartValueThreshold, SensorCalculation* calculation, bool binary) {
2425
lastTick = -1;
2526
lastPost = -1;
2627
_refreshInterval = refreshInterval;
@@ -31,6 +32,7 @@ Sensor::Sensor (long id, String category, String shortName, String name, int ref
3132
_id = id;
3233
_calculation = calculation;
3334
_smartValueThreshold = smartValueThreshold;
35+
_binary = binary;
3436
}
3537

3638
int Sensor::getRefreshInterval() {
@@ -68,6 +70,10 @@ String Sensor::getMqttUnit() {
6870
return _calculation->getValueUnit();
6971
}
7072

73+
bool Sensor::isBinary() {
74+
return _binary;
75+
}
76+
7177
long Sensor::getId() {
7278
return _id;
7379
}

src/input/Sensor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Changes for Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
v29 - First Public Release
1617
*/
@@ -38,10 +39,11 @@ class Sensor {
3839
String _shortName;
3940
long _id;
4041
SensorCalculation* _calculation;
42+
bool _binary;
4143
virtual void preCycle(int);
4244
virtual Data* read(bool);
4345
public:
44-
Sensor (long, String, String, String, int, int, float, SensorCalculation*);
46+
Sensor (long, String, String, String, int, int, float, SensorCalculation*, bool binary);
4547
int getRefreshInterval(void);
4648
int getPostDataInterval(void);
4749
long getId(void);
@@ -50,6 +52,7 @@ class Sensor {
5052
String getCategory(void);
5153
String getMqttClass(void);
5254
String getMqttUnit(void);
55+
bool isBinary();
5356
Data* trySensorRead(unsigned long, int);
5457
Data* forceSensorRead(unsigned long, int);
5558
};

src/input/SensorCalculation.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Added Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
v29 - First Public Release
1617
*/
@@ -35,6 +36,11 @@ String SensorCalculation::getValueUnit()
3536
return _valueUnit;
3637
}
3738

39+
Data* SensorCalculation::calculate(Sensor* sensor, bool boolValue, bool postData)
40+
{
41+
return NULL;
42+
}
43+
3844
SensorCalculationApproxQuad::SensorCalculationApproxQuad(double calcValue1, double calcValue2, double calcValue3, double calcValue4, int portNumber) : SensorCalculation()
3945
{
4046
_valueType = "temperature";
@@ -165,7 +171,7 @@ SensorCalculationRawToPercent::SensorCalculationRawToPercent(float calcValue1, f
165171
SensorCalculationRaw::SensorCalculationRaw(int portNumber) : SensorCalculation()
166172
{
167173
_valueType = "raw";
168-
_valueUnit = "(raw))";
174+
_valueUnit = "";
169175
_portNumber = portNumber;
170176
}
171177

@@ -353,4 +359,13 @@ Data* SensorCalculationRaw::calculate(Sensor* sensor, float rawValue, bool postD
353359
if(!postData)
354360
return NULL;
355361
return new Data (sensor, rawValue, "UNKNOWN");
362+
}
363+
364+
Data* SensorCalculationRaw::calculate(Sensor* sensor, bool boolValue, bool postData)
365+
{
366+
if(display!=NULL && _portNumber>=0)
367+
display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), boolValue, "ON", "OFF");
368+
if(!postData)
369+
return NULL;
370+
return new Data (sensor, boolValue, "NONE");
356371
}

src/input/SensorCalculation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
1212
1313
@section HISTORY
14+
v33 - Added Digital Sensor Switch Support
1415
v32 - Added MQTT Support!
1516
v29 - First Public Release
1617
*/
@@ -38,6 +39,7 @@ class SensorCalculation {
3839
String getValueUnit();
3940
SensorCalculation();
4041
virtual Data* calculate(Sensor* sensor, float, bool);
42+
virtual Data* calculate(Sensor* sensor, bool, bool);
4143
};
4244

4345
class SensorCalculationApproxQuad : public SensorCalculation {
@@ -148,6 +150,8 @@ class SensorCalculationRaw : public SensorCalculation {
148150
public:
149151
SensorCalculationRaw(int);
150152
Data* calculate(Sensor* sensor, float, bool);
153+
Data* calculate(Sensor* sensor, bool, bool);
151154
};
152155

156+
153157
#endif

src/input/SensorDigitalSwitch.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**************************************************************************/
2+
/*!
3+
@file SensorDigitalSwitch.cpp
4+
@author M. Fegerl (Sensate Digital Solutions GmbH)
5+
@license GPL (see LICENSE file)
6+
The Sensate ESP8266 firmware is used to connect ESP8266 based hardware
7+
with the Sensate Cloud and the Sensate apps.
8+
9+
----> https://www.sensate.io
10+
11+
SOURCE: https://github.com/sensate-io/firmware-esp8266.git
12+
13+
@section HISTORY
14+
v33 - Added Digital Sensor Switch
15+
*/
16+
/**************************************************************************/
17+
18+
#include "SensorDigitalSwitch.h"
19+
20+
extern boolean isResetting;
21+
extern int powerMode;
22+
23+
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) {
24+
25+
pinMode(port, INPUT);
26+
_port = port;
27+
28+
}
29+
30+
void SensorDigitalSwitch::preCycle(int cycleId)
31+
{
32+
}
33+
34+
Data* SensorDigitalSwitch::read(bool shouldPostData)
35+
{
36+
if(!isResetting)
37+
{
38+
bool portState = digitalRead(_port);
39+
if(lastPostedValue!=portState)
40+
shouldPostData = true;
41+
42+
Data *data = _calculation->calculate(this, portState, shouldPostData);
43+
44+
if(shouldPostData)
45+
lastPostedValue = portState;
46+
47+
return data;
48+
}
49+
50+
return NULL;
51+
52+
}
53+
54+
boolean SensorDigitalSwitch::smartSensorCheck(float currentRawValue, float threshhold, boolean shouldPostData)
55+
{
56+
if(powerMode == 3)
57+
{
58+
if(!shouldPostData)
59+
{
60+
if(!isnan(lastPostedValue))
61+
{
62+
if(lastPostedValue-currentRawValue>threshhold|| lastPostedValue-currentRawValue<-threshhold)
63+
{
64+
shouldPostData = true;
65+
}
66+
}
67+
}
68+
69+
if(shouldPostData)
70+
lastPostedValue = currentRawValue;
71+
}
72+
73+
return shouldPostData;
74+
}

0 commit comments

Comments
 (0)