-
Notifications
You must be signed in to change notification settings - Fork 6
/
GsmMon.ino
370 lines (338 loc) · 8.36 KB
/
GsmMon.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
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
#include <OneWire.h>
#include <DS18B20.h>
#include <BME280.h>
#include <SimpleSIM.h>
#include <SH1106_I2C_Adaptor.h>
#include <glcd_fonts.h>
#include "nv_utils.h"
#define DS18B20PIN 17
#define GSM_BAUDS 9600
#define GSM_MAX_ERRS 10
#define GSM_RST_PIN 2
#define ON_PIN 16
#define ON_ADDR 0x10
#define REP_ADDR 0x14
#define PIN_ADDR 0x20
#define PEER_ADDR 0x30
#define PIN_MAX_LEN 14
#define PEER_LEN 14
#define PEER_OFF 6
// 128x64 OLED display adapter
// https://github.com/olegv142/Display
static SH1106_I2C_Adaptor g_display;
// BME280 environmental unit controller
// https://github.com/olegv142/BME280
static BME280Sensor g_bme;
// DS18B20 temperature sensor controller
// https://github.com/olegv142/OneWire
static OneWire g_w(DS18B20PIN);
// https://github.com/olegv142/DS18B20
static DS18B20 g_t(g_w);
static bool g_t_present;
// SIM800L GSM module controller
// https://github.com/olegv142/SimpleSIM
static SimpleSIM g_gsm(Serial, GSM_RST_PIN);
// GSM module context
static bool g_gsm_started;
static int8_t g_gsm_err_cnt;
static SIMHook g_gsm_csq("+CSQ");
static SIMHook g_gsm_cmt("+CMT");
static SIMHook g_gsm_msg("#");
// Current temperature and humidity readings
static String g_tstr;
static String g_hstr;
static String g_tstr2;
// AC switch On flag
static int8_t g_on;
// The reporting period in hours
static int8_t g_rep;
static bool g_rep_valid;
// The pin/password
static char g_pin[PIN_MAX_LEN+1];
static bool g_pin_valid;
// The peer address (phone number)
static char g_peer[PEER_LEN+1];
static bool g_peer_valid;
// The last report timestamp in milliseconds
static uint32_t g_last_rep;
static void init_sensors()
{
g_bme.begin();
g_bme.init(sp_62_5ms, sf_16);
g_bme.start(md_Normal, sx_1x, sx_16x, sx_1x);
uint8_t rom[8];
g_t_present = g_t.probe(rom);
if (g_t_present)
g_t.convert();
}
static void read_sensors()
{
int32_t T; // temperature in 0.01 DegC units
uint32_t P; // pressure in Pa with Q24.8 format (24 integer bits and 8 fractional bits)
uint32_t H; // humidity in %% with Q22.10 format (22 integer and 10 fractional bits)
if (g_bme.read32(&T, &P, &H))
{
char str[16] = {0};
String tstr(T/100., 1);
tstr += 'C';
g_tstr = tstr;
String hstr(H/1024);
hstr += '%';
g_hstr = hstr;
}
if (g_t_present) {
int16_t v;
g_t.read(v);
g_t.convert();
String tstr(v/16., 1);
tstr += 'C';
g_tstr2 = tstr;
}
}
static void update_display()
{
uint8_t W = g_display.width();
glcd_print_str_r(&g_display, 0, 0, W/2, g_tstr.c_str(), &g_font_Tahoma19x20, 1);
glcd_print_str_r(&g_display, 0, 4, W/2, g_hstr.c_str(), &g_font_Tahoma19x20, 1);
glcd_print_str_r(&g_display, W/2, 0, W/2, g_tstr2.c_str(), &g_font_Tahoma19x20, 1);
glcd_print_str_r(&g_display, W/2, 4, W/2, g_on ? "On" : "Off", &g_font_Tahoma19x20, 1);
}
static void init_gsm()
{
Serial.begin(GSM_BAUDS);
g_gsm.add_hook(&g_gsm_csq);
g_gsm.add_hook(&g_gsm_cmt);
g_gsm.add_hook(&g_gsm_msg);
g_gsm.begin();
}
static void reset_gsm()
{
g_gsm.reset();
g_gsm_err_cnt = 0;
g_gsm_started = false;
}
static void on_gsm_err()
{
if (++g_gsm_err_cnt >= GSM_MAX_ERRS) {
reset_gsm();
}
}
static inline String peer_addr()
{
return g_gsm_cmt.str().substring(PEER_OFF, PEER_OFF + PEER_LEN);
}
static inline const char* peer_addr_ptr()
{
return g_gsm_cmt.c_str() + PEER_OFF;
}
static bool send_report()
{
String sms_cmd("+CMGS=");
sms_cmd += peer_addr();
String resp('#');
resp += g_on;
resp += ' ';
resp += g_tstr;
resp += ' ';
resp += g_hstr;
resp += ' ';
resp += g_tstr2;
resp += ' ';
resp += g_rep;
resp += 'h';
resp += ' ';
if (sim_ok != g_gsm.send_cmd("+CSQ"))
return false;
resp += g_gsm_csq.str();
if (
sim_prompt == g_gsm.send_cmd(sms_cmd.c_str()) &&
sim_ok == g_gsm.send_msg(resp.c_str())
) {
g_last_rep = millis();
return true;
}
return false;
}
static void update_output()
{
digitalWrite(LED_BUILTIN, g_on);
digitalWrite(ON_PIN, g_on);
}
// Set AC switch state
static void set_output(bool on)
{
g_on = on;
nv_put(&g_on, sizeof(g_on), ON_ADDR);
update_output();
}
// Set new PIN
static void set_pin(const char* pin)
{
strncpy(g_pin, pin, PIN_MAX_LEN);
nv_put(&g_pin, PIN_MAX_LEN, PIN_ADDR);
g_pin_valid = (g_pin[0] != '\0');
}
// Set reporting interval
static void set_reporting_interval(const char* arg)
{
g_rep = atoi(arg);
nv_put(&g_rep, sizeof(g_rep), REP_ADDR);
g_rep_valid = g_rep > 0;
}
// Save sender address so it will be
// implicitely authenticated without PIN
static void save_peer_address()
{
const char* peer = peer_addr_ptr();
if (g_peer_valid && !strncmp(g_peer, peer, PEER_LEN))
return;
memcpy(g_peer, peer, PEER_LEN);
nv_put(&g_peer, PEER_LEN, PEER_ADDR);
g_peer_valid = true;
}
/*
* The incoming SMS message parser.
* The message always starts from the # symbol.
* It may be followed by the following tokens separated by the space or comma:
* 1 turn on AC switch
* 0 turn off AC switch
* /n set reporting interval to n hours
* pPIN authenticate with PIN
* PPIN set new PIN
* The response will be sent to any authenticated message, even the empty one
* (consisting from the single # symbol)
*/
static bool process_message()
{
char* ptr = g_gsm_msg.str().begin() + 1;
char *sptr = 0, *eptr = 0;
// The sender is authenticated implicitly unless the PIN is set and the sender address differs
// from the address used previously. In such case the new sender should provide PIN in order
// to be authenticated.
bool auth = !g_pin_valid || !g_peer_valid || !strncmp(g_peer, peer_addr_ptr(), PEER_LEN);
for (bool done = false; !done; ++ptr) {
if (*ptr) {
if (!sptr) {
// No current token
switch (*ptr) {
case '0':
case '1':
// AC switch control
if (auth)
set_output(*ptr == '1');
break;
case 'p':
case 'P':
case '/':
// Start collecting argument string
sptr = ptr;
break;
default:
;
}
} else {
switch (*ptr) {
case ' ':
case ',':
case '\r':
case '\n':
// Token delimiter found
eptr = ptr;
break;
}
}
} else {
// End of the message string reached
// Terminate current token if any
if (sptr)
eptr = ptr;
done = true;
}
if (eptr) {
// We have the token to process
char* arg = sptr + 1;
// Zero terminate token
*eptr = 0;
switch (*sptr) {
case 'p':
// Authenticate with PIN
auth = !g_pin_valid || !strcmp(g_pin, arg);
break;
case 'P':
if (auth) {
set_pin(arg);
}
break;
case '/':
if (auth) {
set_reporting_interval(arg);
}
break;
}
// We have done with current token
sptr = eptr = 0;
}
}
if (auth) {
// Save peer address and respond
save_peer_address();
return send_report();
}
return true;
}
static void process_gsm()
{
if (!g_gsm_started) {
if (g_gsm.start(GSM_BAUDS) == sim_ok) {
g_gsm_started = true;
} else {
on_gsm_err();
}
} else {
if (g_gsm_msg) {
if (process_message()) {
g_gsm_msg.reset();
} else {
on_gsm_err();
}
} else if (g_rep_valid && g_last_rep + g_rep * 3600000ULL < millis()) {
if (!send_report()) {
on_gsm_err();
}
}
}
}
static void init_output()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(ON_PIN, OUTPUT);
update_output();
}
// Read parameters from non-volatile memory
static void init_params()
{
nv_get(&g_on, sizeof(g_on), ON_ADDR);
g_rep_valid = nv_get(&g_rep, sizeof(g_rep), REP_ADDR) && g_rep > 0;
g_pin_valid = nv_get(&g_pin, PIN_MAX_LEN, PIN_ADDR) && g_pin[0] != '\0';
g_peer_valid = nv_get(&g_peer, PEER_LEN, PEER_ADDR) && g_peer[0] == '"' && g_peer[PEER_LEN-1] == '"';
}
void init_display()
{
g_display.init();
uint8_t W = g_display.width();
glcd_print_str(&g_display, 0, 0, "Starting..", &g_font_Tahoma19x20, 1);
glcd_print_str_r(&g_display, W/2, 4, W/2, g_on ? "On" : "Off", &g_font_Tahoma19x20, 1);
}
void setup() {
init_params();
init_output();
init_display();
init_gsm();
init_sensors();
}
void loop() {
g_gsm.wait(1000);
read_sensors();
update_display();
process_gsm();
}