-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wifi.ino
88 lines (72 loc) · 2.02 KB
/
Wifi.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
// Wifi Functions choose between Station or SoftAP
char *ssid = "";
char *pw = "";
void wifiStartup(){
// Build Hostname
snprintf(sapString, 20, "BatteryCharger-%08X", ESP.getChipId());
// Attempt to connecty to the AP stored on board, if not, start in SoftAP mode
WiFi.mode(WIFI_STA);
delay(2000);
WiFi.hostname(sapString);
if(strlen(ssid)) {
Serial.print("Using supplied ssid/pw");
WiFi.begin(ssid,pw);
} else {
Serial.print("Using stored ssid/pw");
WiFi.begin();
}
delay(5000);
if (WiFi.status() != WL_CONNECTED) {
WiFi.mode(WIFI_AP);
Serial.print("Running in AP mode AP name: ");
Serial.println(sapString);
delay(2000);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
WiFi.softAP(sapString);
// if DNSServer is started with "*" for domain name, it will reply with
// provided IP to all DNS request
dnsServer.start(DNS_PORT, "*", apIP);
needDNS = 1;
} else {
String hostName = "Hostname: ";
hostName = hostName + sapString;
Serial.println(hostName);
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
needDNS = 0;
}
}
// Configure wifi using ESP Smartconfig app on phone
void mySmartConfig() {
// Wipe current credentials
WiFi.disconnect(true); // deletes the wifi credentials
WiFi.mode(WIFI_STA);
delay(2000);
WiFi.begin();
WiFi.beginSmartConfig();
//Wait for SmartConfig packet from mobile
Serial.println("Waiting for SmartConfig.");
while (!WiFi.smartConfigDone()) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("SmartConfig received.");
//Wait for WiFi to connect to AP
Serial.println("Waiting for WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
WiFi.setAutoReconnect(true);
WiFi.persistent(true);
}
void wifiLoop(){
if (needDNS) {
dnsServer.processNextRequest();
}
}