Skip to content

Commit

Permalink
Added Generic Analog Sensor Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel-Sensate committed Aug 5, 2020
1 parent ec95a3b commit c4fb263
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 19 deletions.
3 changes: 2 additions & 1 deletion firmware-esp8266.ino
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
v34 - Added Generic Analog Sensor Support
v33 - Added Digital Sensor Switch Support, Improved MQTT Setup Routine
v32 - Added MQTT Support!
v31 - Fixed an issue with DHT11 Sensors
Expand All @@ -27,7 +28,7 @@

Display* display = NULL;

int currentVersion = 33;
int currentVersion = 34;
boolean printMemory = false;

String board = "Generic";
Expand Down
3 changes: 2 additions & 1 deletion 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
v34 - Added Generic Analog Sensor Support
v33 - Added Digital Sensor Switch Support
v32 - Added MQTT Support!
*/
Expand Down Expand Up @@ -106,7 +107,7 @@ void MQTT::publishForAutoDiscovery(Sensor* sensor)
if(category==NULL)
category = "Unnamed";

if(sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="" || sensor->getMqttClass()=="raw")
if(sensor->getMqttClass()=="plevel" || sensor->getMqttClass()=="resistance" || sensor->getMqttClass()=="altitude" || sensor->getMqttClass()=="flux" || sensor->getMqttClass()=="" || sensor->getMqttClass()=="raw" || sensor->getMqttClass()=="voltage")
{
pPayload = "{\"name\": \""+sensor->getName()+"\", \"state_topic\": \"Sensate/"+category+"/"+sensor->getName()+"/value\"";
if(sensor->isBinary())
Expand Down
9 changes: 9 additions & 0 deletions 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
v34 - Added Generic Analog Sensor Support
v33 - Added Digital Sensor Switch Support
v32 - Added MQTT Support!
v29 - First Public Release
Expand Down Expand Up @@ -604,6 +605,9 @@ void tryInitMQTT() {

void configureExpansionPort(int portNumber, JsonObject& portConfig) {
Serial.println("Configure Expansion Port: ");

portConfig.prettyPrintTo(Serial);
Serial.println("");

SensorCalculation* calc = NULL;

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

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

portConfig.prettyPrintTo(Serial);
Serial.println("");

SensorCalculation* calc = NULL;

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

if(calc==NULL)
{
Expand Down
31 changes: 28 additions & 3 deletions 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
v34 - Added Generic Analog Sensor Support
v33 - Added Digital Sensor Switch Support
v32 - Added MQTT Support!
v29 - First Public Release
Expand Down Expand Up @@ -161,13 +162,22 @@ SensorCalculationCalcAltitude::SensorCalculationCalcAltitude(int portNumber) : S

SensorCalculationRawToPercent::SensorCalculationRawToPercent(float calcValue1, float calcValue2, int portNumber) : SensorCalculation()
{
_valueType = "humidity";
_valueType = "plevel";
_valueUnit = "%";
_portNumber = portNumber;
_calcValue1 = calcValue1;
_calcValue2 = calcValue2;
}

SensorCalculationRawToVoltage::SensorCalculationRawToVoltage(float calcValue1, float calcValue2, int portNumber) : SensorCalculation()
{
_valueType = "voltage";
_valueUnit = "V";
_portNumber = portNumber;
_calcValue1 = calcValue1;
_calcValue2 = calcValue2;
}

SensorCalculationRaw::SensorCalculationRaw(int portNumber) : SensorCalculation()
{
_valueType = "raw";
Expand Down Expand Up @@ -352,17 +362,32 @@ Data* SensorCalculationRawToPercent::calculate(Sensor* sensor, float rawValue, b
return new Data (sensor, percent, "PERCENT");
}

Data* SensorCalculationRawToVoltage::calculate(Sensor* sensor, float rawValue, bool postData)
{
float refVoltage = _calcValue1;
float maxADCValue = _calcValue2;

float rawVoltage = rawValue / maxADCValue;
float vResult = rawVoltage * refVoltage;

if(display!=NULL && _portNumber>=0)
display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), vResult, _valueUnit);
if(!postData)
return NULL;
return new Data (sensor, vResult, "VOLT");
}

Data* SensorCalculationRaw::calculate(Sensor* sensor, float rawValue, bool postData)
{
if(display!=NULL && _portNumber>=0)
display->drawValue(_portNumber, sensor->getName(), sensor->getShortName(), rawValue, _valueUnit);
if(!postData)
return NULL;
return new Data (sensor, rawValue, "UNKNOWN");
return new Data (sensor, rawValue, "NONE");
}

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)
Expand Down
9 changes: 9 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
v34 - Added Generic Analog Sensor Support
v33 - Added Digital Sensor Switch Support
v32 - Added MQTT Support!
v29 - First Public Release
Expand Down Expand Up @@ -153,5 +154,13 @@ class SensorCalculationRaw : public SensorCalculation {
Data* calculate(Sensor* sensor, bool, bool);
};

class SensorCalculationRawToVoltage : public SensorCalculation {
private:
float _calcValue1,_calcValue2;
public:
SensorCalculationRawToVoltage(float, float, int);
Data* calculate(Sensor* sensor, float, bool);
};


#endif
7 changes: 4 additions & 3 deletions src/input/analog/SensorAnalogue.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
v34 - Added Generic Analog Sensor Support
v33 - Changes for Digital Sensor Switch Support
v29 - First Public Release
v32 - Added MQTT Support!
Expand Down Expand Up @@ -44,16 +45,16 @@ Data* SensorAnalogue::read(bool shouldPostData)

for (int i = 0; i < 10; i++)
{
adc = adc + (float) analogRead(0); // ADC = A0
adc = adc + (float) analogRead(0); // ADC = A0
}

adc = adc / (float)10;
adc = adc / 10.0;

if(!std::isinf(adc))
{
if(_rSplit!=0)
{
double rT = ((double) adc)*_rSplit/(1024-adc);
double rT = ((double) adc)*_rSplit/(1023-adc);
shouldPostData = smartSensorCheck(rT, _smartValueThreshold, shouldPostData);
return _calculation->calculate(this, (float)rT, shouldPostData);
}
Expand Down
42 changes: 31 additions & 11 deletions src/input/i2c/Ads1x15.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
v34 - Added Generic Analog Sensor Support
v33 - Changes for Digital Sensor Switch Support
v32 - Added MQTT Support!
v29 - First Public Release
Expand Down Expand Up @@ -41,8 +42,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
if (!init48)
{
init48 = true;
ads1x15_48 = new Adafruit_ADS1015(0x48);
ads1x15_48->setGain(GAIN_ONE);

if(type == "ADS1115")
ads1x15_48 = new Adafruit_ADS1115(0x48);
else
ads1x15_48 = new Adafruit_ADS1015(0x48);

ads1x15_48->setGain(GAIN_TWOTHIRDS);
ads1x15_48->begin();
}
ads1x15 = ads1x15_48;
Expand All @@ -51,8 +57,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
if (!init49)
{
init49 = true;
ads1x15_49 = new Adafruit_ADS1015(0x49);
ads1x15_49->setGain(GAIN_ONE);

if(type == "ADS1115")
ads1x15_48 = new Adafruit_ADS1115(0x49);
else
ads1x15_49 = new Adafruit_ADS1015(0x49);

ads1x15_49->setGain(GAIN_TWOTHIRDS);
ads1x15_49->begin();
}
ads1x15 = ads1x15_49;
Expand All @@ -62,8 +73,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
if (!init4A)
{
init4A = true;
ads1x15_4A = new Adafruit_ADS1015(0x4A);
ads1x15_4A->setGain(GAIN_ONE);

if(type == "ADS1115")
ads1x15_48 = new Adafruit_ADS1115(0x4A);
else
ads1x15_4A = new Adafruit_ADS1015(0x4A);

ads1x15_4A->setGain(GAIN_TWOTHIRDS);
ads1x15_4A->begin();
}
ads1x15 = ads1x15_4A;
Expand All @@ -72,8 +88,13 @@ Ads1x15::Ads1x15 (long id, String category, String shortName, String name, Strin
if (!init4B)
{
init4B = true;
ads1x15_4B = new Adafruit_ADS1015(0x4B);
ads1x15_4B->setGain(GAIN_ONE);

if(type == "ADS1115")
ads1x15_48 = new Adafruit_ADS1115(0x4B);
else
ads1x15_4B = new Adafruit_ADS1015(0x4B);

ads1x15_4B->setGain(GAIN_TWOTHIRDS);
ads1x15_4B->begin();
}
ads1x15 = ads1x15_4B;
Expand All @@ -93,7 +114,7 @@ Data* Ads1x15::read(bool shouldPostData)
for (int i = 0; i < numberOfSamples; i++)
{
adcMax = adcMax + (float) ads1x15->readADC_SingleEnded(0);

if (_channel == 1)
adc = adc + (float) ads1x15->readADC_SingleEnded(1);
if (_channel == 2)
Expand All @@ -108,9 +129,8 @@ Data* Ads1x15::read(bool shouldPostData)
if (adc >= adcMax-20)
adc = adcMax;

if(_preResistor!=0 && _postResistor!=0)
if((_preResistor!=0 && _postResistor!=0) || (_preResistor==0 && _postResistor==0))
{
adc = adc/adcMax*1024;
shouldPostData = smartSensorCheck(adc, _smartValueThreshold, shouldPostData);
return _calculation->calculate(this, adc, shouldPostData);
}
Expand Down

0 comments on commit c4fb263

Please sign in to comment.