165 lines
5.7 KiB
C
165 lines
5.7 KiB
C
#include "driver/gpio.h"
|
|
#include "driver/uart.h"
|
|
#include "esp_event.h"
|
|
#include "esp_log.h"
|
|
#include "esp_netif.h"
|
|
#include "esp_system.h"
|
|
#include "esp_wifi.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "nvs_flash.h"
|
|
#include "lwip/sockets.h"
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
|
|
#include "./log.h"
|
|
|
|
const char *PROGRAM_NAME = "SMART_ADRESS";
|
|
const char *WIFI_NAME = "iPhone de Afonso";
|
|
const char *WIFI_PASSWORD = "afonsolindo22";
|
|
|
|
#define API_SOCKET_IP "172.20.10.2" // Change to your PC's IP
|
|
#define API_SOCKET_PORT 5005
|
|
|
|
#define GPS_UART_NUM UART_NUM_2
|
|
#define GPS_TX_PIN 17 // replace with your TX2 pin number
|
|
#define GPS_RX_PIN 16 // replace with your RX2 pin number
|
|
#define GPS_BUF_SIZE 1024
|
|
#define BUF_SIZE 128
|
|
|
|
#define LED_GPIO 2 // Onboard LED
|
|
#define WIFI_CONNECTED_BIT BIT0
|
|
|
|
// Event group to signal Wi-Fi connection
|
|
static EventGroupHandle_t s_wifi_event_group;
|
|
|
|
// INFO: Exit will just start the LED blink to show the user
|
|
// that the ESP-32 has reached the end
|
|
void stop() {
|
|
while (1) {
|
|
gpio_set_level(LED_GPIO, 1);
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 0);
|
|
vTaskDelay(200 / portTICK_PERIOD_MS);
|
|
}
|
|
}
|
|
|
|
void success() {
|
|
gpio_set_level(LED_GPIO, 1);
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 0);
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 1);
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 0);
|
|
}
|
|
|
|
void error() {
|
|
gpio_set_level(LED_GPIO, 1);
|
|
vTaskDelay(700 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 0);
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 1);
|
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
|
gpio_set_level(LED_GPIO, 0);
|
|
}
|
|
|
|
void gps_init() {
|
|
const uart_config_t uart_config = {
|
|
.baud_rate = 9600, .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, .flow_ctrl = UART_HW_FLOWCTRL_DISABLE};
|
|
uart_param_config(GPS_UART_NUM, &uart_config);
|
|
uart_set_pin(GPS_UART_NUM, GPS_TX_PIN, GPS_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
|
|
uart_driver_install(GPS_UART_NUM, GPS_BUF_SIZE, 0, 0, NULL, 0);
|
|
}
|
|
|
|
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();
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
|
esp_wifi_connect();
|
|
flogf(LOG_LEVEL_WARN | LOG_COLOR_OUT | LOG_TIME_STAMP, "Retrying to connect...");
|
|
error();
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
|
}
|
|
}
|
|
|
|
void wifi_init_sta(const char *ssid, const char *pass) {
|
|
s_wifi_event_group = xEventGroupCreate();
|
|
esp_netif_init();
|
|
esp_event_loop_create_default();
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
esp_wifi_init(&cfg);
|
|
|
|
esp_event_handler_instance_t instance_any_id;
|
|
esp_event_handler_instance_t instance_got_ip;
|
|
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id);
|
|
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip);
|
|
|
|
wifi_config_t wifi_config = {0};
|
|
strncpy((char *)wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
|
|
strncpy((char *)wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
|
|
|
|
esp_wifi_set_mode(WIFI_MODE_STA);
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
|
esp_wifi_start();
|
|
|
|
// Wait for connection
|
|
xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT, false, true, portMAX_DELAY);
|
|
flogf(LOG_LEVEL_SUCCESS | LOG_COLOR_OUT | LOG_TIME_STAMP, "Connected to the internet \"%s\"", ssid);
|
|
success();
|
|
}
|
|
|
|
void app_main(void) {
|
|
esp_rom_gpio_pad_select_gpio(LED_GPIO);
|
|
gpio_set_direction(LED_GPIO, GPIO_MODE_OUTPUT);
|
|
gpio_set_level(LED_GPIO, 0);
|
|
flogf(LOG_LEVEL_INFO | LOG_COLOR_OUT | LOG_TIME_STAMP, "%s Starting", PROGRAM_NAME);
|
|
|
|
flogf(LOG_LEVEL_WARN | LOG_COLOR_OUT | LOG_TIME_STAMP, "Running NVS");
|
|
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);
|
|
flogf(LOG_LEVEL_SUCCESS | LOG_COLOR_OUT | LOG_TIME_STAMP, "NVS OK!");
|
|
|
|
flogf(LOG_LEVEL_INFO | LOG_COLOR_OUT | LOG_TIME_STAMP, "Will now try to connect to \"%s\" with password [%s]", WIFI_NAME, WIFI_PASSWORD);
|
|
wifi_init_sta(WIFI_NAME, WIFI_PASSWORD);
|
|
|
|
flogf(LOG_LEVEL_INFO | LOG_COLOR_OUT | LOG_TIME_STAMP, "Inicializing UDP Socket to %s:%d", API_SOCKET_IP, API_SOCKET_PORT);
|
|
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
|
if (sock < 0) {
|
|
flogf(LOG_LEVEL_ERROR | LOG_COLOR_OUT | LOG_TIME_STAMP, "Failed to initialize socket!");
|
|
vTaskDelete(NULL);
|
|
return;
|
|
}
|
|
|
|
struct sockaddr_in dest_addr;
|
|
dest_addr.sin_family = AF_INET;
|
|
dest_addr.sin_port = htons(API_SOCKET_PORT);
|
|
dest_addr.sin_addr.s_addr = inet_addr(API_SOCKET_IP);
|
|
|
|
flogf(LOG_LEVEL_INFO | LOG_COLOR_OUT | LOG_TIME_STAMP, "Atempting to read GPS signals from PINS: TX%d and RX%d", GPS_TX_PIN, GPS_RX_PIN);
|
|
gps_init();
|
|
flogf(LOG_LEVEL_WARN | LOG_COLOR_OUT | LOG_TIME_STAMP, "Checking...");
|
|
|
|
flogf(LOG_LEVEL_INFO | LOG_COLOR_OUT | LOG_TIME_STAMP, "ESP Should start to send info to the open socket...");
|
|
uint8_t data[BUF_SIZE];
|
|
while (1) {
|
|
int len = uart_read_bytes(GPS_UART_NUM, data, BUF_SIZE, 100 / portTICK_PERIOD_MS);
|
|
if (len > 0) {
|
|
data[len] = '\0';
|
|
// INFO: THIS IS WHERE THE ESP SENDS STUFF TO THE LISTETNING PC
|
|
sendto(sock, data, len, 0, (struct sockaddr *)&dest_addr, sizeof(dest_addr));
|
|
}
|
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
|
}
|
|
|
|
success();
|
|
stop();
|
|
}
|