SensESP 3.0.1
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
13const char* kDefaultSystemInfoSensorPrefix = "sensorDevice.";
14
15namespace sensesp {
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
74 const String& password) {
75 app_->set_ap_ssid(ssid);
76 app_->set_ap_password(password);
77 return this;
78 }
79
89 SensESPAppBuilder* set_sk_server(String address, uint16_t port) {
90 app_->set_sk_server_address(address);
91 app_->set_sk_server_port(port);
92 return this;
93 }
102 SensESPAppBuilder* set_hostname(String hostname) override final {
103 app_->set_hostname(hostname);
104 return this;
105 }
110 SensESPAppBuilder* set_admin_user(const char* username,
111 const char* password) {
112 app_->set_admin_user(username, password);
113 return this;
114 }
115
125 std::shared_ptr<SystemStatusLed> system_status_led) {
126 app_->set_system_status_led(system_status_led);
127 return this;
128 }
139 String prefix = kDefaultSystemInfoSensorPrefix) {
140 auto sensor = std::make_shared<SystemHz>();
141 auto vproducer = std::static_pointer_cast<ValueProducer<float>>(sensor);
142 // We need to store the sensor in the app so it doesn't get garbage
143 // collected.
144 app_->system_hz_sensor_ = vproducer;
145 connect_system_info_sensor(vproducer, prefix, "systemHz");
146 return this;
147 }
155 String prefix = kDefaultSystemInfoSensorPrefix) {
156 auto sensor = std::make_shared<FreeMem>();
157 auto vproducer = std::static_pointer_cast<ValueProducer<uint32_t>>(sensor);
158 // We need to store the sensor in the app so it doesn't get garbage
159 // collected.
160 app_->free_mem_sensor_ = vproducer;
161 connect_system_info_sensor(vproducer, prefix, "freeMemory");
162 return this;
163 }
171 String prefix = kDefaultSystemInfoSensorPrefix) {
172 auto sensor = std::make_shared<Uptime>();
173 auto vproducer = std::static_pointer_cast<ValueProducer<float>>(sensor);
174 // We need to store the sensor in the app so it doesn't get garbage
175 // collected.
176 app_->uptime_sensor_ = vproducer;
177 connect_system_info_sensor(vproducer, prefix, "uptime");
178 return this;
179 }
187 String prefix = kDefaultSystemInfoSensorPrefix) {
188 auto sensor = std::make_shared<IPAddrDev>();
189 auto vproducer = std::static_pointer_cast<ValueProducer<String>>(sensor);
190 // We need to store the sensor in the app so it doesn't get garbage
191 // collected.
192 app_->ip_address_sensor_ = vproducer;
193 connect_system_info_sensor(vproducer, prefix, "ipAddress");
194 return this;
195 }
203 String prefix = kDefaultSystemInfoSensorPrefix) {
204 auto sensor = std::make_shared<WiFiSignal>();
205 auto vproducer = std::static_pointer_cast<ValueProducer<int>>(sensor);
206 // We need to store the sensor in the app so it doesn't get garbage
207 // collected.
208 app_->wifi_signal_sensor_ = vproducer;
209 connect_system_info_sensor(vproducer, prefix, "wifiSignalLevel");
210 return this;
211 }
212
219 app_->set_button_pin(pin);
220 return this;
221 }
222
230 String prefix = kDefaultSystemInfoSensorPrefix) {
231 this->enable_system_hz_sensor(prefix);
232 this->enable_free_mem_sensor(prefix);
233 this->enable_uptime_sensor(prefix);
234 this->enable_ip_address_sensor(prefix);
235 this->enable_wifi_signal_sensor(prefix);
236 return this;
237 }
238
245 SensESPAppBuilder* enable_ota(const char* password) {
246 app_->enable_ota(password);
247 return this;
248 }
249
260 [[deprecated("Use set_wifi_access_point instead")]]
262 app_->set_ap_password(password);
263 return this;
264 }
265
267 // create the wifi disconnect watchdog
268 app_->system_status_controller_
269 ->connect_to(new Debounce<SystemStatus>(
270 3 * 60 * 1000 // 180 s = 180000 ms = 3 minutes
271 ))
272 ->connect_to(new LambdaConsumer<SystemStatus>([](SystemStatus input) {
273 ESP_LOGD(__FILENAME__, "Got system status: %d", (int)input);
274 if (input == SystemStatus::kWifiDisconnected ||
275 input == SystemStatus::kWifiNoAP) {
276 ESP_LOGW(__FILENAME__,
277 "Unable to connect to wifi for too long; restarting.");
278 event_loop()->onDelay(1000, []() { ESP.restart(); });
279 }
280 }));
281
282 return this;
283 }
284
292 std::shared_ptr<SensESPApp> get_app() {
294 app_->setup();
295 return app_;
296 }
297};
298
299} // namespace sensesp
300
301#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.
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 * 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:57
Abstract base class for other SensESP*AppBuilder classes.
std::shared_ptr< reactesp::EventLoop > event_loop()
Definition sensesp.cpp:9
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
const char * kDefaultSystemInfoSensorPrefix