Data Gator
Hardware and software documentation for the Data Gator project.
Loading...
Searching...
No Matches
Atlas_EZO-pH.hpp
Go to the documentation of this file.
1
11#ifndef ATLAS_EZO_PH
12#define ATLAS_EZO_PH
13
14
15#include <Wire.h>
16#include <pHSensor.hpp>
17// default EZO-pH sensor I2C addresses
18#define EZO_I2C_ADDR 99
19#define EZO_I2C_SHALLOW_ADDR 0x01
20#define EZO_I2C_MIDDLE_ADDR 0x02
21#define EZO_I2C_DEEP_ADDR 0x03
22
34class AtlasEZOpH: public pHSensor{
35 private:
37 byte serial_event = 0;
38 byte code = 0;
39 char ph_data[32];
40 byte in_char = 0;
41 byte i = 0;
42 int address = 99;
43
44 public:
45 AtlasEZOpH(){};
47 this->address = address;
48 }
49
65 Wire.beginTransmission(address);
66 byte error = Wire.endTransmission();
67
68 if(error == 0){ // device connected at address
69 return true;
70 }else if(error != 2){
71 return false;
72 }
73
74 return false;
75 }
76
77
78 double getpH(double){return 0.0;}
79
80
88 double getpH(int address){
89 char computerdata[20]; //we make a 20 byte character array to hold incoming data from a pc/mac/other.
90 int reading_delay = 815; // time required to read the sensor
91 Serial.print("\treading from address ");
92 Serial.println(address);
93 Wire.beginTransmission(address);
94 computerdata[0] = 'r';
95 computerdata[1] = 0;
96 Wire.write(computerdata); // signal sensor to take reading
97 Wire.endTransmission();
98
99 // delay for reading
101
102 Wire.requestFrom(address, 32, 1); // request 32 bytes of data
103 code = Wire.read(); // read response code
104
105 switch (code) { //switch case based on what the response code is.
106 case 1: //decimal 1.
107 Serial.println("Success"); //means the command was successful.
108 break; //exits the switch case.
109
110 case 2: //decimal 2.
111 Serial.println("Failed"); //means the command has failed.
112 break; //exits the switch case.
113
114 case 254: //decimal 254.
115 Serial.println("Pending"); //means the command has not yet been finished calculating.
116 break; //exits the switch case.
117
118 case 255: //decimal 255.
119 Serial.println("No Data"); //means there is no further data to send.
120 break; //exits the switch case.
121 //
122 default:
123 Serial.println("unknown code");
124 }
125
126 while(Wire.available()){
127 in_char = Wire.read();
128 ph_data[i] = in_char;
129 i += 1;
130
131 if(in_char == 0){
132 i = 0;
133 break;
134 }
135 }
136 double pH_reading = std::atof(ph_data);
137
138 return pH_reading;
139 }
140
149 double getpH(){
150 return this->getpH(EZO_I2C_ADDR);
151 }
152
158 char* getpH_str(int address){
159 //Serial.println(this->getpH(address));
160 this->getpH(address);
161 return ph_data;
162 //return "-1.0";
163 }
164
165 void clear_pH_str(void){
166 ph_data[0] = 0;
167 }
168
174 std::string getSensorType(void){
175 return "atlas_ezo_ph";
176 }
177
183 std::string toJSON(double voltage){
184 return "\"PH_RAW\": " + std::to_string(voltage) + ", \"PH\":" + std::to_string(this->getpH(voltage));
185 }
186
187};
188#endif
#define EZO_I2C_ADDR
Default I2C address for sensor.
Definition Atlas_EZO-pH.hpp:18
Defines an I2C pH sensor which inherits from pHSensor.hpp.
Definition Atlas_EZO-pH.hpp:34
double getpH(int address)
Reads pH value from sensor as a double on the pH scale.
Definition Atlas_EZO-pH.hpp:88
byte in_char
used as a 1 byte buffer to store inbound bytes from the pH Circuit.
Definition Atlas_EZO-pH.hpp:40
double getpH()
Returns a pH measurement from the default sensor address.
Definition Atlas_EZO-pH.hpp:149
byte i
counter used for ph_data array.
Definition Atlas_EZO-pH.hpp:41
char ph_data[32]
we make a 32 byte character array to hold incoming data from the pH circuit.
Definition Atlas_EZO-pH.hpp:39
byte code
used to hold the I2C response code.
Definition Atlas_EZO-pH.hpp:38
byte received_from_computer
we need to know how many characters have been received.
Definition Atlas_EZO-pH.hpp:36
bool sensor_at_address(int address)
Check if sensor present at address.
Definition Atlas_EZO-pH.hpp:64
double getpH(double)
Convert voltage reading to pH value.
Definition Atlas_EZO-pH.hpp:78
int address
default i2c address
Definition Atlas_EZO-pH.hpp:42
std::string toJSON(double voltage)
Converts a pH voltage reading to a JSON object string for MQTT and debugging.
Definition Atlas_EZO-pH.hpp:183
char * getpH_str(int address)
Get the pH reading as a string.
Definition Atlas_EZO-pH.hpp:158
std::string getSensorType(void)
Get the string identifier for the Atlas EZO pH sensor. For use in MQTT topics and debugging.
Definition Atlas_EZO-pH.hpp:174
byte serial_event
a flag to signal when data has been received from the pc/mac/other.
Definition Atlas_EZO-pH.hpp:37
Interface for pH sensors.
Definition pHSensor.hpp:19
Implements an interface for converting a voltage (analog reading) into a pH value according to the co...
int reset_count
number of resets retrieved for NVS
Definition scheduler.hpp:31