Initial Commit.
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "main.c" "log.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES driver freertos esp_rom esp_event esp_wifi nvs_flash esp_driver_gpio esp_driver_uart
|
||||
)
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
#include "log.h"
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
unsigned int flogf(unsigned short flags, const char *Format, ...) {
|
||||
va_list args;
|
||||
va_start(args, Format);
|
||||
|
||||
unsigned int char_counter = 0;
|
||||
|
||||
time_t t = time(NULL);
|
||||
struct tm *tm_info = localtime(&t);
|
||||
|
||||
if (flags & LOG_TIME_STAMP) {
|
||||
printf("%02d:%02d:%02d - ", tm_info->tm_hour, tm_info->tm_min, tm_info->tm_sec);
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
if (flags & LOG_LEVEL_WARN) {
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_YELLOW);
|
||||
}
|
||||
printf("[WARNING]: \t");
|
||||
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_RESET);
|
||||
}
|
||||
} else if (flags & LOG_LEVEL_ERROR) {
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_RED);
|
||||
}
|
||||
printf("[ERROR]: \t");
|
||||
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_RESET);
|
||||
}
|
||||
} else if (flags & LOG_LEVEL_INFO) {
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_BLUE);
|
||||
}
|
||||
|
||||
printf("[INFO]: \t");
|
||||
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_RESET);
|
||||
}
|
||||
} else if (flags & LOG_LEVEL_SUCCESS) {
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_GREEN);
|
||||
}
|
||||
|
||||
printf("[SUCCESS]: \t");
|
||||
|
||||
if (flags & LOG_COLOR_OUT) {
|
||||
printf(COLOR_RESET);
|
||||
}
|
||||
}
|
||||
|
||||
// Format string processing
|
||||
while (*Format) {
|
||||
if (*Format == '%') {
|
||||
Format++;
|
||||
switch (*Format) {
|
||||
case 'd':
|
||||
printf("%d", va_arg(args, int));
|
||||
break;
|
||||
case 'f':
|
||||
printf("%f", va_arg(args, double));
|
||||
break;
|
||||
case 's':
|
||||
printf("%s", va_arg(args, char *));
|
||||
break;
|
||||
case 'c':
|
||||
printf("%c", va_arg(args, int));
|
||||
break;
|
||||
case 'x':
|
||||
printf("%x", va_arg(args, unsigned int));
|
||||
break;
|
||||
case 'p':
|
||||
printf("%p", va_arg(args, void *));
|
||||
break;
|
||||
case '%':
|
||||
printf("%%");
|
||||
break;
|
||||
default:
|
||||
printf("%%");
|
||||
printf("%c", *Format);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
putchar(*Format);
|
||||
}
|
||||
Format++;
|
||||
char_counter++;
|
||||
}
|
||||
|
||||
putchar('\n'); // Optional newline
|
||||
va_end(args);
|
||||
return char_counter;
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
#ifndef LOGGER_H_INCLUDED
|
||||
#define LOGGER_H_INCLUDED
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <direct.h>
|
||||
#include <windows.h>
|
||||
#define MKDIR(path) _mkdir(path)
|
||||
#else
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#define MKDIR(path) mkdir(path, 0777)
|
||||
#endif
|
||||
|
||||
#define LOG_LEVEL_INFO (1 << 0)
|
||||
#define LOG_LEVEL_WARN (1 << 1)
|
||||
#define LOG_LEVEL_ERROR (1 << 2)
|
||||
#define LOG_LEVEL_SUCCESS (1 << 3)
|
||||
#define LOG_COLOR_OUT (1 << 4)
|
||||
#define LOG_ONLY_ERROR (1 << 5)
|
||||
#define LOG_FILE (1 << 6)
|
||||
#define LOG_TIME_STAMP (1 << 7)
|
||||
|
||||
#define COLOR_RED "\x1b[31m"
|
||||
#define COLOR_GREEN "\x1b[32m"
|
||||
#define COLOR_YELLOW "\x1b[33m"
|
||||
#define COLOR_BLUE "\x1b[34m"
|
||||
#define COLOR_RESET "\x1b[0m"
|
||||
|
||||
unsigned int flogf(unsigned short flags, const char *Format, ...);
|
||||
|
||||
#endif // LOGGER_H_INCLUDED
|
||||
+164
@@ -0,0 +1,164 @@
|
||||
#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();
|
||||
}
|
||||
Reference in New Issue
Block a user