SensESP 3.0.1
Universal Signal K sensor toolkit ESP32
Loading...
Searching...
No Matches
sensesp_app.h
Go to the documentation of this file.
1#ifndef SENSESP_APP_H_
2#define SENSESP_APP_H_
3
4#ifdef LED_BUILTIN
5#define LED_PIN LED_BUILTIN
6#define ENABLE_LED true
7#else
8#define LED_PIN 0
9#define ENABLE_LED false
10#endif
11
16#include "sensesp/net/ota.h"
28#include "sensesp_base_app.h"
29
30namespace sensesp {
31
32class SensESPApp;
33// I'd rather not have this global variable here but many legacy examples
34// access it. Use SensESPApp::get() instead.
35extern std::shared_ptr<SensESPApp> sensesp_app;
36
42class SensESPApp : public SensESPBaseApp {
43 public:
47 SensESPApp(SensESPApp& other) = delete;
48
52 void operator=(const SensESPApp&) = delete;
53
57 static std::shared_ptr<SensESPApp> get() {
58 if (instance_ == nullptr) {
59 instance_ = std::shared_ptr<SensESPApp>(new SensESPApp());
60 }
61 return std::static_pointer_cast<SensESPApp>(instance_);
62 }
63
64 virtual bool destroy() override {
65 bool outside_users = instance_.use_count() > 2;
66 if (outside_users) {
67 ESP_LOGW(
68 __FILENAME__,
69 "SensESPApp instance has active references and won't be properly "
70 "destroyed.");
71 }
72 instance_ = nullptr;
73 // Also destroy the global pointer
74 sensesp_app = nullptr;
75 return !outside_users;
76 }
77
78 // getters for internal members
79 std::shared_ptr<SKDeltaQueue> get_sk_delta() { return this->sk_delta_queue_; }
80 std::shared_ptr<SystemStatusController> get_system_status_controller() {
81 return this->system_status_controller_;
82 }
83 std::shared_ptr<Networking>& get_networking() { return this->networking_; }
84 std::shared_ptr<SKWSClient> get_ws_client() { return this->ws_client_; }
85
86 protected:
95
96 // setters for all constructor arguments
97
98 const SensESPApp* set_hostname(String hostname) {
99 this->SensESPBaseApp::set_hostname(hostname);
100 return this;
101 }
102 const SensESPApp* set_ssid(String ssid) {
103 this->ssid_ = ssid;
104 return this;
105 }
106 const SensESPApp* set_wifi_password(String wifi_password) {
107 this->wifi_client_password_ = wifi_password;
108 return this;
109 }
110 const SensESPApp* set_ap_ssid(const String& ssid) {
111 this->ap_ssid_ = ssid;
112 return this;
113 }
114 const SensESPApp* set_ap_password(const String& password) {
115 this->ap_password_ = password;
116 return this;
117 }
118 const SensESPApp* set_sk_server_address(String sk_server_address) {
119 this->sk_server_address_ = sk_server_address;
120 return this;
121 }
122 const SensESPApp* set_sk_server_port(uint16_t sk_server_port) {
123 this->sk_server_port_ = sk_server_port;
124 return this;
125 }
127 std::shared_ptr<SystemStatusLed>& system_status_led) {
128 this->system_status_led_ = system_status_led;
129 return this;
130 }
131 const SensESPApp* set_admin_user(const char* username, const char* password) {
132 this->http_server_->set_auth_credentials(username, password, true);
133 return this;
134 }
135 const SensESPApp* enable_ota(const char* password) {
136 ota_password_ = password;
137 return this;
138 }
139 const SensESPApp* set_button_pin(int pin) {
140 button_gpio_pin_ = pin;
141 return this;
142 }
143
151 void setup() {
152 // call the parent setup()
154
156
157 // create the networking object
158 networking_ = std::make_shared<Networking>("/System/WiFi Settings", ssid_,
161
163
164 if (ota_password_ != nullptr) {
165 // create the OTA object
166 ota_ = std::make_shared<OTA>(ota_password_);
167 }
168
169 bool captive_portal_enabled = networking_->is_captive_portal_enabled();
170
171 // create the HTTP server
172 this->http_server_ = std::make_shared<HTTPServer>();
173 this->http_server_->set_captive_portal(captive_portal_enabled);
174
175 // Add the default HTTP server response handlers
180
182
183 // create the SK delta object
184 sk_delta_queue_ = std::make_shared<SKDeltaQueue>();
185
186 // create the websocket client
187 bool const use_mdns = sk_server_address_ == "";
188 this->ws_client_ = std::make_shared<SKWSClient>(
189 "/System/Signal K Settings", sk_delta_queue_, sk_server_address_,
190 sk_server_port_, use_mdns);
191
192 ConfigItem(this->ws_client_);
193
194 // connect the system status controller
195 this->networking_->get_wifi_state_producer()->connect_to(
196 &system_status_controller_->get_wifi_state_consumer());
197 this->ws_client_->connect_to(
198 &system_status_controller_->get_ws_connection_state_consumer());
199
200 // create the MDNS discovery object
201 mdns_discovery_ = std::make_shared<MDNSDiscovery>();
202
203 // create a system status led and connect it
204
205 if (system_status_led_ == nullptr) {
206 system_status_led_ = std::make_shared<SystemStatusLed>(LED_PIN);
207 }
208 this->system_status_controller_->connect_to(
209 system_status_led_->get_system_status_consumer());
210 this->ws_client_->get_delta_tx_count_producer().connect_to(
211 system_status_led_->get_delta_tx_count_consumer());
212
213 // create the button handler
214 if (button_gpio_pin_ != -1) {
215 button_handler_ = std::make_shared<ButtonHandler>(button_gpio_pin_);
216 }
217
218 // connect status page items
220 }
221
222 // Collect metrics for the status page
224 this->hostname_->connect_to(&this->hostname_ui_output_);
225 this->event_loop_->onRepeat(4999, [this]() {
226 wifi_ssid_ui_output_.set(WiFi.SSID());
227 free_memory_ui_output_.set(ESP.getFreeHeap());
228 wifi_rssi_ui_output_.set(WiFi.RSSI());
229
230 // Uptime
231 uptime_ui_output_.set(millis() / 1000);
232
233 // Event loop queue sizes
234 int event_loop_queue_size = event_loop_->getEventQueueSize();
235 int event_loop_timed_queue_size = event_loop_->getTimedEventQueueSize();
236 int event_loop_untimed_queue_size =
237 event_loop_->getUntimedEventQueueSize();
238
239 // Total tick count
240 uint64_t current_tick_count = event_loop_->getTickCount();
241 total_tick_count_ui_output_.set(current_tick_count);
242
243 // Event counts
244 uint64_t current_event_count = event_loop_->getEventCount();
245 uint64_t current_timed_event_count = event_loop_->getTimedEventCount();
246 uint64_t current_untimed_event_count = event_loop_->getUntimedEventCount();
247 event_count_ui_output_.set(current_event_count);
248 timed_event_count_ui_output_.set(current_timed_event_count);
249 untimed_event_count_ui_output_.set(current_untimed_event_count);
250
251 // Ticks and events per second during last interval
252 static uint64_t last_tick_count = 0;
253 static uint64_t last_event_count = 0;
254 static uint64_t last_timed_event_count = 0;
255 static uint64_t last_untimed_event_count = 0;
256 static uint64_t last_millis = 0;
257 uint64_t current_millis = millis();
258 float interval_seconds = (current_millis - last_millis) / 1000.0;
259
260 uint64_t ticks_diff = current_tick_count - last_tick_count;
261 uint64_t events_diff = current_event_count - last_event_count;
262 uint64_t timed_events_diff =
263 current_timed_event_count - last_timed_event_count;
264 uint64_t untimed_events_diff =
265 current_untimed_event_count - last_untimed_event_count;
266
267 // Set outputs
268 event_loop_queue_size_ui_output_.set(event_loop_queue_size);
269 event_loop_timed_queue_ui_output_.set(event_loop_timed_queue_size);
270 event_loop_untimed_queue_ui_output_.set(event_loop_untimed_queue_size);
272 event_loop_->getISREventQueueSize());
273
274 ticks_per_second_ui_output_.set(int(ticks_diff / interval_seconds));
275 events_per_second_ui_output_.set(int(events_diff / interval_seconds));
277 int(timed_events_diff / interval_seconds));
279 int(untimed_events_diff / interval_seconds));
280
281 // Update last values
282 last_tick_count = current_tick_count;
283 last_event_count = current_event_count;
284 last_timed_event_count = current_timed_event_count;
285 last_untimed_event_count = current_untimed_event_count;
286 last_millis = current_millis;
287
288 sk_server_address_ui_output_.set(ws_client_->get_server_address());
289 sk_server_port_ui_output_.set(ws_client_->get_server_port());
290 sk_server_connection_ui_output_.set(ws_client_->get_connection_status());
291 });
292 ws_client_->get_delta_tx_count_producer().connect_to(
294 ws_client_->get_delta_rx_count_producer().connect_to(
296 }
297
298 String ssid_ = "";
301 uint16_t sk_server_port_ = 0;
302 String ap_ssid_ = "";
303 String ap_password_ = "thisisfine";
304 const char* ota_password_ = nullptr;
305
306 std::shared_ptr<MDNSDiscovery> mdns_discovery_;
307 std::shared_ptr<HTTPServer> http_server_;
308
309 std::shared_ptr<SystemStatusLed> system_status_led_;
310 std::shared_ptr<SystemStatusController> system_status_controller_ =
311 std::make_shared<SystemStatusController>();
313 std::shared_ptr<ButtonHandler> button_handler_;
314
315 std::shared_ptr<Networking> networking_;
316
317 std::shared_ptr<OTA> ota_;
318 std::shared_ptr<SKDeltaQueue> sk_delta_queue_;
319 std::shared_ptr<SKWSClient> ws_client_;
320
321 StatusPageItem<int> free_memory_ui_output_{"Free memory (bytes)", 0, "System",
322 1000};
323 StatusPageItem<int> uptime_ui_output_{"Uptime (s)", 0, "System", 1100};
324
325 StatusPageItem<String> hostname_ui_output_{"Hostname", "", "Network", 1200};
327 "MAC Address", WiFi.macAddress(), "Network", 1300};
328 StatusPageItem<String> wifi_ssid_ui_output_{"SSID", "", "Network", 1400};
329
330 StatusPageItem<int8_t> wifi_rssi_ui_output_{"WiFi signal strength (dB)", -128,
331 "Network", 1500};
332
334 "", "Signal K", 1600};
336 "Signal K", 1700};
338 "", "Signal K", 1800};
340 "Signal K", 1900};
342 "Signal K", 2000};
343
345 "Event Loop queue size", 0, "Event Loop Queues", 2100};
347 "Event Loop timed queue size", 0, "Event Loop Queues", 2200};
349 "Event Loop untimed queue size", 0, "Event Loop Queues", 2300};
351 "Event Loop interrupt queue size", 0, "Event Loop Queues", 2400};
352
354 "Total ticks processed", 0, "Event Loop Lifetime", 2500};
356 "Event Loop Lifetime", 2600};
358 "Timed events processed", 0, "Event Loop Lifetime", 2700};
360 "Untimed events processed", 0, "Event Loop Lifetime", 2800};
362 "Ticks per second", 0, "Event Loop Performance", 2900};
364 "Events per second", 0, "Event Loop Performance", 3000};
366 "Timed events per second", 0, "Event Loop Performance", 3100};
368 "Untimed events per second", 0, "Event Loop Performance", 3200};
369
371 "SenseESP version", kSensESPVersion, "Software", 3300};
373 "Build date", __DATE__ " " __TIME__, "Software", 3400};
374
375 // Placeholders for system status sensors in case they are created
376 std::shared_ptr<ValueProducer<float>> system_hz_sensor_;
377 std::shared_ptr<ValueProducer<uint32_t>> free_mem_sensor_;
378 std::shared_ptr<ValueProducer<float>> uptime_sensor_;
379 std::shared_ptr<ValueProducer<String>> ip_address_sensor_;
380 std::shared_ptr<ValueProducer<int>> wifi_signal_sensor_;
381
382 friend class WebServer;
383 friend class SensESPAppBuilder;
384};
385
386} // namespace sensesp
387
388#endif
void set(const T &value) override
A class for quickly configuring a SensESP application object before wiring up your sensors.
StatusPageItem< int > event_loop_interrupt_queue_ui_output_
const SensESPApp * set_sk_server_port(uint16_t sk_server_port)
StatusPageItem< String > sensesp_version_ui_output_
static std::shared_ptr< SensESPApp > get()
Get the singleton instance of the SensESPApp.
Definition sensesp_app.h:57
SensESPApp()
SensESPApp constructor.
Definition sensesp_app.h:94
void connect_status_page_items()
std::shared_ptr< SystemStatusLed > system_status_led_
void operator=(const SensESPApp &)=delete
std::shared_ptr< SKDeltaQueue > sk_delta_queue_
const SensESPApp * set_hostname(String hostname)
Definition sensesp_app.h:98
StatusPageItem< uint64_t > timed_event_count_ui_output_
std::shared_ptr< HTTPServer > http_server_
StatusPageItem< String > wifi_ssid_ui_output_
const SensESPApp * set_sk_server_address(String sk_server_address)
StatusPageItem< float > ticks_per_second_ui_output_
StatusPageItem< int8_t > wifi_rssi_ui_output_
virtual bool destroy() override
Destroy the SensESPBaseApp instance.
Definition sensesp_app.h:64
void setup()
Perform initialization of SensESPApp once builder configuration is done.
std::shared_ptr< ValueProducer< String > > ip_address_sensor_
StatusPageItem< uint64_t > untimed_event_count_ui_output_
std::shared_ptr< ValueProducer< float > > uptime_sensor_
StatusPageItem< String > hostname_ui_output_
const SensESPApp * set_wifi_password(String wifi_password)
StatusPageItem< uint16_t > sk_server_port_ui_output_
StatusPageItem< String > sk_server_address_ui_output_
StatusPageItem< int > event_loop_timed_queue_ui_output_
std::shared_ptr< ValueProducer< uint32_t > > free_mem_sensor_
StatusPageItem< String > mac_address_ui_output_
std::shared_ptr< SKWSClient > get_ws_client()
Definition sensesp_app.h:84
const char * ota_password_
StatusPageItem< uint64_t > total_tick_count_ui_output_
StatusPageItem< int > uptime_ui_output_
std::shared_ptr< ValueProducer< int > > wifi_signal_sensor_
StatusPageItem< int > delta_rx_count_ui_output_
StatusPageItem< int > event_loop_untimed_queue_ui_output_
StatusPageItem< int > free_memory_ui_output_
StatusPageItem< float > timed_events_per_second_ui_output_
StatusPageItem< int > event_loop_queue_size_ui_output_
const SensESPApp * enable_ota(const char *password)
const SensESPApp * set_ap_password(const String &password)
const SensESPApp * set_admin_user(const char *username, const char *password)
std::shared_ptr< Networking > & get_networking()
Definition sensesp_app.h:83
friend class WebServer
StatusPageItem< uint64_t > event_count_ui_output_
std::shared_ptr< SystemStatusController > get_system_status_controller()
Definition sensesp_app.h:80
std::shared_ptr< SKWSClient > ws_client_
StatusPageItem< String > build_info_ui_output_
StatusPageItem< float > untimed_events_per_second_ui_output_
const SensESPApp * set_system_status_led(std::shared_ptr< SystemStatusLed > &system_status_led)
StatusPageItem< String > sk_server_connection_ui_output_
std::shared_ptr< SystemStatusController > system_status_controller_
std::shared_ptr< ButtonHandler > button_handler_
std::shared_ptr< MDNSDiscovery > mdns_discovery_
const SensESPApp * set_ap_ssid(const String &ssid)
const SensESPApp * set_ssid(String ssid)
StatusPageItem< float > events_per_second_ui_output_
std::shared_ptr< OTA > ota_
SensESPApp(SensESPApp &other)=delete
std::shared_ptr< SKDeltaQueue > get_sk_delta()
Definition sensesp_app.h:79
std::shared_ptr< Networking > networking_
StatusPageItem< int > delta_tx_count_ui_output_
std::shared_ptr< ValueProducer< float > > system_hz_sensor_
const SensESPApp * set_button_pin(int pin)
The base class for SensESP applications.
static String get_hostname()
Get the current hostname.
static std::shared_ptr< SensESPBaseApp > instance_
std::shared_ptr< reactesp::EventLoop > event_loop_
const SensESPBaseApp * set_hostname(String hostname)
std::shared_ptr< PersistingObservableValue< String > > hostname_
virtual void setup()
Perform initialization of SensESPBaseApp once builder configuration is done.
Item that renders its own value on the web UI status page.
void add_static_file_handlers(std::shared_ptr< HTTPServer > server)
Provide handlers for static web content.
void add_config_handlers(std::shared_ptr< HTTPServer > &server)
Handle HTTP requests to /config.
const char *const kSensESPVersion
std::shared_ptr< SensESPApp > sensesp_app
void add_app_http_command_handlers(std::shared_ptr< HTTPServer > &server)
void add_base_app_http_command_handlers(std::shared_ptr< HTTPServer > &server)
std::shared_ptr< ConfigItemT< T > > ConfigItem(std::shared_ptr< T >)
Register a ConfigItemT with the ConfigItemBase.
#define LED_PIN
Definition sensesp_app.h:8
#define SENSESP_BUTTON_PIN