#include "myUart.h"
#include <esp_wifi.h>
#include <esp_event.h>
#include <esp_log.h>
#include <esp_system.h>
#include <esp_http_server.h>
#include <nvs_flash.h>
#include <sys/param.h>
#include <string.h>
#include "esp_eth.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_adc_cal.h"
#include <stdlib.h>
#define EXAMPLE_ESP_WIFI_SSID "softAP_EXAMPLE"
#define EXAMPLE_ESP_WIFI_PASS "SE_141414"
#define EXAMPLE_MAX_STA_CONN 10
char dynamic_content[100]={0};
static const char *TAG = "softAP_WebServer";
uint8_t led=0;
uint32_t start;
char cad[100]={0},cad2[100]={0};
char nombre[20]={0};
/* An HTTP GET handler */
static esp_err_t hello_get_handler(httpd_req_t *req)
{
char html_response[1000]={0};
uint32_t j=0;
for(int i=0;cad[i]!='\0';i++){
if(cad[i]==';'){
break;
}
else{
cad2[j]=cad[i];
j++;
}
}
cad2[j]='\0';
snprintf(html_response,sizeof(html_response),
"<html>"
"<head>"
"<center>"
"<title>Receptor</title>"
"</head>"
"<body>"
"<h1>%s</h1>"
"<button onclick=\"location.href='/'\"> Menu principal </button>"
"</body>"
"</center>"
"</html>",cad2);
httpd_resp_send(req, html_response, strlen(html_response));
return ESP_OK;
}
static const httpd_uri_t hello = {
.uri = "/",
.method = HTTP_GET,
.handler = hello_get_handler,
};
static httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Iniciar el servidor httpd
ESP_LOGI(TAG, "Iniciando el servidor en el puerto: '%d'", config.server_port);
if (httpd_start(&server, &config) == ESP_OK) {
// Manejadores de URI
ESP_LOGI(TAG, "Registrando manejadores de URI");
httpd_register_uri_handler(server, &hello);
return server;
}
ESP_LOGI(TAG, "Error starting server!");
return NULL;
}
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG, "estacion "MACSTR" se unio, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG, "estacion "MACSTR" se desconecto, AID=%d",
MAC2STR(event->mac), event->aid);
}
}
esp_err_t wifi_init_softap(void)
{
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
wifi_config_t wifi_config = {
.ap = {
.ssid = EXAMPLE_ESP_WIFI_SSID,
.ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
.password = EXAMPLE_ESP_WIFI_PASS,
.max_connection = EXAMPLE_MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK
},
};
if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "Inicializacion de softAP terminada. SSID: %s password: %s",
EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
return ESP_OK;
}
void app_main(void)
{
httpd_handle_t server = NULL;
uartInit(PC_UART_PORT,115200,8,0,1, PC_UART_TX_PIN, PC_UART_RX_PIN);
uartInit(UART2_PORT,115200,8,0,1,UART2_TX_PIN,UART2_RX_PIN);
ESP_ERROR_CHECK(nvs_flash_init());
esp_netif_init();
ESP_ERROR_CHECK(esp_event_loop_create_default());
ESP_LOGI(TAG, "init softAP");
ESP_ERROR_CHECK(wifi_init_softap());
server = start_webserver();
while(1)
{
uartGoto11(0);
uartClrScr(0);
uartSetColor(0,YELLOW);
int len = uart_read_bytes(UART_NUM_2, &cad, sizeof(cad), 20/portTICK_RATE_MS);
uartPuts(0,cad);
delayMs(1000);
}
}