Skip to content

Commit 751b0b5

Browse files
authored
Add support to pH sensor (#410)
1 parent 660e484 commit 751b0b5

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,9 @@ jobs:
449449
- sed -r -i 's/\/\/(#include <sensors\/SensorFPM10A.h>)/\1/' $SKETCH
450450
- eval $OTA_CONFIGURATION
451451
- eval $COMPILE
452+
- name: "SensorPH"
453+
script:
454+
- sed -r -i 's/\/\/(SensorPH .+)/\1/' $SKETCH
455+
- sed -r -i 's/\/\/(#include <sensors\/SensorPH.h>)/\1/' $SKETCH
456+
- eval $OTA_CONFIGURATION
457+
- eval $COMPILE

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ SensorAPDS9960 | 1 | SparkFun RGB and Gesture Sensor
143143
SensorNeopixel | 1 | Control a Neopixel LED | https://github.com/adafruit/Adafruit_NeoPixel
144144
SensorSDS011 | 2 | SDS011 air quality sensor, return concentrations of 2.5 and 10 micrometer particles. | https://github.com/ricki-z/SDS011
145145
SensorFPM10A | 1 | FPM10A fingerprint sensor | https://github.com/adafruit/Adafruit-Fingerprint-Sensor-Library
146-
146+
SensorPH | 1 | PH ( SKU SEN161 ) sensor, measure the analog value from the amplifier module | -
147147

148148
Those sensors requiring a pin to operate would take it as an argument in the constructor.
149149
NodeManager automatically creates all the child_ids, assigning an incremental counter. If you need to set your own child_id, pass it as the last argument to the constructor
@@ -872,6 +872,16 @@ Each sensor class may expose additional methods.
872872
bool getFingerprintIsValid();
873873
~~~
874874

875+
* SensorPH
876+
~~~c
877+
// setting AnalogRefValue (default: 5.0)
878+
void setVoltageRef(float value);
879+
// setting the voltage value @ph = 7 (default: 2.52)
880+
void setPH7Voltage(float value);
881+
// setting the voltage value @ph = 4 (default: 3.04)
882+
void setPH4Voltage(float value);
883+
~~~
884+
875885
### OTA Configuration
876886
877887
When `NODEMANAGER_OTA_CONFIGURATION` is set to ON the API presented above can be also called remotely through `SensorConfiguration`, which is automatically added to NodeManager. SensorConfiguration exposes by default child id 200 that can be used to interact with the service by sending `V_CUSTOM` type of messages and commands within the payload. For each `REQ` message, the node will respond with a `SET` message if successful.

examples/Template/Template.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ NodeManager. Just uncomment the settings you need and the sensors you want to ad
343343
//#include <sensors/SensorFPM10A.h>
344344
//SensorFPM10A fpm10a(4,5);
345345

346+
//#include <sensors/SensorPH.h>
347+
//SensorPH ph(A0);
348+
346349
/***********************************
347350
* Main Sketch
348351
*/

sensors/SensorPH.h

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* The MySensors Arduino library handles the wireless radio link and protocol
3+
* between your home built sensors/actuators and HA controller of choice.
4+
* The sensors forms a self healing radio network with optional repeaters. Each
5+
* repeater and gateway builds a routing tables in EEPROM which keeps track of the
6+
* network topology allowing messages to be routed to nodes.
7+
*
8+
* Created by Henrik Ekblad <[email protected]>
9+
* Copyright (C) 2013-2017 Sensnology AB
10+
* Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors
11+
*
12+
* Documentation: http://www.mysensors.org
13+
* Support Forum: http://forum.mysensors.org
14+
*
15+
* This program is free software; you can redistribute it and/or
16+
* modify it under the terms of the GNU General Public License
17+
* version 2 as published by the Free Software Foundation.
18+
*/
19+
#ifndef SensorPH_h
20+
#define SensorPH_h
21+
22+
/*
23+
* SensorPH: read the ph from an analog signal amplifier + ph probe
24+
*/
25+
26+
class SensorPH: public Sensor {
27+
protected:
28+
float _voltage_ref = 5.0;
29+
float _ph7_voltage = 2.52;
30+
float _ph4_voltage = 3.04;
31+
float _ph_step;
32+
33+
public:
34+
SensorPH(int8_t pin, uint8_t child_id = 255): Sensor(pin) {
35+
_name = "PH";
36+
children.allocateBlocks(1);
37+
new Child(this,FLOAT,nodeManager.getAvailableChildId(child_id),S_WATER_QUALITY,V_PH,_name);
38+
};
39+
40+
// setter/getter
41+
void setVoltageRef(float value) {
42+
_voltage_ref = value;
43+
};
44+
void setPH7Voltage(float value) {
45+
_ph7_voltage = value;
46+
};
47+
void setPH4Voltage(float value) {
48+
_ph4_voltage = value;
49+
};
50+
51+
// what the sensor should do during setup
52+
void onSetup() {
53+
// set the pin as input
54+
_ph_step = 1 - ( 1 + (_ph7_voltage - _ph4_voltage) / (7 - 4));
55+
pinMode(_pin, INPUT);
56+
};
57+
58+
// what the sensor should do during loop
59+
void onLoop(Child* child) {
60+
int buf[10],temp;
61+
// read the voltage across the amplifier and store 10 measurements
62+
for (int i=0; i<10; i++) {
63+
float adc = analogRead(_pin);
64+
// calculate the ph
65+
double reading = _voltage_ref / 1024.0 * adc;
66+
float ph = 7 + ((_ph7_voltage - reading) / _ph_step);
67+
buf[i]= ph * 1000;
68+
delay(10);
69+
}
70+
// ordering : higher to lower values
71+
for (int i=0; i<9; i++) {
72+
for (int j=i+1; j<10; j++) {
73+
if (buf[i] > buf[j]) {
74+
temp = buf[i];
75+
buf[i] = buf[j];
76+
buf[j] = temp;
77+
}
78+
}
79+
}
80+
// discard the 2 highest and lowest walues and calculate mean of 6 remaining values
81+
unsigned long int avgValue = 0;
82+
float ph = 0;
83+
for (int i=2; i<8; i++) {
84+
avgValue += buf[i];
85+
ph = (float)avgValue/1000/6;
86+
}
87+
child->setValue(ph);
88+
};
89+
};
90+
#endif

sensors/SensorTemplate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SensorExample: public Sensor {
3434

3535
public:
3636
// contructor, takes at least a NodeManager object. Calls its superclass
37-
SensorExample(uint8_t child_id = 255): Sensor(child_id) {
37+
SensorExample(int8_t pin, uint8_t child_id = 255): Sensor(pin) {
3838
// set the sensor name. Useful when reviewing the logs and used as description of the child
3939
_name = "EXAMPLE";
4040
// allocate a fixed number of blocks for the list containing the children

0 commit comments

Comments
 (0)