From 4d07d83904bd246a21dd60520590691b40da0d7e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 28 Sep 2024 20:27:12 +0200 Subject: [PATCH] Start to work on GPIOs --- esp32_device/main/constants.h | 5 ----- esp32_device/main/main.c | 3 ++- esp32_device/main/relays.c | 39 ++++++++++++++++++++++++++++++++-- esp32_device/main/relays.h | 10 +++++++++ esp32_device/main/secure_api.c | 3 ++- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/esp32_device/main/constants.h b/esp32_device/main/constants.h index 7f8375a..1f730eb 100644 --- a/esp32_device/main/constants.h +++ b/esp32_device/main/constants.h @@ -10,11 +10,6 @@ */ #define DEV_VERSION "0.0.1" -/** - * Device max number of relays - */ -#define DEV_MAX_RELAYS 5 - /** * Backend unsecure API URL */ diff --git a/esp32_device/main/main.c b/esp32_device/main/main.c index 65c867f..f03a43d 100755 --- a/esp32_device/main/main.c +++ b/esp32_device/main/main.c @@ -23,6 +23,7 @@ void app_main(void) ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device"); // Turn off all relays + relays_setup(); relays_turn_off_all(); // Initialize storage @@ -203,7 +204,7 @@ void app_main(void) sync_response_print(res); - for (size_t i = 0; i < DEV_MAX_RELAYS; i++) + for (size_t i = 0; i < relays_count(); i++) { relays_set(i, sync_response_is_relay_on(res, i)); } diff --git a/esp32_device/main/relays.c b/esp32_device/main/relays.c index 4aba4c3..52d8855 100644 --- a/esp32_device/main/relays.c +++ b/esp32_device/main/relays.c @@ -9,13 +9,42 @@ static const char *TAG = "relays"; /** * Device relays GPIO ids */ -static int DEVICE_GPIO_IDS[5] = {2, 4, 12, 14, 15}; +static int DEVICE_GPIO_IDS[3] = {4, 14, 15}; + +int relays_count() +{ + return sizeof(DEVICE_GPIO_IDS) / sizeof(int); +} + +void relays_setup() +{ + int pin_bit_mask = 0; + for (size_t i = 0; i < relays_count(); i++) + { + pin_bit_mask |= 1ULL << DEVICE_GPIO_IDS[i]; + } + + // zero-initialize the config structure. + gpio_config_t io_conf = {}; + // disable interrupt + io_conf.intr_type = GPIO_INTR_DISABLE; + // set as output mode + io_conf.mode = GPIO_MODE_OUTPUT; + // bit mask of the pins that you want to set,e.g.GPIO18/19 + io_conf.pin_bit_mask = pin_bit_mask; + // disable pull-down mode + io_conf.pull_down_en = 0; + // disable pull-up mode + io_conf.pull_up_en = 0; + // configure GPIO with the given settings + gpio_config(&io_conf); +} void relays_turn_off_all() { ESP_LOGI(TAG, "Turning off all relays..."); - for (size_t i = 0; i < DEV_MAX_RELAYS; i++) + for (size_t i = 0; i < relays_count(); i++) { relays_set(i, false); } @@ -25,4 +54,10 @@ void relays_set(int number, bool is_on) { size_t gpio_id = DEVICE_GPIO_IDS[number]; ESP_LOGI(TAG, "Set relay %d (gpio %d) to %s", number, gpio_id, is_on ? "on" : "off"); + + int res = gpio_set_level(gpio_id, is_on ? 0 : 1); + if (res != ESP_OK) + { + ESP_LOGE(TAG, "Failed to toggle GPIO %d : %d", gpio_id, res); + } } \ No newline at end of file diff --git a/esp32_device/main/relays.h b/esp32_device/main/relays.h index f1a5a50..8f439ec 100644 --- a/esp32_device/main/relays.h +++ b/esp32_device/main/relays.h @@ -11,6 +11,16 @@ extern "C" { #endif + /** + * Get the max number of relays + */ + int relays_count(); + + /** + * Configure the relays + */ + void relays_setup(); + /** * Turn off all relays */ diff --git a/esp32_device/main/secure_api.c b/esp32_device/main/secure_api.c index 14efb5e..63e6308 100644 --- a/esp32_device/main/secure_api.c +++ b/esp32_device/main/secure_api.c @@ -11,6 +11,7 @@ #include "storage.h" #include "http_client.h" #include "jwt.h" +#include "relays.h" #include "esp_log.h" @@ -122,7 +123,7 @@ static cJSON *genDevInfo() return NULL; cJSON_AddStringToObject(json, "reference", DEV_REFERENCE); cJSON_AddStringToObject(json, "version", DEV_VERSION); - cJSON_AddNumberToObject(json, "max_relays", DEV_MAX_RELAYS); + cJSON_AddNumberToObject(json, "max_relays", relays_count()); return json; }