-
Notifications
You must be signed in to change notification settings - Fork 0
/
Universal.ino
291 lines (256 loc) · 7.9 KB
/
Universal.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
288
289
290
291
#include <ESP8266WiFi.h> //ESP8266 Core WiFi Library (you most likely already have this in your sketch)
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <FirebaseArduino.h>
#include "DHT.h"
#include <ArduinoJson.h>
#include <WiFiClientSecure.h>
#define FIREBASE_HOST "PRIVATE"
#define FIREBASE_AUTH "PRIVATE"
#define DHTPIN 14 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
DHT dht(DHTPIN, DHTTYPE);
boolean played = false;
boolean wifiConnectedFlag = false;
const int buzzer = 15; //Buzzer connected to pin 8 of Arduino uno / mega
const int photocellPin = A0;
float averageHeatIndex;
int averageLight = 0;
int count = 0;
String ssid = "notFoundJustYet";
String publicIP;
int chipID = ESP.getChipId();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(buzzer, OUTPUT);
connectToWifi();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
delay(10);
// Initialize a NTPClient to get time
timeClient.begin();
timeClient.setTimeOffset(0);
publicIP = GetExternalIP();
dht.begin();
delay(30);
calibrate();
}
int lightOnLength = 0;
int showerOnLength = 0;
int showerCount = 0;
int showerIndex = 0;
float showerTempValues[30] = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
void loop() {
int lightValue = analogRead(photocellPin);
float f = dht.readTemperature(true);
float h = dht.readHumidity();
float hif = dht.computeHeatIndex(f, h);
if ((averageLight - lightValue) > 60) {
lightOnLength++;
} else {
if (lightOnLength > 2) {
sendLightData(lightOnLength);
}
lightOnLength = 0;
}
if (abs(averageHeatIndex - hif) > 3.5) {
showerOnLength++;
showerCount++;
if (showerOnLength == 10) {
playSuccess();
}
if (showerIndex != 30 && showerCount > 30) {
showerCount = 0;
showerTempValues[showerIndex] = hif;
showerIndex++;
}
} else {
if (showerOnLength > 31) {
sendShowerData(showerOnLength, showerTempValues);
for (int i = 0; i < 30; i++) {
showerTempValues[i] = -1;
}
}
showerOnLength = 0;
showerIndex = 0;
}
delay(980);
Serial.println("showerOnLength: ");
Serial.println(showerOnLength);
Serial.println("lightOnLength: ");
Serial.println(lightOnLength);
}
void calibrate() {
// CALIBRATE HEAT INDEX and CALIBRATE LIGHT
float heatIndexValues[10];
int lightValues[10];
for (int i = 0; i < 10; i++) {
float f = dht.readTemperature(true);
float h = dht.readHumidity();
heatIndexValues[i] = dht.computeHeatIndex(f, h);
lightValues[i] = analogRead(photocellPin);
delay(500);
}
int sum = 0;
int sum2 = 0;
for (int i=0; i<10; i++) {
sum += heatIndexValues[i];
sum2 += lightValues[i];
}
averageHeatIndex = sum/10;
averageLight = sum2/10;
}
void sendLightData(int timeOn) {
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
String formattedTime = timeClient.getFormattedTime();
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;
int currentYear = ptm->tm_year+1900;
//Print complete date:
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
String data = publicIP + "/" + String(chipID) + "/" + currentDate + "/" + timeOn;
Firebase.setString("NewLightDataIn/",data);
}
void sendShowerData(int timeOn, float showerTempValues[]) {
Serial.println("sendShowerdata() ran");
timeClient.update();
unsigned long epochTime = timeClient.getEpochTime();
String formattedTime = timeClient.getFormattedTime();
//Get a time structure
struct tm *ptm = gmtime ((time_t *)&epochTime);
int monthDay = ptm->tm_mday;
int currentMonth = ptm->tm_mon+1;
int currentYear = ptm->tm_year+1900;
int i = 0;
float averageShowerHeat = 0;
while (showerTempValues[i] != -1) {
averageShowerHeat += showerTempValues[i];
i++;
}
averageShowerHeat = (averageShowerHeat / (i));
String averageShowerHeatString = String(averageShowerHeat);
averageShowerHeatString.replace(".", "-");
Serial.println(averageShowerHeatString);
String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay);
String data = publicIP + "/" + String(chipID) + "/" + currentDate + "/" + timeOn + "/" + averageShowerHeatString;
Firebase.setString("NewShowerDataIn/",data);
while (!Firebase.success()) {
Serial.println("it was a failure.");
Serial.println(Firebase.error());
Firebase.setString("NewShowerDataIn/",data);
}
Serial.println("it was a success.");
}
void connectToWifi() {
while (!wifiConnectedFlag) {
WiFiManager wifiManager;
WiFiManagerParameter custom_text("<p>Protect you and others without thinking about it.</p>");
wifiManager.addParameter(&custom_text);
wifiManager.startConfigPortal("BathroomBuddy");
Serial.println("connected...yeey :)");
ssid = WiFi.SSID();
wifiConnectedFlag = true;
}
}
void playTooEarly() {
for (int i = 0; i < 3; i++) {
tone(buzzer,600);
delay(160);
noTone(buzzer);
delay(130);
}
}
void playSuccess() {
for (int i = 0; i < 2; i++) {
playSuccessHelper();
delay(300);
}
played = true;
}
void playSuccessHelper() {
tone(buzzer,2500);
delay(115);
noTone(buzzer);
delay(75);
tone(buzzer,2500);
delay(50);
noTone(buzzer);
delay(100);
tone(buzzer,2660);
delay(300);
noTone(buzzer);
}
String GetExternalIP() {
WiFiClient client;
if (!client.connect("api.ipify.org", 80)) {
Serial.println("Failed to connect with 'api.ipify.org' !");
}
else {
int timeout = millis() + 5000;
client.print("GET /?format=json HTTP/1.1\r\nHost: api.ipify.org\r\n\r\n");
while (client.available() == 0) {
if (timeout - millis() < 0) {
Serial.println(">>> Client Timeout !");
client.stop();
return "IP_NOT_FOUND";
}
}
bool finishedHeaders = false;
bool currentLineIsBlank = true;
bool gotResponse = false;
String title = "";
String headers = "";
String body = "";
long now;
now = millis();
// checking the timeout
while (millis() - now < 1500) {
while (client.available()) {
char c = client.read();
//Serial.print(c);
if (finishedHeaders) {
body=body+c;
} else {
if (currentLineIsBlank && c == '\n') {
finishedHeaders = true;
}
else {
headers = headers + c;
}
}
if (c == '\n') {
currentLineIsBlank = true;
}else if (c != '\r') {
currentLineIsBlank = false;
}
//marking we got a response
gotResponse = true;
}
if (gotResponse) {
DynamicJsonBuffer jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(body);
if (root.success()) {
if (root.containsKey("data")) {
JsonObject& post = root["data"]["children"][0];
if (post.containsKey("data")) {
title = post["data"]["title"].as<String>();
}
}
} else {
Serial.println("failed to parse JSON");
}
break;
}
}
return body;
}
}