Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit 9ae404e

Browse files
authored
Merge pull request #226 from ed7coyne/firethings
Initial implementation of WiFiManager
2 parents 7226a6b + d6b7742 commit 9ae404e

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_([](const char*){}) {}
1010

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

1515
void Portal::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+
namespace thing {
8+
namespace {
9+
10+
const short kDnsPort = 53;
11+
12+
} // namespace
13+
14+
WiFiManager::WiFiManager() : debug_([](const char*){}) {}
15+
16+
bool WiFiManager::StartAP() {
17+
// Use arduino string, casting an int to std::string with arduino C++ support
18+
// isn't easy.
19+
String ssid("FireThing-");
20+
ssid += ESP.getChipId();
21+
22+
WiFi.mode(WIFI_AP_STA);
23+
if (!WiFi.softAP(ssid.c_str())) {
24+
return false;
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(new DNSServer());
31+
dns_->start(kDnsPort, "*", WiFi.softAPIP());
32+
dns_->setTTL(30);
33+
34+
ap_up_ = true;
35+
return true;
36+
}
37+
38+
bool WiFiManager::StopAP() {
39+
if (!WiFi.softAPdisconnect()) {
40+
return false;
41+
}
42+
dns_->stop();
43+
dns_.reset(nullptr);
44+
45+
ap_up_ = false;
46+
return true;
47+
}
48+
49+
void WiFiManager::Loop() {
50+
if (dns_) {
51+
dns_->processNextRequest();
52+
}
53+
}
54+
55+
bool WiFiManager::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+
bool WiFiManager::connected() const {
62+
return WiFi.status() == WL_CONNECTED;
63+
}
64+
65+
void WiFiManager::SetDebugHandler(std::function<void(const char* 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+
#define THING_WIFI_MANAGER_H
3+
4+
#include <ESP8266WiFi.h>
5+
#include <DNSServer.h>
6+
7+
namespace thing {
8+
9+
class WiFiManager {
10+
public:
11+
WiFiManager();
12+
13+
bool StartAP();
14+
bool StopAP();
15+
bool Connect(const std::string& ssid, const std::string& auth);
16+
17+
void Loop();
18+
19+
bool connected() const;
20+
bool ap_up() const { return ap_up_; }
21+
22+
void SetDebugHandler(std::function<void(const char* message)> handler);
23+
private:
24+
bool ap_up_ = false;
25+
std::unique_ptr<DNSServer> dns_;
26+
std::function<void(const char* message)> debug_;
27+
};
28+
29+
};
30+
31+
#endif // THING_WIFI_MANAGER_H

0 commit comments

Comments
 (0)