-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransmit_data_simple.ino
164 lines (127 loc) · 4.12 KB
/
transmit_data_simple.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
#include "Nicla_System.h"
#include "Arduino_BHY2.h"
#include <ArduinoBLE.h>
#define BLE_SENSE_UUID(val) ("19b10000-" val "-537e-4f6c-d104768a1214")
#define ACQUISITION_INTERVAL 5000
#define SCAN_INTERVAL 1000
// Constants
const int VERSION = 0x00000000;
// BLE variables
BLEService service(BLE_SENSE_UUID("0000"));
BLEUnsignedIntCharacteristic versionCharacteristic(BLE_SENSE_UUID("1001"), BLERead | BLENotify);
BLEFloatCharacteristic temperatureCharacteristic(BLE_SENSE_UUID("2001"), BLERead | BLENotify);
BLEUnsignedIntCharacteristic airqualityCharacteristic(BLE_SENSE_UUID("3001"), BLERead | BLENotify);
BLECharacteristic rgbLedCharacteristic(BLE_SENSE_UUID("8001"), BLERead | BLEWrite, 3 * sizeof(byte)); // Array of 3 bytes, RGB
// String for the local and device name
String name;
// Sensor declarations
Sensor temperature(SENSOR_ID_TEMP);
SensorBSEC airquality(SENSOR_ID_BSEC);
// variable to be to check for time to be passed
unsigned long last_acq_time;
unsigned long last_scan_time;
// To take care of central
bool central_discovered;
BLEDevice central;
void setup() {
Serial.begin(115200);
Serial.println("Start");
nicla::begin();
nicla::leds.begin();
nicla::leds.setColor(red);
//Sensors initialization
BHY2.begin(NICLA_STANDALONE);
temperature.begin();
airquality.begin();
if (!BLE.begin()) {
Serial.println("Failed to initialize BLE!");
while (1)
;
}
String address = BLE.address();
Serial.print("address = ");
Serial.println(address);
address.toUpperCase();
name = "NiclaSenseME-";
name += address[address.length() - 5];
name += address[address.length() - 4];
name += address[address.length() - 2];
name += address[address.length() - 1];
Serial.print("name = ");
Serial.println(name);
BLE.setLocalName(name.c_str());
BLE.setDeviceName(name.c_str());
BLE.setAdvertisedService(service);
// Add all the previously defined Characteristics
service.addCharacteristic(versionCharacteristic);
service.addCharacteristic(temperatureCharacteristic);
service.addCharacteristic(rgbLedCharacteristic);
service.addCharacteristic(airqualityCharacteristic);
rgbLedCharacteristic.setEventHandler(BLEWritten, onRgbLedCharacteristicWrite);
versionCharacteristic.setValue(VERSION);
BLE.addService(service);
BLE.advertise();
central_discovered = false;
BLEDevice central;
last_scan_time = 0;
last_acq_time = 0;
}
void loop() {
if (!central_discovered) {
if (millis() - last_scan_time > SCAN_INTERVAL) {
last_scan_time = millis();
// Scan every SCAN_INTERVAL ms
// Search for a central
nicla::leds.setColor(yellow);
central = BLE.central();
Serial.println("Scanning for central device...");
if (central) {
central_discovered = true;
Serial.print("Central device found and connected! Device MAC address: ");
Serial.println(central.address());
}
} else nicla::leds.setColor(red);
} else {
if (millis() - last_acq_time > ACQUISITION_INTERVAL) {
last_acq_time = millis();
// Comms every ACQUISITION_INTERVAL ms
if (central) {
if (central.connected()) {
nicla::leds.setColor(blue);
BHY2.update();
check_subscriptions();
}
} else {
central_discovered = false;
// TODO
}
} else {
nicla::leds.setColor(green);
if (!central.connected()) {
central_discovered = false;
}
}
}
}
void check_subscriptions() {
if (temperatureCharacteristic.subscribed()) {
temperature_notify();
}
if (airqualityCharacteristic.subscribed()) {
airquality_notify();
}
}
void temperature_notify() {
float temperatureValue = temperature.value();
temperatureCharacteristic.writeValue(temperatureValue);
}
void airquality_notify() {
unsigned int iaqValue = airquality.iaq();
airqualityCharacteristic.writeValue(iaqValue);
}
void onRgbLedCharacteristicWrite(BLEDevice central, BLECharacteristic characteristic) {
byte r = rgbLedCharacteristic[0];
byte g = rgbLedCharacteristic[1];
byte b = rgbLedCharacteristic[2];
nicla::leds.setColor(r, g, b);
}