-
Notifications
You must be signed in to change notification settings - Fork 0
/
rf22_sensor.ino
283 lines (248 loc) · 6.56 KB
/
rf22_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
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
// external libraries
#include <avr/wdt.h>
#include <DallasTemperature.h>
#include <RHReliableDatagram.h>
#include <LowPower.h>
#include <OneWire.h>
#if defined DRIVER_RF22
#include <RH_RF22.h>
#elif defined DRIVER_RF95
#include <RH_RF95.h>
#endif
#include <SPI.h>
//#define RAM_DEBUG
// project includes
#include "utils/utils.h"
#include "utils/Measurement.h"
#include "utils/Config.h"
#include "Streaming.h"
#if defined DRIVER_RF22
RH_RF22 radioDriver;
#elif defined DRIVER_RF95
RH_RF95 radioDriver;
#endif
RHReliableDatagram radio(radioDriver, CLIENT_ADDRESS);
Measurement measurement;
uint8_t outBuffer[MAX_MESSAGE_LEN];
uint8_t inBuffer[MAX_MESSAGE_LEN];
unsigned long sentOk = 0;
unsigned long sentFailed = 0;
int sentFailedPower = 0;
uint8_t sleepCycles = DEFAULT_SLEEP_CYCLES;
Config *config;
// ********************
// *** DECLARATIONS ***
// ********************
void measure(MeasurementType type) {
measurement.doMeasure(type);
}
void printStat() {
Serial << INFO_HEADER << F("ok/failed: ") << sentOk << "/" << sentFailed << endl;
}
void sendRF(String message) {
radioDriver.setModeIdle();
Serial << INFO_HEADER << F("Sending over RF: ") << message << endl;
if (message.length() < MAX_MESSAGE_LEN) {
strcpy((char *) outBuffer, (const char*) message.c_str());
} else {
Serial << ERROR_HEADER << F("Sending over RF failed - message too long!") << endl;
return;
}
if (radio.sendtoWait(outBuffer, sizeof(outBuffer), SERVER_ADDRESS)) {
sentOk++;
Serial << INFO_HEADER << F("send OK") << endl;
goodBlink();
Serial << INFO_HEADER << F("waiting for response ...") << endl;
uint8_t len = sizeof(inBuffer);
uint8_t from;
if (radio.recvfromAckTimeout(inBuffer, &len, 800, &from)) {
Serial << INFO_HEADER << F("Response received.") << endl;
sleepCycles = config->getTxPeriod();
sleepCycles = sleepCycles > MAX_SLEEP_CYCLES ? MAX_SLEEP_CYCLES : sleepCycles;
sleepCycles = sleepCycles < 1 ? 1 : sleepCycles;
Serial << INFO_HEADER << F("Setting sleepCycles to: ") << sleepCycles << endl;
uint8_t power = config->getTxPower();
// limit settings
#ifdef DRIVER_RF22
power = power > RH_RF22_TXPOW_20DBM ? RH_RF22_TXPOW_20DBM : power;
#elif defined DRIVER_RF95
power = power > 25 ? 25 : power;
#endif
Serial << INFO_HEADER << F("Setting txPower to: ") << power << endl;
radioDriver.setTxPower(power);
#ifdef RAM_DEBUG
Serial << freeRam() << endl;
#endif
} else {
Serial << WARN_HEADER << F("Waiting for response timed out") << endl;
}
} else {
sentFailed++;
sentFailedPower++;
if (sentFailedPower > SENT_FAILED_THRESHOLD) {
Serial << WARN_HEADER << F("sentFailedPower > ") << SENT_FAILED_THRESHOLD << endl;
Serial << WARN_HEADER << F("setting power to default: ") << DEFAULT_TX_POW << endl;
radioDriver.setTxPower(DEFAULT_TX_POW);
sentFailedPower = 0;
}
Serial << WARN_HEADER << F("send FAILED") << endl;
badBlink();
#ifdef RAM_DEBUG
Serial << freeRam() << endl;
#endif
}
#ifdef RAM_DEBUG
Serial << freeRam() << endl;
#endif
radioDriver.sleep();
}
void sendRFStat() {
String message;
message = String(MQTT_STAT_OK_TOPIC) + " ";
message += sentOk;
sendRF(message);
message = String(MQTT_STAT_ERR_TOPIC) + " ";
message += sentFailed;
sendRF(message);
}
void doAndSendAllMeasurements() {
Serial << F("-----") << endl;
#ifdef DS18B20_ENABLED
if (dsOk) {
measure(MeasurementType::dsTemp);
wdt_reset();
}
#endif
#ifdef DHT22_ENABLED
if (dhOk) {
measure(MeasurementType::dhTemp);
wdt_reset();
measure(MeasurementType::dhHum);
wdt_reset();
}
#endif
#ifdef SHT_ENABLED
if (shtOk) {
measure(MeasurementType::shtTemp);
wdt_reset();
measure(MeasurementType::shtHum);
wdt_reset();
}
#endif
#ifdef HTU21D_ENABLED
measure(MeasurementType::htuTemp);
wdt_reset();
measure(MeasurementType::htuHum);
wdt_reset();
#endif
measure(MeasurementType::battVoltage);
wdt_reset();
String measurementContent =
#if defined BATT_PIN
String(measurement.getBattVoltage()) + "/" +
#else
"0/" +
#endif
String(sentOk) + "/" +
String(sentFailed) + "/" +
#ifdef DS18B20_ENABLED
String(measurement.getDsTemp()) + "/" +
#else
"0/" +
#endif
#ifdef DHT22_ENABLED
String(measurement.getDhTemp()) + "/" +
String(measurement.getDhHum()) + "/" +
#else
"0/0/" +
#endif
#ifdef SHT_ENABLED
String(measurement.getShtTemp()) + "/" +
String(measurement.getShtHum()) + "/" +
#else
"0/0/" +
#endif
#ifdef HTU21D_ENABLED
String(measurement.getHtuTemp()) + "/" +
String(measurement.getHtuHum());
#else
"0/0";
#endif
sendRF(measurementContent);
wdt_reset();
printStat();
wdt_reset();
#ifdef RAM_DEBUG
Serial << freeRam() << endl;
#endif
delay(100);
}
void initialSendingLoop() {
for (int i=0; i<INITIAL_LOOP_CYCLES; i++) {
Serial << endl << INFO_HEADER << F("Initial loop ") << i << F("/") << INITIAL_LOOP_CYCLES << endl;
doAndSendAllMeasurements();
delay(5000);
}
}
// ********************
// **** EXECUTION *****
// ********************
void setup()
{
config = new Config(inBuffer);
Serial.begin(SERIAL_BAUD);
Serial << endl << endl << INFO_HEADER << F("Sensor rf22 start ...") << endl;
#ifdef RAM_DEBUG
Serial << freeRam() << endl;
#endif
// LED
pinMode(LED_PIN, OUTPUT);
initBlink();
// watchdog
wdt_reset();
Serial << INFO_HEADER << "enabling WDTO_8S" << endl;
wdt_enable(WDTO_8S);
// init measurement
if (!measurement.begin()) {
Serial << ERROR_HEADER << F("measurement init failed! Stopping.") << endl;
} else {
Serial << INFO_HEADER << F("measurement init ok ...") << endl;
}
// init radio
pinMode(RADIO_SHDN, OUTPUT);
radioOff();
radioOn();
delay(200);
if (!radio.init()) {
Serial << ERROR_HEADER << F("radio init failed! Stopping.") << endl;
while(1);
} else {
Serial << INFO_HEADER << F("radio init ok ...") << endl;
}
radioDriver.setTxPower(DEFAULT_TX_POW);
delay(200);
wdt_reset();
// initialSendingLoop();
}
void loop()
{
wdt_reset();
wdt_enable(WDTO_8S);
static unsigned char sleepCounter = 0;
if (sleepCounter%sleepCycles == 0) {
sleepCounter = 0;
doAndSendAllMeasurements();
}
sleepCounter++;
wdt_reset();
wdt_disable();
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
#ifdef RAM_DEBUG
int freeRam()
{
extern int __heap_start, *__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}
#endif