Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to pH sensor #392

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion NodeManager.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ SensorRain | 1 | USE_ANALOG_INPUT | Rain sensor, return the
SensorSoilMoisture | 1 | USE_ANALOG_INPUT | Soil moisture sensor, return the percentage of moisture from an attached analog sensor | -
SensorThermistor | 1 | USE_THERMISTOR | Thermistor sensor, return the temperature based on the attached thermistor | -
SensorML8511 | 1 | USE_ML8511 | ML8511 sensor, return UV intensity | -
SensorACS712 | 1 | USE_ACS712 | ACS712 sensor, measure the current going through the attached module | -
SensorACS712 | 1 | USE_ACS712 | ACS712 sensor, measure the current going through the attached module | -SensorPH | 1 | USE_PH | PH ( SKU SEN161 ) sensor, measure the analog value from the amplifier module | -
SensorDigitalInput | 1 | USE_DIGITAL_INPUT | Generic digital sensor, return a pin's digital value | -
SensorDigitalOutput | 1 | USE_DIGITAL_OUTPUT | Generic digital output sensor, allows setting the digital output of a pin to the requested value | -
SensorRelay | 1 | USE_DIGITAL_OUTPUT | Relay sensor, allows activating the relay | -
Expand Down Expand Up @@ -257,6 +257,7 @@ FEATURE_HOOKING | OFF | allow custom code to be hooked in the ou
//#define USE_THERMISTOR
//#define USE_ML8511
//#define USE_ACS712
//#define USE_PH
//#define USE_DIGITAL_INPUT
//#define USE_DIGITAL_OUTPUT
//#define USE_DHT
Expand Down Expand Up @@ -334,6 +335,7 @@ NodeManager node;
//SensorThermistor thermistor(node,A0);
//SensorML8511 ml8511(node,A0);
//SensorACS712 acs712(node,A0);
//SensorPh ph(node,A0);
//SensorDigitalInput digitalIn(node,6);
//SensorDigitalOutput digitalOut(node,6);
//SensorRelay relay(node,6);
Expand Down
26 changes: 26 additions & 0 deletions NodeManagerLibrary.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,32 @@ class SensorACS712: public Sensor {
};
#endif


#ifdef USE_PH
/*
SensorPh: read the ph from an analog signal amplifier + ph probe
*/
class SensorPh: public Sensor {
public:
SensorPh(NodeManager& node_manager, int pin, int child_id = -255);
// [101] setting AnalogRefValue (default: 5.0)
void setVoltageRef(float value);
// [102] setting the voltage value @ph = 7 (default: 2.52)
void setPH7Voltage(float value);
// [103] setting the voltage value @ph = 4 (default: 3.04)
void setPH4Voltage(float value);
// define what to do at each stage of the sketch
void onSetup();
void onLoop(Child* child);
void onReceive(MyMessage* message);
protected:
float _voltage_ref = 5.0;
float _ph7_voltage = 2.52;
float _ph4_voltage = 3.04;
float _ph_step = 1 - ( 1 + (_ph7_voltage - _ph4_voltage) / (7 - 4));
};
#endif

#ifdef USE_DIGITAL_INPUT
/*
SensorDigitalInput: read the digital input of the configured pin
Expand Down
83 changes: 83 additions & 0 deletions NodeManagerLibrary.ino
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,89 @@ void SensorACS712::onReceive(MyMessage* message) {

#endif


#ifdef USE_PH
/*
SensorPh
*/

// contructor
SensorPh::SensorPh(NodeManager& node_manager, int pin, int child_id): Sensor(node_manager, pin) {
_name = "PH";
children.allocateBlocks(1);
new ChildFloat(this,_node->getAvailableChildId(child_id),S_WATER_QUALITY,V_PH,_name);
}

// setter/getter
void SensorPh::setVoltageRef(float value) {
_voltage_ref = value;
}
void SensorPh::setPH7Voltage(float value) {
_ph7_voltage = value;
}
void SensorPh::setPH4Voltage(float value) {
_ph4_voltage = value;
}

// what to do during setup
void SensorPh::onSetup() {
// set the pin as input
float _ph_step = 1 - ( 1 + (_ph7_voltage - _ph4_voltage) / (7 - 4));
pinMode(_pin, INPUT);
}

// what to do during loop
void SensorPh::onLoop(Child* child) {
int buf[10],temp;
// read the voltage across the amplifier and store 10 measurements
for (int i=0; i<10; i++) {
float adc = analogRead(_pin);
// calculate the ph
double reading = _voltage_ref / 1024.0 * adc;
float ph = 7 + ((_ph7_voltage - reading) / _ph_step);
buf[i]= ph * 1000;
delay(10);
}
// ordering : higher to lower values
for (int i=0; i<9; i++) {
for (int j=i+1; j<10; j++) {
if (buf[i] > buf[j]) {
temp = buf[i];
buf[i] = buf[j];
buf[j] = temp;
}
}
}
// discard the 2 highest and lowest walues and calculate mean of 6 remaining values
unsigned long int avgValue = 0;
float ph = 0;
for (int i=2; i<8; i++) {
avgValue += buf[i];
ph = (float)avgValue/1000/6;
}

#if FEATURE_DEBUG == ON
Serial.print(_name);
Serial.print(F(" I="));
Serial.print(child->getChildId());
Serial.print(F(" ^="));
Serial.print(_ph_step, 3);
Serial.print(F(" PH="));
Serial.println(ph,3);
#endif
//delay(20);
// store the value
((ChildFloat*)child)->setValueFloat(ph);
}

// what to do as the main task when receiving a message
void SensorPh::onReceive(MyMessage* message) {
Child* child = getChild(message->sensor);
if (child == nullptr) return;
if (message->getCommand() == C_REQ && message->type == child->getType()) onLoop(child);
}
#endif

#ifdef USE_DIGITAL_INPUT
/*
SensorDigitalInput
Expand Down