2020-09-03 14:10:52 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "Preferences.h"
|
|
|
|
#include <WiFi.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manages connection and provisioning of WiFI
|
|
|
|
*
|
|
|
|
* Starts in state AP_PROVISIONING, where device is an access point without password.
|
|
|
|
* Device should not work normally in this mode, only API is served to set password.
|
|
|
|
* Two options:
|
|
|
|
* - connect to existing WiFi (station mode), via setStaCredentials
|
|
|
|
* - create secured access point, via setApCredentials
|
|
|
|
*
|
|
|
|
* When operating in access point mode, the device IP is 192.168.42.1
|
|
|
|
*
|
2021-06-06 15:56:30 +02:00
|
|
|
* call iteration() regularly, it has a wifiWatchdog to reconnect in station mode if connection was lost
|
2020-09-03 14:10:52 +02:00
|
|
|
*/
|
|
|
|
class WifiManager
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum State
|
|
|
|
{
|
|
|
|
INVALID,
|
|
|
|
STA,
|
|
|
|
AP_PROVISIONING,
|
|
|
|
AP_SECURE,
|
|
|
|
};
|
|
|
|
WifiManager() : state_(INVALID) {}
|
|
|
|
|
2021-07-22 18:38:28 +02:00
|
|
|
void begin(const String &hostname, const String & wifiStaModeName);
|
2020-09-03 14:10:52 +02:00
|
|
|
|
|
|
|
void setStaCredentials(const char *wifiName, const char *password);
|
|
|
|
void setApCredentials(const char *password);
|
|
|
|
void resetToApProvisioning();
|
|
|
|
|
2021-06-06 15:56:30 +02:00
|
|
|
void iteration();
|
2020-09-03 14:10:52 +02:00
|
|
|
|
|
|
|
bool inProvisioningMode() const { return state_ == INVALID || state_ == AP_PROVISIONING; }
|
|
|
|
|
|
|
|
State state() const { return state_; }
|
|
|
|
const char *stateStr() const { return stateToString(state_); }
|
|
|
|
|
2021-07-26 14:35:25 +02:00
|
|
|
const String & hostname() const { return hostname_; }
|
2020-09-03 14:10:52 +02:00
|
|
|
static const char *stateToString(State state);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void startWifi();
|
|
|
|
Preferences prefs_;
|
|
|
|
State state_;
|
|
|
|
String hostname_;
|
2021-07-22 18:38:28 +02:00
|
|
|
String wifiStaModeName_;
|
2020-09-03 14:10:52 +02:00
|
|
|
};
|