-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreezerBurn.ino
176 lines (156 loc) · 4.94 KB
/
FreezerBurn.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
#include <DNSServer.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h" //Library that allows the captive portal to function, version 2.4.3 by Me-No-Dev
/*
ONE FINAL NOTE ABOUT ESP ASYNC WEBSERVER
This library has dependencies that come from AsyncTCP and will not compile if AsyncTCP is not installed, even though
AsyncTCP is not actually called in this program. Version used is "AsyncTCP" by dvarrel, version 1.1.4
*/
#include "./page/page.h" //Webpage for the captive portal
#define magnetPin 21
#define ledPin 2
DNSServer dnsServer;
AsyncWebServer server(80);
byte magnetConnected;
int seconds = 0;
int maxSeconds = 10;
//Setup stuff here
bool timeRecieved = false;
bool wifirecieved = false; //Has the esp gotten the information needed to connect to wifi?
bool wifiConnecting = false; //Even more so, is the Wifi being connected/has connected?
int userMinutes = 0;
String minutesInput;
String userName;
String currentNetwork;
String wifiPass;
//Code that I didn't copy paste
class CaptiveRequestHandler : public AsyncWebHandler
{
public:
CaptiveRequestHandler() {}
virtual ~CaptiveRequestHandler() {}
bool canHandle(AsyncWebServerRequest *request)
{
request->addInterestingHeader("ANY");
return true;
}
void handleRequest(AsyncWebServerRequest *request)
{
request->send_P(200, "text/html", index_html);
}
};
void setupServer(){
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html);
Serial.println("Client Connected");
});
server.on("/get", HTTP_GET, [] (AsyncWebServerRequest *request) {
String inputMessage;
String inputParam;
wifiConnecting = false;
if (request->hasParam("seconds")) { //It's actually minutes; this naming mishap is because of the way I originally wrote the webpage
inputMessage = request->getParam("seconds")->value();
inputParam = "seconds";
minutesInput = inputMessage;
Serial.println(inputMessage);
timeRecieved = true;
}
if (request->hasParam("name"))
{
inputMessage = request->getParam("name")->value();
inputParam = "name";
userName = inputMessage;
Serial.print("Hello, ");
Serial.println(inputMessage);
}
if (request->hasParam("network"))
{
inputMessage = request->getParam("network")->value();
inputParam = "network";
if (wifiConnecting == true && currentNetwork != inputMessage) //Let wifi be reconnected if this value differs from what's already stored
{
wifiConnecting == false;
}
currentNetwork = inputMessage;
Serial.print("The current preferred network is ");
Serial.println(currentNetwork);
}
if (request->hasParam("netpass"))
{
inputMessage = request->getParam("netpass")->value();
inputParam = "netpass";
wifiPass = inputMessage;
}
if (wifiPass > "" && currentNetwork > " ") wifirecieved = true;
//This goes at the end of the entire sequence
request->send(200, "text/html", "The values entered by you have been successfully sent to the device <br><a href=\"/\">Return to Home Page</a>");
});
}
//And now it's honesty hour again
void secondsInc()
{
seconds ++;
Serial.println(seconds);
}
void setup()
{
Serial.begin(9600);
//Non-web stuff
pinMode(magnetPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
//Back to web stuff
WiFi.mode(WIFI_AP);
WiFi.softAP("Freezer Burn");
setupServer();
dnsServer.start(53, "*", WiFi.softAPIP());
server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);
server.begin();
}
void wifiConnect()
{
if (wifirecieved == true && wifiConnecting == false)
{
wifiConnecting = true; //Perhaps find a way to where if the captive portal results differ from the inputted value, set this to false again and reconnect?
WiFi.begin(currentNetwork, wifiPass);
Serial.println("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);
}
Serial.println("Successful connection");
}
}
void loop()
{
//DNS stuff
dnsServer.processNextRequest();
wifiConnect(); //It's probably better to try it through setup but it's not an option here because the wifi nextworks are not defined at first
if (timeRecieved == true)
{
userMinutes = minutesInput.toInt(); //String to number conversion
maxSeconds = (userMinutes * 60);
}
//And now my stuff
magnetConnected = digitalRead(magnetPin);
if (magnetConnected == LOW)
{
if (seconds < maxSeconds)
{
secondsInc();
if (seconds == maxSeconds)
{
digitalWrite(ledPin, HIGH);
Serial.print(userName);
Serial.println(", your freezer or fridge is open. Please close it immediately.");
//And then this is where other stuff would happen
}
}
}
else
{
seconds = 0;
digitalWrite(ledPin, LOW);
}
delay(1000);
}