Start to work on GPIOs

This commit is contained in:
Pierre HUBERT 2024-09-28 20:27:12 +02:00
parent 274b7089d1
commit 4d07d83904
5 changed files with 51 additions and 9 deletions

View File

@ -10,11 +10,6 @@
*/ */
#define DEV_VERSION "0.0.1" #define DEV_VERSION "0.0.1"
/**
* Device max number of relays
*/
#define DEV_MAX_RELAYS 5
/** /**
* Backend unsecure API URL * Backend unsecure API URL
*/ */

View File

@ -23,6 +23,7 @@ void app_main(void)
ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device"); ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device");
// Turn off all relays // Turn off all relays
relays_setup();
relays_turn_off_all(); relays_turn_off_all();
// Initialize storage // Initialize storage
@ -203,7 +204,7 @@ void app_main(void)
sync_response_print(res); 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)); relays_set(i, sync_response_is_relay_on(res, i));
} }

View File

@ -9,13 +9,42 @@ static const char *TAG = "relays";
/** /**
* Device relays GPIO ids * 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() void relays_turn_off_all()
{ {
ESP_LOGI(TAG, "Turning off all relays..."); 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); relays_set(i, false);
} }
@ -25,4 +54,10 @@ void relays_set(int number, bool is_on)
{ {
size_t gpio_id = DEVICE_GPIO_IDS[number]; 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"); 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);
}
} }

View File

@ -11,6 +11,16 @@ extern "C"
{ {
#endif #endif
/**
* Get the max number of relays
*/
int relays_count();
/**
* Configure the relays
*/
void relays_setup();
/** /**
* Turn off all relays * Turn off all relays
*/ */

View File

@ -11,6 +11,7 @@
#include "storage.h" #include "storage.h"
#include "http_client.h" #include "http_client.h"
#include "jwt.h" #include "jwt.h"
#include "relays.h"
#include "esp_log.h" #include "esp_log.h"
@ -122,7 +123,7 @@ static cJSON *genDevInfo()
return NULL; return NULL;
cJSON_AddStringToObject(json, "reference", DEV_REFERENCE); cJSON_AddStringToObject(json, "reference", DEV_REFERENCE);
cJSON_AddStringToObject(json, "version", DEV_VERSION); cJSON_AddStringToObject(json, "version", DEV_VERSION);
cJSON_AddNumberToObject(json, "max_relays", DEV_MAX_RELAYS); cJSON_AddNumberToObject(json, "max_relays", relays_count());
return json; return json;
} }