-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBME280SensorTest.ino
More file actions
397 lines (324 loc) · 8.76 KB
/
BME280SensorTest.ino
File metadata and controls
397 lines (324 loc) · 8.76 KB
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
/*
This example code for Flutter is
Copyright 2015, Taylor Alexander and Flutter Wireless, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "Flutter.h"
//
// TinyPacks is data serialization format for constrained environments
// like 8-bit and 16-bit microcontrollers. It can be downloaded to your
// Arduino Libraries folder from https://github.com/francc/tinypacks
//
#include "TinyPacks.h"
boolean _running = false;
Flutter flutter;
unsigned long delayTime;
byte mydata = 0;
float Temp = 0;
float Pressure = 0;
float Altitude = 0;
float Humidity = 0;
// To test this program, flash one board as a transmitter and then
// comment out this line (with //) to flash another board as a receiver
#define TRANSMITTER
#ifdef TRANSMITTER
//Setup Adafruit BME280 Pressure Sensor on I2C
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <Wire.h>
#include <SPI.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//initiate TinyPacks as a writer inorder to transmit data
PackWriter writer;
#else
//initiate TinyPacks as a writer to unpack data
PackReader reader;
#endif
//TinyPacks Constants
#define MAX_PACKED_DATA 256
unsigned char packed_data[MAX_PACKED_DATA];
int packed_data_length;
void setup()
{
//Starts Serial on pins D2 and D3
Serial.begin(115200);
/********************************************************************/
* Using SerialUSB has it quirks so becareful in using it
* 1. It appears that the radio is somehow linked to the operation
* of the USB so you can not use it unless you actually have it
* operational either as a transmitter or as a receiver.
* 2. When you first start the radio as in the initRadio function
* you can not use SerialUSB, if you try it will hang the sketch
* at the initializing the radio.
* 3. After loading a sketch and you restart the Flutter (may restart
* automatically but more often than not you have to unplug it
* and plug it back in) it will appear as a Arduino Due programmers
* port. Not to worry it still works.
* 4. If you open the serialUSB port while the sketch is running and
* then close it, the sketch will stop, period and you have to
* disconnect from the computer and then reconnect it. This goes
* for both the transmit and receive sketches
* 5. Bi-directional communction is not implemented.
*
*********************************************************************/
SerialUSB.begin(115200); //Starts Serial output on USB Connector
// I put a delay of 5 seconds here only to give me time to open SerialUSB
// in case i want to see the data while debugging
delay(5000);
// Initialized the radio and network
initRadio();
// Since the sketch works for both transmit and recieve this starts the
// transmitter portion of the sketch
#ifdef TRANSMITTER
// Initializing the BME280
SerialUSB.println(F("BME280 Initializing..."));
bool status1;
// default settings for the BME280
status1 = bme.begin();
if (!status1) {
SerialUSB.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
#endif
delayTime = 1000;
SerialUSB.println();
delay(100); // let sensor boot up
}
void loop()
{
// Checks status of the flutter radio
status();
#ifdef TRANSMITTER
mydata++;
//Get BME280 Sensor Data
Temp = bme.readTemperature();
Pressure = bme.readPressure() / 100.0F;
Altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
Humidity = bme.readHumidity();
//Pack Sensor Data
writer.setBuffer(packed_data, MAX_PACKED_DATA);
writer.openMap();
writer.putString("Temp");
writer.putReal( Temp);
writer.putString("Pressure");
writer.putReal( Pressure);
writer.putString("Altitude");
writer.putReal( Altitude);
writer.putString("Humidity");
writer.putReal( Humidity);
writer.close();
packed_data_length = writer.getOffset();
// Prints the packed data from TinyPacks as a debugging aid
SerialUSB.println(packed_data_length);
SerialUSB.println("PACKED DATA");
for(int i = 0; i < packed_data_length; i++)
SerialUSB.print(packed_data[i]);
SerialUSB.println();
// send a byte array over the radio
flutter.sendData(packed_data, packed_data_length, 2); //legth is 3, 2 is car's address
switch ((mydata - 1) % 3)
{
case 0:
flutter.setLED(RED);
break;
case 1:
flutter.setLED(BLUE);
break;
case 2:
flutter.setLED(GREEN);
break;
default:
break;
}
delay(40); //spend some time smelling the roses
#else
if (flutter.dataAvailable() > 0)
{
int packetSize = flutter.nextPacketLength();
unsigned char array[packetSize];
flutter.readBytes(array, packetSize);
SerialUSB.print("Packet Size: ");
SerialUSB.println(packetSize);
for (int i = 0; i < packetSize; i++)
{
SerialUSB.print("[");
SerialUSB.print(array[i]);
SerialUSB.print("]");
}
// Have to start at index 5 since 0-4 are used as a header
// by the radio
for (int i = 5; i < packetSize; i++){
packed_data[i-5] = array[i];
}
// Unpack
reader.setBuffer(packed_data, packetSize-5);
reader.next();
if(reader.openMap()) {
while(reader.next()) {
if (reader.match("Temp")) Temp = reader.getReal();
else if(reader.match("Pressure")) Pressure = reader.getReal();
else if(reader.match("Altitude")) Altitude = reader.getReal();
else if(reader.match("Humidity")) Humidity = reader.getReal();
else reader.next();
}
reader.close();
}
// Print unpacked data
SerialUSB.println();
SerialUSB.println("Map contents:");
SerialUSB.print(" Temp: ");
SerialUSB.print(Temp);
SerialUSB.println(" Press: ");
SerialUSB.print(Pressure);
SerialUSB.println(" Altitude: ");
SerialUSB.print(Altitude);
SerialUSB.println(" Humidity: ");
SerialUSB.print(Humidity);
SerialUSB.println();
SerialUSB.println();
mydata = array[6];
int rssi = flutter.packetRSSI(array, packetSize);
SerialUSB.print("RSSI: ");
SerialUSB.print(rssi);
SerialUSB.println(" dBm");
for (int j = 0; j > rssi; j--)
{
SerialUSB.print("=");
}
SerialUSB.println("");
switch (mydata % 3)
{
case 0:
flutter.setLED(RED);
break;
case 1:
flutter.setLED(BLUE);
break;
case 2:
flutter.setLED(GREEN);
break;
default:
break;
}
flutter.nextPacket();
}
#endif
}
void initRadio()
{
flutter.band = BAND;
flutter.setNetworkName("Range Test");
Serial.println("Initializing...");
if (flutter.init() == true)
{
Serial.println("Init success.");
flutter.ledLightShow();
delay(500);
//analogWrite(LED_R, 128);
}
else
{
flutter.setLED(RED);
Serial.println("Init failed.");
while (true);
}
//flutter.setAddress(1);
#ifdef TRANSMITTER
flutter.setAddress(1);
#else
flutter.setAddress(2);
#endif
flutter.connect(1); //form a network with this and one other device
}
void status()
{
if(flutter.getState()!=NORMAL_OPERATION) //if we aren't synchronized with another radio, just loop and blink lights.
{
if(millis()%400<200)
{
flutter.setLED(RED);
}
else
{
flutter.setLED(BLUE);
}
}
}
void button1()
{
interrupts();
int val = digitalRead(BUTTON1); //top button
if (val == HIGH)
{
// _button1=255;
}
else
{
// _button1=0;
}
// buttonsChanged=true;
}
void button2()
{
interrupts();
int val = digitalRead(BUTTON2);
#ifdef FLUTTER_R2
if (val == HIGH)
#else
if (val == LOW)
#endif
{
//_button2=255;
}
else
{
//_button2=0;
}
// buttonsChanged=true;
}
void systemReset()
{
flutter.setLED(0, 0, 255);
delayMicroseconds(16000);
delayMicroseconds(16000);
flutter.setLED(0, 0, 0);
delayMicroseconds(16000);
delayMicroseconds(16000);
flutter.setLED(0, 255, 0);
delayMicroseconds(16000);
delayMicroseconds(16000);
flutter.setLED(0, 0, 0);
delayMicroseconds(16000);
delayMicroseconds(16000);
flutter.setLED(255, 0, 0);
delayMicroseconds(16000);
delayMicroseconds(16000);
flutter.setLED(0, 0, 0);
initiateReset(1);
tickReset();
}
void radioInterrupt()
{
flutter.interrupt();
}
void softInt()
{
flutter.processSoftInt();
}
extern boolean tickInterrupt()
{
if (!flutter.initialized)
{
return true;
}
return flutter.tickInt();
}