-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamMetabolizer
180 lines (140 loc) · 7.3 KB
/
StreamMetabolizer
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <SoftwareSerial.h>
#include <Time.h>
#include <TimeAlarms.h>
#define rxpin 2 //set the RX pin to pin 2
#define txpin 3 //set the TX pin to pin 3
#define SYNC_INTERVAL 1000 //mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; //time of last sync()
RTC_DS1307 RTC; //define the Real Time Clock object
float temp; //where the final temperature data is stored
int analogPin = A0;
String inputstring = ""; //a string to hold incoming data from the PC
String sensorstring = ""; //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false; //have we received all the data from the PC
boolean sensor_stringcomplete = false; //have we received all the data from the Atlas Scientific product
SoftwareSerial myserial(rxpin, txpin); //enable the soft serial port
const int chipSelect = 10; //for the data logging shield, we use digital pin 10 for the SD cs line
File logfile; //the logging file
void error(char *str) //generic error function
{
Serial.print("error: ");
Serial.println(str);
while(1);
}
time_t syncProvider() //function that does the syncing between the internal clock and the RTC
{
return RTC.now().unixtime();
}
void setup() { //main loop
Serial.begin(38400); //set up the hardware serial port to run at 38400
myserial.begin(38400); //set baud rate for software serial port to 38400
inputstring.reserve(5); //set aside some bytes for receiving data from the PC
sensorstring.reserve(30); //set aside some bytes for receiving data from Atlas Scientific product
Wire.begin(); //start the wire protocol
RTC.begin(); //start the real time clock
setSyncProvider(syncProvider); //syncs the internal Arduino clock with the RTC
Alarm.timerRepeat(0, 5, 0, Repeats); //timer for every 300 seconds (5 minutes)
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) { // see if the card is present and can be initialized:
error("Card failed, or not present");
}
Serial.println("Welcome to the Stream Metabolizer.");
char filename[] = "LOGGER00.CSV"; // create a new file
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i/10 + '0';
filename[7] = i%10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Serial.print("Logging to: ");
Serial.println(filename);
logfile.println("DateTime, Temperature (C),DO %,DO mg/L");
Serial.println("DateTime, Temperature (C),DO %,DO mg/L");
}
void serialEvent() { //if the hardware serial port receives a char
char inchar = (char)Serial.read(); //get the char we just received
inputstring += inchar; //add it to the inputString
if(inchar == '\r') {input_stringcomplete = true;} //if the incoming character is a <CR>, set the flag
}
void loop() {
Alarm.delay(1000); // need this here for the alarms and timers to be called
}
void Repeats(){
DateTime now;
temp = read_temp(); //call the function “read_temp” and return the temperature in C°
if (input_stringcomplete){ //if a string from the PC has been recived in its entierty
myserial.print(inputstring); //send that string to the Atlas Scientific product
inputstring = ""; //clear the string:
input_stringcomplete = false; //reset the flage used to tell if we have recived a completed string from the PC
}
myserial.print(temp);
myserial.print(",0");
myserial.print('\r');
while (myserial.available()) { //while a char is holding in the serial buffer
char inchar = (char)myserial.read(); //get the new cha
sensorstring += inchar; //add it to the sensorString
if (inchar == '\r') {sensor_stringcomplete = true;} //if the incoming character is a <CR>, set the flag
}
now = RTC.now();
if (sensor_stringcomplete){ //if a string from the Atlas Scientific product has been received in its entirety
Serial.print('"');
Serial.print(now.year(), DEC);
Serial.print("/");
Serial.print(now.month(), DEC);
Serial.print("/");
Serial.print(now.day(), DEC);
Serial.print(" ");
Serial.print(now.hour(), DEC);
Serial.print(":");
Serial.print(now.minute(), DEC);
Serial.print(":");
Serial.print(now.second(), DEC);
Serial.print('"');
Serial.print(", ");
Serial.print(temp); //print the temperature data
Serial.print(", ");
Serial.print(sensorstring); //use the hardware serial port to send that data to the PC
Serial.println("");
logfile.print('"');
logfile.print(now.year(), DEC);
logfile.print("/");
logfile.print(now.month(), DEC);
logfile.print("/");
logfile.print(now.day(), DEC);
logfile.print(" ");
logfile.print(now.hour(), DEC);
logfile.print(":");
logfile.print(now.minute(), DEC);
logfile.print(":");
logfile.print(now.second(), DEC);
logfile.print('"');
logfile.print(", ");
logfile.print(temp); //print the temperature data
logfile.print(", ");
logfile.print(sensorstring); //use the hardware serial port to send that data to the PC
logfile.println("");
sensorstring = ""; //clear the string:
sensor_stringcomplete = false; //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
logfile.flush();
}
}
float read_temp(void){ //the read temperature function
float v_out; //voltage output from temp sensor
float temp; //the final temperature is stored here
digitalWrite(analogPin, LOW); //set pull-up on analog pin
delay(2); //wait 2 ms for temp to stabilize
v_out = analogRead(analogPin); //read the input pin
v_out*=.0048; //convert ADC points to volts (we are using .0048 because this device is running at 5 volts)
v_out*=1000; //convert volts to millivolts
temp= 0.0512 * v_out -20.5128; //the equation from millivolts to temperature
return temp; //send back the temp
}