-
Notifications
You must be signed in to change notification settings - Fork 492
Initial implementation of WiFiManager #226
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "thing/Transcriber.h" | ||
#include "thing/Portal.h" | ||
#include "thing/WiFiManager.h" |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include "thing/WiFiManager.h" | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <DNSServer.h> | ||
|
||
|
||
namespace thing { | ||
namespace { | ||
|
||
const short kDnsPort = 53; | ||
|
||
} // namespace | ||
|
||
WiFiManager::WiFiManager() : debug_([](const char*){}) {} | ||
|
||
bool WiFiManager::StartAP() { | ||
// Use arduino string, casting an int to std::string with arduino C++ support | ||
// isn't easy. | ||
String ssid("FireThing-"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be a constant format string? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Best I can tell there is no easy printf support in arduino. We can use c printf but you need to allocate a buffer and do the manipulations there which is what I was avoiding by just using arduino's string and concatenation. |
||
ssid += ESP.getChipId(); | ||
|
||
WiFi.mode(WIFI_AP_STA); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use the mixed mode where we can be both client and station? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is this one. AP is access point and STA is wifi's wierd name for a client. |
||
if (!WiFi.softAP(ssid.c_str())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. snould we retry if that fail? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The caller of this is free to retry on failure. I don't actually know what would cause this to fail. |
||
return false; | ||
} | ||
|
||
debug_((String("WiFi AP : ") + ssid).c_str()); | ||
debug_((String("Wifi AP IP : ") + WiFi.softAPIP().toString()).c_str()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did you explore mdns support so that you don't have to rely on the IP? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The IP is actually always the same, it is hard coded to 192.168.4.1 . mdns would be great for when we are in station mode though, that way we can be configured all the time without having to access our captive portal. I haven't looked into it though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be great if we can have the same mdns name for STA and AP mode:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that does sound pretty compelling. I spawned #233 to explore this, don't see any point blocking this PR on it though as this will be the basis for that work. |
||
|
||
dns_.reset(new DNSServer()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is that also doing dhcp or just dns? if the late could we return 8.8.8.8 instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is how you do a captive portal. You become the dns server and just return your own ip address for every lookup attempt. This is how WiFiManager (the arduino library) was doing it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ack. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to bring down the dns once the device is connected to wifi? Will it only get request from the WiFi AP network? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could leave it running indefinitely without any issues. |
||
dns_->start(kDnsPort, "*", WiFi.softAPIP()); | ||
dns_->setTTL(30); | ||
|
||
ap_up_ = true; | ||
return true; | ||
} | ||
|
||
bool WiFiManager::StopAP() { | ||
if (!WiFi.softAPdisconnect()) { | ||
return false; | ||
} | ||
dns_->stop(); | ||
dns_.reset(nullptr); | ||
|
||
ap_up_ = false; | ||
return true; | ||
} | ||
|
||
void WiFiManager::Loop() { | ||
if (dns_) { | ||
dns_->processNextRequest(); | ||
} | ||
} | ||
|
||
bool WiFiManager::Connect(const std::string& ssid, const std::string& auth) { | ||
auto status = WiFi.begin(ssid.c_str(), auth.c_str()); | ||
|
||
return status != WL_CONNECT_FAILED; | ||
} | ||
|
||
bool WiFiManager::connected() const { | ||
return WiFi.status() == WL_CONNECTED; | ||
} | ||
|
||
void WiFiManager::SetDebugHandler(std::function<void(const char* message)> handler) { | ||
debug_ = std::move(handler); | ||
} | ||
|
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#ifndef THING_WIFI_MANAGER_H | ||
#define THING_WIFI_MANAGER_H | ||
|
||
#include <ESP8266WiFi.h> | ||
#include <DNSServer.h> | ||
|
||
namespace thing { | ||
|
||
class WiFiManager { | ||
public: | ||
WiFiManager(); | ||
|
||
bool StartAP(); | ||
bool StopAP(); | ||
bool Connect(const std::string& ssid, const std::string& auth); | ||
|
||
void Loop(); | ||
|
||
bool connected() const; | ||
bool ap_up() const { return ap_up_; } | ||
|
||
void SetDebugHandler(std::function<void(const char* message)> handler); | ||
private: | ||
bool ap_up_ = false; | ||
std::unique_ptr<DNSServer> dns_; | ||
std::function<void(const char* message)> debug_; | ||
}; | ||
|
||
}; | ||
|
||
#endif // THING_WIFI_MANAGER_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
std::to_string didn't work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope, not defined for some reason. There are other std:: things that arduino c++ doesn't support as well.