-
Notifications
You must be signed in to change notification settings - Fork 1
/
dino.ino
116 lines (95 loc) · 2.92 KB
/
dino.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
/*
* dino.ino
* DINO project aims to demonstrate usage of SigFox.
* A package is equipped with Sigfox enabled MCU.
* When it is opened or dropped a messsage is sent to SigFox backend.
*/
// Includes
#include <Arduino.h>
#include <SigFox.h>
#include <ArduinoLowPower.h>
#include <Wire.h>
#include "accelerometerSigfoxSensor.h"
#include "compositeDataSink.h"
#include "sigfoxMessage.h"
#include "humiditySigfoxSensor.h"
#include "lightSigfoxSensor.h"
// Global settings
#define DEBUG // Debug ON
#define REPORT_FREQUENCY_MILLIS 60000 // Send message every 60s
#define LIGHT_SENSOR_POLL_MILLIS 5000 // Send message every 60s
// Prototypes
void freeFallDetectedISR();
// Only print messages when debug is ON
#ifdef DEBUG
# define LOG(x) Serial.println(x)
#else
# define LOG(x) do {} while (0)
#endif
CompositeDataSink dataSink;
AccelerometerSigfoxSensor accelerometer;
HumiditySigfoxSensor humidity;
LightSigfoxSensor light;
SigfoxMessage sigfoxMessage;
// Run once
void setup() {
// Debug serial port start
Serial.begin(9600);
digitalWrite(LED_BUILTIN, HIGH);
LOG("Setup start");
delay(1000); // if not Serial not ready, slow!
digitalWrite(LED_BUILTIN, LOW);
while (!Serial) {};
// Starting SigFox module
if (!SigFox.begin()) {
LOG("SigFox begin failed, rebooting!");
NVIC_SystemReset();
while (1);
}
// Enable debug and print useful data
SigFox.debug();
LOG("SigFox FW version " + SigFox.SigVersion());
LOG("ID = " + SigFox.ID());
LOG("PAC = " + SigFox.PAC());
LOG("");
// Send the module to the deepest sleep
SigFox.end();
accelerometer.initialise();
humidity.initialise();
light.initialise();
// Can be used to show info
//pinMode(LED_BUILTIN, OUTPUT);
// Attach pin 0 and 1 interrupts on voltage falling event
pinMode(1, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(1), AccelerometerSigfoxSensor::freeFallDetectedISR, FALLING);
LOG("Setup done");
}
// Run forever
void loop()
{
// Create time triggers
static unsigned long timeout = 0;
static unsigned long timeout_light = 0;
// Poll light sensor periodically
if( millis() >= timeout_light){
// Compute new time triggers
timeout_light = millis() + LIGHT_SENSOR_POLL_MILLIS;
//Read light sensor
LOG("Polling light sensor");
light.updatePacketOpenState();
}
// Send message via SigFox after 60s or when packet is dropped
if ((millis() >= timeout) || accelerometer.packetDropped() || light.getPacketOpen())
{
// Compute new time triggers
timeout = millis() + REPORT_FREQUENCY_MILLIS;
timeout_light = millis() + LIGHT_SENSOR_POLL_MILLIS;
// Populate and send SigFox Message
LOG("Sending SigFox message");
accelerometer.logMessage(sigfoxMessage);
humidity.logMessage(sigfoxMessage);
light.updatePacketOpenState();
light.logMessage(sigfoxMessage);
dataSink.sendData(sigfoxMessage.message(), 12);
}
}