-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathASConductivity.cpp
More file actions
95 lines (82 loc) · 2.65 KB
/
ASConductivity.cpp
File metadata and controls
95 lines (82 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "ASConductivity.h"
#include <string.h>
#include <Wire.h>
#include "utilities.h"
ASConductivity::ASConductivity(TEMPREADFUN _tempReadFun){
this->tempReadFun = _tempReadFun;
}
ASConductivity::~ASConductivity(){
}
void ASConductivity::getReading(CONDREADING* dst){
char responseBuffer[48];
// Try to set temperature adjustment first
char tempCommand[16];
dst->temperature = this->tempReadFun();
if(dst->temperature >= 0 && dst->temperature < 85){
tempCommand[0] = 'T';
tempCommand[1] = ',';
unsigned int len = dtostrf(dst->temperature, 2, tempCommand + 2);
tempCommand[len++] = '\0';
this->sendCommand(tempCommand, NULL, 0);
}
// Request a single reading
byte responseCode = this->sendCommand("R", responseBuffer, 48);
if(responseCode == ASSUCCESS){
Serial.print(F("Conductivity (")); Serial.print(strlen(responseBuffer)); Serial.print(F("): ")); Serial.println(responseBuffer);
char* conductivity = strtok(responseBuffer, ",");
dst->conductivity = atof(conductivity);
char* salinity = strtok(NULL, ",");
dst->salinity = atof(salinity);
} else {
Serial.println(F("No data received from conductivity board"));
}
}
byte ASConductivity::receiveResponse(char* response, byte responseBufferLength){
Wire.requestFrom(ASCONADDRESS, responseBufferLength+1, true); // +1 for code
byte code = Wire.read();
switch (code){
case ASSUCCESS:
Serial.println(F("Success"));
break;
case ASFAILURE:
Serial.println(F("Failed"));
case ASPENDING:
Serial.println(F("Pending"));
case ASNODATA:
Serial.println(F("No Data"));
Wire.endTransmission();
return code;
}
char in_char;
uint8_t i=0;
while(Wire.available() && i < responseBufferLength){
in_char = Wire.read();
response[i++] = in_char;
if(in_char == 0){
Wire.endTransmission();
break;
}
}
return code;
}
byte ASConductivity::sendCommand(const char* command, char* response, byte responseBufferLength){
Serial.print(F("Sending to AS Cond: ")); Serial.println(command);
Wire.beginTransmission(ASCONADDRESS);
Wire.write((uint8_t*)command, strlen(command));
Wire.endTransmission();
// Delay per requirements in Atlas Scientific Docs
// https://www.atlas-scientific.com/_files/code/ec-i2c.pdf?
if(command[0] == 'R' || command[0] == 'C'){
delay(1500);
} else {
delay(400);
}
byte code = this->receiveResponse(response, responseBufferLength);
// Delay and try again if pending
if(code == 254){
Serial.print(F("Retrying after 500ms..."));
delay(500);
code = this->receiveResponse(response, responseBufferLength);
}
return code;
}