add RaspberryPi V1.0

This commit is contained in:
chocho2018
2018-11-02 10:31:59 +08:00
parent cd5fe1e162
commit 94184948a8
20 changed files with 785 additions and 172 deletions

View File

@@ -28,11 +28,11 @@
DFRobot_PH::DFRobot_PH()
{
this->_temperature = 25.0;
this->_phValue = 7.0;
this->_acidVoltage = 2032.44; // buffer solution 4.0 at 25C
this->_neutralVoltage = 1500.0; //buffer solution 7.0 at 25C
this->_voltage = 1500.0;
this->_temperature = 25.0;
this->_phValue = 7.0;
this->_acidVoltage = 2032.44; //buffer solution 4.0 at 25C
this->_neutralVoltage = 1500.0; //buffer solution 7.0 at 25C
this->_voltage = 1500.0;
}
DFRobot_PH::~DFRobot_PH()
@@ -45,19 +45,16 @@ void DFRobot_PH::begin()
EEPROM_read(PHVALUEADDR, this->_neutralVoltage); //load the neutral (pH = 7.0)voltage of the pH board from the EEPROM
Serial.print("_neutralVoltage:");
Serial.println(this->_neutralVoltage);
if(EEPROM.read(PHVALUEADDR)==0xFF && EEPROM.read(PHVALUEADDR+1)==0xFF && EEPROM.read(PHVALUEADDR+2)==0xFF && EEPROM.read(PHVALUEADDR+3)==0xFF)
{
this->_neutralVoltage = 1500.0; // new EEPROM, write typical voltage
EEPROM_write(PHVALUEADDR, this->_neutralVoltage);
if(EEPROM.read(PHVALUEADDR)==0xFF && EEPROM.read(PHVALUEADDR+1)==0xFF && EEPROM.read(PHVALUEADDR+2)==0xFF && EEPROM.read(PHVALUEADDR+3)==0xFF){
this->_neutralVoltage = 1500.0; // new EEPROM, write typical voltage
EEPROM_write(PHVALUEADDR, this->_neutralVoltage);
}
EEPROM_read(PHVALUEADDR+4, this->_acidVoltage);//load the acid (pH = 4.0) voltage of the pH board from the EEPROM
Serial.print("_acidVoltage:");
Serial.println(this->_acidVoltage);
if(EEPROM.read(PHVALUEADDR+4)==0xFF && EEPROM.read(PHVALUEADDR+5)==0xFF && EEPROM.read(PHVALUEADDR+6)==0xFF && EEPROM.read(PHVALUEADDR+7)==0xFF)
{
this->_acidVoltage = 2032.44; // new EEPROM, write typical voltage
EEPROM_write(PHVALUEADDR+4, this->_acidVoltage);
if(EEPROM.read(PHVALUEADDR+4)==0xFF && EEPROM.read(PHVALUEADDR+5)==0xFF && EEPROM.read(PHVALUEADDR+6)==0xFF && EEPROM.read(PHVALUEADDR+7)==0xFF){
this->_acidVoltage = 2032.44; // new EEPROM, write typical voltage
EEPROM_write(PHVALUEADDR+4, this->_acidVoltage);
}
}
@@ -74,13 +71,19 @@ float DFRobot_PH::readPH(float voltage, float temperature)
}
void DFRobot_PH::calibration(float voltage, float temperature)
{
void DFRobot_PH::calibration(float voltage, float temperature,char* cmd)
{
this->_voltage = voltage;
this->_temperature = temperature;
if(cmdSerialDataAvailable() > 0)
{
strupr(cmd);
phCalibration(cmdParse(cmd)); // if received Serial CMD from the serial monitor, enter into the calibration mode
}
void DFRobot_PH::calibration(float voltage, float temperature)
{
this->_voltage = voltage;
this->_temperature = temperature;
if(cmdSerialDataAvailable() > 0){
phCalibration(cmdParse()); // if received Serial CMD from the serial monitor, enter into the calibration mode
}
}
@@ -89,112 +92,113 @@ boolean DFRobot_PH::cmdSerialDataAvailable()
{
char cmdReceivedChar;
static unsigned long cmdReceivedTimeOut = millis();
while (Serial.available()>0)
{
if (millis() - cmdReceivedTimeOut > 500U)
{
this->_cmdReceivedBufferIndex = 0;
memset(this->_cmdReceivedBuffer,0,(ReceivedBufferLength));
}
cmdReceivedTimeOut = millis();
cmdReceivedChar = Serial.read();
if (cmdReceivedChar == '\n' || this->_cmdReceivedBufferIndex==ReceivedBufferLength-1){
this->_cmdReceivedBufferIndex = 0;
strupr(this->_cmdReceivedBuffer);
return true;
}else{
this->_cmdReceivedBuffer[this->_cmdReceivedBufferIndex] = cmdReceivedChar;
this->_cmdReceivedBufferIndex++;
}
while(Serial.available()>0){
if(millis() - cmdReceivedTimeOut > 500U){
this->_cmdReceivedBufferIndex = 0;
memset(this->_cmdReceivedBuffer,0,(ReceivedBufferLength));
}
cmdReceivedTimeOut = millis();
cmdReceivedChar = Serial.read();
if (cmdReceivedChar == '\n' || this->_cmdReceivedBufferIndex==ReceivedBufferLength-1){
this->_cmdReceivedBufferIndex = 0;
strupr(this->_cmdReceivedBuffer);
return true;
}else{
this->_cmdReceivedBuffer[this->_cmdReceivedBufferIndex] = cmdReceivedChar;
this->_cmdReceivedBufferIndex++;
}
}
return false;
}
byte DFRobot_PH::cmdParse(const char* cmd)
{
byte modeIndex = 0;
if(strstr(cmd, "ENTERPH") != NULL){
modeIndex = 1;
}else if(strstr(cmd, "EXITPH") != NULL){
modeIndex = 3;
}else if(strstr(cmd, "CALPH") != NULL){
modeIndex = 2;
}
return modeIndex;
}
byte DFRobot_PH::cmdParse()
{
byte modeIndex = 0;
if(strstr(this->_cmdReceivedBuffer, "ENTER") != NULL)
modeIndex = 1;
else if(strstr(this->_cmdReceivedBuffer, "EXIT") != NULL)
modeIndex = 3;
else if(strstr(this->_cmdReceivedBuffer, "CAL") != NULL)
modeIndex = 2;
return modeIndex;
byte modeIndex = 0;
if(strstr(this->_cmdReceivedBuffer, "ENTERPH") != NULL){
modeIndex = 1;
}else if(strstr(this->_cmdReceivedBuffer, "EXITPH") != NULL){
modeIndex = 3;
}else if(strstr(this->_cmdReceivedBuffer, "CALPH") != NULL){
modeIndex = 2;
}
return modeIndex;
}
void DFRobot_PH::phCalibration(byte mode)
{
char *receivedBufferPtr;
static boolean phCalibrationFinish = 0;
static boolean phCalibrationFinish = 0;
static boolean enterCalibrationFlag = 0;
switch(mode)
{
case 0:
if(enterCalibrationFlag)
Serial.println(F(">>>Command Error<<<"));
break;
case 1:
enterCalibrationFlag = 1;
phCalibrationFinish = 0;
Serial.println();
Serial.println(F(">>>Enter Calibration Mode<<<"));
Serial.println(F(">>>Please put the probe into the 4.0 or 7.0 standard buffer solution<<<"));
Serial.println();
break;
case 2:
if(enterCalibrationFlag)
{
if((this->_voltage>1322)&&(this->_voltage<1678)) // buffer solution:7.0
{
Serial.println();
Serial.print(F(">>>Buffer Solution:7.0"));
this->_neutralVoltage = this->_voltage;
Serial.println(F(",Send EXIT to Save and Exit<<<"));
Serial.println();
phCalibrationFinish = 1;
}
else if((this->_voltage>1854)&&(this->_voltage<2210)) //buffer solution:4.0
{
Serial.println();
Serial.print(F(">>>Buffer Solution:4.0"));
this->_acidVoltage = this->_voltage;
Serial.println(F(",Send EXIT to Save and Exit<<<"));
Serial.println();
phCalibrationFinish = 1;
}
else{
Serial.println();
Serial.print(F(">>>Buffer Solution Error<<<"));
Serial.println(); // not buffer solution or faulty operation
phCalibrationFinish = 0;
}
}
break;
switch(mode){
case 0:
if(enterCalibrationFlag){
Serial.println(F(">>>Command Error<<<"));
}
break;
case 1:
enterCalibrationFlag = 1;
phCalibrationFinish = 0;
Serial.println();
Serial.println(F(">>>Enter PH Calibration Mode<<<"));
Serial.println(F(">>>Please put the probe into the 4.0 or 7.0 standard buffer solution<<<"));
Serial.println();
break;
case 2:
if(enterCalibrationFlag){
if((this->_voltage>1322)&&(this->_voltage<1678)){ // buffer solution:7.0{
Serial.println();
Serial.print(F(">>>Buffer Solution:7.0"));
this->_neutralVoltage = this->_voltage;
Serial.println(F(",Send EXITPH to Save and Exit<<<"));
Serial.println();
phCalibrationFinish = 1;
}else if((this->_voltage>1854)&&(this->_voltage<2210)){ //buffer solution:4.0
Serial.println();
Serial.print(F(">>>Buffer Solution:4.0"));
this->_acidVoltage = this->_voltage;
Serial.println(F(",Send EXITPH to Save and Exit<<<"));
Serial.println();
phCalibrationFinish = 1;
}else{
Serial.println();
Serial.print(F(">>>Buffer Solution Error Try Again<<<"));
Serial.println(); // not buffer solution or faulty operation
phCalibrationFinish = 0;
}
}
break;
case 3:
if(enterCalibrationFlag)
{
if(enterCalibrationFlag){
Serial.println();
if(phCalibrationFinish)
{
if((this->_voltage>1322)&&(this->_voltage<1678))
{
EEPROM_write(PHVALUEADDR, this->_neutralVoltage);
}
else if((this->_voltage>1854)&&(this->_voltage<2210))
{
EEPROM_write(PHVALUEADDR+4, this->_acidVoltage);
}
Serial.print(F(">>>Calibration Successful"));
if(phCalibrationFinish){
if((this->_voltage>1322)&&(this->_voltage<1678)){
EEPROM_write(PHVALUEADDR, this->_neutralVoltage);
}else if((this->_voltage>1854)&&(this->_voltage<2210)){
EEPROM_write(PHVALUEADDR+4, this->_acidVoltage);
}
Serial.print(F(">>>Calibration Successful"));
}else{
Serial.print(F(">>>Calibration Failed"));
}
else Serial.print(F(">>>Calibration Failed"));
Serial.println(F(",Exit Calibration Mode<<<"));
Serial.println(F(",Exit PH Calibration Mode<<<"));
Serial.println();
phCalibrationFinish = 0;
phCalibrationFinish = 0;
enterCalibrationFlag = 0;
}
break;

View File

@@ -26,24 +26,26 @@ class DFRobot_PH
public:
DFRobot_PH();
~DFRobot_PH();
void calibration(float voltage, float temperature); //calibration by Serial CMD
float readPH(float voltage, float temperature); // voltage to pH value, with temperature compensation
void begin(); //initialization
void calibration(float voltage, float temperature,char* cmd); //calibration by Serial CMD
void calibration(float voltage, float temperature);
float readPH(float voltage, float temperature); // voltage to pH value, with temperature compensation
void begin(); //initialization
private:
float _phValue;
float _acidVoltage;
float _neutralVoltage;
float _voltage;
float _temperature;
float _phValue;
float _acidVoltage;
float _neutralVoltage;
float _voltage;
float _temperature;
char _cmdReceivedBuffer[ReceivedBufferLength]; //store the Serial CMD
byte _cmdReceivedBufferIndex;
char _cmdReceivedBuffer[ReceivedBufferLength]; //store the Serial CMD
byte _cmdReceivedBufferIndex;
private:
boolean cmdSerialDataAvailable();
void phCalibration(byte mode); // calibration process, wirte key parameters to EEPROM
byte cmdParse();
void phCalibration(byte mode); // calibration process, wirte key parameters to EEPROM
byte cmdParse(const char* cmd);
byte cmdParse();
};
#endif

View File

@@ -1,53 +0,0 @@
/*
* file DFRobot_PH.ino
* @ https://github.com/DFRobot/DFRobot_PH
*
* This is the sample code for Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2
* In order to guarantee precision, a temperature sensor such as DS18B20 is needed, to execute automatic temperature compensation.
* You can send commands in the serial monitor to execute the calibration.
* Serial Commands:
* enter -> enter the calibration mode
* cal -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
* exit -> save the calibrated parameters and exit from calibration mode
*
* Copyright [DFRobot](http://www.dfrobot.com), 2018
* Copyright GNU Lesser General Public License
*
* version V1.0
* date 2018-04
*/
#include "DFRobot_PH.h"
#include <EEPROM.h>
#define PH_PIN A1
float voltage,phValue,temperature = 25;
DFRobot_PH ph;
void setup()
{
Serial.begin(115200);
ph.begin();
}
void loop()
{
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U) //time interval: 1s
{
timepoint = millis();
voltage = analogRead(PH_PIN)/1024.0*5000; // read the voltage
//temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
phValue = ph.readPH(voltage,temperature); // convert voltage to pH with temperature compensation
Serial.print("temperature:");
Serial.print(temperature,1);
Serial.print("^C pH:");
Serial.println(phValue,2);
}
ph.calibration(voltage,temperature); // calibration process by Serail CMD
}
float readTemperature()
{
//add your code here to get the temperature from your temperature sensor
}

View File

@@ -0,0 +1,150 @@
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# I2C address of the device
ADS1115_IIC_ADDRESS0 = 0x48
ADS1115_IIC_ADDRESS1 = 0x49
# ADS1115 Register Map
ADS1115_REG_POINTER_CONVERT = 0x00 # Conversion register
ADS1115_REG_POINTER_CONFIG = 0x01 # Configuration register
ADS1115_REG_POINTER_LOWTHRESH = 0x02 # Lo_thresh register
ADS1115_REG_POINTER_HITHRESH = 0x03 # Hi_thresh register
# ADS1115 Configuration Register
ADS1115_REG_CONFIG_OS_NOEFFECT = 0x00 # No effect
ADS1115_REG_CONFIG_OS_SINGLE = 0x80 # Begin a single conversion
ADS1115_REG_CONFIG_MUX_DIFF_0_1 = 0x00 # Differential P = AIN0, N = AIN1 (default)
ADS1115_REG_CONFIG_MUX_DIFF_0_3 = 0x10 # Differential P = AIN0, N = AIN3
ADS1115_REG_CONFIG_MUX_DIFF_1_3 = 0x20 # Differential P = AIN1, N = AIN3
ADS1115_REG_CONFIG_MUX_DIFF_2_3 = 0x30 # Differential P = AIN2, N = AIN3
ADS1115_REG_CONFIG_MUX_SINGLE_0 = 0x40 # Single-ended P = AIN0, N = GND
ADS1115_REG_CONFIG_MUX_SINGLE_1 = 0x50 # Single-ended P = AIN1, N = GND
ADS1115_REG_CONFIG_MUX_SINGLE_2 = 0x60 # Single-ended P = AIN2, N = GND
ADS1115_REG_CONFIG_MUX_SINGLE_3 = 0x70 # Single-ended P = AIN3, N = GND
ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # +/-6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # +/-4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # +/-2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # +/-1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # +/-0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # +/-0.256V range = Gain 16
ADS1115_REG_CONFIG_MODE_CONTIN = 0x00 # Continuous conversion mode
ADS1115_REG_CONFIG_MODE_SINGLE = 0x01 # Power-down single-shot mode (default)
ADS1115_REG_CONFIG_DR_8SPS = 0x00 # 8 samples per second
ADS1115_REG_CONFIG_DR_16SPS = 0x20 # 16 samples per second
ADS1115_REG_CONFIG_DR_32SPS = 0x40 # 32 samples per second
ADS1115_REG_CONFIG_DR_64SPS = 0x60 # 64 samples per second
ADS1115_REG_CONFIG_DR_128SPS = 0x80 # 128 samples per second (default)
ADS1115_REG_CONFIG_DR_250SPS = 0xA0 # 250 samples per second
ADS1115_REG_CONFIG_DR_475SPS = 0xC0 # 475 samples per second
ADS1115_REG_CONFIG_DR_860SPS = 0xE0 # 860 samples per second
ADS1115_REG_CONFIG_CMODE_TRAD = 0x00 # Traditional comparator with hysteresis (default)
ADS1115_REG_CONFIG_CMODE_WINDOW = 0x10 # Window comparator
ADS1115_REG_CONFIG_CPOL_ACTVLOW = 0x00 # ALERT/RDY pin is low when active (default)
ADS1115_REG_CONFIG_CPOL_ACTVHI = 0x08 # ALERT/RDY pin is high when active
ADS1115_REG_CONFIG_CLAT_NONLAT = 0x00 # Non-latching comparator (default)
ADS1115_REG_CONFIG_CLAT_LATCH = 0x04 # Latching comparator
ADS1115_REG_CONFIG_CQUE_1CONV = 0x00 # Assert ALERT/RDY after one conversions
ADS1115_REG_CONFIG_CQUE_2CONV = 0x01 # Assert ALERT/RDY after two conversions
ADS1115_REG_CONFIG_CQUE_4CONV = 0x02 # Assert ALERT/RDY after four conversions
ADS1115_REG_CONFIG_CQUE_NONE = 0x03 # Disable the comparator and put ALERT/RDY in high state (default)
mygain=0x02
coefficient=0.125
addr_G=ADS1115_IIC_ADDRESS0
class ADS1115():
def setGain(self,gain):
global mygain
global coefficient
mygain=gain
if mygain == ADS1115_REG_CONFIG_PGA_6_144V:
coefficient = 0.1875
elif mygain == ADS1115_REG_CONFIG_PGA_4_096V:
coefficient = 0.125
elif mygain == ADS1115_REG_CONFIG_PGA_2_048V:
coefficient = 0.0625
elif mygain == ADS1115_REG_CONFIG_PGA_1_024V:
coefficient = 0.03125
elif mygain == ADS1115_REG_CONFIG_PGA_0_512V:
coefficient = 0.015625
elif mygain == ADS1115_REG_CONFIG_PGA_0_256V:
coefficient = 0.0078125
else:
coefficient = 0.125
def setAddr_ADS1115(self,addr):
global addr_G
addr_G=addr
def setChannel(self,channel):
global mygain
"""Select the Channel user want to use from 0-3
For Single-ended Output
0 : AINP = AIN0 and AINN = GND
1 : AINP = AIN1 and AINN = GND
2 : AINP = AIN2 and AINN = GND
3 : AINP = AIN3 and AINN = GND
For Differential Output
0 : AINP = AIN0 and AINN = AIN1
1 : AINP = AIN0 and AINN = AIN3
2 : AINP = AIN1 and AINN = AIN3
3 : AINP = AIN2 and AINN = AIN3"""
self.channel = channel
while self.channel > 3 :
self.channel = 0
return self.channel
def setSingle(self):
global addr_G
if self.channel == 0:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_0 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
elif self.channel == 1:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_1 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
elif self.channel == 2:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_2 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
elif self.channel == 3:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_SINGLE_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
bus.write_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONFIG, CONFIG_REG)
def setDifferential(self):
global addr_G
if self.channel == 0:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_0_1 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
elif self.channel == 1:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_0_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
elif self.channel == 2:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_1_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
elif self.channel == 3:
CONFIG_REG = [ADS1115_REG_CONFIG_OS_SINGLE | ADS1115_REG_CONFIG_MUX_DIFF_2_3 | mygain | ADS1115_REG_CONFIG_MODE_CONTIN, ADS1115_REG_CONFIG_DR_128SPS | ADS1115_REG_CONFIG_CQUE_NONE]
bus.write_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONFIG, CONFIG_REG)
def readValue(self):
"""Read data back from ADS1115_REG_POINTER_CONVERT(0x00), 2 bytes
raw_adc MSB, raw_adc LSB"""
global coefficient
global addr_G
data = bus.read_i2c_block_data(addr_G, ADS1115_REG_POINTER_CONVERT, 2)
# Convert the data
raw_adc = data[0] * 256 + data[1]
if raw_adc > 32767:
raw_adc -= 65535
raw_adc = int(float(raw_adc)*coefficient)
return {'r' : raw_adc}
def readVoltage(self,channel):
self.setChannel(channel)
self.setSingle()
time.sleep(0.1)
return self.readValue()
def ComparatorVoltage(self,channel):
self.setChannel(channel)
self.setDifferential()
time.sleep(0.1)
return self.readValue()

Binary file not shown.

View File

@@ -0,0 +1,88 @@
import time
import sys
_kvalue = 1.0
_kvalueLow = 1.0
_kvalueHigh = 1.0
_cmdReceivedBufferIndex = 0
_voltage = 0.0
_temperature = 25.0
class DFRobot_EC():
def begin(self):
global _kvalueLow
global _kvalueHigh
try:
with open('ecdata.txt','r') as f:
kvalueLowLine = f.readline()
kvalueLowLine = kvalueLowLine.strip('kvalueLow=')
_kvalueLow = float(kvalueLowLine)
kvalueHighLine = f.readline()
kvalueHighLine = kvalueHighLine.strip('kvalueHigh=')
_kvalueHigh = float(kvalueHighLine)
except :
print "ecdata.txt ERROR ! Please run DFRobot_EC_Reset"
sys.exit(1)
def readEC(self,voltage,temperature):
global _kvalueLow
global _kvalueHigh
global _kvalue
rawEC = 1000*voltage/820.0/200.0
valueTemp = rawEC * _kvalue
if(valueTemp > 2.5):
_kvalue = _kvalueHigh
elif(valueTemp < 2.0):
_kvalue = _kvalueLow
value = rawEC * _kvalue
value = value / (1.0+0.0185*(temperature-25.0))
return value
def calibration(self,voltage,temperature):
rawEC = 1000*voltage/820.0/200.0
if (rawEC>0.9 and rawEC<1.9):
compECsolution = 1.413*(1.0+0.0185*(temperature-25.0))
KValueTemp = 820.0*200.0*compECsolution/1000.0/voltage
round(KValueTemp,2)
print ">>>Buffer Solution:1.413us/cm"
f=open('ecdata.txt','r+')
flist=f.readlines()
flist[0]='kvalueLow='+ str(KValueTemp) + '\n'
f=open('ecdata.txt','w+')
f.writelines(flist)
f.close()
print ">>>EC:1.413us/cm Calibration completed,Please enter Ctrl+C exit calibration in 5 seconds"
time.sleep(5.0)
elif (rawEC>9 and rawEC<16.8):
compECsolution = 12.88*(1.0+0.0185*(temperature-25.0))
KValueTemp = 820.0*200.0*compECsolution/1000.0/voltage
print ">>>Buffer Solution:12.88ms/cm"
f=open('ecdata.txt','r+')
flist=f.readlines()
flist[1]='kvalueHigh='+ str(KValueTemp) + '\n'
f=open('ecdata.txt','w+')
f.writelines(flist)
f.close()
print ">>>EC:12.88ms/cm Calibration completed,Please enter Ctrl+C exit calibration in 5 seconds"
time.sleep(5.0)
else:
print ">>>Buffer Solution Error Try Again<<<"
def reset(self):
_kvalueLow = 1.0;
_kvalueHigh = 1.0;
try:
f=open('ecdata.txt','r+')
flist=f.readlines()
flist[0]='kvalueLow=' + str(_kvalueLow) + '\n'
flist[1]='kvalueHigh='+ str(_kvalueHigh) + '\n'
f=open('ecdata.txt','w+')
f.writelines(flist)
f.close()
print ">>>Reset to default parameters<<<"
except:
f=open('ecdata.txt','w')
#flist=f.readlines()
flist ='kvalueLow=' + str(_kvalueLow) + '\n'
flist +='kvalueHigh='+ str(_kvalueHigh) + '\n'
#f=open('data.txt','w+')
f.writelines(flist)
f.close()
print ">>>Reset to default parameters<<<"

Binary file not shown.

View File

@@ -0,0 +1,73 @@
import time
import sys
_temperature = 25.0
_acidVoltage = 2032.44
_neutralVoltage = 1500.0
class DFRobot_PH():
def begin(self):
global _acidVoltage
global _neutralVoltage
try:
with open('phdata.txt','r') as f:
neutralVoltageLine = f.readline()
neutralVoltageLine = neutralVoltageLine.strip('neutralVoltage=')
_neutralVoltage = float(neutralVoltageLine)
acidVoltageLine = f.readline()
acidVoltageLine = acidVoltageLine.strip('acidVoltage=')
_acidVoltage = float(acidVoltageLine)
except :
print "phdata.txt ERROR ! Please run DFRobot_PH_Reset"
sys.exit(1)
def readPH(self,voltage,temperature):
global _acidVoltage
global _neutralVoltage
slope = (7.0-4.0)/((_neutralVoltage-1500.0)/3.0 - (_acidVoltage-1500.0)/3.0)
intercept = 7.0 - slope*(_neutralVoltage-1500.0)/3.0
_phValue = slope*(voltage-1500.0)/3.0+intercept
round(_phValue,2)
return _phValue
def calibration(self,voltage):
if (voltage>1322 and voltage<1678):
print ">>>Buffer Solution:7.0"
f=open('phdata.txt','r+')
flist=f.readlines()
flist[0]='neutralVoltage='+ str(voltage) + '\n'
f=open('phdata.txt','w+')
f.writelines(flist)
f.close()
print ">>>PH:7.0 Calibration completed,Please enter Ctrl+C exit calibration in 5 seconds"
time.sleep(5.0)
elif (voltage>1854 and voltage<2210):
print ">>>Buffer Solution:4.0"
f=open('phdata.txt','r+')
flist=f.readlines()
flist[1]='acidVoltage='+ str(voltage) + '\n'
f=open('phdata.txt','w+')
f.writelines(flist)
f.close()
print ">>>PH:4.0 Calibration completed,Please enter Ctrl+C exit calibration in 5 seconds"
time.sleep(5.0)
else:
print ">>>Buffer Solution Error Try Again<<<"
def reset(self):
_acidVoltage = 2032.44
_neutralVoltage = 1500.0
try:
f=open('phdata.txt','r+')
flist=f.readlines()
flist[0]='neutralVoltage='+ str(_neutralVoltage) + '\n'
flist[1]='acidVoltage='+ str(_acidVoltage) + '\n'
f=open('phdata.txt','w+')
f.writelines(flist)
f.close()
print ">>>Reset to default parameters<<<"
except:
f=open('phdata.txt','w')
#flist=f.readlines()
flist ='neutralVoltage='+ str(_neutralVoltage) + '\n'
flist +='acidVoltage='+ str(_acidVoltage) + '\n'
#f=open('data.txt','w+')
f.writelines(flist)
f.close()
print ">>>Reset to default parameters<<<"

Binary file not shown.

View File

@@ -0,0 +1,51 @@
## DFRobot_PH.py Library for Raspberry pi
---------------------------------------------------------
This is the sample code for Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2
## Table of Contents
* [Installation](#installation)
* [Methods](#methods)
<snippet>
<content>
## Installation
The Analog pH Sensor should work with ADS1115
(https://github.com/DFRobot/DFRobot_ADS1115/tree/master/RaspberryPi/Python)
Run the program:
```cpp
$> python DFRobot_ADS1115.py
$> python DFRobot_PH.py
```
## Methods
```C++
/*
* @brief Init The Analog pH Sensor
*/
def begin(self);
/*
* @brief Convert voltage to PH with temperature compensation
*/
def readPH(self,voltage,temperature);
/*
* @brief Calibrate the calibration data
*/
def calibration(self,voltage,temperature);
/*
* @brief Reset the calibration data to default value
*/
def reset(self);
```
## Credits
Written by Jiawei Zhang, 2018. (Welcome to our [website](https://www.dfrobot.com/))

View File

@@ -0,0 +1,30 @@
import sys
sys.path.append('../')
import time
ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16
from DFRobot_ADS1115 import ADS1115
from DFRobot_EC import DFRobot_EC
ads1115 = ADS1115()
ec = DFRobot_EC()
ec.begin()
while True :
#Read your temperature sensor to execute temperature compensation
temperature = 25
#Set the IIC address
ads1115.setAddr_ADS1115(0x48)
#Sets the gain and input voltage range.
ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
#Get the Digital Value of Analog of selected channel
adc0 = ads1115.readVoltage(0)
print "A0:%dmV "%(adc0['r'])
#Calibrate the calibration data
ec.calibration(adc0['r'],temperature)
time.sleep(3.0)

View File

@@ -0,0 +1,11 @@
#This example ues to reset ecdata.txt to default value
import sys
sys.path.append('../')
import time
from DFRobot_EC import DFRobot_EC
ec = DFRobot_EC()
ec.reset()
time.sleep(0.5)
sys.exit(1)

View File

@@ -0,0 +1,29 @@
import sys
sys.path.append('../')
import time
ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16
from DFRobot_ADS1115 import ADS1115
from DFRobot_PH import DFRobot_PH
ads1115 = ADS1115()
ph = DFRobot_PH()
ph.begin()
while True :
temperature = 25
#Set the IIC address
ads1115.setAddr_ADS1115(0x48)
#Sets the gain and input voltage range.
ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
#Get the Digital Value of Analog of selected channel
adc0 = ads1115.readVoltage(0)
print "A0:%dmV "%(adc0['r'])
#Calibrate the calibration data
ph.calibration(adc0['r'])
time.sleep(1.0)

View File

@@ -0,0 +1,35 @@
import sys
sys.path.append('../')
import time
ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16
from DFRobot_ADS1115 import ADS1115
from DFRobot_EC import DFRobot_EC
from DFRobot_PH import DFRobot_PH
ads1115 = ADS1115()
ec = DFRobot_EC()
ph = DFRobot_PH()
ec.begin()
ph.begin()
while True :
#Read your temperature sensor to execute temperature compensation
temperature = 25
#Set the IIC address
ads1115.setAddr_ADS1115(0x48)
#Sets the gain and input voltage range.
ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
#Get the Digital Value of Analog of selected channel
adc0 = ads1115.readVoltage(0)
adc1 = ads1115.readVoltage(1)
#Convert voltage to EC with temperature compensation
EC = ec.readEC(adc0['r'],temperature)
PH = ph.readPH(adc1['r'],temperature)
print "Temperature:%.1f ^C EC:%.2f ms/cm PH:%.2f " %(temperature,EC,PH)
time.sleep(1.0)

View File

@@ -0,0 +1,30 @@
import sys
sys.path.append('../')
import time
ADS1115_REG_CONFIG_PGA_6_144V = 0x00 # 6.144V range = Gain 2/3
ADS1115_REG_CONFIG_PGA_4_096V = 0x02 # 4.096V range = Gain 1
ADS1115_REG_CONFIG_PGA_2_048V = 0x04 # 2.048V range = Gain 2 (default)
ADS1115_REG_CONFIG_PGA_1_024V = 0x06 # 1.024V range = Gain 4
ADS1115_REG_CONFIG_PGA_0_512V = 0x08 # 0.512V range = Gain 8
ADS1115_REG_CONFIG_PGA_0_256V = 0x0A # 0.256V range = Gain 16
from DFRobot_ADS1115 import ADS1115
from DFRobot_PH import DFRobot_PH
ads1115 = ADS1115()
ph = DFRobot_PH()
ph.begin()
while True :
#Read your temperature sensor to execute temperature compensation
temperature = 25
#Set the IIC address
ads1115.setAddr_ADS1115(0x48)
#Sets the gain and input voltage range.
ads1115.setGain(ADS1115_REG_CONFIG_PGA_6_144V)
#Get the Digital Value of Analog of selected channel
adc0 = ads1115.readVoltage(0)
#Convert voltage to PH with temperature compensation
PH = ph.readPH(adc0['r'],temperature)
print "Temperature:%.1f ^C PH:%.2f" %(temperature,PH)
time.sleep(1.0)

View File

@@ -0,0 +1,11 @@
#This example ues to reset phdata.txt to default value
import sys
sys.path.append('../')
import time
from DFRobot_PH import DFRobot_PH
ph = DFRobot_PH()
ph.reset()
time.sleep(0.5)
sys.exit(1)

View File

@@ -0,0 +1,2 @@
kvalueLow=1.0
kvalueHigh=1.0

View File

@@ -0,0 +1,2 @@
neutralVoltage=1500.0
acidVoltage=2032.44

View File

@@ -0,0 +1,96 @@
/*
* file DFRobot_PH_EC.ino
* @ https://github.com/DFRobot/DFRobot_PH
*
* This is the sample code for The Mixed use of two sensors:
* 1、Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2
* 2、Analog Electrical Conductivity Sensor / Meter Kit V2 (K=1.0), SKU: DFR0300.
* In order to guarantee precision, a temperature sensor such as DS18B20 is needed, to execute automatic temperature compensation.
* You can send commands in the serial monitor to execute the calibration.
* Serial Commands:
*
* PH Calibration
* enterph -> enter the PH calibration mode
* calph -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
* exitph -> save the calibrated parameters and exit from PH calibration mode
*
* EC Calibration
* enterec -> enter the EC calibration mode
* calec -> calibrate with the standard buffer solution, two buffer solutions(1413us/cm and 12.88ms/cm) will be automaticlly recognized
* exitec -> save the calibrated parameters and exit from EC calibration mode
*
* Copyright [DFRobot](http://www.dfrobot.com), 2018
* Copyright GNU Lesser General Public License
*
* version V1.0
* date 2018-04
*/
#include "DFRobot_PH.h"
#include "DFRobot_EC.h"
#include <EEPROM.h>
#define PH_PIN A1
#define EC_PIN A2
float voltagePH,voltageEC,phValue,ecValue,temperature = 25;
DFRobot_PH ph;
DFRobot_EC ec;
void setup()
{
Serial.begin(115200);
ph.begin();
ec.begin();
}
void loop()
{
char cmd[10];
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U){ //time interval: 1s
timepoint = millis();
//temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
voltagePH = analogRead(PH_PIN)/1024.0*5000; // read the ph voltage
phValue = ph.readPH(voltagePH,temperature); // convert voltage to pH with temperature compensation
Serial.print("pH:");
Serial.print(phValue,2);
voltageEC = analogRead(EC_PIN)/1024.0*5000;
ecValue = ec.readEC(voltageEC,temperature); // convert voltage to EC with temperature compensation
Serial.print(", EC:");
Serial.print(ecValue,2);
Serial.println("ms/cm");
}
if(readSerial(cmd)){
strupr(cmd);
if(strstr(cmd,"PH")){
ph.calibration(voltagePH,temperature,cmd); //PH calibration process by Serail CMD
}
if(strstr(cmd,"EC")){
ec.calibration(voltageEC,temperature,cmd); //EC calibration process by Serail CMD
}
}
}
int i = 0;
bool readSerial(char result[]){
while(Serial.available() > 0){
char inChar = Serial.read();
if(inChar == '\n'){
result[i] = '\0';
Serial.flush();
i=0;
return true;
}
if(inChar != '\r'){
result[i] = inChar;
i++;
}
delay(1);
}
return false;
}
float readTemperature()
{
//add your code here to get the temperature from your temperature sensor
}

View File

@@ -0,0 +1,52 @@
/*
* file DFRobot_PH.ino
* @ https://github.com/DFRobot/DFRobot_PH
*
* This is the sample code for Gravity: Analog pH Sensor / Meter Kit V2, SKU:SEN0161-V2
* In order to guarantee precision, a temperature sensor such as DS18B20 is needed, to execute automatic temperature compensation.
* You can send commands in the serial monitor to execute the calibration.
* Serial Commands:
* enterph -> enter the calibration mode
* calph -> calibrate with the standard buffer solution, two buffer solutions(4.0 and 7.0) will be automaticlly recognized
* exitph -> save the calibrated parameters and exit from calibration mode
*
* Copyright [DFRobot](http://www.dfrobot.com), 2018
* Copyright GNU Lesser General Public License
*
* version V1.0
* date 2018-04
*/
#include "DFRobot_PH.h"
#include <EEPROM.h>
#define PH_PIN A1
float voltage,phValue,temperature = 25;
DFRobot_PH ph;
void setup()
{
Serial.begin(115200);
ph.begin();
}
void loop()
{
static unsigned long timepoint = millis();
if(millis()-timepoint>1000U){ //time interval: 1s
timepoint = millis();
//temperature = readTemperature(); // read your temperature sensor to execute temperature compensation
voltage = analogRead(PH_PIN)/1024.0*5000; // read the voltage
phValue = ph.readPH(voltage,temperature); // convert voltage to pH with temperature compensation
Serial.print("temperature:");
Serial.print(temperature,1);
Serial.print("^C pH:");
Serial.println(phValue,2);
}
ph.calibration(voltage,temperature); // calibration process by Serail CMD
}
float readTemperature()
{
//add your code here to get the temperature from your temperature sensor
}