-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2.ino
105 lines (84 loc) · 2.83 KB
/
CO2.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
#include "M5StickCPlus.h"
#include "Adafruit_SGP30.h"
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_SGP30.h"
#include "BLEDevice.h"
#include "BLEServer.h" // Bluetooth Low Energy
#include "BLEUtils.h" // Bluetooth Low Energy
#include "BLE2902.h"
//define the bluetooh server name
#define bleServerName "CO2_data"
//define service UUID
#define SERVICE_UUID "91bad492-b950-4226-aa2b-4ede9fa42f59"
// init variables to be used
float CO2;
Adafruit_SGP30 sgp;
bool deviceConnected = false;
// init timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 60000; // 1min
static BLEUUID bmeServiceUUID("91bad492-b950-4226-aa2b-4ede9fa42f59");
//BLE characteristic and descriptor
BLECharacteristic bmeCarbonDioxideCharacteristics("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor bmeCarbonDioxideDescriptor(BLEUUID((uint16_t)0x2904));
//Setup callbacks onConnect and onDisconnect
class MyServerCallbacks: public BLEServerCallbacks {
void onConnect(BLEServer* pServer) {
deviceConnected = true;
};
void onDisconnect(BLEServer* pServer) {
deviceConnected = false;
}
};
//setup bluetooh and start advertising
void setupBluetooth(){
//create the BLE device
BLEDevice::init(bleServerName);
// Create the BLE Server
BLEDevice::init(bleServerName);
BLEServer *pServer = BLEDevice::createServer();
pServer->setCallbacks(new MyServerCallbacks());
//create BLE service
BLEService *bmeService = pServer->createService(SERVICE_UUID);
// setting bluetooth characteristics
bmeService->addCharacteristic(&bmeCarbonDioxideCharacteristics);
bmeCarbonDioxideDescriptor.setValue("BME Carbon Dioxide Value");
bmeCarbonDioxideCharacteristics.addDescriptor(&bmeCarbonDioxideDescriptor);
// Start the service
bmeService->start();
// Start advertising
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pServer->getAdvertising()->start();
Serial.println("Waiting a client connection to notify...");
}
void setup(){
// setting up of serial and M5 sticks parameters
Serial.begin(115200);
M5.begin();
sgp.begin();
M5.Lcd.setRotation(3);
M5.Lcd.setCursor(0, 0, 2);
setupBluetooth();
}
void loop() {
// check for any devices connection
if(deviceConnected){
if((millis() - lastTime) > timerDelay){
// measure the CO2
sgp.IAQmeasure();
float CO2 = sgp.eCO2;
M5.Lcd.setCursor(0, 20, 2);
M5.Lcd.printf("CO2: %2.1f", CO2);
static char carbon[6];
dtostrf(CO2, 6, 2, carbon);
// setting the values into the bluetooth characteristic
bmeCarbonDioxideCharacteristics.setValue(carbon);
bmeCarbonDioxideCharacteristics.notify();
Serial.print("Carbon Dioxide: ");
Serial.println(CO2);
lastTime = millis();
}
}
}