-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatena4460_sensor.ino
681 lines (569 loc) · 18.3 KB
/
catena4460_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
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/* catena4460m101_sensor.ino Mon Jan 08 2018 13:11:40 mukeshbharath */
/*
Module: catena4460m101_sensor.ino
Function:
Code for testing the low power state of Catena 4450.
Version:
V0.1.0 Mon Jan 08 2018 13:11:40 mukeshbharath Edit level 1
Copyright notice:
This file copyright (C) 2017 by
MCCI Corporation
3520 Krums Corners Road
Ithaca, NY 14850
An unpublished work. All rights reserved.
This file is proprietary information, and may not be disclosed or
copied without the prior permission of MCCI Corporation.
Author:
Mukesh Bharath Ramalingam, MCCI Corporation December 2017
Revision history:
0.1.0 Mon Jan 08 2018 13:11:40 mukeshbharath
Module created.
*/
#include "ThisCatena.h"
#include <Catena_Led.h>
#include <Catena_TxBuffer.h>
#include <Catena_CommandStream.h>
#include <Catena_Totalizer.h>
#include <Wire.h>
//#include <Adafruit_BME280.h>
/* Catena 4460 */
#include <Adafruit_BME680.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
/* Catena 4460 */
#include <Arduino_LoRaWAN.h>
#include <BH1750.h>
#include <lmic.h>
#include <hal/hal.h>
#include <mcciadk_baselib.h>
#include <cmath>
#include <type_traits>
/****************************************************************************\
|
| Manifest constants & typedefs.
|
| This is strictly for private types and constants which will not
| be exported.
|
\****************************************************************************/
using namespace McciCatena;
//using ThisCatena = Catena4450;
/* how long do we wait between measurements (in seconds) */
enum {
// set this to interval between measurements, in seconds
// Actual time will be a little longer because have to
// add measurement and broadcast time.
CATCFG_T_CYCLE = 6 * 60, // ten messages/hour
CATCFG_T_WARMUP = 1,
CATCFG_T_SETTLE = 5,
CATCFG_T_INTERVAL = CATCFG_T_CYCLE - (CATCFG_T_WARMUP +
CATCFG_T_SETTLE),
};
// forwards
static void settleDoneCb(osjob_t *pSendJob);
static void warmupDoneCb(osjob_t *pSendJob);
static void txFailedDoneCb(osjob_t *pSendJob);
static void sleepDoneCb(osjob_t *pSendJob);
static Arduino_LoRaWAN::SendBufferCbFn sendBufferDoneCb;
/* Catena 4460 */
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
/* Catena 4460 */
/****************************************************************************\
|
| Read-only data.
|
| If program is to be ROM-able, these must all be tagged read-only
| using the ROM storage class; they may be global.
|
\****************************************************************************/
static const char sVersion[] = "0.1.0";
/****************************************************************************\
|
| VARIABLES:
|
| If program is to be ROM-able, these must be initialized
| using the BSS keyword. (This allows for compilers that require
| every variable to have an initializer.) Note that only those
| variables owned by this module should be declared here, using the BSS
| keyword; this allows for linkers that dislike multiple declarations
| of objects.
|
\****************************************************************************/
// globals
ThisCatena gCatena;
//
// the LoRaWAN backhaul. Note that we use the
// ThisCatena version so it can provide hardware-specific
// information to the base class.
//
ThisCatena::LoRaWAN gLoRaWAN;
//
// the LED
//
StatusLed gLed (ThisCatena::PIN_STATUS_LED);
// the RTC instance, used for sleeping
#ifdef ARDUINO_ARCH_SAMD
CatenaRTC gRtc;
#elif defined(ARDUINO_ARCH_STM32)
CatenaStm32L0Rtc gRtc;
#endif
// The temperature/humidity sensor
//Adafruit_BME280 bme; // The default initalizer creates an I2C connection
/* Catena 4460 */
Adafruit_BME680 bme; // I2C
/* Catena 4460 */
bool fBme;
// The LUX sensor
BH1750 bh1750;
bool fLux;
// The contact sensors
bool fHasPower1;
uint8_t kPinPower1P1;
uint8_t kPinPower1P2;
cTotalizer gPower1P1;
cTotalizer gPower1P2;
// the job that's used to synchronize us with the LMIC code
static osjob_t sensorJob;
void sensorJob_cb(osjob_t *pJob);
// function for scaling power
static uint16_t
dNdT_getFrac(
uint32_t deltaC,
uint32_t delta_ms
);
/*
Name: setup()
Function:
Arduino setup function.
Definition:
void setup(
void
);
Description:
This function is called by the Arduino framework after
basic framework has been initialized. We initialize the sensors
that are present on the platform, set up the LoRaWAN connection,
and (ultimately) return to the framework, which then calls loop()
forever.
Returns:
No explicit result.
*/
void setup(void)
{
gCatena.begin();
gCatena.SafePrintf("Catena 4460 sensor1 V%s\n", sVersion);
gLed.begin();
gCatena.registerObject(&gLed);
// set up the RTC object
gRtc.begin();
gCatena.SafePrintf("LoRaWAN init: ");
if (!gLoRaWAN.begin(&gCatena))
{
gCatena.SafePrintf("failed\n");
gCatena.registerObject(&gLoRaWAN);
}
else
{
gCatena.SafePrintf("OK\n");
gCatena.registerObject(&gLoRaWAN);
}
ThisCatena::UniqueID_string_t CpuIDstring;
gCatena.SafePrintf("CPU Unique ID: %s\n",
gCatena.GetUniqueIDstring(&CpuIDstring)
);
/* find the platform */
const ThisCatena::EUI64_buffer_t *pSysEUI = gCatena.GetSysEUI();
uint32_t flags;
const CATENA_PLATFORM * const pPlatform = gCatena.GetPlatform();
if (pPlatform)
{
gCatena.SafePrintf("EUI64: ");
for (unsigned i = 0; i < sizeof(pSysEUI->b); ++i)
{
gCatena.SafePrintf("%s%02x", i == 0 ? "" : "-", pSysEUI->b[i]);
}
gCatena.SafePrintf("\n");
flags = gCatena.GetPlatformFlags();
gCatena.SafePrintf(
"Platform Flags: %#010x\n",
flags
);
gCatena.SafePrintf(
"Operating Flags: %#010x\n",
gCatena.GetOperatingFlags()
);
}
else
{
gCatena.SafePrintf("**** no platform, check provisioning ****\n");
flags = 0;
}
/* initialize the lux sensor */
if (flags & ThisCatena::fHasLuxRohm)
{
bh1750.begin();
fLux = true;
bh1750.configure(BH1750_CONTINUOUS_HIGH_RES_MODE_2);
}
else
{
fLux = false;
}
/* initialize the BME280 */
/*
if (!bme.begin(BME280_ADDRESS, Adafruit_BME280::OPERATING_MODE::Sleep))
{
gCatena.SafePrintf("No BME280 found: check wiring\n");
fBme = false;
}
else
{
fBme = true;
}
*/
/* Catena 4460 */
if (!bme.begin())
{
gCatena.SafePrintf("No BME680 found: check wiring\n");
fBme = false;
}
else
{
fBme = true;
}
/* Set up oversampling and filter initialization */
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
/* Catena 4460 */
/* is it modded? */
uint32_t modnumber = gCatena.PlatformFlags_GetModNumber(flags);
fHasPower1 = false;
if (modnumber != 0)
{
gCatena.SafePrintf("Catena 4450-M%u\n", modnumber);
if (modnumber == 101)
{
fHasPower1 = true;
kPinPower1P1 = A0;
kPinPower1P2 = A1;
}
else
{
gCatena.SafePrintf("unknown mod number %d\n", modnumber);
}
}
else
{
gCatena.SafePrintf("No mods detected\n");
}
if (fHasPower1)
{
if (! gPower1P1.begin(kPinPower1P1) ||
! gPower1P2.begin(kPinPower1P2))
{
fHasPower1 = false;
}
}
/* now, we kick off things by sending our first message */
gLed.Set(LedPattern::Joining);
// unit testing for the scaling functions
//gCatena.SafePrintf(
// "dNdT_getFrac tests: "
// "0/0: %04x 90/6m: %04x 89/6:00.1: %04x 1439/6m: %04x\n",
// dNdT_getFrac(0, 0),
// dNdT_getFrac(90, 6 * 60 * 1000),
// dNdT_getFrac(89, 6 * 60 * 1000 + 100),
// dNdT_getFrac(1439, 6 * 60 * 1000)
// );
//gCatena.SafePrintf(
// "dNdT_getFrac tests: "
// "1/6m: %04x 20/6m: %04x 1/60:00.1: %04x 1440/5:59.99: %04x\n",
// dNdT_getFrac(1, 6 * 60 * 1000),
// dNdT_getFrac(20, 6 * 60 * 1000),
// dNdT_getFrac(1, 60 * 60 * 1000 + 100),
// dNdT_getFrac(1440, 6 * 60 * 1000 - 10)
// );
/* warm up the BME280 by discarding a measurement */
if (fBme)
(void)bme.readTemperature();
/* trigger a join by sending the first packet */
startSendingUplink();
}
// The Arduino loop routine -- in our case, we just drive the other loops.
// If we try to do too much, we can break the LMIC radio. So the work is
// done by outcalls scheduled from the LMIC os loop.
void loop()
{
gCatena.poll();
}
static uint16_t dNdT_getFrac(
uint32_t deltaC,
uint32_t delta_ms
)
{
if (delta_ms == 0 || deltaC == 0)
return 0;
// this is a value in [0,1)
float dNdTperHour = float(deltaC * 250) / float(delta_ms);
if (dNdTperHour <= 0)
return 0;
else if (dNdTperHour >= 1)
return 0xFFFF;
else
{
int iExp;
float normalValue;
normalValue = frexpf(dNdTperHour, &iExp);
// dNdTperHour is supposed to be in [0..1), so useful exp
// is [0..-15]
iExp += 15;
if (iExp < 0)
iExp = 0;
if (iExp > 15)
return 0xFFFF;
return (uint16_t)((iExp << 12u) + (unsigned) scalbnf(normalValue, 12));
}
}
void startSendingUplink(void)
{
TxBuffer_t b;
LedPattern savedLed = gLed.Set(LedPattern::Measuring);
b.begin();
FlagsSensor2 flag;
flag = FlagsSensor2(0);
b.put(FormatSensor2); /* the flag for this record format */
uint8_t * const pFlag = b.getp();
b.put(0x00); /* will be set to the flags */
// vBat is sent as 5000 * v
float vBat = gCatena.ReadVbat();
gCatena.SafePrintf("vBat: %d mV\n", (int) (vBat * 1000.0f));
b.putV(vBat);
flag |= FlagsSensor2::FlagVbat;
uint32_t bootCount;
if (gCatena.getBootCount(bootCount))
{
b.putBootCountLsb(bootCount);
flag |= FlagsSensor2::FlagBoot;
}
/*
if (fBme)
{
Adafruit_BME280::Measurements m = bme.readTemperaturePressureHumidity();
// temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C
// pressure is 2 bytes, hPa * 10.
// humidity is one byte, where 0 == 0/256 and 0xFF == 255/256.
gCatena.SafePrintf(
"BME280: T: %d P: %d RH: %d\n",
(int) m.Temperature,
(int) m.Pressure,
(int) m.Humidity
);
b.putT(m.Temperature);
b.putP(m.Pressure);
b.putRH(m.Humidity);
flag |= FlagsSensor2::FlagTPH;
}
*/
/* Catena 4460 */
if (fBme)
{
if (! bme.performReading()) {
gCatena.SafePrintf("Failed to perform reading :(");
return;
}
gCatena.SafePrintf("BME680: Temperature = %f *C\n", bme.temperature);
gCatena.SafePrintf("Pressure = %f hPa\n", (bme.pressure / 100.0));
gCatena.SafePrintf("Humidity = %f %\n", bme.humidity);
gCatena.SafePrintf("Gas = %f KOhms\n", (bme.gas_resistance / 1000.0));
gCatena.SafePrintf("Approx. Altitude = %f m\n\n", bme.readAltitude(SEALEVELPRESSURE_HPA));
/*
Serial.print("Temperature = ");
Serial.print(bme.temperature);
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.pressure / 100.0);
Serial.println(" hPa");
Serial.print("Humidity = ");
Serial.print(bme.humidity);
Serial.println(" %");
Serial.print("Gas = ");
Serial.print(bme.gas_resistance / 1000.0);
Serial.println(" KOhms");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.println();
*/
b.putT(bme.temperature);
b.putP(bme.pressure);
b.putRH(bme.humidity);
b.putG(bme.gas_resistance);
b.putG(bme.readAltitude(SEALEVELPRESSURE_HPA));
flag |= FlagsSensor2::FlagTPH;
}
/* Catena 4460 */
if (fLux)
{
/* Get a new sensor event */
uint16_t light;
light = bh1750.readLightLevel();
gCatena.SafePrintf("BH1750: %u lux\n", light);
b.putLux(light);
flag |= FlagsSensor2::FlagLux;
}
if (fHasPower1)
{
uint32_t power1in, power1out;
uint32_t power1in_dc, power1in_dt;
uint32_t power1out_dc, power1out_dt;
power1in = gPower1P1.getcurrent();
gPower1P1.getDeltaCountAndTime(power1in_dc, power1in_dt);
gPower1P1.setReference();
power1out = gPower1P2.getcurrent();
gPower1P2.getDeltaCountAndTime(power1out_dc, power1out_dt);
gPower1P2.setReference();
gCatena.SafePrintf(
"Power: IN: %u OUT: %u\n",
power1in,
power1out
);
b.putWH(power1in);
b.putWH(power1out);
flag |= FlagsSensor2::FlagWattHours;
// we know that we get at most 4 pulses per second, no matter
// the scaling. Therefore, if we convert to pulses/hour, we'll
// have a value that is no more than 3600 * 4, or 14,400.
// This fits in 14 bits. At low pulse rates, there's more
// info in the denominator than in the numerator. So FP is really
// called for. We use a simple floating point format, since this
// is unsigned: 4 bits of exponent, 12 bits of fraction, and we
// don't bother to omit the MSB of the (normalized fraction);
// and we multiply by 2^(exp+1) (so 0x0800 is 2 * 0.5 == 1,
// 0xF8000 is 2^16 * 1 = 65536).
uint16_t fracPower1In = dNdT_getFrac(power1in_dc, power1in_dt);
uint16_t fracPower1Out = dNdT_getFrac(power1out_dc, power1out_dt);
b.putPulseFraction(
fracPower1In
);
b.putPulseFraction(
fracPower1Out
);
gCatena.SafePrintf(
"Power: IN: %u/%u (%04x) OUT: %u/%u (%04x)\n",
power1in_dc, power1in_dt,
fracPower1In,
power1out_dc, power1out_dt,
fracPower1Out
);
flag |= FlagsSensor2::FlagPulsesPerHour;
}
*pFlag = uint8_t(flag);
if (savedLed != LedPattern::Joining)
gLed.Set(LedPattern::Sending);
else
gLed.Set(LedPattern::Joining);
gLoRaWAN.SendBuffer(b.getbase(), b.getn(), sendBufferDoneCb, NULL);
}
static void
sendBufferDoneCb(
void *pContext,
bool fStatus
)
{
osjobcb_t pFn;
gLed.Set(LedPattern::Settling);
if (! fStatus)
{
gCatena.SafePrintf("send buffer failed\n");
pFn = txFailedDoneCb;
}
else
{
pFn = settleDoneCb;
}
os_setTimedCallback(
&sensorJob,
os_getTime()+sec2osticks(CATCFG_T_SETTLE),
pFn
);
}
static void
txFailedDoneCb(
osjob_t *pSendJob
)
{
gCatena.SafePrintf("not provisioned, idling\n");
gLoRaWAN.Shutdown();
gLed.Set(LedPattern::NotProvisioned);
}
//
// the following API is added to delay.c, .h in the BSP. It adjust millis()
// forward after a deep sleep.
//
// extern "C" { void adjust_millis_forward(unsigned); };
//
// If you don't have it, check the following commit at github:
// https://github.com/mcci-catena/ArduinoCore-samd/commit/78d8440dbcd29bf5ac659fd65514268c1334f683
//
static void settleDoneCb(
osjob_t *pSendJob
)
{
uint32_t startTime;
#if defined(ARDUINO_ARCH_SAMD)
const bool fNoSleep = Serial.dtr() || fHasPower1;
#else
const bool fNoSleep = true;
#endif
// if connected to USB, don't sleep
// ditto if we're monitoring pulses.
if (fNoSleep)
{
gLed.Set(LedPattern::Sleeping);
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_INTERVAL),
sleepDoneCb
);
return;
}
#if defined(ARDUINO_ARCH_SAMD)
/* ok... now it's time for a deep sleep */
gLed.Set(LedPattern::Off);
startTime = millis();
gRtc.SetAlarm(CATCFG_T_INTERVAL);
gRtc.SleepForAlarm(
gRtc.MATCH_HHMMSS,
gRtc.SleepMode::IdleCpuAhbApb
);
// add the number of ms that we were asleep to the millisecond timer.
// we don't need extreme accuracy.
adjust_millis_forward(CATCFG_T_INTERVAL * 1000);
/* and now... we're awake again. trigger another measurement */
sleepDoneCb(pSendJob);
#endif // ARDUINO_ARCH_SAMD
}
static void sleepDoneCb(
osjob_t *pJob
)
{
gLed.Set(LedPattern::WarmingUp);
os_setTimedCallback(
&sensorJob,
os_getTime() + sec2osticks(CATCFG_T_WARMUP),
warmupDoneCb
);
}
static void warmupDoneCb(
osjob_t *pJob
)
{
startSendingUplink();
}