create separate folder for arduino and espidf
This commit is contained in:
parent
bc3989f111
commit
a5e0f81930
22 changed files with 0 additions and 0 deletions
100
SourceCode/esp-idf/src/common/theGPIO.c
Normal file
100
SourceCode/esp-idf/src/common/theGPIO.c
Normal file
|
@ -0,0 +1,100 @@
|
|||
#include "common/theGPIO.h"
|
||||
|
||||
static const char *TAG = "GPIO";
|
||||
struct GPIO
|
||||
{
|
||||
int pin;
|
||||
char description[20];
|
||||
int state;
|
||||
};
|
||||
// OLIMEX ESP-32-EVB Relays
|
||||
struct GPIO Relay1 = {32, "Relay 1", 0};
|
||||
struct GPIO Relay2 = {33, "Relay 2", 0};
|
||||
|
||||
static void print_gpio_status(struct GPIO *gpio)
|
||||
{
|
||||
ESP_LOGI(TAG, "%s (Pin: %i) is %s", gpio->description, gpio->pin, gpio->state == 0 ? "Off" : "On");
|
||||
}
|
||||
|
||||
static void change_gpio_state(struct GPIO *gpio, int state)
|
||||
{
|
||||
gpio_set_direction(gpio->pin, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(gpio->pin, state);
|
||||
gpio->state = state;
|
||||
print_gpio_status(gpio);
|
||||
}
|
||||
|
||||
static void turn_gpio_on(struct GPIO *gpio)
|
||||
{
|
||||
change_gpio_state(gpio, 1);
|
||||
}
|
||||
|
||||
static void turn_gpio_off(struct GPIO *gpio)
|
||||
{
|
||||
change_gpio_state(gpio, 0);
|
||||
}
|
||||
|
||||
static void toggle_gpio_state(struct GPIO *gpio)
|
||||
{
|
||||
change_gpio_state(gpio, gpio->state == 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
static void inching_gpio_state(struct GPIO *gpio, int duration)
|
||||
{
|
||||
toggle_gpio_state(gpio);
|
||||
vTaskDelay(duration / portTICK_PERIOD_MS);
|
||||
toggle_gpio_state(gpio);
|
||||
}
|
||||
|
||||
esp_err_t relay_put_handler(httpd_req_t *req)
|
||||
{
|
||||
custom_request_middleware(req);
|
||||
size_t buf_len = httpd_req_get_url_query_len(req) + 1;
|
||||
if (buf_len > 1)
|
||||
{
|
||||
char *buf = malloc(buf_len);
|
||||
if (httpd_req_get_url_query_str(req, buf, buf_len) == ESP_OK)
|
||||
{
|
||||
char relayParameter[2];
|
||||
char stateParameter[7];
|
||||
struct GPIO *relayPtr = NULL;
|
||||
if ((httpd_query_key_value(buf, "relay", relayParameter, sizeof(relayParameter)) == ESP_OK) &&
|
||||
(httpd_query_key_value(buf, "state", stateParameter, sizeof(stateParameter)) == ESP_OK))
|
||||
{
|
||||
if (strcmp(relayParameter, "1") == 0)
|
||||
relayPtr = &Relay1;
|
||||
else if (strcmp(relayParameter, "2") == 0)
|
||||
relayPtr = &Relay2;
|
||||
else
|
||||
{
|
||||
custom_json_response(req, "not a valid relay", HTTPD_400);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (strcmp(stateParameter, "on") == 0)
|
||||
turn_gpio_on(relayPtr);
|
||||
else if (strcmp(stateParameter, "off") == 0)
|
||||
turn_gpio_off(relayPtr);
|
||||
else if (strcmp(stateParameter, "toggle") == 0)
|
||||
toggle_gpio_state(relayPtr);
|
||||
else if (strcmp(stateParameter, "inch") == 0)
|
||||
inching_gpio_state(relayPtr, 1000);
|
||||
else
|
||||
{
|
||||
custom_json_response(req, "not a valid state", HTTPD_400);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
custom_json_response(req, "please provide relay and state query parameter", HTTPD_400);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
free(buf);
|
||||
custom_json_response(req, "success", HTTPD_200);
|
||||
return ESP_OK;
|
||||
}
|
||||
custom_json_response(req, "please provide relay and state query parameter", HTTPD_400);
|
||||
return ESP_FAIL;
|
||||
}
|
48
SourceCode/esp-idf/src/common/theLogger.c
Normal file
48
SourceCode/esp-idf/src/common/theLogger.c
Normal file
|
@ -0,0 +1,48 @@
|
|||
#include "common/theLogger.h"
|
||||
|
||||
static const char *TAG = "System";
|
||||
|
||||
void print_logo()
|
||||
{
|
||||
const char *Logo[] = {
|
||||
"\n ______ _____ _____ ____ ___ ______ _ _ _ \n",
|
||||
" | ____|/ ____| __ \\___ \\__ \\ | ____(_) | | |\n",
|
||||
" | |__ | (___ | |__) |__) | ) |_____| |__ _ _ __ _____ ____ _| | |\n",
|
||||
" | __| \\___ \\| ___/|__ < / /______| __| | | '__/ _ \\ \\ /\\ / / _` | | |\n",
|
||||
" | |____ ____) | | ___) / /_ | | | | | | __/\\ V V / (_| | | |\n",
|
||||
" |______|_____/|_| |____/____| |_| |_|_| \\___| \\_/\\_/ \\__,_|_|_|\n\n",
|
||||
};
|
||||
int logoSize = sizeof Logo / sizeof *Logo;
|
||||
for (int i = 0; i < logoSize; ++i)
|
||||
{
|
||||
printf(Logo[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void print_esp32_information()
|
||||
{
|
||||
esp_chip_info_t chip_info;
|
||||
esp_chip_info(&chip_info);
|
||||
|
||||
ESP_LOGI(TAG, "This is an ESP32 chip with %d CPU cores, WiFi%s%s, silicon revision %d",
|
||||
chip_info.cores,
|
||||
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
|
||||
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "",
|
||||
chip_info.revision);
|
||||
|
||||
ESP_LOGI(TAG, "%dMB %s flash, minimum free heap size: %d bytes\n", spi_flash_get_chip_size() / (1024 * 1024),
|
||||
(chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external",
|
||||
esp_get_minimum_free_heap_size());
|
||||
}
|
||||
|
||||
void setup_all_log_levels()
|
||||
{
|
||||
esp_log_level_set("*", ESP_LOG_ERROR);
|
||||
esp_log_level_set(TAG, ESP_LOG_INFO);
|
||||
esp_log_level_set("NVS", ESP_LOG_INFO);
|
||||
esp_log_level_set("WiFi", ESP_LOG_INFO);
|
||||
esp_log_level_set("SNTP", ESP_LOG_INFO);
|
||||
esp_log_level_set("GPIO", ESP_LOG_INFO);
|
||||
esp_log_level_set("WebServer", ESP_LOG_INFO);
|
||||
esp_log_level_set("Firewall", ESP_LOG_INFO);
|
||||
}
|
15
SourceCode/esp-idf/src/common/theNVS.c
Normal file
15
SourceCode/esp-idf/src/common/theNVS.c
Normal file
|
@ -0,0 +1,15 @@
|
|||
#include "common/theNVS.h"
|
||||
|
||||
static const char *TAG = "NVS";
|
||||
|
||||
void setup_nvs(void)
|
||||
{
|
||||
esp_err_t ret = nvs_flash_init();
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(ret);
|
||||
ESP_LOGI(TAG, "Initialization done");
|
||||
}
|
37
SourceCode/esp-idf/src/common/theSNTP.c
Normal file
37
SourceCode/esp-idf/src/common/theSNTP.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include "common/theSNTP.h"
|
||||
|
||||
static const char *TAG = "SNTP";
|
||||
|
||||
void sntp_sync_notification_call_back(struct timeval *tv)
|
||||
{
|
||||
ESP_LOGI(TAG, "Time synchronization done");
|
||||
}
|
||||
|
||||
static void initialize_sntp()
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing SNTP");
|
||||
sntp_setoperatingmode(SNTP_OPMODE_POLL);
|
||||
sntp_setservername(0, "pool.ntp.org");
|
||||
sntp_set_time_sync_notification_cb(sntp_sync_notification_call_back);
|
||||
sntp_init();
|
||||
ESP_LOGI(TAG, "Getting current time from \"%s\"", sntp_getservername(0));
|
||||
}
|
||||
|
||||
static void set_time_zone_to_germany()
|
||||
{
|
||||
// Timezone Codes: https://github.com/nayarsystems/posix_tz_db/blob/master/zones.csv
|
||||
time_t now;
|
||||
struct tm timeinfo;
|
||||
|
||||
setenv("TZ", "CET-1CEST,M3.5.0,M10.5.0/3", 1);
|
||||
tzset();
|
||||
time(&now);
|
||||
localtime_r(&now, &timeinfo);
|
||||
ESP_LOGI(TAG, "Timezone set to Europe/Berlin");
|
||||
}
|
||||
|
||||
void setup_sntp()
|
||||
{
|
||||
set_time_zone_to_germany();
|
||||
initialize_sntp();
|
||||
}
|
Reference in a new issue