-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathespardwificonfig.ino
150 lines (125 loc) · 3.97 KB
/
espardwificonfig.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
#include <Arduino.h>
#include "FS.h"
#include <LittleFS.h>
#include <WiFi.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include <ArduinoOTA.h>
#define FORMAT_LITTLEFS_IF_FAILED true
char ssid[33] = "";
char password[65] = "";
char deviceName[17] = "";
const char *apssid = "ESPap";
const char *appassword = "thereisnospoon";
const String favicon = "<link rel=\"shortcut icon\" href=\"data:image/vnd.microsoft.icon;base64,AA ==\"></link>";
bool ledState = LOW;
unsigned long previousBlinkMillis = 0;
int blinkInterval = 1000;
IPAddress IP;
WebServer server( 80 );
void setup ( void ) {
Serial.begin ( 115200 );
// wait at least 5 seconds for Serial
for (int i = 0; i <= 5; i++) {
if (Serial) {
Serial.println("start.");
break;
} else {
delay(1000);
}
}
if(!LittleFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
Serial.println("LittleFS Mount Failed");
}
pinMode(LED_BUILTIN, OUTPUT); // Initialize the LED_BUILTIN pin as an output
readFile2(LittleFS, "/wifi/ssid").toCharArray(ssid, 33);
readFile2(LittleFS, "/wifi/pass").toCharArray(password, 65);
readFile2(LittleFS, "/wifi/name").toCharArray(deviceName, 17);
Serial.println(" ");
//Serial.println(ssid);
//Serial.println(password);
WiFi.mode(WIFI_STA);
WiFi.begin ( ssid, password );
Serial.println(" ");
Serial.print("Connecting to: ");
Serial.print(ssid);
Serial.print(" ");
for (int i = 0; i <= 20; i++) {
if (WiFi.status() == WL_CONNECTED ) {
Serial.println("connected");
IP = WiFi.localIP();
break;
}
//Serial.println(WiFi.status());
Serial.print(".");
delay(500);
}
if (WiFi.status() != WL_CONNECTED ) {
Serial.println("connection failed.");
Serial.print("Starting AP: ");
Serial.println(apssid);
WiFi.mode(WIFI_AP);
WiFi.softAP(apssid, appassword);
IP = WiFi.softAPIP();
blinkInterval = 250;
}
server.on ( "/", handleRoot );
server.on ( "/ConfigWIFI", handleConfigWIFI);
server.on ( "/restart", handleRestart);
server.on ( "/formatLittleFS", handleFormatLittleFS);
//server.on ( "/led_on", led_on);
//server.on ( "/led_off", led_off);
server.onNotFound ( handleNotFound );
server.begin();
Serial.println ( "HTTP server started" );
if (!MDNS.begin(deviceName)) {
Serial.println("Error setting up MDNS responder!");
} else {
Serial.print(deviceName);
Serial.println(".local MDNS responder started");
MDNS.addService("http", "tcp", 80);
}
Serial.print("IP address: ");
Serial.println(IP);
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
digitalWrite(LED_BUILTIN, HIGH); // Turn the LED off
} //setup()
void loop ( void ) {
server.handleClient();
ArduinoOTA.handle();
unsigned long currentMillis = millis();
if (currentMillis - previousBlinkMillis > blinkInterval) {
// save the last time you blinked the LED
previousBlinkMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(LED_BUILTIN, ledState);
}
} //loop()