SensESP 3.5.1-alpha
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp_app_builder.h
Go to the documentation of this file.
1#ifndef SENSESP_APP_BUILDER_H
2#define SENSESP_APP_BUILDER_H
3
4#include <memory>
5
6#include "Arduino.h"
10#include "sensesp_app.h"
12
13namespace sensesp {
14
15constexpr const char* kDefaultSystemInfoSensorPrefix = "sensorDevice.";
16
22 private:
23 String hostname_ = "SensESP";
24 String ssid_ = "";
25 String password_ = "";
26 String sk_server_address_ = "";
27 uint16_t sk_server_port_ = 0;
28
29 protected:
30 std::shared_ptr<SensESPApp> app_;
31
32 public:
40 app_ = std::shared_ptr<SensESPApp>(new SensESPApp());
41 app_->set_instance(app_);
42 }
53 SensESPAppBuilder* set_wifi_client(String ssid, String password) {
54 app_->set_ssid(ssid);
55 app_->set_wifi_password(password);
56 return this;
57 }
58
59 [[deprecated("Use set_wifi_client instead")]]
60 SensESPAppBuilder* set_wifi(String ssid, String password) {
61 return set_wifi_client(ssid, password);
62 }
63
82 const std::vector<ClientSSIDConfig>& wifi_client_configs) {
83 app_->set_wifi_clients(wifi_client_configs);
84 return this;
85 }
86
97 const String& password) {
98 app_->set_ap_ssid(ssid);
99 app_->set_ap_password(password);
100 return this;
101 }
102
116 app_->set_ssid("");
117 app_->set_wifi_password("");
118 app_->set_ap_ssid("");
119 app_->set_ap_password("");
120 return this;
121 }
122
149 template <typename ProvisionerConfigT>
150 SensESPAppBuilder* set_ethernet(ProvisionerConfigT config) {
151 disable_wifi();
152 app_->set_network_provisioner_factory(
153 [config]() -> std::shared_ptr<NetworkProvisioner> {
154 return std::make_shared<typename ProvisionerConfigT::ProvisionerType>(
155 config);
156 });
157 return this;
158 }
159
169 SensESPAppBuilder* set_sk_server(String address, uint16_t port) {
170 app_->set_sk_server_address(address);
171 app_->set_sk_server_port(port);
172 return this;
173 }
182 SensESPAppBuilder* set_hostname(String hostname) override final {
183 app_->set_hostname(hostname);
184 return this;
185 }
190 SensESPAppBuilder* set_admin_user(const char* username,
191 const char* password) {
192 app_->set_admin_user(username, password);
193 return this;
194 }
195
205 std::shared_ptr<SystemStatusLed> system_status_led) {
206 app_->set_system_status_led(system_status_led);
207 return this;
208 }
219 String prefix = kDefaultSystemInfoSensorPrefix) {
220 auto sensor = std::make_shared<SystemHz>();
221 auto vproducer = std::static_pointer_cast<ValueProducer<float>>(sensor);
222 // We need to store the sensor in the app so it doesn't get garbage
223 // collected.
224 app_->system_hz_sensor_ = vproducer;
225 connect_system_info_sensor(vproducer, prefix, "systemHz");
226 return this;
227 }
235 String prefix = kDefaultSystemInfoSensorPrefix) {
236 auto sensor = std::make_shared<FreeMem>();
237 auto vproducer = std::static_pointer_cast<ValueProducer<uint32_t>>(sensor);
238 // We need to store the sensor in the app so it doesn't get garbage
239 // collected.
240 app_->free_mem_sensor_ = vproducer;
241 connect_system_info_sensor(vproducer, prefix, "freeMemory");
242 return this;
243 }
251 String prefix = kDefaultSystemInfoSensorPrefix) {
252 auto sensor = std::make_shared<Uptime>();
253 auto vproducer = std::static_pointer_cast<ValueProducer<float>>(sensor);
254 // We need to store the sensor in the app so it doesn't get garbage
255 // collected.
256 app_->uptime_sensor_ = vproducer;
257 connect_system_info_sensor(vproducer, prefix, "uptime");
258 return this;
259 }
267 String prefix = kDefaultSystemInfoSensorPrefix) {
268 auto sensor = std::make_shared<IPAddrDev>();
269 auto vproducer = std::static_pointer_cast<ValueProducer<String>>(sensor);
270 // We need to store the sensor in the app so it doesn't get garbage
271 // collected.
272 app_->ip_address_sensor_ = vproducer;
273 connect_system_info_sensor(vproducer, prefix, "ipAddress");
274 return this;
275 }
283 String prefix = kDefaultSystemInfoSensorPrefix) {
284 auto sensor = std::make_shared<WiFiSignal>();
285 auto vproducer = std::static_pointer_cast<ValueProducer<int>>(sensor);
286 // We need to store the sensor in the app so it doesn't get garbage
287 // collected.
288 app_->wifi_signal_sensor_ = vproducer;
289 connect_system_info_sensor(vproducer, prefix, "wifiSignalLevel");
290 return this;
291 }
292
299 app_->set_button_pin(pin);
300 return this;
301 }
302
310 String prefix = kDefaultSystemInfoSensorPrefix) {
311 this->enable_system_hz_sensor(prefix);
312 this->enable_free_mem_sensor(prefix);
313 this->enable_uptime_sensor(prefix);
314 this->enable_ip_address_sensor(prefix);
315 this->enable_wifi_signal_sensor(prefix);
316 return this;
317 }
318
325 SensESPAppBuilder* enable_ota(const char* password) {
326 app_->enable_ota(password);
327 return this;
328 }
329
340 [[deprecated("Use set_wifi_access_point instead")]]
342 app_->set_ap_password(password);
343 return this;
344 }
345
347 // create the wifi disconnect watchdog
348 app_->system_status_controller_
349 ->connect_to(new Debounce<SystemStatus>(
350 3 * 60 * 1000 // 180 s = 180000 ms = 3 minutes
351 ))
352 ->connect_to(new LambdaConsumer<SystemStatus>([](SystemStatus input) {
353 ESP_LOGD(__FILENAME__, "Got system status: %d", (int)input);
354 if (input == SystemStatus::kWifiDisconnected ||
355 input == SystemStatus::kWifiNoAP) {
356 ESP_LOGW(__FILENAME__,
357 "Unable to connect to wifi for too long; restarting.");
358 event_loop()->onDelay(1000, []() { ESP.restart(); });
359 }
360 }));
361
362 return this;
363 }
364
372 std::shared_ptr<SensESPApp> get_app() {
374 app_->setup();
375 return app_;
376 }
377};
378
379} // namespace sensesp
380
381#endif
Implements debounce code for a button or switch.
Definition debounce.h:32
Provides an easy way of calling a function based on the output of any ValueProducer.
A class for quickly configuring a SensESP application object before wiring up your sensors.
SensESPAppBuilder * set_wifi_clients(const std::vector< ClientSSIDConfig > &wifi_client_configs)
Set multiple Wi-Fi client (STA) networks for failover.
const SensESPAppBuilder * enable_wifi_watchdog()
SensESPAppBuilder()
Construct a new SensESPApp Builder object.
SensESPAppBuilder * set_admin_user(const char *username, const char *password)
Set admin username and password for the web interface.
SensESPAppBuilder * set_hostname(String hostname) override final
Set the device hostname.
SensESPAppBuilder * disable_wifi()
Clear the default WiFi STA/AP settings.
SensESPAppBuilder * set_ethernet(ProvisionerConfigT config)
Configure the app to use a non-WiFi NetworkProvisioner.
SensESPAppBuilder * set_system_status_led(std::shared_ptr< SystemStatusLed > system_status_led)
Set the system status led object.
SensESPAppBuilder * set_sk_server(String address, uint16_t port)
Set the Signal K server address and port.
SensESPAppBuilder * set_wifi(String ssid, String password)
std::shared_ptr< SensESPApp > app_
SensESPAppBuilder * set_wifi_client(String ssid, String password)
Set the Wi-Fi network SSID and password.
SensESPAppBuilder * set_wifi_manager_password(const char *password)
Set the wifi manager password.
SensESPAppBuilder * enable_ip_address_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Report the IP address of the device.
SensESPAppBuilder * enable_free_mem_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Enable the free memory sensor.
SensESPAppBuilder * set_wifi_access_point(const String &ssid, const String &password)
Set the wifi access point object SSID and password.
SensESPAppBuilder * set_button_pin(int pin)
Set the button GPIO pin.
std::shared_ptr< SensESPApp > get_app()
Get the SensESPApp object.
SensESPAppBuilder * enable_system_hz_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Enable the System Hz sensor.
SensESPAppBuilder * enable_system_info_sensors(String prefix=kDefaultSystemInfoSensorPrefix)
Enable all built-in system info sensors.
SensESPAppBuilder * enable_uptime_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Report the system uptime in seconds since the last reboot.
SensESPAppBuilder * enable_wifi_signal_sensor(String prefix=kDefaultSystemInfoSensorPrefix)
Report the Wi-Fi signal strength.
SensESPAppBuilder * enable_ota(const char *password)
Enable over-the-air updates for the device.
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
Definition sensesp_app.h:56
Abstract base class for other SensESP*AppBuilder classes.
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
constexpr const char * kDefaultSystemInfoSensorPrefix
void connect_system_info_sensor(std::shared_ptr< ValueProducer< T > > &sensor, const String &prefix, const String &name)
Connect a system information sensor to SKOutput.
Definition system_info.h:18