forked from D3R-ST3FAN/LaCrosseGatewayMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessPoint.cpp
More file actions
62 lines (50 loc) · 1.49 KB
/
Copy pathAccessPoint.cpp
File metadata and controls
62 lines (50 loc) · 1.49 KB
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
#include "AccessPoint.h"
#include <DNSServer.h>
static DNSServer dnsServer;
AccessPoint::AccessPoint(IPAddress ip, IPAddress gateway, IPAddress subnet, String ssidPrefix) {
m_ip = ip;
m_gateway = gateway;
m_subnet = subnet;
m_ssidPrefix = ssidPrefix;
}
void AccessPoint::SetLogItemCallback(LogItemCallbackType *callback) {
m_logItemCallback = callback;
}
void AccessPoint::Begin(int autoClose) {
if (m_logItemCallback != NULL) m_logItemCallback("Starting ...");
WiFi.persistent(false);
WiFi.disconnect(true);
delay(100);
WiFi.mode(WiFiMode::WIFI_AP);
delay(100);
WiFi.softAPConfig(m_ip, m_ip, m_subnet);
String ssid = m_ssidPrefix + "_" + String((unsigned int)ESP.getChipId());
WiFi.softAP(ssid.c_str());
delay(500);
dnsServer.setErrorReplyCode(DNSReplyCode::NoError);
dnsServer.start(53, "*", m_ip);
if (m_logItemCallback != NULL) m_logItemCallback("running, SSID=" + ssid);
m_autoClose = autoClose;
m_startMillis = millis();
m_running = true;
}
void AccessPoint::End() {
if (m_logItemCallback != NULL) m_logItemCallback("Closing ...");
dnsServer.stop();
WiFi.softAPdisconnect(true);
m_running = false;
m_autoClose = 0;
WiFi.mode(WiFiMode::WIFI_STA);
delay(100);
if (m_logItemCallback != NULL) m_logItemCallback("closed");
}
void AccessPoint::Handle() {
if (m_running) {
dnsServer.processNextRequest();
if (m_autoClose > 0) {
if (millis() - m_startMillis > (uint32_t)m_autoClose * 1000UL) {
End();
}
}
}
}