-
Notifications
You must be signed in to change notification settings - Fork 0
/
Internot.ino
198 lines (163 loc) · 5.66 KB
/
Internot.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
//
// a modified version of "ESP_AsyncFSBrowser.ino" example
// in "ESPAsyncWebServer" library of Hristo Gochkov (me-no-dev @ github)
//
// licensed under "LPGL-3.0"
//
//
// at first, set of modifications suggested by the gist, https://gist.github.com/dsteinman/f792f0af25ce6d7d1db4b62d29dd4d9e, applied.
//
// --> Hristo Gochkov's original code only supports "esp8266" at the time of this writing.
// Suggested patched version makes the same ex. code runnable on "esp32"
//
// --> refer to the issue @ https://github.com/me-no-dev/ESPAsyncWebServer/issues/327#issuecomment-382811459
//
//
// afterwards, more changes applied for
// (2) use a captive portal
//
/***************************************/
/* CONFIGURATIONS */
/***************************************/
const bool is_AP = true; // Set to false to just run as webserver, convenient for testing.
const char* ssid = "YOUR_SSID"; // Wifi credentials
const char* password = "YOUR_PASSWORD";
// AP identifications & credentials
String AP_ssid = "Internot";
/***************************************/
/* ALL HEADERS and GLOBAL VARIABLES */
/***************************************/
//file system
#include <FS.h>
#include <SPIFFS.h>
//wifi / tcp
#include <WiFi.h>
#include <WiFiClient.h>
#include <AsyncTCP.h>
//dns server
#include <DNSServer.h>
DNSServer dnsServer;
// json
#include <ArduinoJson.h>
// captive portal -> a self-assigned ip
IPAddress apIP(192, 168, 4, 1);
//web server
#include <ESPAsyncWebServer.h> // <-- this includes, also, "AsyncWebSocket.h" and "AsyncEventSource.h"
AsyncWebServer server(80);
DynamicJsonDocument doc(2048);
String IP = "";
void setup() {
//serial monitoring
Serial.begin( 115200 );
Serial.setDebugOutput( true );
delay( 10 );
Serial.println( "START" );
SPIFFS.begin(); //file system
JSONFromFile();
AP_ssid = getSSIDFromFile();
//wifi
if ( is_AP ) {
WiFi.mode( WIFI_AP );
//start ap
WiFi.softAPConfig( apIP, apIP, IPAddress( 255, 255, 255, 0 ) );
WiFi.softAP( AP_ssid.c_str(), NULL, 6 ); //channel selection : 6 (choose one among 6-13 for world-wide accessibility)
//my ip
delay( 500 );
Serial.print( "Captive Network. IP address:" ); Serial.println( WiFi.softAPIP() );
IP = WiFi.softAPIP().toString();
} else {
WiFi.mode( WIFI_STA );
WiFi.begin( ssid, password );
while ( WiFi.status() != WL_CONNECTED ) {
delay( 500 );
Serial.print( "." );
}
Serial.print( "Connected to " ); Serial.println( ssid );
Serial.print( "IP address: " ); Serial.println( WiFi.localIP() );
IP = WiFi.localIP().toString();
}
//dns service (captive portal)
dnsServer.start( 53, "*", apIP ); // reply with provided IP(apIP) to all("*") DNS request
server.on( "/post", HTTP_ANY, []( AsyncWebServerRequest * request ) {
//List all parameters
Serial.println( "Request for /post" );
JsonObject message = doc.createNestedObject(); // create object for storing the message
int params = request->params();
for ( int i = 0; i < params; i++ ) {
AsyncWebParameter* p = request->getParam( i );
if ( p->isFile() ) { //p->isPost() is also true
Serial.printf( "FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size() );
} else if ( p->isPost() ) {
String key = p->name().c_str();
String value = p->value().c_str();
Serial.printf( "POST[%s]: %s\n", key, value );
message[ key ] = value;
} else {
Serial.printf( "GET[%s]: %s\n", p->name().c_str(), p->value().c_str() );
}
}
JSONToFile(); // save the message to file
request->redirect( String( "http://" ) + IP + "/" ); // redirect back to the form
} );
server.on( "/messages", HTTP_ANY, [](AsyncWebServerRequest * request ) {
//List all parameters
Serial.println( "Request for /messages" );
AsyncResponseStream *response = request->beginResponseStream( "text/plain" );
response->addHeader( "Server", "ESP Async Web Server" );
response->addHeader( "Access-Control-Allow-Origin", "*" ); // allows for testing from a static file:// url
String json = "";
serializeJson( doc, json );
response->print( json );
request->send( response ); //send the response last
});
//serve-root
server.serveStatic( "/", SPIFFS, "/" ).setDefaultFile( "index.htm" );
server.onNotFound( onRequest );
//start the web service
server.begin();
}
void onRequest( AsyncWebServerRequest * request ) {
//Handle Unknown Request
Serial.println( "Unknown request. Redirect to default" );
request->redirect( String( "http://" ) + apIP.toString() + "/" );
}
void JSONFromFile() {
File file = SPIFFS.open( "/messages.json", FILE_READ );
String s = file.readString();
deserializeJson( doc, s );
// JsonArray array = doc.to<JsonArray>();
file.close();
}
void JSONToFile() {
File file = SPIFFS.open( "/messages.json", FILE_WRITE );
String json = "";
serializeJson( doc, json );
if ( ! file.print( json ) ) {
Serial.println( "File write failed" );
}
file.close();
}
String getSSIDFromFile() {
String ssid = "Internot";
File root = SPIFFS.open( "/" );
File file = root.openNextFile();
while (file) {
String n = file.name();
int dot = n.lastIndexOf(".");
String ext = n.substring( dot );
if ( ext == ".ssid" ) {
// if the file starts with a "/", skip it
// older versions of spiFFS add a "/" to the beginning of the file name
if ( n[0] == '/' ) {
ssid = n.substring( 1, dot ); // first index is 1 to skip the "/"
} else {
ssid = n.substring( 0, dot ); // otherwise not :)
}
}
file = root.openNextFile();
}
return ssid;
}
void loop() {
dnsServer.processNextRequest(); // handle DNS
}