-
Notifications
You must be signed in to change notification settings - Fork 2
/
ap.ino
157 lines (123 loc) · 4.58 KB
/
ap.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
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void accessPointSwitchHandler(){
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
//Also toggle the device's Access Point
if (buttonState == HIGH) {
ledState = !ledState;
//Switch on the device's Access Point if the ledstate is HIGH
if(ledState == HIGH){
//Load the Access Point Config File found in the data folder in the sketch folder of this sketch
if (!loadAccessPointConfig()) {
Serial.println("Failed to load the AP Config file");
} else {
Serial.println("AP Config file loaded SUCCESSFULLY");
}
//Setup the Access Point
setUpAccessPoint();
}else{
//Switch Off the device's Access Point if the ledstate is LOW
WiFi.softAPdisconnect(true);
Serial.println("Access Point Switched Off");
}
}
}
}
// set the LED:
digitalWrite(ledPin, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
void setUpAccessPoint(){
//Configure device Access Point
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(ap_ssid, ap_password);
dnsServer.setTTL(300);
dnsServer.setErrorReplyCode(DNSReplyCode::ServerFailure);
dnsServer.start(DNS_PORT, UNIQUE_ID_DNS, apIP);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
}
void updateAccessPointConfig(){
String _ap_ssid = server.arg("ap_ssid");
String _ap_password = server.arg("ap_password");
//Save configurations
StaticJsonDocument<200> doc;
doc["ap_ssid"] = _ap_ssid;
doc["ap_password"] = _ap_password;
File configFile = SPIFFS.open("/apConfig.json", "w");
if (!configFile) {
Serial.println("Failed to open the file apConfig.json for writing");
server.send(200, "text/html", "failed");
}
serializeJson(doc, configFile);
server.send(200, "text/html", "success");
//Resetting the unit's Access Point
Serial.println("Resetting the unit's AP.");
loadAccessPointConfig();
setUpAccessPoint();
}
bool loadAccessPointConfig() {
File configFile = SPIFFS.open("/apConfig.json", "r");
if (!configFile) {
Serial.println("Failed to open the apConfig.json file");
return false;
}
size_t size = configFile.size();
if (size > 1024) {
Serial.println("The AP Config file size is too large");
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
// We don't use String here because ArduinoJson library requires the input
// buffer to be mutable. If you don't use ArduinoJson, you may as well
// use configFile.readString instead.
configFile.readBytes(buf.get(), size);
StaticJsonDocument<200> doc;
auto error = deserializeJson(doc, buf.get());
if (error) {
Serial.println("Failed to parse AP config file");
}
const char* ssid_ = doc["ap_ssid"];
const char* password_ = doc["ap_password"];
ap_ssid = ssid_;
ap_password = password_;
// Real world application would store these values in some variables for
// later use.
Serial.print("Loaded AP SSID: ");
Serial.println(ap_ssid);
Serial.print("Loaded AP Password: ");
Serial.println(ap_password);
return true;
}
void returnAPConfig(){
//Encode JSON using all sensor data
String webPage;
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
StaticJsonDocument<200> jsonDocument;
jsonDocument["ap_ssid"] = ap_ssid;
jsonDocument["ap_password"] = ap_password;
// Write JSON document
serializeJsonPretty(jsonDocument, webPage);
server.send(200, "text/html", webPage);
}