Ready to implement GPIO logic to manage relays

This commit is contained in:
Pierre HUBERT 2024-09-28 20:02:34 +02:00
parent e574bed96f
commit 274b7089d1
8 changed files with 86 additions and 6 deletions

View File

@ -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"
}
}

View File

@ -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 ".")

View File

@ -13,7 +13,7 @@
/**
* Device max number of relays
*/
#define DEV_MAX_RELAYS 1
#define DEV_MAX_RELAYS 5
/**
* Backend unsecure API URL

View File

@ -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);
}

View File

@ -0,0 +1,28 @@
#include <driver/gpio.h>
#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");
}

View File

@ -0,0 +1,26 @@
/**
* Relays management
*/
#pragma once
#include <stddef.h>
#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

View File

@ -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 === ");
@ -61,3 +62,14 @@ 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];
}

View File

@ -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