-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsigfox_smart_weather.ino
128 lines (111 loc) · 2.45 KB
/
sigfox_smart_weather.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
#include <LPS25H.h>
#include <HTS221.h>
#include <Wire.h>
#include <Arduino.h>
#define SIGFOX_FRAME_LENGTH 12
#define INTERVAL 600000
#define DEBUG 0
unsigned long previousSendTime = 0;
struct data {
int humidity;
float temperature;
int pressure;
};
void setup() {
// Init UART devices
if (DEBUG) {
SerialUSB.begin(115200);
}
smeHumidity.begin();
smePressure.begin();
SigFox.begin(19200);
initSigfox();
}
void loop() {
data frame;
frame.humidity = smeHumidity.readHumidity();
frame.temperature = (smeHumidity.readTemperature() + smePressure.readTemperature())/2.0;
frame.pressure = smePressure.readPressure();
if (DEBUG) {
SerialUSB.print("Temp ");
SerialUSB.println(frame.temperature, 6);
SerialUSB.print("\tHumidity ");
SerialUSB.println(frame.humidity);
SerialUSB.print("\tPressure ");
SerialUSB.println(frame.pressure);
}
bool answer = sendSigfox(&frame, sizeof(data));
// Light LED depending on modem answer
if (answer) {
ledGreenLight(HIGH);
ledRedLight(LOW);
} else {
ledGreenLight(LOW);
ledRedLight(HIGH);
}
delay(1000);
ledGreenLight(LOW);
ledRedLight(LOW);
delay(INTERVAL);
}
void initSigfox(){
SigFox.print("+++");
while (!SigFox.available()){
delay(100);
}
while (SigFox.available()){
byte serialByte = SigFox.read();
if (DEBUG){
SerialUSB.print(serialByte);
}
}
if (DEBUG){
SerialUSB.println("\n ** Setup OK **");
}
}
String getSigfoxFrame(const void* data, uint8_t len){
String frame = "";
uint8_t* bytes = (uint8_t*)data;
if (len < SIGFOX_FRAME_LENGTH){
//fill with zeros
uint8_t i = SIGFOX_FRAME_LENGTH;
while (i-- > len){
frame += "00";
}
}
//0-1 == 255 --> (0-1) > len
for(uint8_t i = len-1; i < len; --i) {
if (bytes[i] < 16) {frame+="0";}
frame += String(bytes[i], HEX);
}
return frame;
}
bool sendSigfox(const void* data, uint8_t len){
String frame = getSigfoxFrame(data, len);
String status = "";
char output;
if (DEBUG){
SerialUSB.print("AT$SF=");
SerialUSB.println(frame);
}
SigFox.print("AT$SF=");
SigFox.print(frame);
SigFox.print("\r");
while (!SigFox.available());
while(SigFox.available()){
output = (char)SigFox.read();
status += output;
delay(10);
}
if (DEBUG){
SerialUSB.print("Status \t");
SerialUSB.println(status);
}
if (status == "OK\r"){
//Success :)
return true;
}
else{
return false;
}
}