SensESP 2.7.2
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
networking.h
Go to the documentation of this file.
1#ifndef _networking_H_
2#define _networking_H_
3
4#include "Arduino.h"
5
6// Local WebServer used to serve the configuration portal
7#include <ESPAsyncWebServer.h>
8#include <ESPAsyncWiFiManager.h>
9
16
17namespace sensesp {
18
28class WiFiStateProducer : public ValueProducer<WiFiState>, public Startable {
29 public:
34
38 void operator=(const WiFiStateProducer&) = delete;
39
44
45 virtual void start() override {
47 // Emit the current state immediately
48 this->emit(this->output);
49 }
50
51 protected:
53
55 WiFi.onEvent(
58 },
59 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
60 WiFi.onEvent([this](WiFiEvent_t event,
62 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_START);
63 WiFi.onEvent([this](WiFiEvent_t event,
65 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
66 WiFi.onEvent([this](WiFiEvent_t event,
68 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STOP);
69 }
70
72 debugI("Connected to wifi, SSID: %s (signal: %d)", WiFi.SSID().c_str(),
73 WiFi.RSSI());
74 debugI("IP address of Device: %s", WiFi.localIP().toString().c_str());
75 debugI("Default route: %s", WiFi.gatewayIP().toString().c_str());
76 debugI("DNS server: %s", WiFi.dnsIP().toString().c_str());
78 }
79
81 debugI("WiFi Access Point enabled, SSID: %s", WiFi.softAPSSID().c_str());
82 debugI("IP address of Device: %s", WiFi.softAPIP().toString().c_str());
83
84 // Setting the AP mode happens immediately,
85 // so this callback is likely called already before all startables have been
86 // initiated. Delay the WiFi state update until the start of the event loop.
87 ReactESP::app->onDelay(
88 0, [this]() { this->emit(WiFiState::kWifiAPModeActivated); });
89 }
90
92 debugI("Disconnected from wifi.");
94 }
95
97};
98
103 public Startable,
104 public Resettable,
105 public ValueProducer<WiFiState> {
106 public:
108 const char* wifi_manager_password);
109 virtual void start() override;
110 virtual void reset() override;
111
112 virtual void get_configuration(JsonObject& doc) override final;
113 virtual bool set_configuration(const JsonObject& config) override final;
114 virtual String get_config_schema() override;
115
116 void enable_wifi_manager(bool state) { wifi_manager_enabled_ = state; }
117
119
120 void set_wifi_manager_ap_ssid(String ssid) { wifi_manager_ap_ssid_ = ssid; }
121
122 void set_ap_mode(bool state) { ap_mode_ = state; }
123
124 protected:
125 void setup_saved_ssid();
126 void setup_wifi_manager();
127
128 // callbacks
129
133
134 private:
135 AsyncWebServer* server;
136 // FIXME: DNSServer and AsyncWiFiManager could be instantiated in
137 // respective methods to save some runtime memory
138 DNSServer* dns;
139 AsyncWiFiManager* wifi_manager = nullptr;
140
141 bool wifi_manager_enabled_ = true;
142
143 // If true, the device will set up its own WiFi access point
144
145 bool ap_mode_ = false;
146
147 // values provided by WiFiManager or saved from previous configuration
148
149 String ap_ssid = "";
150 String ap_password = "";
151
152 // hardcoded values provided as constructor parameters
153
154 String wifi_manager_ap_ssid_ = "";
155 String preset_ssid = "";
156 String preset_password = "";
157 String preset_hostname = "";
158
159 // original value of hardcoded hostname; used to detect changes
160 // in the hardcoded value
161 String default_hostname = "";
162
163 WiFiStateProducer* wifi_state_producer;
164
165 const char* wifi_manager_password_;
166};
167
168} // namespace sensesp
169
170#endif
An object that is capable of having configuration data that can be set remotely using a RESTful API,...
Construct a new transform based on a single function.
Manages the ESP's connection to the Wifi network.
Definition networking.h:105
virtual void reset() override
virtual void get_configuration(JsonObject &doc) override final
void setup_wifi_manager()
Start WiFi using WiFi Manager.
virtual String get_config_schema() override
void set_wifi_manager_ap_ssid(String ssid)
Definition networking.h:120
void enable_wifi_manager(bool state)
Definition networking.h:116
void setup_saved_ssid()
Start WiFi using preset SSID and password.
virtual bool set_configuration(const JsonObject &config) override final
virtual void start() override
void wifi_station_connected()
void set_ap_mode(bool state)
Definition networking.h:122
Automatic calling of the reset() method when the device needs to be reset.
Definition resettable.h:20
Automatic calling of the start() method at startup.
Definition startable.h:20
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(WiFiState new_value)
Provide information about the current WiFi state.
Definition networking.h:28
static WiFiStateProducer * instance_
Definition networking.h:96
static WiFiStateProducer * get_singleton()
Get the singleton instance of the WiFiStateProducer.
virtual void start() override
Definition networking.h:45
void operator=(const WiFiStateProducer &)=delete
WiFiStateProducer(WiFiStateProducer &other)=delete
#define debugI(fmt,...)
Definition local_debug.h:48