-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.ino
60 lines (51 loc) · 1.82 KB
/
Network.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
char* ap_ssid = "kneebrace";
char* ap_password = "smartorthotics!";
bool usingAP = false;
void networkSetup() {
String configString = readFile(LittleFS, "/config.json");
JSONVar configJson = JSON.parse(configString);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
Serial.println(WiFi.macAddress());
int n = WiFi.scanNetworks();
for (int i = 0; i < n; i++) {
String ssid = WiFi.SSID(i);
if (configJson["networks"].hasOwnProperty(ssid)) {
for (int j = 0; j < 10; j++ ) {
Serial.println("Connecting to " + ssid);
String password = configJson["networks"][ssid];
WiFi.begin(WiFi.SSID(i).c_str(), password.c_str());
delay(1000);
if (WiFi.status() == WL_CONNECTED) {
break;
}
}
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(WiFi.localIP());
break;
}
}
if (WiFi.status() != WL_CONNECTED) {
Serial.println("Unable to connect to WiFi, starting access point");
usingAP = true;
WiFi.mode(WIFI_AP);
WiFi.softAP(ap_ssid, ap_password);
}
if (!MDNS.begin("kneebrace")) {
Serial.println("Error starting mDNS");
} else {
Serial.println("Started mDNS, visit `kneebrace.local` in browser");
}
}
unsigned long networkCurrentMillis = millis();
unsigned long networkPreviousMillis = millis();
unsigned long networkInterval = 10000;
void networkLoop() {
networkCurrentMillis = millis();
if (!usingAP && (WiFi.status() != WL_CONNECTED) && (networkCurrentMillis - networkPreviousMillis >=networkInterval)) {
Serial.println("Reconnecting to WiFi...");
networkSetup();
networkPreviousMillis = networkCurrentMillis;
}
}