ESP-LiveControl  1.99.1
HTTP server, AJAX API backend and Vue.js web application implementing self-contained, zero-install WiFi remote control of hardware modules attached to the Espressif ESP32 SoC
app_config.hpp
Go to the documentation of this file.
1 
9 #ifndef APP_CONFIG_HPP__
10 #define APP_CONFIG_HPP__
11 
12 #include "freertos/FreeRTOS.h"
13 #include "driver/mcpwm.h"
14 #include "driver/gpio.h"
15 #include "driver/ledc.h"
16 #include "driver/adc.h"
17 
18 #include "IPAddress.h"
19 #include "ArduinoJson.h"
20 
21 
22 struct SerialConfig {
23  static constexpr auto serial_baudrate = 115200ul;
24 };
25 
26 
34 struct NetworkConfig {
36  static constexpr size_t ssid_maxlen = 32 + 1;
37  static constexpr size_t psk_maxlen = 63 + 1;
38  static constexpr size_t hostname_maxlen = 32 + 1;
39 
40  // For NetworkConfigurator class
41  static constexpr const char* get_wifi_config_endpoint = "/get_wifi_config";
42  static constexpr const char* set_wifi_config_endpoint = "/set_wifi_config";
43  static constexpr uint16_t http_tcp_port = 80;
44 
45  // Maximum number of device reboots when multiple reconnections have failed
46  static constexpr uint32_t max_reboots = 4;
47  // Maximum number of connection attempts for configured acces point in station mode
48  static constexpr uint32_t max_reconnections = 3;
49  static constexpr uint32_t reconnection_timeout_ms = 3000;
50  static constexpr uint32_t dns_ttl = 3000;
51 
52  // Domain suffix for DNS name ==> http://eal.lan
53  static constexpr const char* dns_tld = ".lan";
54 
56 
57  // Run initially in access point mode when true
58  bool ap_mode_active = true;
59  // Auto-configure IP4 address in station mode when set to true
60  bool sta_use_dhcp = true;
61 
62  // Activate DNS and/or MDNS service
63  bool dns_active = true;
64  bool mdns_active = false;
65 
66  char hostname[hostname_maxlen] = "eal";
67 
68  char ssid[ssid_maxlen] = "esp_ajax_lab";
69  // Default value to be overridden with custom value on NVS.
70  char psk[psk_maxlen] = "123FOO456";
71 
72  IPAddress ip4_addr = {192, 168, 4, 1};
73  IPAddress ip4_gw = {192, 168, 4, 1};
74  IPAddress ip4_mask = {255, 255, 0, 0};
75 };
76 
77 
79 {
80  // When set to yes, mount SPIFFS filesystem and serve static content
81  // from files contained in data/www subfolder at the "/static/" URL route.
82  bool serve_static_from_spiffs = true;
83  // Name of the SPIFFS subfolder storing the static files. Default is "www"
84  const char* spiffs_static_files_folder = "www";
85  // The name of the URL route for static file content. Default is "/".
86  const char* static_route = "/";
87  // Route for which all sub-routes should be served the same index.html file
88  const char* app_route = "/app";
89  // Main page / main application HTML file (public location not SPIFFS path).
90  // Served when application route or sub-routes are requested.
91  const char* index_html_file = "/index.html";
92  // Add to static file responses a CacheControl header.
93  // Default is "public, max-age=86400" which instructs remote clients to
94  // normally not request again for the specified time in seconds (one day).
95  const char* cache_control = "public, max-age=86400";
96 
97  // Activate template processing when defined.
98  // This should only make sense when not using AJAX..
99  bool template_processing_activated = false;
100 
101  // Common API endpoint for AJAX GET requests registered via register_ajax_cb()
102  const char* api_endpoint = "/cmd";
103  // For AJAX, reply with an plain string, default is empty string.
104  // When not using AJAX, reply with content from string "api_return_html" as
105  // defined in separate header http_content.hpp
106  bool api_is_ajax = true;
107  const char* ajax_return_text = "OK";
108 
109  // Activate Server-Sent-Event source
110  bool use_sse = true;
111  // Default URL for the SSE endpoint is "/events"
112  const char* sse_endpoint = "/events";
113 
114  // When set to true, reboot the system on request or after updates
115  bool reboot_enabled = false;
116 
117  // Activate HTTP Basic Authentication, set to true when user/password is given
118  bool http_auth_activated = false;
119  // HTTP Basic Authentication username and password
120  const char* http_user = "";
121  const char* http_pass = "";
122 
123  // Errors
124  const char* error_404_html = "Error 404, file not found!";
125 };
126 
127 
131 {
132  // Objects are constexpr, so members can be used as template parameters etc.
133  constexpr AppConstants(){};
134 
136  // App state serialization using the ArduionJSON module takes a lot of it
137  uint32_t app_event_task_stack_size = 4096;
138  // Arduino loop task has 1; async_tcp task has 3.
139  // Assuming 2 is a good choice in-between..
140  UBaseType_t app_event_task_priority = 2;
141  // PRO_CPU_NUM == 1; APP_CPU_NUM == 0 on ESP32
142  BaseType_t app_event_task_core_id = APP_CPU_NUM;
143  // Fast timer for ADC conversion triggering etc
144  uint32_t timer_fast_interval_ms = 20;
148  uint32_t timer_slow_interval_ms = 750;
151  const char *settings_filename = "/www/settings.json";
152 
154  // MCPWM unit can be [0,1]
155  mcpwm_unit_t mcpwm_num = MCPWM_UNIT_0;
156  // GPIO config for PWM output
157  gpio_num_t gpio_pwm0a_out = GPIO_NUM_27; // PWM0A := LEAD leg, Low Side
158  gpio_num_t gpio_pwm0b_out = GPIO_NUM_26; // PWM0B := LEAD leg, High Side
159  gpio_num_t gpio_pwm1a_out = GPIO_NUM_25; // PWM1A := LAG leg, Low Side
160  gpio_num_t gpio_pwm1b_out = GPIO_NUM_33; // PWM1B := LAG leg, High Side
161  // Shutdown/fault input for PWM outputs
162  gpio_num_t gpio_fault_shutdown = GPIO_NUM_4;
163  // Active low / active high selection for fault input pin
164  mcpwm_fault_input_level_t fault_pin_active_level = MCPWM_LOW_LEVEL_TGR;
165  // Define here if the output pins shall be forced low or high
166  // or high-impedance when a fault condition is triggered.
167  // PWMxA and PWMxB have the same type of action, see declaration in mcpwm.h
168  mcpwm_action_on_pwmxa_t disable_action_lag_leg = MCPWM_FORCE_MCPWMXA_LOW;
169  // Lead leg might have a different configuration, e.g. stay at last output level
170  mcpwm_action_on_pwmxa_t disable_action_lead_leg = MCPWM_FORCE_MCPWMXA_LOW;
171 
172  // Default runtime frequency setpoint limits
173  float frequency_min = 50e3f;
174  float frequency_max = 300e3f;
175  // Initial frequency setpoint
176  float init_frequency = 100e3f;
177  // Initial phase-shift setpoint
178  float init_ps_duty = 0.45f;
179  // Initial leading leg dead-time value
180  float init_lead_dt = 125e-9f;
181  // Initial lagging leg dead-time value
182  float init_lag_dt = 125e-9f;
183  // Initial output state should be "false" representing "off"
184  bool init_power_pwm_active = false;
185 };
186 
187 
191 {
192  // Objects are constexpr, so members can be used as template parameters etc.
193  constexpr AuxHwDrvConfig(){};
194 
195  // Threshold values for activating/deactivating the fan in automatic mode
196  float temp_1_fan_threshold_hi = 45.0f;
197  float temp_1_fan_threshold_lo = 40.0f;
198  float temp_2_fan_threshold_hi = 45.0f;
199  float temp_2_fan_threshold_lo = 40.0f;
200  // Analog inputs config //
202  adc1_channel_t temp_ch_1 = ADC1_CHANNEL_0; // Sensor VP
204  adc1_channel_t temp_ch_2 = ADC1_CHANNEL_3; // Sensor VN
205  // Digital output GPIOs //
206  gpio_num_t gpio_fan = GPIO_NUM_2;
207  gpio_num_t gpio_overcurrent_reset = GPIO_NUM_16;
208  gpio_num_t gpio_relay_ref = GPIO_NUM_18;
209  gpio_num_t gpio_relay_dut = GPIO_NUM_19;
210  gpio_num_t gpio_delta_sigma_out = GPIO_NUM_21;
211  gpio_num_t gpio_drv_supply_en = GPIO_NUM_23;
212  gpio_num_t gpio_drv_disable = GPIO_NUM_32;
213  // Handled by LEDC PWM API
214  gpio_num_t gpio_curr_limit_reference_pwm = GPIO_NUM_17;
215  // GPIO config, inputs //
216  gpio_num_t gpio_delta_sigma_in = GPIO_NUM_22;
217  // Structures for GPIO and PWM API //
218  gpio_config_t aux_periph_gpio_output_config {
219  .pin_bit_mask = ((1ULL<<gpio_fan)
220  |(1ULL<<gpio_overcurrent_reset)
221  |(1ULL<<gpio_relay_ref)
222  |(1ULL<<gpio_relay_dut)
223  |(1ULL<<gpio_delta_sigma_out)
224  |(1ULL<<gpio_drv_supply_en)
225  |(1ULL<<gpio_drv_disable)
226  ),
227  .mode = GPIO_MODE_OUTPUT,
228  .pull_up_en = GPIO_PULLUP_DISABLE,
229  .pull_down_en = GPIO_PULLDOWN_DISABLE,
230  .intr_type = GPIO_INTR_DISABLE
231  };
232  gpio_config_t aux_periph_gpio_input_config {
233  .pin_bit_mask = ((1ULL<<gpio_delta_sigma_in)
234  ),
235  .mode = GPIO_MODE_INPUT,
236  .pull_up_en = GPIO_PULLUP_DISABLE,
237  .pull_down_en = GPIO_PULLDOWN_ENABLE,
238  .intr_type = GPIO_INTR_DISABLE
239  };
240  // PWM outputs config
241  // Maximum PWM frequency for given resolution in N bits is:
242  // freq_hz = 80 MHz / 2^N
243  ledc_timer_config_t pwm_timer_config {
244  .speed_mode = LEDC_HIGH_SPEED_MODE,
245  .duty_resolution = LEDC_TIMER_12_BIT,
246  .timer_num = LEDC_TIMER_0,
247  .freq_hz = 19500,
248  .clk_cfg = LEDC_USE_APB_CLK // 80 MHz
249  };
250  ledc_channel_config_t curr_lim_pwm_ch_config {
251  .gpio_num = gpio_curr_limit_reference_pwm,
252  .speed_mode = LEDC_HIGH_SPEED_MODE,
253  .channel = LEDC_CHANNEL_0,
254  .intr_type = LEDC_INTR_DISABLE,
255  .timer_sel = LEDC_TIMER_0,
256  .duty = 0,
257  .hpoint = 0
258  };
259  // Same PWM timer is used for isolated external delta_sigma hardware pin
260  ledc_channel_config_t delta_sigma_out_pwm_ch_config {
261  .gpio_num = gpio_delta_sigma_out,
262  .speed_mode = LEDC_HIGH_SPEED_MODE,
263  .channel = LEDC_CHANNEL_0,
264  .intr_type = LEDC_INTR_DISABLE,
265  .timer_sel = LEDC_TIMER_0,
266  .duty = 0,
267  .hpoint = 0
268  };
269  // Overcurrent reset output pulse length. Must be at least equal to
270  // FreeRTOS scheduler tick period.
271  uint32_t oc_reset_pulse_length_ms = 20;
272  // Calibration values for current limit PWM
273  float curr_limit_pwm_scale = 1.0f/100.0f * (float)(
274  1 << pwm_timer_config.duty_resolution);
275  uint32_t curr_limit_pwm_offset = 0;
277 };
278 
279 
280 
281 #endif
Definition: app_config.hpp:79
Constant / compile-time config values go here!
Definition: app_config.hpp:131
uint32_t timer_slow_interval_ms
In addition to event-based async state update telegrams, we also send cyclic updates to the HTTP clie...
Definition: app_config.hpp:148
const char * settings_filename
Filename for persistent storage of runtime settings.
Definition: app_config.hpp:151
Hardware configuration for AuxHwDrv.
Definition: app_config.hpp:191
adc1_channel_t temp_ch_2
ADC channel for second temperature sensor.
Definition: app_config.hpp:204
adc1_channel_t temp_ch_1
ADC channel for first temperature sensor.
Definition: app_config.hpp:202
WiFi network configuration structure with default values.
Definition: app_config.hpp:34
Definition: app_config.hpp:22