Start to work on GPIOs
This commit is contained in:
parent
274b7089d1
commit
4d07d83904
@ -10,11 +10,6 @@
|
||||
*/
|
||||
#define DEV_VERSION "0.0.1"
|
||||
|
||||
/**
|
||||
* Device max number of relays
|
||||
*/
|
||||
#define DEV_MAX_RELAYS 5
|
||||
|
||||
/**
|
||||
* Backend unsecure API URL
|
||||
*/
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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
|
||||
*/
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user