Skip to content

Commit

Permalink
Firmware: Extract wifi and ip handler for better debugging, add shutd…
Browse files Browse the repository at this point in the history
…own button
  • Loading branch information
Dennis960 committed Apr 16, 2024
1 parent b4c4684 commit 33fe45e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
12 changes: 10 additions & 2 deletions ESPlant-Firmware/main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,20 @@ void configuration_mode(bool isConfigured)
{
sensors_playStartupSound();
plantfi_initWifiApSta();
bool userConnectedToAp = false;
uint64_t start_time = esp_timer_get_time();
uint64_t current_time = start_time;
int32_t timeoutMs = DEFAULT_CONFIGURATION_MODE_TIMEOUT_MS;
plantstore_getConfigurationModeTimeoutMs(&timeoutMs);
ESP_LOGI("MODE", "Starting configuration mode%s", isConfigured ? " (sensor is configured)" : " (no config)");
bool userConnectedToAp = false;
plantfi_configureAp("Blumy", "", 4, &userConnectedToAp);

ESP_LOGI("MODE", "Starting webserver");
httpd_handle_t webserver = webserver = start_webserver();
plantfi_configureStaFromPlantstore();

bool wasBootButtonPressed = false;
sensors_attach_boot_button_interrupt(&wasBootButtonPressed);
while (1)
{
if (isConfigured && !userConnectedToAp)
Expand All @@ -98,7 +101,12 @@ void configuration_mode(bool isConfigured)
break;
}
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
if (wasBootButtonPressed)
{
ESP_LOGI("MODE", "Boot button pressed, going to sleep");
break;
}
vTaskDelay(10 / portTICK_PERIOD_MS); // Reset watchdog
}
stop_webserver(webserver);
sensors_playShutdownSound();
Expand Down
3 changes: 3 additions & 0 deletions ESPlant-Firmware/main/peripherals/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#define MOISTURE_SQUARE_WAVE_SIGNAL GPIO_NUM_7 // LP
#define BUZZER GPIO_NUM_21

// BOOT BUTTON
#define BOOT_BUTTON GPIO_NUM_9

// LEDC CHANNELS
#define BUZZER_CHANNEL LEDC_CHANNEL_0
#define LED_RED_CHANNEL LEDC_CHANNEL_1
Expand Down
19 changes: 19 additions & 0 deletions ESPlant-Firmware/main/peripherals/sensors.c
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,23 @@ void sensors_playShutdownSound()
sensors_playToneSync(784, tick);
vTaskDelay(tick / portTICK_PERIOD_MS);
sensors_playToneSync(587, tick);
}

void boot_button_isr(void *arg)
{
bool *wasBootButtonPressed = (bool *)arg;
*wasBootButtonPressed = true;
}

void sensors_attach_boot_button_interrupt(bool *wasBootButtonPressed)
{
gpio_config_t io_conf;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.pin_bit_mask = 1ULL << BOOT_BUTTON;
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 1;
io_conf.pull_down_en = 0;
gpio_config(&io_conf);
gpio_install_isr_service(0);
gpio_isr_handler_add(BOOT_BUTTON, boot_button_isr, wasBootButtonPressed);
}
3 changes: 2 additions & 1 deletion ESPlant-Firmware/main/peripherals/sensors.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ void sensors_initSensors();
void sensors_deinitSensors();
void sensors_full_read(sensors_full_data_t *data);
void sensors_playStartupSound();
void sensors_playShutdownSound();
void sensors_playShutdownSound();
void sensors_attach_boot_button_interrupt(bool *wasBootButtonPressed);
56 changes: 40 additions & 16 deletions ESPlant-Firmware/main/plantfi.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ char plantfi_password[DEFAULT_PASSWORD_MAX_LENGTH];
bool _credentialsChanged = false;
bool *_userConnectedToAp = NULL;

void plantfi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
void plantfi_wifi_event_handler(int32_t event_id)
{
ESP_LOGI(PLANTFI_TAG, "Event dispatched from event loop base=%s, event_id=%ld", event_base, event_id);
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED)
if (event_id == WIFI_EVENT_STA_DISCONNECTED)
{
if (plantfi_retry_num < plantfi_max_retry)
{
Expand All @@ -51,7 +49,7 @@ void plantfi_event_handler(void *arg, esp_event_base_t event_base,
}
ESP_LOGI(PLANTFI_TAG, "connect to the AP fail");
}
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED)
else if (event_id == WIFI_EVENT_STA_CONNECTED)
{
ESP_LOGI(PLANTFI_TAG, "connect to the AP success");
if (_credentialsChanged)
Expand All @@ -61,31 +59,57 @@ void plantfi_event_handler(void *arg, esp_event_base_t event_base,
}
plantfi_sta_status = PLANTFI_STA_STATUS_CONNECTED;
}
else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP)
else if (event_id == WIFI_EVENT_AP_STACONNECTED)
{
if (_userConnectedToAp != NULL)
{
*_userConnectedToAp = true;
}
ESP_LOGI(PLANTFI_TAG, "User connected to AP");
}
else
{
ESP_LOGI(PLANTFI_TAG, "No handler for event_id %ld", event_id);
}
}

void plantfi_ip_event_handler(int32_t event_id, void *event_data)
{
if (event_id == IP_EVENT_STA_GOT_IP)
{
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(PLANTFI_TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
plantfi_retry_num = 0;
xEventGroupSetBits(plantfi_sta_event_group, PLANTFI_CONNECTED_BIT);
}
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED)
else
{
if (_userConnectedToAp != NULL)
{
*_userConnectedToAp = true;
}
ESP_LOGI(PLANTFI_TAG, "User connected to AP");
ESP_LOGI(PLANTFI_TAG, "No handler for event_id %ld", event_id);
}
}

void plantfi_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data)
{
ESP_LOGI(PLANTFI_TAG, "Event dispatched from event loop base=%s, event_id=%ld", event_base, event_id);
if (event_base == WIFI_EVENT)
{
plantfi_wifi_event_handler(event_id);
}
else if (event_base == IP_EVENT)
{
plantfi_ip_event_handler(event_id, event_data);
}
}

void plantfi_initWifi(bool staOnly)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &plantfi_event_handler, &instance_any_id)); // TODO unregister event handlers on deinit
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &plantfi_event_handler, &instance_got_ip));
esp_event_handler_instance_t instance_any_wifi_event;
esp_event_handler_instance_t instance_any_ip_event;
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &plantfi_event_handler, &instance_any_wifi_event)); // TODO unregister event handlers on deinit
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &plantfi_event_handler, &instance_any_ip_event));

esp_netif_create_default_wifi_sta();
if (!staOnly)
Expand Down

0 comments on commit 33fe45e

Please sign in to comment.