-
Notifications
You must be signed in to change notification settings - Fork 2
/
heatseek_sensor.ino
166 lines (128 loc) · 4.05 KB
/
heatseek_sensor.ino
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
#include <Wire.h>
#include <DHT.h>
#include <SD.h>
#include <SPI.h>
#include "user_config.h"
#include "transmit.h"
#include "config.h"
#include "watchdog.h"
#include "rtc.h"
static DHT dht(DHT_DATA, DHT22);
uint32_t startup_millis = 0;
void setup() {
watchdog_init();
Serial.begin(9600);
delay(2000);
Serial.print("initializing heatseek data logger: ");
#ifdef TRANSMITTER_WIFI
Serial.println("WIFI");
#else
Serial.println("cellular");
#endif
initialize_sd();
rtc_initialize();
dht.begin();
if (!read_config()) set_default_config();
watchdog_feed();
startup_millis = millis();
}
void loop() {
float temperature_f;
float humidity;
float heat_index;
int32_t current_time = rtc.now().unixtime();
int32_t last_reading_time = get_last_reading_time();
int32_t time_since_last_reading = current_time - last_reading_time;
char command = Serial.read();
if (command == 'C') {
enter_configuration();
}
Serial.print("Time since last reading: ");
Serial.print(time_since_last_reading);
Serial.print(", reading_interval: ");
Serial.print(CONFIG.data.reading_interval_s);
Serial.print(". Code version: ");
Serial.print(CODE_VERSION);
Serial.println(". Press 'C' to enter config.");
if (millis() - startup_millis < 15000) {
Serial.println("Allowing 15 seconds to enter config mode [C] before taking first reading.");
watchdog_feed();
delay(2000);
return;
}
if (CONFIG.data.reading_interval_s - time_since_last_reading > SEND_SAVED_READINGS_THRESHOLD) {
Serial.println("Checking for queued temperature readings");
watchdog_feed();
transmit_queued_temps();
delay(2000);
watchdog_feed();
return;
} else if (time_since_last_reading < CONFIG.data.reading_interval_s) {
delay(2000);
watchdog_feed();
return;
}
watchdog_feed();
read_temperatures(&temperature_f, &humidity, &heat_index);
log_to_sd(temperature_f, humidity, heat_index, current_time);
watchdog_feed();
update_last_reading_time(current_time);
watchdog_feed();
transmit(temperature_f, humidity, heat_index, current_time);
watchdog_feed();
delay(2000);
}
void read_temperatures(float *temperature_f, float *humidity, float *heat_index) {
while (true) {
bool success = true;
*temperature_f = dht.readTemperature(true);
*humidity = dht.readHumidity();
if (!isnan(*temperature_f) && !isnan(*humidity)) {
Serial.print("Temperature (actual reading): ");
Serial.print(*temperature_f);
Serial.println(" *F");
*temperature_f = *temperature_f + CONFIG.data.temperature_offset_f;
Serial.print("Temperature (after calibration): ");
Serial.print(*temperature_f);
Serial.println(" *F");
Serial.print("Humidity: ");
Serial.print(*humidity);
Serial.println("%");
*heat_index = dht.computeHeatIndex(*temperature_f, *humidity);
Serial.print("Heat index: ");
Serial.println(*heat_index);
return;
} else {
Serial.println("Error reading temperatures!");
}
delay(2000);
// if we continue to fail to read a temperature, the watchdog will
// eventually cause a reboot
}
}
void log_to_sd(float temperature_f, float humidity, float heat_index, uint32_t current_time) {
Serial.println("writing to SD card...");
File data_file;
if (data_file = SD.open("data.csv", FILE_WRITE)) {
data_file.print(current_time); data_file.print(",");
data_file.print(temperature_f); data_file.print(",");
data_file.print(humidity); data_file.print(",");
data_file.print(heat_index); data_file.println();
Serial.println("wrote to SD");
data_file.close();
} else {
Serial.println("unable to open data.csv");
while(true); // watchdog will reboot
}
}
void initialize_sd() {
// Stop LORA module from interfering with SPI
#ifdef TRANSMITTER_GSM
pinMode(LORA_CS, OUTPUT);
digitalWrite(LORA_CS, HIGH);
#endif
while (!SD.begin(SD_CS)) {
Serial.println("failed to initialize SD card");
delay(1000); // watchdog will reboot
}
}