SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
networking.h
Go to the documentation of this file.
1#ifndef SENSESP_NET_NETWORKING_H_
2#define SENSESP_NET_NETWORKING_H_
3
4#include <Arduino.h>
5#include <DNSServer.h>
6#include <WiFi.h>
7
14#include "sensesp_base_app.h"
15
16namespace sensesp {
17
18constexpr int kMaxNumClientConfigs = 3;
19
29class WiFiStateProducer : public ValueProducer<WiFiState> {
30 public:
33
35
36 // Emit the current state as soon as the event loop starts
37 event_loop()->onDelay(0, [this]() { this->emit(this->output_); });
38 }
39
41
43 void operator=(const WiFiStateProducer&) = delete;
44
45 protected:
47 wifi_event_id_t wifi_ap_start_event_id_;
49 wifi_event_id_t wifi_ap_stop_event_id_;
50
52 wifi_sta_got_ip_event_id_ = WiFi.onEvent(
53 [this](WiFiEvent_t event, WiFiEventInfo_t info) {
55 },
56 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_GOT_IP);
58 WiFi.onEvent([this](WiFiEvent_t event,
59 WiFiEventInfo_t info) { this->wifi_ap_enabled(); },
60 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_START);
62 [this](WiFiEvent_t event, WiFiEventInfo_t info) {
63 this->wifi_disconnected();
64 },
65 WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
66 wifi_ap_stop_event_id_ = WiFi.onEvent(
67 [this](WiFiEvent_t event, WiFiEventInfo_t info) {
68 this->wifi_disconnected();
69 },
70 WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STOP);
71 }
72
74 WiFi.removeEvent(wifi_sta_got_ip_event_id_);
75 WiFi.removeEvent(wifi_ap_start_event_id_);
76 WiFi.removeEvent(wifi_sta_disconnected_event_id_);
77 WiFi.removeEvent(wifi_ap_stop_event_id_);
78 }
79
81 ESP_LOGI(__FILENAME__, "Connected to wifi, SSID: %s (signal: %d)",
82 WiFi.SSID().c_str(), WiFi.RSSI());
83 ESP_LOGI(__FILENAME__, "IP address of Device: %s",
84 WiFi.localIP().toString().c_str());
85 ESP_LOGI(__FILENAME__, "Default route: %s",
86 WiFi.gatewayIP().toString().c_str());
87 ESP_LOGI(__FILENAME__, "DNS server: %s", WiFi.dnsIP().toString().c_str());
89 }
90
92 ESP_LOGI(__FILENAME__, "WiFi Access Point enabled, SSID: %s",
93 WiFi.softAPSSID().c_str());
94 ESP_LOGI(__FILENAME__, "IP address of Device: %s",
95 WiFi.softAPIP().toString().c_str());
96
97 // Setting the AP mode happens immediately,
98 // so this callback is likely called already before all startables have been
99 // initiated. Delay the WiFi state update until the start of the event loop.
100 event_loop()->onDelay(
101 0, [this]() { this->emit(WiFiState::kWifiAPModeActivated); });
102 }
103
105 ESP_LOGI(__FILENAME__, "Disconnected from wifi.");
107 }
108};
109
115 public:
116 AccessPointSettings(bool enabled = true, String ssid = "",
117 String password = "", int channel = 9,
118 bool hidden = false, bool captive_portal_enabled = true)
119 : enabled_{enabled},
120 ssid_{ssid},
121 password_{password},
122 channel_{channel},
123 hidden_{hidden},
124 captive_portal_enabled_{captive_portal_enabled} {
125 if (ssid_ == "") {
126 // If no SSID is provided, use the hostname as the SSID
128 }
129 };
130
132 String ssid_;
133 String password_;
137
138 static AccessPointSettings from_json(const JsonObject& json) {
139 AccessPointSettings settings;
140 settings.enabled_ = json["enabled"] | false;
141 settings.ssid_ = json["name"] | "";
142 settings.password_ = json["password"] | "";
143 settings.channel_ = json["channel"] | 1;
144 settings.hidden_ = json["hidden"] | false;
145 settings.captive_portal_enabled_ = json["captivePortalEnabled"] | true;
146 return settings;
147 }
148
149 void as_json(JsonObject& doc) {
150 doc["enabled"] = enabled_;
151 doc["name"] = ssid_;
152 doc["password"] = password_;
153 doc["channel"] = channel_;
154 doc["hidden"] = hidden_;
155 doc["captivePortalEnabled"] = captive_portal_enabled_;
156 }
157};
158
164 public:
165 ClientSSIDConfig(String ssid = "", String password = "", bool use_dhcp = true,
166 IPAddress ip = IPAddress(169, 254, 0, 1),
167 IPAddress netmask = IPAddress(255, 255, 255, 0),
168 IPAddress gateway = IPAddress(192, 168, 0, 1),
169 IPAddress dns_server = IPAddress(8, 8, 8, 8))
170 : ssid_{ssid},
171 password_{password},
172 use_dhcp_{use_dhcp},
173 ip_{ip},
174 netmask_{netmask},
175 gateway_{gateway},
176 dns_server_{dns_server} {};
177
178 String ssid_;
179 String password_;
181 IPAddress ip_;
182 IPAddress netmask_;
183 IPAddress gateway_;
184 IPAddress dns_server_;
185
186 static ClientSSIDConfig from_json(const JsonObject& json) {
187 ClientSSIDConfig config;
188 config.ssid_ = json["name"] | "";
189 config.password_ = json["password"] | "";
190 config.use_dhcp_ = json["useDHCP"] | true;
191 config.ip_.fromString(json["ipAddress"] | "169.254.0.1");
192 config.netmask_.fromString(json["netmask"] | "255.255.255.0");
193 config.gateway_.fromString(json["gateway"] | "192.168.0.1");
194 config.dns_server_.fromString(json["dnsServer"] | "8.8.8.8");
195 return config;
196 }
197
198 void as_json(JsonObject& doc) {
199 doc["name"] = ssid_;
200 doc["password"] = password_;
201 doc["useDHCP"] = use_dhcp_;
202 doc["ipAddress"] = ip_.toString();
203 doc["netmask"] = netmask_.toString();
204 doc["gateway"] = gateway_.toString();
205 doc["dnsServer"] = dns_server_.toString();
206 }
207};
208
214 public:
216 : ssid_{""}, rssi_{0}, encryption_{0}, bssid_{0}, channel_{0} {}
217 WiFiNetworkInfo(String ssid, int32_t rssi, uint8_t encryption, uint8_t* bssid,
218 int32_t channel)
219 : ssid_{ssid}, rssi_{rssi}, encryption_{encryption}, channel_{channel} {
220 memcpy(bssid_, bssid, 6);
221 }
222
223 String ssid_;
224 int32_t rssi_;
225 uint8_t encryption_;
226 uint8_t bssid_[6];
227 int32_t channel_;
228
229 void as_json(JsonObject& doc) {
230 doc["ssid"] = ssid_;
231 doc["rssi"] = rssi_;
232 doc["encryption"] = encryption_;
233 doc["channel"] = channel_;
234 // Add bssid as a string
235 doc["bssid"] = String(bssid_[0], HEX) + ":" + String(bssid_[1], HEX) + ":" +
236 String(bssid_[2], HEX) + ":" + String(bssid_[3], HEX) + ":" +
237 String(bssid_[4], HEX) + ":" + String(bssid_[5], HEX);
238 }
239};
240
245 public Resettable,
246 public ValueProducer<WiFiState> {
247 public:
248 Networking(const String& config_path, const String& client_ssid = "",
249 const String& client_password = "", const String& ap_ssid = "",
250 const String& ap_password = "");
251 ~Networking();
252
253 virtual void reset() override;
254
255 virtual bool to_json(JsonObject& doc) override;
256 virtual bool from_json(const JsonObject& config) override;
257
258 void start_wifi_scan();
259 int16_t get_wifi_scan_results(std::vector<WiFiNetworkInfo>& ssid_list);
260
264
265 const std::shared_ptr<WiFiStateProducer>& get_wifi_state_producer() {
267 }
268
269 protected:
270 void start_access_point();
272
274
275 bool client_enabled_ = false;
276 std::vector<ClientSSIDConfig> client_settings_;
277
278 std::unique_ptr<DNSServer> dns_server_;
279
280 std::shared_ptr<WiFiStateProducer> wifi_state_producer_ =
281 std::make_shared<WiFiStateProducer>();
282
283 std::shared_ptr<LambdaConsumer<WiFiState>> wifi_state_emitter_;
284};
285
286inline const String ConfigSchema(const Networking& obj) {
287 return "null";
288}
289
290inline bool ConfigRequiresRestart(const Networking& obj) { return true; }
291
292} // namespace sensesp
293
294#endif
Storage object for WiFi access point settings.
Definition networking.h:114
static AccessPointSettings from_json(const JsonObject &json)
Definition networking.h:138
AccessPointSettings(bool enabled=true, String ssid="", String password="", int channel=9, bool hidden=false, bool captive_portal_enabled=true)
Definition networking.h:116
void as_json(JsonObject &doc)
Definition networking.h:149
Storage object for WiFi client settings.
Definition networking.h:163
static ClientSSIDConfig from_json(const JsonObject &json)
Definition networking.h:186
void as_json(JsonObject &doc)
Definition networking.h:198
ClientSSIDConfig(String ssid="", String password="", bool use_dhcp=true, IPAddress ip=IPAddress(169, 254, 0, 1), IPAddress netmask=IPAddress(255, 255, 255, 0), IPAddress gateway=IPAddress(192, 168, 0, 1), IPAddress dns_server=IPAddress(8, 8, 8, 8))
Definition networking.h:165
Manages the ESP's connection to the Wifi network.
Definition networking.h:246
virtual void reset() override
std::unique_ptr< DNSServer > dns_server_
Definition networking.h:278
void start_client_autoconnect()
Start WiFi using preset SSID and password.
virtual bool from_json(const JsonObject &config) override
void start_access_point()
Start an access point.
int16_t get_wifi_scan_results(std::vector< WiFiNetworkInfo > &ssid_list)
Networking(const String &config_path, const String &client_ssid="", const String &client_password="", const String &ap_ssid="", const String &ap_password="")
const std::shared_ptr< WiFiStateProducer > & get_wifi_state_producer()
Definition networking.h:265
virtual bool to_json(JsonObject &doc) override
Serialize the current configuration to a JSON document.
std::shared_ptr< LambdaConsumer< WiFiState > > wifi_state_emitter_
Definition networking.h:283
std::vector< ClientSSIDConfig > client_settings_
Definition networking.h:276
AccessPointSettings ap_settings_
Definition networking.h:273
bool is_captive_portal_enabled()
Definition networking.h:261
std::shared_ptr< WiFiStateProducer > wifi_state_producer_
Definition networking.h:280
Automatic calling of the reset() method when the device needs to be reset.
Definition resettable.h:20
static String get_hostname()
Get the current hostname.
A base class for any sensor or piece of code that outputs a value for consumption elsewhere.
void emit(const WiFiState &new_value)
WiFi Network Information storage class.
Definition networking.h:213
WiFiNetworkInfo(String ssid, int32_t rssi, uint8_t encryption, uint8_t *bssid, int32_t channel)
Definition networking.h:217
void as_json(JsonObject &doc)
Definition networking.h:229
Provide information about the current WiFi state.
Definition networking.h:29
wifi_event_id_t wifi_sta_got_ip_event_id_
Definition networking.h:46
wifi_event_id_t wifi_ap_start_event_id_
Definition networking.h:47
void operator=(const WiFiStateProducer &)=delete
wifi_event_id_t wifi_sta_disconnected_event_id_
Definition networking.h:48
wifi_event_id_t wifi_ap_stop_event_id_
Definition networking.h:49
WiFiStateProducer(WiFiStateProducer &other)=delete
const String ConfigSchema(const SmartSwitchController &obj)
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
constexpr int kMaxNumClientConfigs
Definition networking.h:18
bool ConfigRequiresRestart(const HTTPServer &obj)