-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensor.ino
More file actions
373 lines (260 loc) · 8.53 KB
/
sensor.ino
File metadata and controls
373 lines (260 loc) · 8.53 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SoftwareSerial.h>
#include <dht.h>
#define UNIT_NAME "sensorname"
#define HOST_NAME "hostname"
#define HOST_PORT 80
#define DATA_RELAY_INTERVAL 30000
#define generic_timeout 5000
#define WIFI_ENABLE_PIN 9
#define DHT22_PIN 8
#define ONE_WIRE_WATER_SENSOR_PIN 7
struct
{
uint32_t total;
uint32_t ok;
uint32_t crc_error;
uint32_t time_out;
uint32_t connect;
uint32_t ack_l;
uint32_t ack_h;
uint32_t unknown;
} stat = { 0,0,0,0,0,0,0,0};
//SoftwareSerial monitor_serial(2, 3); // RX, TX
dht DHT;
float water_temperature = 0.0;
OneWire waterTemperatureOneWire(ONE_WIRE_WATER_SENSOR_PIN);
DallasTemperature waterSensors(&waterTemperatureOneWire);
bool good_air_temperature_reading;
bool good_water_temperature_reading;
void setup() {
Serial.begin(9600);
delay(1000);
// while(!Serial) {
// ; // we wait for serial port to connect. Needed for native USB support only
// }
Serial.println("--setup routine starting.");
Serial.println("");
Serial1.begin(9600);
while(!Serial1) {
; // we wait for serial1 port to connect to the wifi module
}
// we delay/do a single read because we seem to have been getting odd temperature readings otherwise
waterSensors.begin();
delay(500);
get_water_temperature_info();
delay(500);
//Serial1.println("AT+UART_DEF=9600,8,1,0,0");
}
void loop() {
good_air_temperature_reading = false;
good_air_temperature_reading = false;
get_air_temperature_info();
get_water_temperature_info();
send_temperature_info();
delay(DATA_RELAY_INTERVAL);
}
bool get_air_temperature_info() {
int chk = DHT.read22(DHT22_PIN);
if(chk != DHTLIB_OK) {
Serial.println("--there was an error getting the air temperature sensor info.");
return false;
}
Serial.println("--temperature info received from air sensor.");
Serial.print("--temperature: ");
Serial.println(DHT.temperature);
Serial.print("--rel humidity: ");
Serial.println(DHT.humidity);
good_air_temperature_reading = true;
return true;
}
bool get_water_temperature_info() {
good_water_temperature_reading = waterSensors.requestTemperaturesByIndex(0);
water_temperature = waterSensors.getTempCByIndex(0);
Serial.println("--temperature info received from water sensor.");
Serial.print("--temperature: ");
Serial.println(water_temperature);
good_water_temperature_reading = true;
return true;
}
void send_temperature_info() {
String host_string = String(HOST_NAME);
Serial.println("");
Serial.println("--starting send data function.");
Serial.println("--initializing/reseting wifi module.");
pinMode(WIFI_ENABLE_PIN, OUTPUT);
digitalWrite(WIFI_ENABLE_PIN, LOW);
delay(100);
digitalWrite(WIFI_ENABLE_PIN, HIGH);
delay(1000);
//Serial1.println("AT+CIPMODE=1");
//delay(2000);
while(Serial1.available()) {
String s = Serial1.readString();
Serial.println(s);
}
Serial.println("--finished initializing wifi module.");
//get_wifi_version();
if(wifi_set_mode(3)) {
Serial.println("--wifi mode set.");
} else {
Serial.println("--error setting wifi mode.");
}
//if(wifi_join_access_point("blurb", "blurb222", 10000)) {
if(wifi_join_access_point("ssid", "password", 20000)) {
Serial.println("--connected to ssid.");
} else {
Serial.println("--unable to connect to ssid.");
}
if(wifi_set_mux(0)) {
Serial.println("--wifi mux set.");
} else {
Serial.println("--error setting wifi mux.");
}
if(wifi_create_tcp(host_string, HOST_PORT)) {
Serial.println("--tcp setup.");
} else {
Serial.println("--tcp setup faled.");
}
uint8_t buffer[1024] = {0};
// first we build a string with our request, embedding our information as GET variables
// it is easier to build a string than a character array, so we simply do that and then
// convert to a character array
//String req_string = "GET /ip";
String req_string = "GET /temp/" + String(UNIT_NAME);
req_string = req_string + "/" + String(good_air_temperature_reading ? DHT.temperature : 0);
req_string = req_string + "/" + String(good_air_temperature_reading ? DHT.humidity : 0);
req_string = req_string + "/" + String(water_temperature);
req_string = req_string + "/" + String(good_air_temperature_reading ? "1" : "0");
req_string = req_string + "/" + String(good_water_temperature_reading ? "1" : "0");
req_string = req_string + " HTTP/1.1\r\nHost: " + host_string + "\r\nConnection: close\r\n\r\n";
Serial.println("--request string is:");
Serial.println(req_string);
Serial.println("");
int req_len = req_string.length();
char reqx[req_len+1];
Serial.println("--request char array is (should be identical to string):");
req_string.toCharArray(reqx, req_len + 1);
Serial.println(reqx);
Serial.println("--attempting to send data. currently no error checking in place");
wifi_send(reqx);
Serial.println("--finished attempt. data may or may not have been sent");
Serial.println("");
Serial.println("--attempting to close the tcp connection");
wifi_close_tcp();
Serial.println("--send data function complete.");
Serial.println("");
}
void wifi_get_version() {
Serial1.println("AT+GMR");
unsigned long start = millis();
while (millis() - start < generic_timeout) {
if(Serial1.available()) {
String s = Serial1.readString();
Serial.println(s);
return;
}
}
Serial.println("unable to retrieve version information.");
}
bool wifi_set_mode(int mode) {
Serial1.print("AT+CWMODE=");
Serial1.println(mode);
unsigned long start = millis();
while (millis() - start < generic_timeout) {
if(Serial1.available()) {
String s = Serial1.readString();
Serial.println(s);
if(s.indexOf("OK") != -1) {
return true;
}
//Serial.write((char)Serial1.read());
//delay(10);
}
}
return false;
}
bool wifi_join_access_point(String SSID, String password, int join_timeout) {
Serial1.print("AT+CWJAP=\"");
Serial1.print(SSID);
Serial1.print("\",\"");
Serial1.print(password);
Serial1.println("\"");
unsigned long start = millis();
while (millis() - start < join_timeout) {
if(Serial1.available()) {
String s = Serial1.readString();
Serial.println(s);
if(s.indexOf("WIFI GOT IP") != -1) {
return true;
}
//return true;
}
}
return false;
}
bool wifi_set_mux(int mux) {
Serial1.print("AT+CIPMUX=");
Serial1.println(mux);
unsigned long start = millis();
while (millis() - start < generic_timeout) {
if(Serial1.available()) {
String s = Serial1.readString();
Serial.println(s);
if(s.indexOf("OK") != -1) {
return true;
}
}
}
return false;
}
bool wifi_create_tcp(String host_name, int host_port) {
Serial1.print("AT+CIPSTART=\"TCP\",\"");
Serial1.print(host_name);
Serial1.print("\",");
Serial1.println(host_port);
unsigned long start = millis();
while (millis() - start < generic_timeout) {
if(Serial1.available()) {
String s = Serial1.readString();
Serial.println(s);
if(s.indexOf("OK") != -1) {
return true;
}
}
}
return false;
}
bool wifi_close_tcp() {
Serial.println("destroying tcp");
Serial1.println("AT+CIPCLOSE");
unsigned long start = millis();
while (millis() - start < generic_timeout) {
if(Serial1.available()) {
String s = Serial1.readString();
Serial.print(s);
if(s.indexOf("OK") != -1) {
return true;
}
return false;
}
}
return false;
}
bool wifi_send(char *req) {
// need to add some logic in this functio to know that this was done properly.
Serial1.print("AT+CIPSEND=");
Serial1.println(strlen(req));
delay(1000);
Serial1.println(req);
bool req_sent = false;
unsigned long start = millis();
while (millis() - start < generic_timeout * 2) {
while(Serial1.available()) {
byte s = Serial1.read();
Serial.write(s);
}
}
return false;
}