Skip to content

Commit c4fb263

Browse files
Added Generic Analog Sensor Support
1 parent ec95a3b commit c4fb263

File tree

7 files changed

+85
-19
lines changed

7 files changed

+85
-19
lines changed

firmware-esp8266.ino

Lines changed: 2 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Added Digital Sensor Switch Support, Improved MQTT Setup Routine
1516
v32 - Added MQTT Support!
1617
v31 - Fixed an issue with DHT11 Sensors
@@ -27,7 +28,7 @@
2728

2829
Display* display = NULL;
2930

30-
int currentVersion = 33;
31+
int currentVersion = 34;
3132
boolean printMemory = false;
3233

3334
String board = "Generic";

src/communication/MQTT.cpp

Lines changed: 2 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Added Digital Sensor Switch Support
1516
v32 - Added MQTT Support!
1617
*/
@@ -106,7 +107,7 @@ void MQTT::publishForAutoDiscovery(Sensor* sensor)
106107
if(category==NULL)
107108
category = "Unnamed";
108109

109-
if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="" || sensor->getMqttClass()=="raw")
110+
if(sensor->getMqttClass()=="plevel" || sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="" || sensor->getMqttClass()=="raw" || sensor->getMqttClass()=="voltage")
110111
{
111112
pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\"";
112113
if(sensor->isBinary())

src/controller/Bridge.cpp

Lines changed: 9 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Added Digital Sensor Switch Support
1516
v32 - Added MQTT Support!
1617
v29 - First Public Release
@@ -604,6 +605,9 @@ void tryInitMQTT() {
604605

605606
void configureExpansionPort(int portNumber, JsonObject& portConfig) {
606607
Serial.println("Configure Expansion Port: ");
608+
609+
portConfig.prettyPrintTo(Serial);
610+
Serial.println("");
607611

608612
SensorCalculation* calc = NULL;
609613

@@ -641,6 +645,8 @@ void configureExpansionPort(int portNumber, JsonObject& portConfig) {
641645
calc = new SensorCalculationRawToPercent(portConfig["c1"], portConfig["c2"], portNumber);
642646
else if (portConfig["s"]["cf"] == "RAW")
643647
calc = new SensorCalculationRaw(portNumber);
648+
else if (portConfig["s"]["cf"] == "CALC_RAW_VREF")
649+
calc = new SensorCalculationRawToVoltage(portConfig["c1"], portConfig["c2"], portNumber);
644650

645651
if(calc==NULL)
646652
{
@@ -709,6 +715,7 @@ void configurePort(int portNumber, JsonObject& portConfig) {
709715
Serial.println("Configure Onboard Port:" + port);
710716

711717
portConfig.prettyPrintTo(Serial);
718+
Serial.println("");
712719

713720
SensorCalculation* calc = NULL;
714721

@@ -746,6 +753,8 @@ void configurePort(int portNumber, JsonObject& portConfig) {
746753
calc = new SensorCalculationRawToPercent(portConfig["c1"], portConfig["c2"], portNumber);
747754
else if (portConfig["s"]["cf"] == "RAW")
748755
calc = new SensorCalculationRaw(portNumber);
756+
else if (portConfig["s"]["cf"] == "CALC_RAW_VREF")
757+
calc = new SensorCalculationRawToVoltage(portConfig["c1"], portConfig["c2"], portNumber);
749758

750759
if(calc==NULL)
751760
{

src/input/SensorCalculation.cpp

Lines changed: 28 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Added Digital Sensor Switch Support
1516
v32 - Added MQTT Support!
1617
v29 - First Public Release
@@ -161,13 +162,22 @@ SensorCalculationCalcAltitude::SensorCalculationCalcAltitude(int portNumber) : S
161162

162163
SensorCalculationRawToPercent::SensorCalculationRawToPercent(float calcValue1, float calcValue2, int portNumber) : SensorCalculation()
163164
{
164-
_valueType = "humidity";
165+
_valueType = "plevel";
165166
_valueUnit = "%";
166167
_portNumber = portNumber;
167168
_calcValue1 = calcValue1;
168169
_calcValue2 = calcValue2;
169170
}
170171

172+
SensorCalculationRawToVoltage::SensorCalculationRawToVoltage(float calcValue1, float calcValue2, int portNumber) : SensorCalculation()
173+
{
174+
_valueType = "voltage";
175+
_valueUnit = "V";
176+
_portNumber = portNumber;
177+
_calcValue1 = calcValue1;
178+
_calcValue2 = calcValue2;
179+
}
180+
171181
SensorCalculationRaw::SensorCalculationRaw(int portNumber) : SensorCalculation()
172182
{
173183
_valueType = "raw";
@@ -352,17 +362,32 @@ Data* SensorCalculationRawToPercent::calculate(Sensor* sensor, float rawValue, b
352362
return new Data (sensor, percent, "PERCENT");
353363
}
354364

365+
Data* SensorCalculationRawToVoltage::calculate(Sensor* sensor, float rawValue, bool postData)
366+
{
367+
float refVoltage = _calcValue1;
368+
float maxADCValue = _calcValue2;
369+
370+
float rawVoltage = rawValue / maxADCValue;
371+
float vResult = rawVoltage * refVoltage;
372+
373+
if(display!=NULL && _portNumber>=0)
374+
display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), vResult, _valueUnit);
375+
if(!postData)
376+
return NULL;
377+
return new Data (sensor, vResult, "VOLT");
378+
}
379+
355380
Data* SensorCalculationRaw::calculate(Sensor* sensor, float rawValue, bool postData)
356381
{
357382
if(display!=NULL && _portNumber>=0)
358383
display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), rawValue, _valueUnit);
359384
if(!postData)
360385
return NULL;
361-
return new Data (sensor, rawValue, "UNKNOWN");
386+
return new Data (sensor, rawValue, "NONE");
362387
}
363388

364389
Data* SensorCalculationRaw::calculate(Sensor* sensor, bool boolValue, bool postData)
365-
{
390+
{
366391
if(display!=NULL && _portNumber>=0)
367392
display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), boolValue, "ON", "OFF");
368393
if(!postData)

src/input/SensorCalculation.h

Lines changed: 9 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Added Digital Sensor Switch Support
1516
v32 - Added MQTT Support!
1617
v29 - First Public Release
@@ -153,5 +154,13 @@ class SensorCalculationRaw : public SensorCalculation {
153154
Data* calculate(Sensor* sensor, bool, bool);
154155
};
155156

157+
class SensorCalculationRawToVoltage : public SensorCalculation {
158+
private:
159+
float _calcValue1,_calcValue2;
160+
public:
161+
SensorCalculationRawToVoltage(float, float, int);
162+
Data* calculate(Sensor* sensor, float, bool);
163+
};
164+
156165

157166
#endif

src/input/analog/SensorAnalogue.cpp

Lines changed: 4 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Changes for Digital Sensor Switch Support
1516
v29 - First Public Release
1617
v32 - Added MQTT Support!
@@ -44,16 +45,16 @@ Data* SensorAnalogue::read(bool shouldPostData)
4445

4546
for (int i = 0; i < 10; i++)
4647
{
47-
adc = adc + (float) analogRead(0); // ADC = A0
48+
adc = adc + (float) analogRead(0); // ADC = A0
4849
}
4950

50-
adc = adc / (float)10;
51+
adc = adc / 10.0;
5152

5253
if(!std::isinf(adc))
5354
{
5455
if(_rSplit!=0)
5556
{
56-
double rT = ((double) adc)*_rSplit/(1024-adc);
57+
double rT = ((double) adc)*_rSplit/(1023-adc);
5758
shouldPostData = smartSensorCheck(rT, _smartValueThreshold, shouldPostData);
5859
return _calculation->calculate(this, (float)rT, shouldPostData);
5960
}

src/input/i2c/Ads1x15.cpp

Lines changed: 31 additions & 11 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+
v34 - Added Generic Analog Sensor Support
1415
v33 - Changes for Digital Sensor Switch Support
1516
v32 - Added MQTT Support!
1617
v29 - First Public Release
@@ -41,8 +42,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
4142
if (!init48)
4243
{
4344
init48 = true;
44-
ads1x15_48 = new Adafruit_ADS1015(0x48);
45-
ads1x15_48->setGain(GAIN_ONE);
45+
46+
if(type == "ADS1115")
47+
ads1x15_48 = new Adafruit_ADS1115(0x48);
48+
else
49+
ads1x15_48 = new Adafruit_ADS1015(0x48);
50+
51+
ads1x15_48->setGain(GAIN_TWOTHIRDS);
4652
ads1x15_48->begin();
4753
}
4854
ads1x15 = ads1x15_48;
@@ -51,8 +57,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
5157
if (!init49)
5258
{
5359
init49 = true;
54-
ads1x15_49 = new Adafruit_ADS1015(0x49);
55-
ads1x15_49->setGain(GAIN_ONE);
60+
61+
if(type == "ADS1115")
62+
ads1x15_48 = new Adafruit_ADS1115(0x49);
63+
else
64+
ads1x15_49 = new Adafruit_ADS1015(0x49);
65+
66+
ads1x15_49->setGain(GAIN_TWOTHIRDS);
5667
ads1x15_49->begin();
5768
}
5869
ads1x15 = ads1x15_49;
@@ -62,8 +73,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
6273
if (!init4A)
6374
{
6475
init4A = true;
65-
ads1x15_4A = new Adafruit_ADS1015(0x4A);
66-
ads1x15_4A->setGain(GAIN_ONE);
76+
77+
if(type == "ADS1115")
78+
ads1x15_48 = new Adafruit_ADS1115(0x4A);
79+
else
80+
ads1x15_4A = new Adafruit_ADS1015(0x4A);
81+
82+
ads1x15_4A->setGain(GAIN_TWOTHIRDS);
6783
ads1x15_4A->begin();
6884
}
6985
ads1x15 = ads1x15_4A;
@@ -72,8 +88,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
7288
if (!init4B)
7389
{
7490
init4B = true;
75-
ads1x15_4B = new Adafruit_ADS1015(0x4B);
76-
ads1x15_4B->setGain(GAIN_ONE);
91+
92+
if(type == "ADS1115")
93+
ads1x15_48 = new Adafruit_ADS1115(0x4B);
94+
else
95+
ads1x15_4B = new Adafruit_ADS1015(0x4B);
96+
97+
ads1x15_4B->setGain(GAIN_TWOTHIRDS);
7798
ads1x15_4B->begin();
7899
}
79100
ads1x15 = ads1x15_4B;
@@ -93,7 +114,7 @@ Data* Ads1x15::read(bool shouldPostData)
93114
for (int i = 0; i < numberOfSamples; i++)
94115
{
95116
adcMax = adcMax + (float) ads1x15->readADC_SingleEnded(0);
96-
117+
97118
if (_channel == 1)
98119
adc = adc + (float) ads1x15->readADC_SingleEnded(1);
99120
if (_channel == 2)
@@ -108,9 +129,8 @@ Data* Ads1x15::read(bool shouldPostData)
108129
if (adc >= adcMax-20)
109130
adc = adcMax;
110131

111-
if(_preResistor!=0 && _postResistor!=0)
132+
if((_preResistor!=0 && _postResistor!=0) || (_preResistor==0 && _postResistor==0))
112133
{
113-
adc = adc/adcMax*1024;
114134
shouldPostData = smartSensorCheck(adc, _smartValueThreshold, shouldPostData);
115135
return _calculation->calculate(this, adc, shouldPostData);
116136
}

0 commit comments

Comments
 (0)