-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
esp comunication init
- Loading branch information
Showing
14 changed files
with
2,136 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
idf_component_register( SRC_DIRS "." | ||
INCLUDE_DIRS "." | ||
REQUIRES config esp_http_client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include "https_task.h" | ||
#include "network_config.h" | ||
|
||
#include "esp_event.h" | ||
#include "esp_log.h" | ||
|
||
#define TAG "APP_INIT" | ||
|
||
void app_init_task(void *pvParameter) { | ||
ESP_LOGI(TAG, "app_init_task started."); | ||
|
||
if (wifi_init() != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to initialize wifi."); | ||
vTaskDelete(NULL); | ||
} | ||
|
||
http_post_task_create(); | ||
|
||
ESP_LOGI(TAG, "https_task created."); | ||
|
||
ESP_LOGI(TAG, "app_init_task finished."); | ||
|
||
vTaskDelete(NULL); | ||
} | ||
|
||
void app_task_create(void) { | ||
xTaskCreate(&app_init_task, "app_init_task", 4096, NULL, 5, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef APP_INIT_TASK_H | ||
#define APP_INIT_TASK_H | ||
|
||
void app_init_task(void *pvParameter); | ||
|
||
void app_task_create(void); | ||
|
||
#endif // APP_INIT_TASK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
#include "esp_http_client.h" | ||
#include "esp_log.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include <string.h> | ||
|
||
#define POST_REQUEST_DELAY_MS 20000 | ||
|
||
#define POST_URL "http://localhost:8080/weather/masurements" | ||
|
||
#define TAG "HTTPS_TASK" | ||
|
||
esp_err_t _http_event_handler(esp_http_client_event_t *evt) { | ||
switch (evt->event_id) { | ||
case HTTP_EVENT_REDIRECT: | ||
ESP_LOGI(TAG, "HTTP_EVENT_REDIRECT"); | ||
break; | ||
case HTTP_EVENT_ERROR: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ERROR"); | ||
break; | ||
case HTTP_EVENT_ON_CONNECTED: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED"); | ||
break; | ||
case HTTP_EVENT_HEADER_SENT: | ||
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT"); | ||
break; | ||
case HTTP_EVENT_ON_HEADER: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER, key=%s, value=%s", evt->header_key, | ||
evt->header_value); | ||
break; | ||
case HTTP_EVENT_ON_DATA: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len); | ||
if (!esp_http_client_is_chunked_response(evt->client)) { | ||
// Write out data | ||
printf("%.*s", evt->data_len, (char *)evt->data); | ||
} | ||
break; | ||
case HTTP_EVENT_ON_FINISH: | ||
ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH"); | ||
break; | ||
case HTTP_EVENT_DISCONNECTED: | ||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); | ||
break; | ||
} | ||
return ESP_OK; | ||
} | ||
|
||
void generate_random_data(char *buffer, size_t buffer_size) { | ||
float pressure = (float)(rand() % 100) / 10.0; | ||
float temperature1 = (float)(rand() % 100) / 10.0; | ||
float temperature2 = (float)(rand() % 100) / 10.0; | ||
bool rainDetected = rand() % 2; | ||
float humidity = (float)(rand() % 100) / 10.0; | ||
float lightIntensity = (float)(rand() % 100) / 10.0; | ||
float gasConcentration = (float)(rand() % 100) / 10.0; | ||
|
||
snprintf( | ||
buffer, buffer_size, | ||
"{\n\t\"pressure\": %.1f, \"temperature1\": %.1f, \"temperature2\": " | ||
"%.1f, " | ||
"\"rainDetected\": %s, \"humidity\": %.1f, \"lightIntensity\": %.1f, " | ||
"\"gasConcentration\": %.1f\n}", | ||
pressure, temperature1, temperature2, rainDetected ? "true" : "false", | ||
humidity, lightIntensity, gasConcentration); | ||
} | ||
|
||
void http_post_task(void *pvParameters) { | ||
|
||
char post_data[256]; | ||
|
||
while (1) { | ||
|
||
generate_random_data(post_data, sizeof(post_data)); | ||
|
||
esp_http_client_config_t config = { | ||
.url = POST_URL, | ||
.event_handler = _http_event_handler, | ||
}; | ||
esp_http_client_handle_t client = esp_http_client_init(&config); | ||
|
||
esp_http_client_set_url(client, POST_URL); | ||
esp_http_client_set_method(client, HTTP_METHOD_POST); | ||
esp_http_client_set_post_field(client, post_data, strlen(post_data)); | ||
|
||
esp_err_t err = esp_http_client_perform(client); | ||
|
||
if (err == ESP_OK) { | ||
// ESP_LOGI(TAG, "HTTPS POST Status = %d, content_length = %d", | ||
// esp_http_client_get_status_code(client), | ||
// esp_http_client_get_content_length(client)); | ||
} else { | ||
// ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err)); | ||
} | ||
|
||
esp_http_client_cleanup(client); | ||
|
||
// Delay for 20 seconds | ||
vTaskDelay(POST_REQUEST_DELAY_MS / portTICK_PERIOD_MS); | ||
} | ||
} | ||
|
||
void http_post_task_create(void) { | ||
xTaskCreate(&http_post_task, "https_post_task", 8192, NULL, 5, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef HTTPS_TASK_H | ||
#define HTTPS_TASK_H | ||
|
||
void http_post_task(void *pvParameters); | ||
|
||
void http_post_task_create(void); | ||
|
||
#endif // HTTPS_TASK_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
idf_component_register( SRC_DIRS "." | ||
INCLUDE_DIRS "." | ||
REQUIRES esp_wifi nvs_flash) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#include "esp_event.h" | ||
#include "esp_system.h" | ||
#include "esp_wifi.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
|
||
#include "nvs_flash.h" | ||
|
||
#include "esp_log.h" | ||
|
||
#include "network_config.h" | ||
|
||
#define WIFI_SSID CONFIG_WIFI_SSID | ||
#define WIFI_PASSWORD CONFIG_WIFI_PASSWORD | ||
|
||
#define TAG "NETWORK_CONFIG" | ||
|
||
static void event_handler(void *arg, esp_event_base_t event_base, | ||
int32_t event_id, void *event_data) { | ||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { | ||
esp_wifi_connect(); | ||
ESP_LOGI(TAG, "Connecting to WiFi..."); | ||
} else if (event_base == WIFI_EVENT && | ||
event_id == WIFI_EVENT_STA_DISCONNECTED) { | ||
ESP_LOGI(TAG, "Disconnected from WiFi, retrying..."); | ||
esp_wifi_connect(); | ||
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_CONNECTED) { | ||
ESP_LOGI(TAG, "Connected to WiFi"); | ||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { | ||
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; | ||
// ESP_LOGI(TAG, "Got IP: %s", esp_ip4addr_ntoa(&event->ip_info.ip)); | ||
} | ||
} | ||
|
||
esp_err_t wifi_init(void) { | ||
esp_err_t ret = nvs_flash_init(); | ||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || | ||
ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { | ||
ESP_LOGI(TAG, "Failed to initialize flash memory. Erasing NVS flash..."); | ||
nvs_flash_erase(); | ||
ret = nvs_flash_init(); | ||
} | ||
|
||
if (ret != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to initialize flash memory."); | ||
return ret; | ||
} | ||
|
||
ret = esp_netif_init(); | ||
|
||
if (ret != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to initialize network interface."); | ||
return ret; | ||
} | ||
|
||
ret = esp_event_loop_create_default(); | ||
|
||
if (ret != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to create event loop."); | ||
return ret; | ||
} | ||
|
||
esp_netif_create_default_wifi_sta(); | ||
|
||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); | ||
if (esp_wifi_init(&cfg) != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to initialize wifi."); | ||
return ESP_FAIL; | ||
} | ||
|
||
esp_event_handler_instance_t instance_any_id; | ||
esp_event_handler_instance_t instance_got_ip; | ||
|
||
if (esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, | ||
&event_handler, NULL, | ||
&instance_any_id) != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to register event handler for any event."); | ||
return ESP_FAIL; | ||
} | ||
|
||
if (esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, | ||
&event_handler, NULL, | ||
&instance_got_ip) != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to register event handler for IP event."); | ||
return ESP_FAIL; | ||
} | ||
|
||
wifi_config_t wifi_config = { | ||
.sta = | ||
{ | ||
.ssid = WIFI_SSID, | ||
.password = WIFI_PASSWORD, | ||
}, | ||
}; | ||
|
||
if (esp_wifi_set_mode(WIFI_MODE_STA) != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to set wifi mode."); | ||
return ESP_FAIL; | ||
} | ||
|
||
if (esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to set wifi configuration."); | ||
return ESP_FAIL; | ||
} | ||
|
||
if (esp_wifi_start() != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to start wifi."); | ||
return ESP_FAIL; | ||
} | ||
|
||
ESP_LOGI(TAG, "wifi_init_sta finished."); | ||
|
||
if (esp_wifi_connect() != ESP_OK) { | ||
ESP_LOGE(TAG, "Failed to connect to wifi."); | ||
return ESP_FAIL; | ||
} | ||
|
||
return ESP_OK; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef NETWORK_CONFIG_H | ||
#define NETWORK_CONFIG_H | ||
|
||
#include "esp_err.h" | ||
|
||
esp_err_t wifi_init(void); | ||
|
||
#endif // NETWORK_CONFIG_H |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
idf_component_register(SRCS "weather.c" | ||
INCLUDE_DIRS ".") | ||
INCLUDE_DIRS "." | ||
REQUIRES app config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
menu "Weather application configuration" | ||
|
||
menu "WIFI Configuration" | ||
config WIFI_SSID | ||
string "WIFI SSID" | ||
default "SSID" | ||
help | ||
"Enter the SSID of the WIFI network you want to connect to." | ||
|
||
config WIFI_PASSWORD | ||
string "WIFI Password" | ||
default "PASSWORD" | ||
help | ||
"Enter the password of the WIFI network you want to connect to." | ||
endmenu | ||
|
||
endmenu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.