TinyWifiDongle 0.1.0
Header-only USB WiFi dongle for Arduino
Loading...
Searching...
No Matches
TinyWifiDongleSetup.h
Go to the documentation of this file.
1#pragma once
10#include "core/Logger.h"
11#include "setup/Board.h"
13#include "setup/SetupPortal.h"
14
42 public:
44 explicit TinyWifiDongleSetup(const char* setupNetwork = "TinyWifiDongle-Setup", const char* setupPassword = nullptr)
45 : portal_(setupNetwork, setupPassword) {}
46
48 void setForgetWindow(uint32_t ms) { forgetWindowMs_ = ms; }
49
51 void setJoinTimeout(uint32_t ms) { joinTimeoutMs_ = ms; }
52
54 void begin() {
55 bool known = store_.load();
56 if (forgetWindowMs_ && twd::Board::buttonPressedWithin(forgetWindowMs_)) {
57 TWD_LOGGER.info("setup: button pressed, forgetting the saved network");
58 if (!store_.forget()) TWD_LOGGER.error("setup: could not write the flash");
59 known = false;
60 }
61 if (!known) {
62 String ssid, password;
63 portal_.run(ssid, password);
64 if (!store_.save(ssid.c_str(), password.c_str())) {
65 TWD_LOGGER.error("setup: could not write the flash, the network is only used until the next start");
66 }
67 }
68 startedAt_ = millis();
69 }
70
72 const char* ssid() const { return store_.ssid(); }
73
75 const char* password() const { return store_.password(); }
76
78 void forget() { store_.forget(); }
79
81 void update(bool connected) {
82 if (connected) {
83 connected_ = true;
84 } else if (!connected_ && joinTimeoutMs_ && millis() - startedAt_ > joinTimeoutMs_) {
85 TWD_LOGGER.warning("setup: could not join '%s' within %u ms, forgetting it and restarting", ssid(),
86 (unsigned)joinTimeoutMs_);
87 if (!store_.forget()) TWD_LOGGER.error("setup: could not write the flash");
88 TWD_LOGGER.flush();
90 }
91 }
92
93 private:
95 twd::SetupPortal portal_;
96 uint32_t forgetWindowMs_ = 3000;
97 uint32_t joinTimeoutMs_ = 60000;
98 uint32_t startedAt_ = 0;
99 bool connected_ = false;
100};
Printf-style logging through the global TWD_LOGGER object.
static twd::LoggerClass TWD_LOGGER
The logger used by all components; configure it in the sketch with TWD_LOGGER.begin(Serial).
Definition Logger.h:107
TinyWifiDongleSetup(const char *setupNetwork="TinyWifiDongle-Setup", const char *setupPassword=nullptr)
The setup network is called setupNetwork; setupPassword (at least 8 characters) protects it,...
void forget()
Removes the saved network; the next start opens the setup.
void setJoinTimeout(uint32_t ms)
How long a start may take to join the network before the saved network is forgotten and the setup ope...
void setForgetWindow(uint32_t ms)
How long, after a start, the BOOT button (ESP32) or BOOTSEL button (Pico) forgets the saved network; ...
void update(bool connected)
Call regularly with TinyWifiDongle::isWifiConnected(). If the board has not joined the network within...
void begin()
Loads the saved network, or opens the setup and blocks until the user has entered one.
const char * ssid() const
Name of the network to join; valid after begin().
const char * password() const
Password of the network to join, nullptr for an open network; valid after begin().
static bool buttonPressedWithin(uint32_t ms)
True if the BOOT button (ESP32) or the BOOTSEL button (Raspberry Pi Pico) is pressed at any time duri...
Definition Board.h:15
static void restart()
Restarts the board.
Definition Board.h:32
Keeps the name and password of one WiFi network in flash.
Captive portal in which the user picks a WiFi network and enters its password.
Definition SetupPortal.h:20