-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrashLAB_AutoConnectWithFeedbackLED_SimpleWebServer.ino
147 lines (117 loc) · 3.83 KB
/
CrashLAB_AutoConnectWithFeedbackLED_SimpleWebServer.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
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
const char* apssid = "CrashLAB WeMos D1";
const char* password = "espconfig";
const char* host = "crashlab-wemos-d1";
//for LED status
#include <Ticker.h>
Ticker ticker;
void tick()
{
//toggle state
int state = digitalRead(BUILTIN_LED); // get the current state of GPIO1 pin
digitalWrite(BUILTIN_LED, !state); // set pin to the opposite state
}
//gets called when WiFiManager enters configuration mode
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
//entered config mode, make led toggle faster
ticker.attach(0.2, tick);
}
ESP8266WebServer server(80);
void handleNotFound(){
//digitalWrite(led, 1);
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
//digitalWrite(led, 0);
}
void handleRoot() {
//digitalWrite(led, 1);
server.send(200, "text/plain", "hello from esp8266!");
//digitalWrite(led, 0);
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
//set led pin as output
pinMode(BUILTIN_LED, OUTPUT);
// start ticker with 0.5 because we start in AP mode and try to connect
ticker.attach(0.5, tick);
wifi_station_set_hostname((char *)host);
//WiFiManager
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
//fetches ssid and pass and tries to connect
//if it does not connect it starts an access point with the specified name
//and goes into a blocking loop awaiting configuration
//if (!wifiManager.autoConnect()) {
if (!wifiManager.autoConnect(apssid, password)) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
ticker.detach();
// MDNS responder
if (MDNS.begin(host)) {
MDNS.addService("http", "tcp", 80);
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/inline", [](){
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
// HTTP Server
server.begin(); // Start the HTTP Server
Serial.println("HTTP server started");
//MDNS.addService("http", "tcp", 80);
Serial.printf("Ready! Open http://%s in your browser\n", host);
Serial.print("Or open http://");
Serial.print(WiFi.localIP());
Serial.println("/ in your browser");
}
void loop() {
server.handleClient();
// put your main code here, to run repeatedly:
digitalWrite(BUILTIN_LED, HIGH);
delay(800);
digitalWrite(BUILTIN_LED, LOW);
delay(85);
digitalWrite(BUILTIN_LED, HIGH);
delay(85);
digitalWrite(BUILTIN_LED, LOW);
delay(85);
digitalWrite(BUILTIN_LED, HIGH);
delay(85);
digitalWrite(BUILTIN_LED, LOW);
delay(85);
digitalWrite(BUILTIN_LED, HIGH);
delay(85);
digitalWrite(BUILTIN_LED, LOW);
delay(85);
}