This repository has been archived by the owner on Apr 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
climactic-station.ino
287 lines (227 loc) · 6.23 KB
/
climactic-station.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
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
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include "psswd.h"
#include "user_interface.h"
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,20,4);
#include <BME280I2C.h>
#include <Wire.h>
BME280I2C bme; // Default : forced mode, standby time = 1000 ms
// Oversampling = pressure ×1, temperature ×1, humidity ×1, filter off,
const char* ssid = STASSID;
const char* password = STAPSK;
WiFiUDP Udp;
unsigned int localUdpPort = 2390;
char packetBuffer[255];
char registredBuffer[] = "R";
IPAddress broadcast;
IPAddress remoteServer;
const String version = "0.5.1";
ESP8266WebServer server(80);
const int BUZZER = 14;
// Status LEDs
const int RED = 15; // Error
const int YELLOW = 13; // Booting, waiting, etc
const int GREEN = 12; // Everything good!
const int NONE = -1;
const int STATUS_LED[] = {
GREEN,
YELLOW,
RED
};
int currentStatus = STATUS_LED[1];
void setupStatusLeds() {
for(int i = 0; i < 3; i++) {
pinMode(STATUS_LED[i], OUTPUT);
}
}
void setStatus(int status) {
if(status == currentStatus) {
return;
}
if(currentStatus != -1) {
digitalWrite(currentStatus, LOW);
}
if(status != NONE) {
digitalWrite(status, HIGH);
}
currentStatus = status;
}
void shutOnboardLeds() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, 1);
}
void beep(int delayMS = 250) {
digitalWrite(BUZZER, HIGH);
delay(delayMS);
digitalWrite(BUZZER, LOW);
}
void doubleBeep() {
beep(200);
delay(50);
beep(200);
}
boolean discoverServer() {
Serial.println("Sending discovery request");
Udp.beginPacket(broadcast, localUdpPort);
Udp.write("D");
Udp.endPacket();
}
void setup(void) {\
pinMode(BUZZER, OUTPUT);
beep();
setupStatusLeds();
setStatus(YELLOW);
Serial.begin(115200);
while(!Serial) {} // Wait
Wire.begin();
WiFi.mode(WIFI_STA);
wifi_set_sleep_type(NONE_SLEEP_T); //LIGHT_SLEEP_T and MODE_SLEEP_T
WiFi.begin(ssid, password);
Serial.println("");
int wifiAttempts = 0;
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
setStatus(NONE);
delay(250);
Serial.print(".");
setStatus(YELLOW);
delay(250);
wifiAttempts++;
if(wifiAttempts >= 50) {
setStatus(RED);
doubleBeep();
Serial.println("");
Serial.println("**ERROR: Unable to connect to network, are your credentials right?**");
return;
}
}
Serial.println("");
Serial.print("Connected.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("IP: ");
lcd.setCursor(0,1);
lcd.print(WiFi.localIP().toString());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", []() {
server.send(200, "text/json", "{\"version\":\"" + String(version) + "\"}");
});
// Kinda... useless _for now_
server.on("/info", []() {
String res = "{\"ip_address\":\"" + WiFi.localIP().toString() + "\"}";
server.send(200, "text/json", res);
});
server.on("/data", []() {
server.send(200, "text/json", envSensorData());
});
server.on("/datah", []() {
server.send(200, "text/html", envSensorDataHTML());
});
server.on("/beep", []() {
if(server.method() == HTTP_POST) {
beep();
server.send(200, "text/json", "{\"done\": true}");
} else {
server.send(405);
}
});
server.on("/dbeep", []() {
if(server.method() == HTTP_POST) {
doubleBeep();
server.send(200, "text/json", "{\"done\": true}");
} else {
server.send(405);
}
});
// Used by the master server to verify if a sara-air node
server.on("/climactic-station-node", []() {
server.send(200, "text/json", "{\"climactic-station-node\": true}");
});
server.begin();
Serial.println("HTTP server started");
Serial.println("");
Serial.println("Connecting to BME280 sensor");
while(!bme.begin())
{
setStatus(NONE);
delay(250);
Serial.print(".");
setStatus(YELLOW);
delay(250);
}
switch(bme.chipModel())
{
case BME280::ChipModel_BME280:
Serial.println("Found BME280 sensor! Success.");
break;
case BME280::ChipModel_BMP280:
Serial.println("Found BMP280 sensor! No Humidity available.");
break;
default:
Serial.println("Found UNKNOWN sensor! Error!");
setStatus(RED);
return;
}
Serial.println("");
Udp.begin(localUdpPort);
Serial.print("UDP socket listening on port ");
Serial.println(localUdpPort);
broadcast = IPAddress( WiFi.localIP().v4() | ( ~ WiFi.subnetMask().v4() ));
Serial.print("Broadcasting on ");
Serial.print(broadcast);
Serial.println("");
discoverServer();
}
String envSensorData()
{
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pres, temp, hum, tempUnit, presUnit);
return "{\"temp\":" + String(temp) + ",\"humidity\":" + String(hum) + ",\"pressure\":" + String(pres) + "}";
}
String envSensorDataHTML()
{
float temp(NAN), hum(NAN), pres(NAN);
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
BME280::PresUnit presUnit(BME280::PresUnit_Pa);
bme.read(pres, temp, hum, tempUnit, presUnit);
String res = "<body><h1>Room data</h1><h2>Temp: " + String(temp) + " "+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F');
res += "<h2>Humidity: " + String(hum) + " % RH</h2>";
res += "<h2>Pressure: " + String(pres) + " Pa</h2>";
res += "</body>";
return res;
}
boolean remoteServerErrorShown = false;
boolean noWifiErrorShown = false;
void loop(void) {
if(WiFi.status() != WL_CONNECTED && !noWifiErrorShown) {
setStatus(RED);
doubleBeep();
noWifiErrorShown = true;
}
int packetSize = Udp.parsePacket();
if (packetSize) {
int len = Udp.read(packetBuffer, 255);
if (len > 0) {
packetBuffer[len] = 0;
}
if(strcmp(packetBuffer, "R") == 0) {
Serial.print("Found server at ");
Serial.println(Udp.remoteIP());
remoteServer = Udp.remoteIP();
doubleBeep();
setStatus(GREEN);
}
}
server.handleClient();
MDNS.update();
}