Files
ota_template/main/main.c
2025-07-17 17:25:38 -06:00

175 lines
5.2 KiB
C

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_chip_info.h"
#include "wifi_manager.h"
#include "ota_server.h"
#include "led_strip.h"
static const char *TAG = "MAIN";
// WiFi credentials - Change these to your network
#define WIFI_SSID "YOUR_SSID"
#define WIFI_PASSWORD "YOUR_PASSWORD"
// Application version
#define APP_VERSION "1.0.0"
// LED colors and timing
typedef struct {
uint8_t r, g, b;
const char *name;
} color_t;
static const color_t colors[] = {
{255, 0, 0, "Red"},
{0, 255, 0, "Green"},
{0, 0, 255, "Blue"},
{255, 255, 0, "Yellow"},
{255, 0, 255, "Magenta"},
{0, 255, 255, "Cyan"},
{255, 255, 255, "White"},
};
#define NUM_COLORS (sizeof(colors) / sizeof(colors[0]))
#define BLINK_DELAY_MS 500
// WiFi event handler
static void wifi_event_handler(wifi_state_t state)
{
switch (state) {
case WIFI_STATE_CONNECTED:
ESP_LOGI(TAG, "WiFi connected - starting OTA server");
ota_server_start();
break;
case WIFI_STATE_DISCONNECTED:
ESP_LOGW(TAG, "WiFi disconnected - stopping OTA server");
ota_server_stop();
break;
case WIFI_STATE_ERROR:
ESP_LOGE(TAG, "WiFi connection failed");
break;
default:
break;
}
}
// OTA progress handler
static void ota_progress_handler(int percent)
{
ESP_LOGI(TAG, "OTA Progress: %d%%", percent);
}
void print_chip_info(void)
{
esp_chip_info_t chip_info;
esp_chip_info(&chip_info);
ESP_LOGI(TAG, "This is %s chip with %d CPU core(s), WiFi%s%s, ",
CONFIG_IDF_TARGET,
chip_info.cores,
(chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
(chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "");
ESP_LOGI(TAG, "silicon revision %d, ", chip_info.revision);
ESP_LOGI(TAG, "Minimum free heap size: %d bytes", esp_get_minimum_free_heap_size());
}
void app_main(void)
{
ESP_LOGI(TAG, "ESP32-S3 Thing Plus RGB Blinker v%s", APP_VERSION);
// Print chip information
print_chip_info();
// Initialize RGB LED
led_strip_t *led_strip = led_strip_init(LED_STRIP_GPIO, LED_STRIP_LED_COUNT);
if (!led_strip) {
ESP_LOGE(TAG, "Failed to initialize LED strip");
} else {
ESP_LOGI(TAG, "RGB LED initialized on GPIO %d", LED_STRIP_GPIO);
// Turn LED off initially
led_strip_clear(led_strip);
}
// Initialize WiFi manager
ESP_ERROR_CHECK(wifi_manager_init());
wifi_manager_register_callback(wifi_event_handler);
// Initialize OTA server
ESP_ERROR_CHECK(ota_server_init());
ota_server_set_version(APP_VERSION);
ota_server_register_progress_callback(ota_progress_handler);
// Check if we have stored WiFi credentials
char stored_ssid[33] = {0};
char stored_pass[65] = {0};
// Force update with new credentials (remove this after first successful connection)
ESP_LOGI(TAG, "Updating WiFi credentials - SSID: '%s'", WIFI_SSID);
wifi_manager_set_credentials(WIFI_SSID, WIFI_PASSWORD);
/*
// Normal flow - only update if no credentials stored
if (wifi_manager_get_credentials(stored_ssid, sizeof(stored_ssid),
stored_pass, sizeof(stored_pass)) != ESP_OK) {
ESP_LOGI(TAG, "No stored WiFi credentials, saving default ones");
ESP_LOGI(TAG, "Setting SSID: '%s'", WIFI_SSID);
wifi_manager_set_credentials(WIFI_SSID, WIFI_PASSWORD);
} else {
ESP_LOGI(TAG, "Found stored credentials - SSID: '%s'", stored_ssid);
}
*/
// Start WiFi connection
esp_err_t ret = wifi_manager_start();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to start WiFi manager");
}
// Main loop with RGB LED blinking
int color_index = 0;
bool led_on = false;
while (1) {
// Blink the RGB LED through different colors
if (led_strip) {
if (led_on) {
// Turn LED on with current color
led_strip_set_pixel(led_strip, 0,
colors[color_index].r,
colors[color_index].g,
colors[color_index].b);
led_strip_refresh(led_strip);
ESP_LOGI(TAG, "LED ON - Color: %s", colors[color_index].name);
} else {
// Turn LED off
led_strip_clear(led_strip);
ESP_LOGI(TAG, "LED OFF");
// Move to next color when turning off
color_index = (color_index + 1) % NUM_COLORS;
}
led_on = !led_on;
}
// Print heap info every 10 blinks (5 seconds)
static int blink_count = 0;
if (++blink_count >= 10) {
ESP_LOGI(TAG, "Free heap: %d bytes, WiFi: %s",
esp_get_free_heap_size(),
wifi_manager_is_connected() ? "Connected" : "Disconnected");
blink_count = 0;
}
vTaskDelay(BLINK_DELAY_MS / portTICK_PERIOD_MS);
}
}