Ready to implement GPIO logic to manage relays
This commit is contained in:
		
							
								
								
									
										5
									
								
								esp32_device/.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								esp32_device/.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@@ -50,6 +50,9 @@
 | 
				
			|||||||
    "stdlib.h": "c",
 | 
					    "stdlib.h": "c",
 | 
				
			||||||
    "secure_api.h": "c",
 | 
					    "secure_api.h": "c",
 | 
				
			||||||
    "jwt.h": "c",
 | 
					    "jwt.h": "c",
 | 
				
			||||||
    "sync_response.h": "c"
 | 
					    "sync_response.h": "c",
 | 
				
			||||||
 | 
					    "gpio.h": "c",
 | 
				
			||||||
 | 
					    "esp_system.h": "c",
 | 
				
			||||||
 | 
					    "relays.h": "c"
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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"
 | 
					                        "dev_name.c"
 | 
				
			||||||
                    INCLUDE_DIRS ".")
 | 
					                    INCLUDE_DIRS ".")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,7 +13,7 @@
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Device max number of relays
 | 
					 * Device max number of relays
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
#define DEV_MAX_RELAYS 1
 | 
					#define DEV_MAX_RELAYS 5
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Backend unsecure API URL
 | 
					 * Backend unsecure API URL
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,6 +10,7 @@
 | 
				
			|||||||
#include "secure_api.h"
 | 
					#include "secure_api.h"
 | 
				
			||||||
#include "ethernet.h"
 | 
					#include "ethernet.h"
 | 
				
			||||||
#include "constants.h"
 | 
					#include "constants.h"
 | 
				
			||||||
 | 
					#include "relays.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const char *TAG = "main";
 | 
					static const char *TAG = "main";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -21,7 +22,8 @@ void app_main(void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device");
 | 
					    ESP_LOGI(TAG, "SolarEnergy WT32-ETH01 device");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO : turn off all relays
 | 
					    // Turn off all relays
 | 
				
			||||||
 | 
					    relays_turn_off_all();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Initialize storage
 | 
					    // Initialize storage
 | 
				
			||||||
    if (storage_init() == false)
 | 
					    if (storage_init() == false)
 | 
				
			||||||
@@ -182,7 +184,7 @@ void app_main(void)
 | 
				
			|||||||
            if (fails > 5)
 | 
					            if (fails > 5)
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                ESP_LOGE(TAG, "Many failures, will stop all relays...");
 | 
					                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
 | 
					            // Restart the card after too much failures
 | 
				
			||||||
@@ -199,9 +201,13 @@ void app_main(void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        fails = 0;
 | 
					        fails = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // TODO : apply sync
 | 
					 | 
				
			||||||
        sync_response_print(res);
 | 
					        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);
 | 
					        sync_response_free(res);
 | 
				
			||||||
        system_sleep(SYNC_TIME_INTERVAL);
 | 
					        system_sleep(SYNC_TIME_INTERVAL);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										28
									
								
								esp32_device/main/relays.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								esp32_device/main/relays.c
									
									
									
									
									
										Normal 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");
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										26
									
								
								esp32_device/main/relays.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								esp32_device/main/relays.h
									
									
									
									
									
										Normal 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
 | 
				
			||||||
@@ -51,6 +51,7 @@ sync_response *sync_response_parse(cJSON *res)
 | 
				
			|||||||
void sync_response_print(sync_response *res)
 | 
					void sync_response_print(sync_response *res)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    ESP_LOGI(TAG, " === sync response begin === ");
 | 
					    ESP_LOGI(TAG, " === sync response begin === ");
 | 
				
			||||||
 | 
					    ESP_LOGI(TAG, "# of relays: %d", res->len);
 | 
				
			||||||
    for (size_t i = 0; i < res->len; i++)
 | 
					    for (size_t i = 0; i < res->len; i++)
 | 
				
			||||||
        ESP_LOGI(TAG, "Relay[%d]=%s", i, res->relays[i] ? "ON" : "off");
 | 
					        ESP_LOGI(TAG, "Relay[%d]=%s", i, res->relays[i] ? "ON" : "off");
 | 
				
			||||||
    ESP_LOGI(TAG, " === sync response end === ");
 | 
					    ESP_LOGI(TAG, " === sync response end === ");
 | 
				
			||||||
@@ -61,3 +62,14 @@ void sync_response_free(sync_response *res)
 | 
				
			|||||||
    if (res != NULL)
 | 
					    if (res != NULL)
 | 
				
			||||||
        free(res);
 | 
					        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];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -34,6 +34,11 @@ extern "C"
 | 
				
			|||||||
     */
 | 
					     */
 | 
				
			||||||
    void sync_response_free(sync_response *res);
 | 
					    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
 | 
					#ifdef __cplusplus
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user