Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd6b7742

Browse files
committed
Initial implementation of WiFiManager
1 parentde2ef09 commitd6b7742

File tree

6 files changed

+106
-3
lines changed

6 files changed

+106
-3
lines changed

‎src/Portal.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/Thing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#include"thing/Transcriber.h"
2+
#include"thing/Portal.h"
3+
#include"thing/WiFiManager.h"

‎src/Transcriber.h

Lines changed: 0 additions & 1 deletion
This file was deleted.

‎src/thing/Portal.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Portal::Portal(const Config& config)
99
debug_([](constchar*){}) {}
1010

1111
voidPortal::SetDebugHandler(std::function<void(constchar* message)> handler) {
12-
debug_ = handler;
12+
debug_ =std::move(handler);
1313
}
1414

1515
voidPortal::Start() {
@@ -150,6 +150,8 @@ void Portal::Start() {
150150
entry["ssid"] = WiFi.SSID(i);
151151
entry["rssi"] = WiFi.RSSI(i);
152152
}
153+
// Free station info from memory.
154+
WiFi.scanDelete();
153155

154156
String buffer;
155157
data.printTo(buffer);

‎src/thing/WiFiManager.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include"thing/WiFiManager.h"
2+
3+
#include<ESP8266WiFi.h>
4+
#include<DNSServer.h>
5+
6+
7+
namespacething {
8+
namespace {
9+
10+
constshortkDnsPort =53;
11+
12+
}// namespace
13+
14+
WiFiManager::WiFiManager() : debug_([](constchar*){}) {}
15+
16+
boolWiFiManager::StartAP() {
17+
// Use arduino string, casting an int to std::string with arduino C++ support
18+
// isn't easy.
19+
Stringssid("FireThing-");
20+
ssid += ESP.getChipId();
21+
22+
WiFi.mode(WIFI_AP_STA);
23+
if (!WiFi.softAP(ssid.c_str())) {
24+
returnfalse;
25+
}
26+
27+
debug_((String("WiFi AP :") + ssid).c_str());
28+
debug_((String("Wifi AP IP :") + WiFi.softAPIP().toString()).c_str());
29+
30+
dns_.reset(newDNSServer());
31+
dns_->start(kDnsPort,"*", WiFi.softAPIP());
32+
dns_->setTTL(30);
33+
34+
ap_up_ =true;
35+
returntrue;
36+
}
37+
38+
boolWiFiManager::StopAP() {
39+
if (!WiFi.softAPdisconnect()) {
40+
returnfalse;
41+
}
42+
dns_->stop();
43+
dns_.reset(nullptr);
44+
45+
ap_up_ =false;
46+
returntrue;
47+
}
48+
49+
voidWiFiManager::Loop() {
50+
if (dns_) {
51+
dns_->processNextRequest();
52+
}
53+
}
54+
55+
boolWiFiManager::Connect(const std::string& ssid,const std::string& auth) {
56+
auto status = WiFi.begin(ssid.c_str(), auth.c_str());
57+
58+
return status != WL_CONNECT_FAILED;
59+
}
60+
61+
boolWiFiManager::connected()const {
62+
return WiFi.status() == WL_CONNECTED;
63+
}
64+
65+
voidWiFiManager::SetDebugHandler(std::function<void(constchar* message)> handler) {
66+
debug_ =std::move(handler);
67+
}
68+
69+
};

‎src/thing/WiFiManager.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef THING_WIFI_MANAGER_H
2+
#defineTHING_WIFI_MANAGER_H
3+
4+
#include<ESP8266WiFi.h>
5+
#include<DNSServer.h>
6+
7+
namespacething {
8+
9+
classWiFiManager {
10+
public:
11+
WiFiManager();
12+
13+
boolStartAP();
14+
boolStopAP();
15+
boolConnect(const std::string& ssid,const std::string& auth);
16+
17+
voidLoop();
18+
19+
boolconnected()const;
20+
boolap_up()const {return ap_up_; }
21+
22+
voidSetDebugHandler(std::function<void(constchar* message)> handler);
23+
private:
24+
bool ap_up_ =false;
25+
std::unique_ptr<DNSServer> dns_;
26+
std::function<void(constchar* message)> debug_;
27+
};
28+
29+
};
30+
31+
#endif// THING_WIFI_MANAGER_H

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp