-
Notifications
You must be signed in to change notification settings - Fork 1
/
dht_ble.ino
103 lines (88 loc) · 3.08 KB
/
dht_ble.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
#include <Arduino.h>
#include <CurieBLE.h>
#include "DHT.h"
#define DHTPIN 2 // data pin
#define DHTTYPE DHT22
// Initialize the sensor
DHT dht(DHTPIN, DHTTYPE);
// Initialize BLE
BLEPeripheral ble;
BLEService informationService("180A");
BLECharacteristic modelCharacteristic("2A24", BLERead, "101");
BLECharacteristic manufacturerCharacteristic("2A29", BLERead, "Arduino");
BLECharacteristic serialNumberCharacteristic("2A25", BLERead, "2.71828");
// Services and characteristics
BLEService humidityService("10525F60-CF73-11E6-9598-F8E2251C9A69");
BLEFloatCharacteristic humidityCharacteristic("10525F61-CF73-11E6-9598-F8E2251C9A69", BLERead | BLENotify);
BLEService thermometerService("F489CB00-C177-11E6-9598-9854249C9A66");
BLEFloatCharacteristic temperatureCharacteristic("F489CB01-C177-11E6-9598-9854249C9A66", BLERead | BLENotify);
void setup() {
Serial.begin(9600);
dht.begin();
ble.setLocalName("ArduinoSensor");
ble.setAdvertisedServiceUuid(thermometerService.uuid());
ble.addAttribute(informationService);
ble.addAttribute(modelCharacteristic);
ble.addAttribute(manufacturerCharacteristic);
ble.addAttribute(serialNumberCharacteristic);
ble.addAttribute(humidityService);
ble.addAttribute(humidityCharacteristic);
humidityCharacteristic.setValueLE(0.0);
ble.addAttribute(thermometerService);
ble.addAttribute(temperatureCharacteristic);
temperatureCharacteristic.setValueLE(0.0);
ble.setEventHandler(BLEConnected, centralConnect);
ble.setEventHandler(BLEDisconnected, centralDisconnect);
ble.begin();
Serial.println("Bluetooth on");
}
void loop() {
// We can't read data from the sensor more often
// than every 2 seconds
delay(2000);
ble.poll();
// Humidity
float humidity = dht.readHumidity();
// Check if any reads failed and exit early (to try again)
if (isnan(humidity)) {
Serial.println("Failed to read humidity from DHT sensor!");
return;
}
float prevHumidity = humidityCharacteristic.valueLE();
if (abs(humidity - prevHumidity) > 1.50) {
humidityCharacteristic.setValueLE(humidity);
Serial.print("Update humidity | ");
Serial.print(humidity, 0);
Serial.println("%");
} else {
Serial.print("Humidity | ");
Serial.print(humidity, 0);
Serial.println("%");
}
// Temperature
float celsiusTemp = dht.readTemperature();
// Check if any reads failed and exit early (to try again)
if (isnan(celsiusTemp)) {
Serial.println("Failed to read temperature from DHT sensor!");
return;
}
float prevCelsiusTemp = temperatureCharacteristic.valueLE();
if (abs(celsiusTemp - prevCelsiusTemp) > 0.20) {
temperatureCharacteristic.setValueLE(celsiusTemp);
Serial.print("Update temperature | ");
Serial.print(celsiusTemp, 2);
Serial.println("*C");
} else {
Serial.print("Temperature | ");
Serial.print(celsiusTemp, 2);
Serial.println("*C");
}
}
void centralConnect(BLECentral& central) {
Serial.print("Central connected | ");
Serial.println(central.address());
}
void centralDisconnect(BLECentral& central) {
Serial.print("Central disconnected | ");
Serial.println(central.address());
}