-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathreadsettings.ino
More file actions
145 lines (141 loc) · 4.32 KB
/
readsettings.ino
File metadata and controls
145 lines (141 loc) · 4.32 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
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
#include <SD.h>
#include <ctype.h>
File myFile;
struct parameters {
int interval;
boolean co2;
boolean temp;
boolean rh;
boolean lux;
boolean valid;
boolean heater;
String lokaal;
} settings;
// Setting for SD-card reader
const int chipSelect = 10;
void getSettings()
{
// Open the settings file for reading:
myFile = SD.open("settings.txt");
char character;
String description = "";
String value = "";
boolean valid = true;
// read from the file until there's nothing else in it:
while (myFile.available()) {
character = myFile.read();
if(character == '/') {
// Comment - ignore this line
while(character != '\n'){
character = myFile.read();
};
} else if(isalnum(character)) { // Add a character to the description
description.concat(character);
} else if(character =='=') { // start checking the value for possible results
// First going to trim out all trailing white spaces
do {
character = myFile.read();
} while(character == ' ');
if(description == "interval") {
value = "";
while(character != '\n') {
if(isdigit(character)) {
value.concat(character);
} else if(character != '\n') {
// Use of invalid values
valid = false;
}
character = myFile.read();
};
if (valid) {
// Convert string to array of chars
char charBuf[value.length()+1];
value.toCharArray(charBuf,value.length()+1);
// Convert chars to integer
settings.interval = atoi(charBuf);
} else {
// revert to default value for invalid entry in settings
settings.interval = 60;
}
} else if(description == "co2") {
if (character=='1') {
settings.co2 = true;
} else {
settings.co2 = false;
}
} else if(description == "rh") {
if (character=='1') {
settings.rh = true;
} else {
settings.rh = false;
}
} else if(description == "temp") {
if (character=='1') {
settings.temp = true;
} else {
settings.temp = false;
}
} else if(description == "lux") {
if (character=='1') {
settings.lux = true;
} else {
settings.lux = false;
}
} else if(description == "heater") {
if (character=='1') {
settings.heater = true;
} else {
settings.heater = false;
}
} else if(description == "location") {
value = "";
do {
value.concat(character);
character = myFile.read();
} while(character != '\n');
settings.lokaal = value;
}else { // unknown parameter
while(character != '\n')
character = myFile.read();
}
description = "";
} else {
// Ignore this character (could be space, tab, newline, carriage return or something else)
}
}
// close the file:
myFile.close();
}
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.println("initialization failed!");
return;
}
getSettings();
Serial.print("Interval: ");
Serial.println(settings.interval);
Serial.print("CO2: ");
if(settings.co2) { Serial.println("YES"); } else { Serial.println("NO"); }
Serial.print("TEMP:");
if(settings.temp) { Serial.println("YES"); } else { Serial.println("NO"); }
Serial.print("RH: ");
if(settings.rh) { Serial.println("YES"); } else { Serial.println("NO"); }
Serial.print("lux: ");
if(settings.lux) { Serial.println("YES"); } else { Serial.println("NO"); }
Serial.print("htr: ");
if(settings.heater) { Serial.println("YES"); } else { Serial.println("NO"); }
Serial.print("Lokaal: ");
Serial.println(settings.lokaal);
}
void loop()
{
// nothing happens after setup
}