-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWolk-Arduino-Environment-Monitoring.ino
276 lines (221 loc) · 5.89 KB
/
Wolk-Arduino-Environment-Monitoring.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
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
#include <bme680_defs.h>
#include <bme680.h>
#include <WiFi101.h>
#include <RTCZero.h>
#include <FlashStorage.h>
#include "WolkConn.h"
#include "MQTTClient.h"
/*Number of outbound_message_t to store*/
#define STORAGE_SIZE 32
#define SEALEVELPRESSURE_HPA (1013.25)
/*Circular buffer to store outbound messages to persist*/
typedef struct{
boolean valid;
outbound_message_t outbound_messages[STORAGE_SIZE];
uint32_t head;
uint32_t tail;
boolean empty;
boolean full;
} Messages;
static Messages data;
/*Connection details*/
const char* ssid = "ssid";
const char* wifi_pass = "wifi_pass";
const char *device_key = "device_key";
const char *device_password = "device_pass";
const char* hostname = "api-demo.wolkabout.com";
int portno = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
/* WolkConnect-Arduino Connector context */
static wolk_ctx_t wolk;
/* Init flash storage */
FlashStorage(flash_store, Messages);
/*Init i2c sensor communication*/
Adafruit_BME680 bme;
RTCZero rtc;
bool read;
/*Read sensor every minute. If you change this parameter
make sure that it's <60*/
const byte readEvery = 1;
bool publish;
/*Publish every 10 minutes. If you change this parameter
make sure that it's <60*/
const byte publishEvery = 10;
byte publishMin;
/*Flash storage and custom persistence implementation*/
void _flash_store()
{
data.valid = true;
flash_store.write(data);
}
void increase_pointer(uint32_t* pointer)
{
if ((*pointer) == (STORAGE_SIZE - 1))
{
(*pointer) = 0;
}
else
{
(*pointer)++;
}
}
void _init()
{
data = flash_store.read();
if (data.valid == false)
{
data.head = 0;
data.tail = 0;
data.empty = true;
data.full = false;
}
}
bool _push(outbound_message_t* outbound_message)
{
if(data.full)
{
increase_pointer(&data.head);
}
memcpy(&data.outbound_messages[data.tail], outbound_message, sizeof(outbound_message_t));
increase_pointer(&data.tail);
data.empty = false;
data.full = (data.tail == data.head);
return true;
}
bool _peek(outbound_message_t* outbound_message)
{
memcpy(outbound_message, &data.outbound_messages[data.head], sizeof(outbound_message_t));
return true;
}
bool _pop(outbound_message_t* outbound_message)
{
memcpy(outbound_message, &data.outbound_messages[data.head], sizeof(outbound_message_t));
increase_pointer(&data.head);
data.full = false;
data.empty = (data.tail == data.head);
return true;
}
bool _is_empty()
{
return data.empty;
}
void init_wifi()
{
if ( WiFi.status() != WL_CONNECTED) {
while (WiFi.begin(ssid, wifi_pass) != WL_CONNECTED) {
delay(1000);
}
}
}
void setup_wifi()
{
delay(10);
if ( WiFi.status() != WL_CONNECTED) {
int numAttempts = 0;
while (WiFi.begin(ssid, wifi_pass) != WL_CONNECTED) {
numAttempts++;
if(numAttempts == 10){
Serial.println("Couldn't reach WiFi!");
break;
}
delay(1000);
}
}
}
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
/*Initialize the circular buffer structure*/
_init();
init_wifi();
wolk_init(&wolk, NULL, NULL, NULL, NULL,
device_key, device_password, &client, hostname, portno, PROTOCOL_JSON_SINGLE, NULL, NULL);
wolk_init_custom_persistence(&wolk, _push, _peek, _pop, _is_empty);
/*The on board LED will turn on if something went wrong*/
if(!bme.begin())
{
digitalWrite(LED_BUILTIN, HIGH);
}
/*Sensor init*/
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
delay(200);
read = true;
publish = true;
/*Request epoch time from platform*/
wolk_connect(&wolk);
delay(100);
/*This function requests and waits for
* the epoch time from the platform.
* If a problem occurred, the on-board LED will be on.
*/
wolk_update_epoch(&wolk);
wolk_disconnect(&wolk);
rtc.begin();
rtc.setEpoch(wolk.epoch_time);
rtc.setAlarmTime(rtc.getHours(), (rtc.getMinutes() + readEvery) % 60, rtc.getSeconds());
rtc.enableAlarm(rtc.MATCH_MMSS);
rtc.attachInterrupt(alarmMatch);
publishMin = (rtc.getMinutes() + publishEvery) % 60;
WiFi.lowPowerMode();
}
void loop() {
/*In order to keep the interrupt routine as short as possible
routine only sets the tasks to be done
read = true means that the sensor reading should be done
publish = true means that the readings should be published to platform
or persisted in flash if the connection is not available
*/
if(read)
{
read = false;
if (!bme.performReading()) {
digitalWrite(LED_BUILTIN, HIGH);
}
wolk_add_numeric_sensor_reading(&wolk, "T", bme.temperature, rtc.getEpoch());
wolk_add_numeric_sensor_reading(&wolk, "H", bme.humidity, rtc.getEpoch());
wolk_add_numeric_sensor_reading(&wolk, "P", bme.pressure / 100.0, rtc.getEpoch());
wolk_add_numeric_sensor_reading(&wolk, "GR", bme.gas_resistance, rtc.getEpoch());
wolk_add_numeric_sensor_reading(&wolk, "A", bme.readAltitude(SEALEVELPRESSURE_HPA), rtc.getEpoch());
/*set new alarm*/
int alarmMin = (rtc.getMinutes() + readEvery) % 60;
rtc.setAlarmMinutes(alarmMin);
delay(100);
}
if(publish)
{
publish = false;
setup_wifi();
wolk_connect(&wolk);
if(!wolk.is_connected)
{
_flash_store();
}
delay(100);
if(wolk_publish(&wolk) == W_TRUE)
{
_flash_store();
}
/*set new publish time*/
publishMin = (rtc.getMinutes() + publishEvery) % 60;
delay(100);
wolk_disconnect(&wolk);
delay(100);
}
delay(100);
}
/*Timed interrupt routine*/
void alarmMatch()
{
read = true;
if(publishMin == rtc.getMinutes())
{
publish = true;
}
}