diff --git a/esp32_device/.vscode/settings.json b/esp32_device/.vscode/settings.json index 0bf7131..bef44a6 100644 --- a/esp32_device/.vscode/settings.json +++ b/esp32_device/.vscode/settings.json @@ -50,6 +50,9 @@ "stdlib.h": "c", "secure_api.h": "c", "jwt.h": "c", - "sync_response.h": "c" + "sync_response.h": "c", + "gpio.h": "c", + "esp_system.h": "c", + "relays.h": "c" } } diff --git a/esp32_device/main/CMakeLists.txt b/esp32_device/main/CMakeLists.txt index a7b6504..882b510 100755 --- a/esp32_device/main/CMakeLists.txt +++ b/esp32_device/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "sync_response.c" "jwt.c" "secure_api.c" "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" +idf_component_register(SRCS "relays.c" "sync_response.c" "jwt.c" "secure_api.c" "http_client.c" "ethernet.c" "unsecure_api.c" "system.c" "crypto.c" "random.c" "storage.c" "main.c" "dev_name.c" INCLUDE_DIRS ".") diff --git a/esp32_device/main/constants.h b/esp32_device/main/constants.h index eb0f81a..7f8375a 100644 --- a/esp32_device/main/constants.h +++ b/esp32_device/main/constants.h @@ -13,7 +13,7 @@ /** * Device max number of relays */ -#define DEV_MAX_RELAYS 1 +#define DEV_MAX_RELAYS 5 /** * Backend unsecure API URL diff --git a/esp32_device/main/main.c b/esp32_device/main/main.c index d1f89d4..65c867f 100755 --- a/esp32_device/main/main.c +++ b/esp32_device/main/main.c @@ -10,6 +10,7 @@ #include "secure_api.h" #include "ethernet.h" #include "constants.h" +#include "relays.h" static const char *TAG = "main"; @@ -21,7 +22,8 @@ void app_main(void) ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device"); - // TODO : turn off all relays + // Turn off all relays + relays_turn_off_all(); // Initialize storage if (storage_init() == false) @@ -182,7 +184,7 @@ void app_main(void) if (fails > 5) { ESP_LOGE(TAG, "Many failures, will stop all relays..."); - // TODO : turn off all relays + relays_turn_off_all(); } // Restart the card after too much failures @@ -199,9 +201,13 @@ void app_main(void) fails = 0; - // TODO : apply sync sync_response_print(res); + for (size_t i = 0; i < DEV_MAX_RELAYS; i++) + { + relays_set(i, sync_response_is_relay_on(res, i)); + } + sync_response_free(res); system_sleep(SYNC_TIME_INTERVAL); } diff --git a/esp32_device/main/relays.c b/esp32_device/main/relays.c new file mode 100644 index 0000000..4aba4c3 --- /dev/null +++ b/esp32_device/main/relays.c @@ -0,0 +1,28 @@ +#include +#include "esp_log.h" + +#include "relays.h" +#include "constants.h" + +static const char *TAG = "relays"; + +/** + * Device relays GPIO ids + */ +static int DEVICE_GPIO_IDS[5] = {2, 4, 12, 14, 15}; + +void relays_turn_off_all() +{ + ESP_LOGI(TAG, "Turning off all relays..."); + + for (size_t i = 0; i < DEV_MAX_RELAYS; i++) + { + relays_set(i, false); + } +} + +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"); +} \ No newline at end of file diff --git a/esp32_device/main/relays.h b/esp32_device/main/relays.h new file mode 100644 index 0000000..f1a5a50 --- /dev/null +++ b/esp32_device/main/relays.h @@ -0,0 +1,26 @@ +/** + * Relays management + */ + +#pragma once + +#include + +#ifdef __cplusplus +extern "C" +{ +#endif + + /** + * Turn off all relays + */ + void relays_turn_off_all(); + + /** + * Turn on / off a relay + */ + void relays_set(int number, bool is_on); + +#ifdef __cplusplus +} +#endif diff --git a/esp32_device/main/sync_response.c b/esp32_device/main/sync_response.c index 7ba3690..e8cf56e 100644 --- a/esp32_device/main/sync_response.c +++ b/esp32_device/main/sync_response.c @@ -51,6 +51,7 @@ sync_response *sync_response_parse(cJSON *res) void sync_response_print(sync_response *res) { ESP_LOGI(TAG, " === sync response begin === "); + ESP_LOGI(TAG, "# of relays: %d", res->len); for (size_t i = 0; i < res->len; i++) ESP_LOGI(TAG, "Relay[%d]=%s", i, res->relays[i] ? "ON" : "off"); ESP_LOGI(TAG, " === sync response end === "); @@ -60,4 +61,15 @@ void sync_response_free(sync_response *res) { if (res != NULL) free(res); +} + +bool sync_response_is_relay_on(sync_response *res, int relay_number) +{ + if (res->len <= relay_number) + { + ESP_LOGW(TAG, "Requested state of an unconfigured relay (%d). Defaulting to off.", relay_number); + return false; + } + + return res->relays[relay_number]; } \ No newline at end of file diff --git a/esp32_device/main/sync_response.h b/esp32_device/main/sync_response.h index b4a81db..828e074 100644 --- a/esp32_device/main/sync_response.h +++ b/esp32_device/main/sync_response.h @@ -34,6 +34,11 @@ extern "C" */ void sync_response_free(sync_response *res); + /** + * Check the desired status of a relay + */ + bool sync_response_is_relay_on(sync_response *res, int relay_number); + #ifdef __cplusplus } #endif