-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.cpp
276 lines (221 loc) · 5.74 KB
/
Logging.cpp
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Logging.cpp
*
* Created on: Jan 15, 2011
* Author: Andy
*/
// temp is in Kelvin. normalized to K*100
#include "Logging.h"
const char FILENAME[] = "datalog.txt";
IPAddress TEMP_LOG_SERVER_IP( 10, 12, 34, 135 );
const int chipSelect = 4;
static unsigned long SecondsTempsLastLogged[NUMBER_OF_TEMP_SENSORS];
Sd2Card card;
SdVolume volume;
SdFile root;
SdFile file;
void LogToSDCard(char * msg);
void LogTempToSD(uint8_t sensor, uint16_t kelv);
void LogToServer( int sensor, int temp);
void InitializeLogging()
{
Serial1.println("Initializing SD card...");
Serial.println("Initializing SD card...");
/*
if (!SD.begin(chipSelect))
{
Serial1.println("Card failed, or not present");
}
else
{
Serial1.println("card initialized.");
}
*/
int initSuccess = 1;
// initialize the SD card
if (!card.init(SPI_HALF_SPEED, chipSelect))
{
initSuccess = 0;
Serial.println("card.init failed");
}
// initialize a FAT volume
if (!volume.init(card))
{
initSuccess = 0;
Serial.println("volume.init failed");
}
// open root directory
if (!root.openRoot(&volume))
{
initSuccess = 0;
Serial.println("openRoot failed");
}
if (initSuccess == 0)
{
Serial.println("Card init FAILED");
}
else
{
Serial.println("Card init SUCCESS");
LogToSDCard("System Startup");
}
for (int i = 0; i < NUMBER_OF_TEMP_SENSORS; i++)
{
SecondsTempsLastLogged[i] = 0;
}
// get starting address for data logging
uint8_t lsb = EEPROM.read(ADDR_LSB);
uint8_t msb = EEPROM.read(ADDR_MSB);
currentEEAddress = msb << 4;
currentEEAddress = currentEEAddress | lsb;
char * startMsg = "Starting Address: ";
LogLine(startMsg, (int)strlen(startMsg));
LogLine(currentEEAddress);
}
void LogLine(char * msg, int len)
{
msg[len] = '\0';
//sprintf(mg,"%sh",msg,len);
Serial.println(msg);
}
void Log(char* msg, int len)
{
char * mg = "";
msg[len] = '\0';
sprintf(mg,"%sh",msg,len);
Serial.print(msg);
}
void LogLine(int val)
{
Serial.println(val, DEC);
}
void LogTemp(uint8_t sensor, uint16_t kelvInt)
{
if (millis()/1000 - SecondsTempsLastLogged[sensor] > TEMP_LOGGING_INTERVAL)
{
LogToServer( (int)sensor, (int)kelvInt);
LogTempToSD(sensor,kelvInt);
PrintTime();
SecondsTempsLastLogged[sensor] = millis()/1000;
static int writes = 0;
Serial.print("Logging temp from sensor: ");
Serial.print(sensor);
Serial.print(" Temp in K: ");
Serial.println(kelvInt, DEC);
Serial.print("Logging to: ");
Serial.println(currentEEAddress, DEC);
uint8_t msbK = kelvInt >> 8;
uint8_t lsbK = kelvInt & 0xFF;
if (currentEEAddress > EEPROM_SIZE)
{
currentEEAddress = EEPROM_DATA_START;
}
EEPROM.write(currentEEAddress , sensor);
EEPROM.write(currentEEAddress + ADDR_MSB + 1, msbK);
EEPROM.write(currentEEAddress + ADDR_LSB + 1, lsbK);
writes++;
currentEEAddress += 3;
if (((writes + 1) % 30) == 0)
{
writes = 0;
WriteStartAddress(currentEEAddress);
}
}
}
void DisplayEEProm()
{
for (int index = 0; index < EEPROM_SIZE; index++)
{
uint8_t b = EEPROM.read(index);
char m[3];
sprintf(m,"%02X",b);
Serial.print(m);
if (((index + 1) % 32) == 0)
{
Serial.println();
}
}
}
void ResetEEProm()
{
Serial.println("Resetting Log Pointer");
currentEEAddress = EEPROM_DATA_START;
WriteStartAddress(currentEEAddress);
}
void WriteStartAddress(uint16_t startingAddress)
{
uint8_t lsb = (uint8_t)startingAddress;
uint8_t msb = startingAddress >> 8;
Serial.println("Writing starting address");
Serial.print("low:");
Serial.print(lsb,HEX);
Serial.print("high:");
Serial.println(msb,HEX);
EEPROM.write(ADDR_MSB, msb);
EEPROM.write(ADDR_LSB, lsb);
}
void PrintTime()
{
DateTime now = RTC.now();
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.println();
}
void LogTempToSD(uint8_t sensor, uint16_t kelvInt)
{
char buf[255] = "";
DateTime now = RTC.now();
sprintf(buf,"%d/%d/%d %d:%d:%d,%d,%d",now.year(),now.month(),now.day(),now.hour(),now.minute(),now.second(),sensor,kelvInt);
LogToSDCard(buf);
}
void LogToServer( int sensor, int temp)
{
char buf[255] = "";
DateTime now = RTC.now();
long epochSeconds = now.unixtime();
Serial.println(epochSeconds);
sprintf(buf,"GET /restInterface/sensor_%d-temp_%d-datetime_%lu/ HTTP/1.1",sensor,temp, epochSeconds);
GET_asClient(buf, TEMP_LOG_SERVER_IP);
}
void SendMsgToServer (char * msg)
{
char buf[255] = "";
sprintf(buf,"GET /restInterface/msg/%s/ HTTP/1.1",msg);
GET_asClient(buf, TEMP_LOG_SERVER_IP);
}
void LogToSDCard(char * msg)
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
//File dataFile = SD.open(FILENAME, FILE_WRITE);
digitalWrite(4,LOW);
delay(100);
file.open(&root,"tmplog.txt",FILE_WRITE | O_APPEND);
// if the file is available, write to it:
if (file.isOpen())
{
Serial.print("Logging to SD->");
Serial.println(msg);
file.println(msg);
file.close();
// print to the serial port too:
//Serial.println(dataString);
}
// if the file isn't open, pop up an error:
else
{
Serial.println("error opening datalog.txt");
SetMessage("SD ERROR", strlen("SD ERROR"));
}
delay(100);
digitalWrite(4,HIGH);
}