-
Notifications
You must be signed in to change notification settings - Fork 2
/
BLEThermostat.ino
187 lines (155 loc) · 5.48 KB
/
BLEThermostat.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
#include <ArduinoBLE.h>
#include "DHT.h"
#define DHTPIN D1
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
#define RELAYPIN D2
#define ONPIN D3
#define HYSTERESIS 2
BLEService bleService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy LED Service
// Bluetooth® Low Energy LED Switch Characteristic - custom 128-bit UUID, read and writable by central
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite | BLENotify);
BLEByteCharacteristic switchOverrideCharacteristic("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
BLEByteCharacteristic relayCharacteristic("19B10003-E8F2-537E-4F6C-D104768A1214", BLERead | BLENotify);
BLEStringCharacteristic temperatureChar("2A6E", BLERead | BLEWrite | BLENotify, 10);
BLEStringCharacteristic humidityChar("2A6F", BLERead | BLENotify, 10);
const int redLED = LED_BUILTIN;
const int blueLED = LEDB;
long previousMillis = 0;
int previousOverride = 0;
bool onState = false;
int setTemperature = 21;
float currentTemperature;
float currentHumidity;
void setup() {
Serial.begin(9600);
//while (!Serial);
// set pin mode
pinMode(redLED, OUTPUT);
pinMode(RELAYPIN, OUTPUT);
pinMode(ONPIN, INPUT_PULLUP);
digitalWrite(RELAYPIN, HIGH);
// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");
while (1);
}
// set advertised local name and service UUID:
BLE.setLocalName("Thermostat");
BLE.setAdvertisedService(bleService);
// add the characteristic to the service
bleService.addCharacteristic(switchCharacteristic);
bleService.addCharacteristic(switchOverrideCharacteristic);
bleService.addCharacteristic(relayCharacteristic);
bleService.addCharacteristic(temperatureChar);
bleService.addCharacteristic(humidityChar);
// add service
BLE.addService(bleService);
// set the initial value for the characeristic:
switchCharacteristic.writeValue(0);
switchOverrideCharacteristic.writeValue(0);
relayCharacteristic.writeValue(0);
temperatureChar.writeValue("0");
humidityChar.writeValue("0");
// start advertising
BLE.advertise();
Serial.println("BLE Thermostat");
dht.begin();
}
void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();
// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
// while the central is still connected to peripheral:
while (central.connected()) {
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) {
Serial.println("LED on");
digitalWrite(redLED, LOW); // changed from HIGH to LOW
onState = true;
} else {
Serial.println(F("LED off"));
digitalWrite(redLED, HIGH); // changed from LOW to HIGH
onState = false;
}
} else {
switchCharacteristic.writeValue(onState || previousOverride ? 1 : 0);
}
if (temperatureChar.written()) {
setTemperature = temperatureChar.value().toInt();
temperatureChar.writeValue(String(currentTemperature, 2));
Serial.print(F("Set new temperature: "));
Serial.println(setTemperature);
}
getTemperatureAndHumidity();
handleRelayOutput();
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
//relay output should be handled even if BT is not connected
getTemperatureAndHumidity();
handleRelayOutput();
}
void getTemperatureAndHumidity() {
//check read temperature once every 5s
long currentMillis = millis();
if (currentMillis - previousMillis >= 5000) {
previousMillis = currentMillis;
currentHumidity = dht.readHumidity();
currentTemperature = dht.readTemperature();
if (isnan(currentHumidity) || isnan(currentTemperature)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print(F("Humidity: "));
Serial.println(currentHumidity);
humidityChar.writeValue(String(currentHumidity, 2));
Serial.print(F("Temperature: "));
Serial.println(currentTemperature);
temperatureChar.writeValue(String(currentTemperature, 2));
}
}
void handleRelayOutput() {
bool isItON = digitalRead(ONPIN) == HIGH;
//indicate manual mode
if(isItON) {
if(previousOverride == 1) {
digitalWrite(blueLED, HIGH);
switchOverrideCharacteristic.writeValue(0);
previousOverride = 0;
}
} else {
if(previousOverride == 0) {
digitalWrite(blueLED, LOW);
switchOverrideCharacteristic.writeValue(1);
previousOverride = 1;
}
}
if(isItON && !onState) {
//only turn on thermostat if switch or state are on
digitalWrite(RELAYPIN, HIGH);
relayCharacteristic.writeValue(0);
return;
}
if(isnan(currentTemperature)) {
//temperature is not set, default to thermostat off
digitalWrite(RELAYPIN, HIGH);
relayCharacteristic.writeValue(0);
return;
}
if(currentTemperature >= setTemperature + HYSTERESIS) {
//temperature acheived, turn off relay
digitalWrite(RELAYPIN, HIGH);
relayCharacteristic.writeValue(0);
} else if (currentTemperature - HYSTERESIS <= setTemperature) {
//temperature below set, turn on heating
digitalWrite(RELAYPIN, LOW);
relayCharacteristic.writeValue(1);
}
}