-
Notifications
You must be signed in to change notification settings - Fork 1
/
WiFiSAO_AccessPoint.ino
102 lines (82 loc) · 2.78 KB
/
WiFiSAO_AccessPoint.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
#include <ESP8266WiFi.h>
#include <FastLED.h>
#include <ESP8266WebServer.h>
#include <FS.h>
// ESP8266wifi DOCS: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/readme.html
// FastLED docs: http://fastled.io/
// SPIFFS filesystem install docs (use newest version): https://randomnerdtutorials.com/install-esp8266-filesystem-uploader-arduino-ide/
#define NUM_LEDS 2
#define DATA_PIN 14
CRGB leds[NUM_LEDS];
// Set web server port number
//WiFiServer server(80);
ESP8266WebServer server(80);
//Variable to store teh HTTP request
String header;
String ssid = "SmartBadge";
int numClients;
String webPage = "<html><body><h1>Hello world, this is my smart badge!</h1></body></html>";
void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
// Initialize LEDs
FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
FastLED.setBrightness( 20 );
leds[0] = CRGB::Black;
leds[1] = CRGB::Black;
FastLED.show();
FastLED.show();
Serial.print("Setting up access point");
Serial.println(WiFi.softAP(ssid) ? "Success" : "Fail");
Serial.print("Access Point AP Address");
Serial.println(WiFi.softAPIP());
server.on("/", handleRoot);
server.on("/feedback.html", handleFeedback);
server.begin();
Serial.println("HTTP server started");
}
void loop() {
// how many clients connected?
if(numClients != WiFi.softAPgetStationNum()){
numClients = WiFi.softAPgetStationNum();
Serial.printf("Stations connected = %d\n", numClients);
//delay(3000);
}
server.handleClient();
}
/* verification message on connect. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
* (not currently working on phone, but works on laptop)
*/
void handleRoot() {
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
server.send(200, "text/html", "<html><body><h1>Hello world, this is my smart badge!<BR>Unfortunately, the file system is not working today.</h1></body></html>");
}
else{
File file = SPIFFS.open("/index.html", "r");
if(!file){
Serial.println("Failed to open file for reading");
}
else {
Serial.println();
Serial.println("File Content:");
while(file.available()){
webPage = file.readString();
//Serial.write(file.read());
Serial.println(webPage);
}
file.close();
}
}
server.send(200, "text/html", webPage);
Serial.println("Client request handled");
}
void handleFeedback(){
Serial.println("Feedback handling");
Serial.println(server.arg("contactbox"));
server.send(200, "text/html", "<html><body><h1>Thank you for your feedback, " + server.arg("contactbox") + ". Unfortunately, I've already forgotten it! Good talk.");
//server.sendHeader("Location", "/");
//server.send(303);
}