-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComboWithCaptivePortal
265 lines (223 loc) · 7.8 KB
/
ComboWithCaptivePortal
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
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <DNSServer.h>
#include <vector>
#include <queue>
extern "C" {
#include "user_interface.h"
}
const int STATUS_LED_PIN = 2;
const char* ap_ssid = ".openChat";
const char* ap_password = "";
const int CHANNEL = 1;
const int MAX_POWER = 20.5;
unsigned long previousMillis = 0;
const long interval = 10;
std::queue<String> messageQueue;
const int MAX_MESSAGES = 10;
AsyncWebServer server(80);
AsyncWebSocket ws("/ws");
DNSServer dnsServer;
byte rnd;
byte i;
byte count;
byte wifipkt[128] = { 0x80, 0x00, 0x00, 0x00,
/*4*/ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
/*10*/ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
/*16*/ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
/*22*/ 0xc0, 0x6c,
/*24*/ 0x83, 0x51, 0xf7, 0x8f, 0x0f, 0x00, 0x00, 0x00,
/*32*/ 0x64, 0x00,
/*34*/ 0x01, 0x04,
/*36*/ 0x00};
byte pktsuffix[] = {
0x01, 0x08, 0x82, 0x84,
0x8b, 0x96, 0x24, 0x30, 0x48, 0x6c, 0x03, 0x01,
0x04 };
class CaptiveRequestHandler : public AsyncWebHandler {
public:
CaptiveRequestHandler() {}
virtual ~CaptiveRequestHandler() {}
bool canHandle(AsyncWebServerRequest *request) {
return true;
}
void handleRequest(AsyncWebServerRequest *request) {
request->redirect("http://192.168.4.1");
}
};
void onWsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len) {
if(type == WS_EVT_CONNECT){
Serial.printf("WebSocket client #%u connected from %s\n", client->id(), client->remoteIP().toString().c_str());
std::queue<String> tempQueue = messageQueue;
while(!tempQueue.empty()) {
client->text(tempQueue.front());
tempQueue.pop();
}
} else if(type == WS_EVT_DISCONNECT){
Serial.printf("WebSocket client #%u disconnected\n", client->id());
} else if(type == WS_EVT_DATA){
String message = "";
for(size_t i=0; i < len; i++) {
message += (char) data[i];
}
if(messageQueue.size() >= MAX_MESSAGES) {
messageQueue.pop();
}
messageQueue.push(message);
ws.textAll(message);
}
}
void setup() {
Serial.begin(115200);
Serial.println("\n\nStarting WiFi Access Point...");
pinMode(STATUS_LED_PIN, OUTPUT);
digitalWrite(STATUS_LED_PIN, HIGH);
WiFi.disconnect(true);
delay(100);
WiFi.mode(WIFI_AP);
delay(100);
if(WiFi.softAPConfig(IPAddress(192,168,4,1), IPAddress(192,168,4,1), IPAddress(255,255,255,0))) {
Serial.println("AP Config successful");
} else {
Serial.println("AP Config failed!");
}
WiFi.setOutputPower(MAX_POWER);
if(WiFi.softAP(ap_ssid, ap_password, CHANNEL, false, 8)) {
Serial.println("AP started successfully!");
digitalWrite(STATUS_LED_PIN, LOW);
} else {
Serial.println("AP failed to start!");
for(int i = 0; i < 10; i++) {
digitalWrite(STATUS_LED_PIN, !digitalRead(STATUS_LED_PIN));
delay(200);
}
}
Serial.print("AP SSID: ");
Serial.println(ap_ssid);
Serial.print("AP Password: ");
Serial.println(ap_password);
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
Serial.print("AP MAC address: ");
Serial.println(WiFi.softAPmacAddress());
Serial.print("Channel: ");
Serial.println(CHANNEL);
// Start DNS server for captive portal
dnsServer.start(53, "*", WiFi.softAPIP());
ws.onEvent(onWsEvent);
server.addHandler(&ws);
// Handle root page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
String html = ""
"<!DOCTYPE html>\n"
"<html>\n"
"<head>\n"
" <title>ESP8266 Chat Room</title>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
" <style>\n"
" body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }\n"
" #chatBox { height: 300px; border: 1px solid #ccc; overflow-y: scroll; margin-bottom: 10px; padding: 10px; }\n"
" input { width: 70%; padding: 5px; }\n"
" button { padding: 5px 15px; }\n"
" </style>\n"
"</head>\n"
"<body>\n"
" <h1>ESP8266 Chat Room</h1>\n"
" <div id=\"chatBox\"></div>\n"
" <input type=\"text\" id=\"messageInput\" placeholder=\"Type your message...\">\n"
" <button onclick=\"sendMessage()\">Send</button>\n"
" <script>\n"
" var ws = new WebSocket('ws://' + window.location.hostname + '/ws');\n"
" ws.onopen = function() {\n"
" console.log('Connected to WebSocket server');\n"
" };\n"
" ws.onmessage = function(event) {\n"
" var chatBox = document.getElementById('chatBox');\n"
" chatBox.innerHTML += event.data + '<br>';\n"
" chatBox.scrollTop = chatBox.scrollHeight;\n"
" };\n"
" function sendMessage() {\n"
" var input = document.getElementById('messageInput');\n"
" if(input.value) {\n"
" ws.send(input.value);\n"
" input.value = \"\";\n"
" }\n"
" }\n"
" document.getElementById('messageInput').addEventListener('keypress', function(e) {\n"
" if(e.key === 'Enter') {\n"
" sendMessage();\n"
" }\n"
" });\n"
" </script>\n"
"</body>\n"
"</html>\n";
AsyncWebServerResponse *response = request->beginResponse(200, "text/html", html);
response->addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response->addHeader("Pragma", "no-cache");
response->addHeader("Expires", "-1");
request->send(response);
});
// Captive portal response handler
server.addHandler(new CaptiveRequestHandler()).setFilter(ON_AP_FILTER);
// Handle 404 - redirect to home page
server.onNotFound([](AsyncWebServerRequest *request){
request->redirect("/");
});
server.begin();
wifi_promiscuous_enable(1);
Serial.println("Web server and WebSocket started");
}
void loop() {
dnsServer.processNextRequest();
static unsigned long lastStatus = 0;
if (millis() - lastStatus >= 5000) {
lastStatus = millis();
Serial.printf("Connected stations: %d\n", WiFi.softAPgetStationNum());
Serial.printf("AP status: %s\n", WiFi.softAPIP() != IPAddress(0,0,0,0) ? "Running" : "Failed");
}
if (WiFi.softAPIP() == IPAddress(0,0,0,0)) {
digitalWrite(STATUS_LED_PIN, (millis() / 200) % 2 == 0 ? LOW : HIGH);
} else if (WiFi.softAPgetStationNum() == 0) {
digitalWrite(STATUS_LED_PIN, (millis() / 1000) % 2 == 0 ? LOW : HIGH);
} else {
digitalWrite(STATUS_LED_PIN, LOW);
}
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sendBeacons();
}
}
void sendBeacons() {
if (messageQueue.empty()) return;
wifipkt[10] = wifipkt[16] = random(256);
wifipkt[11] = wifipkt[17] = random(256);
wifipkt[12] = wifipkt[18] = random(256);
wifipkt[13] = wifipkt[19] = random(256);
wifipkt[14] = wifipkt[20] = random(256);
wifipkt[15] = wifipkt[21] = random(256);
count = 37;
std::queue<String> tempQueue = messageQueue;
rnd = random(tempQueue.size());
for(int i = 0; i < rnd; i++) {
tempQueue.pop();
}
String beaconMessage = tempQueue.front();
if(beaconMessage.length() > 32) {
beaconMessage = beaconMessage.substring(0, 32);
}
wifipkt[count++] = beaconMessage.length();
for (i = 0; i < beaconMessage.length(); i++) {
wifipkt[count++] = beaconMessage[i];
}
for (i = 0; i < sizeof(pktsuffix); i++) {
wifipkt[count++] = pktsuffix[i];
}
wifi_set_channel(CHANNEL);
wifipkt[count-1] = CHANNEL;
wifi_send_pkt_freedom(wifipkt, count, 0);
wifi_send_pkt_freedom(wifipkt, count, 0);
wifi_send_pkt_freedom(wifipkt, count, 0);
delay(1);
}