2018-09-02 20:14:26 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "driver/uart.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
2018-09-03 18:35:55 -04:00
|
|
|
#include "esp32-wifi-manager.h"
|
|
|
|
|
2018-09-02 20:14:26 -04:00
|
|
|
#define TAG "WM-TEST"
|
|
|
|
#define BUF_SIZE (1024)
|
|
|
|
#define RD_BUF_SIZE (BUF_SIZE)
|
|
|
|
#define EX_UART_NUM UART_NUM_0
|
|
|
|
|
|
|
|
#define ECHO_UART0 (true)
|
|
|
|
|
|
|
|
static uint8_t id;
|
|
|
|
static QueueHandle_t uart0_queue;
|
|
|
|
|
2018-09-22 00:29:41 -04:00
|
|
|
EventGroupHandle_t wm_event_group;
|
|
|
|
|
2018-09-02 20:14:26 -04:00
|
|
|
static void uart_event_task(void *pvParameters) {
|
|
|
|
uart_event_t event;
|
|
|
|
uint8_t *dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
|
|
|
|
uint8_t size = 0;
|
|
|
|
|
|
|
|
bzero(dtmp, RD_BUF_SIZE);
|
|
|
|
|
|
|
|
for(;;) {
|
|
|
|
if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
|
|
|
|
switch(event.type) {
|
|
|
|
case UART_DATA:
|
|
|
|
uart_read_bytes(UART_NUM_0, dtmp + size, event.size, portMAX_DELAY);
|
|
|
|
#ifdef ECHO_UART0
|
|
|
|
uart_write_bytes(UART_NUM_0, (const char*) dtmp + size, event.size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
size += event.size;
|
|
|
|
|
|
|
|
if(dtmp[size - 1] == '\n') {
|
|
|
|
uart_write_bytes(UART_NUM_0, (const char*) dtmp, size);
|
|
|
|
|
|
|
|
bzero(dtmp, RD_BUF_SIZE);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UART_BREAK:
|
|
|
|
case UART_BUFFER_FULL:
|
|
|
|
case UART_FIFO_OVF:
|
|
|
|
case UART_FRAME_ERR:
|
|
|
|
case UART_PARITY_ERR:
|
|
|
|
case UART_DATA_BREAK:
|
|
|
|
case UART_PATTERN_DET:
|
|
|
|
case UART_EVENT_MAX:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop(void *p) {
|
|
|
|
ESP_LOGI(TAG, "starting main loop");
|
|
|
|
|
|
|
|
while(true) {
|
2018-09-22 00:30:30 -04:00
|
|
|
EventBits_t ev_bits = xEventGroupGetBits(wm_event_group);
|
2018-09-02 20:14:26 -04:00
|
|
|
|
2018-09-22 00:30:30 -04:00
|
|
|
if(ev_bits & WIFI_CONNECTED) {
|
|
|
|
ESP_LOGI(TAG, "Got connection!");
|
2018-09-22 01:34:04 -04:00
|
|
|
} else if(ev_bits & WIFI_IDLE) {
|
|
|
|
ESP_LOGI(TAG, "WiFi idle.");
|
2018-09-22 01:35:54 -04:00
|
|
|
|
|
|
|
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Attempting reconnect");
|
2018-09-22 02:23:11 -04:00
|
|
|
|
|
|
|
wifi_manager_connect();
|
2018-09-22 00:30:30 -04:00
|
|
|
}
|
2018-09-22 01:34:04 -04:00
|
|
|
|
|
|
|
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
2018-09-02 20:14:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main() {
|
|
|
|
esp_err_t ret;
|
|
|
|
|
|
|
|
// Initialize NVS.
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
|
2018-09-03 18:35:55 -04:00
|
|
|
ESP_LOGI(TAG, "Erasing flash memory");
|
2018-09-02 20:14:26 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_erase());
|
|
|
|
ret = nvs_flash_init();
|
|
|
|
}
|
|
|
|
ESP_ERROR_CHECK( ret );
|
|
|
|
|
|
|
|
uart_config_t uart_config = {
|
|
|
|
.baud_rate = 115200,
|
|
|
|
.data_bits = UART_DATA_8_BITS,
|
|
|
|
.parity = UART_PARITY_DISABLE,
|
|
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
|
|
|
|
};
|
|
|
|
|
|
|
|
esp_log_level_set(TAG, ESP_LOG_INFO);
|
|
|
|
ESP_ERROR_CHECK(uart_param_config(EX_UART_NUM, &uart_config));
|
|
|
|
ESP_ERROR_CHECK(uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
|
|
|
ESP_ERROR_CHECK(uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0));
|
|
|
|
|
|
|
|
//Create a task to handler UART event from ISR
|
|
|
|
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 1, NULL);
|
|
|
|
|
|
|
|
uint8_t *mac;
|
|
|
|
mac = (uint8_t *)malloc(6);
|
|
|
|
esp_efuse_mac_get_default(mac);
|
|
|
|
id = mac[5];
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "MAC: %X:%X:%X:%X:%X:%X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
|
|
|
|
2018-09-22 00:29:41 -04:00
|
|
|
wm_event_group = wifi_manager_start();
|
2018-09-22 00:30:58 -04:00
|
|
|
wifi_manager_reset_store();
|
2018-09-03 18:35:55 -04:00
|
|
|
|
|
|
|
if(wifi_manager_ap_count() == 0) {
|
2019-02-22 18:27:02 -05:00
|
|
|
wifi_manager_bootstrap_config();
|
2018-09-03 18:35:55 -04:00
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "Got AP from store, waiting for connection");
|
|
|
|
}
|
|
|
|
|
2018-09-02 20:14:26 -04:00
|
|
|
xTaskCreate(&loop, "loop", 2048, NULL, 6, NULL);
|
|
|
|
};
|