Added MQTT
This commit is contained in:
210
main/main.c
210
main/main.c
@ -5,54 +5,89 @@
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_chip_info.h"
|
||||
#include "esp_random.h"
|
||||
#include "wifi_manager.h"
|
||||
#include "ota_server.h"
|
||||
#include "led_strip.h"
|
||||
|
||||
#include "plant_mqtt.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
static const char *TAG = "MAIN";
|
||||
|
||||
// WiFi credentials - Change these to your network
|
||||
// #define WIFI_SSID "YOUR_SSID"
|
||||
// #define WIFI_PASSWORD "YOUR_PASSWORD"
|
||||
|
||||
const char *ssid = CONFIG_WIFI_SSID;
|
||||
const char *password = CONFIG_WIFI_PASSWORD;
|
||||
|
||||
// Application version
|
||||
#define APP_VERSION "1.0.1"
|
||||
#define APP_VERSION "2.0.0-mqtt"
|
||||
|
||||
// LED colors and timing
|
||||
typedef struct {
|
||||
uint8_t r, g, b;
|
||||
const char *name;
|
||||
} color_t;
|
||||
// Test data
|
||||
static int test_moisture_1 = 45;
|
||||
static int test_moisture_2 = 62;
|
||||
static bool test_pump_1 = false;
|
||||
static bool test_pump_2 = false;
|
||||
|
||||
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"},
|
||||
};
|
||||
// MQTT Callbacks
|
||||
static void mqtt_connected_callback(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "MQTT Connected - Publishing initial status");
|
||||
|
||||
// Publish initial states
|
||||
mqtt_publish_moisture(1, test_moisture_1);
|
||||
mqtt_publish_moisture(2, test_moisture_2);
|
||||
mqtt_publish_pump_state(1, test_pump_1);
|
||||
mqtt_publish_pump_state(2, test_pump_2);
|
||||
}
|
||||
|
||||
#define NUM_COLORS (sizeof(colors) / sizeof(colors[0]))
|
||||
#define BLINK_DELAY_MS 200
|
||||
static void mqtt_disconnected_callback(void)
|
||||
{
|
||||
ESP_LOGW(TAG, "MQTT Disconnected");
|
||||
}
|
||||
|
||||
static void mqtt_data_callback(const char* topic, const char* data, int data_len)
|
||||
{
|
||||
ESP_LOGI(TAG, "MQTT Data received on topic: %s", topic);
|
||||
ESP_LOGI(TAG, "Data: %.*s", data_len, data);
|
||||
|
||||
// Handle pump control commands
|
||||
if (strcmp(topic, TOPIC_PUMP_1_CMD) == 0) {
|
||||
if (strncmp(data, "on", data_len) == 0) {
|
||||
test_pump_1 = true;
|
||||
ESP_LOGI(TAG, "Pump 1 turned ON");
|
||||
mqtt_publish_pump_state(1, test_pump_1);
|
||||
} else if (strncmp(data, "off", data_len) == 0) {
|
||||
test_pump_1 = false;
|
||||
ESP_LOGI(TAG, "Pump 1 turned OFF");
|
||||
mqtt_publish_pump_state(1, test_pump_1);
|
||||
}
|
||||
} else if (strcmp(topic, TOPIC_PUMP_2_CMD) == 0) {
|
||||
if (strncmp(data, "on", data_len) == 0) {
|
||||
test_pump_2 = true;
|
||||
ESP_LOGI(TAG, "Pump 2 turned ON");
|
||||
mqtt_publish_pump_state(2, test_pump_2);
|
||||
} else if (strncmp(data, "off", data_len) == 0) {
|
||||
test_pump_2 = false;
|
||||
ESP_LOGI(TAG, "Pump 2 turned OFF");
|
||||
mqtt_publish_pump_state(2, test_pump_2);
|
||||
}
|
||||
} else if (strcmp(topic, TOPIC_CONFIG) == 0) {
|
||||
ESP_LOGI(TAG, "Configuration update received");
|
||||
// Parse JSON configuration here
|
||||
}
|
||||
}
|
||||
|
||||
// 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");
|
||||
ESP_LOGI(TAG, "WiFi connected - starting services");
|
||||
ota_server_start();
|
||||
|
||||
// Start MQTT client
|
||||
if (mqtt_client_start() != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to start MQTT client");
|
||||
}
|
||||
break;
|
||||
|
||||
case WIFI_STATE_DISCONNECTED:
|
||||
ESP_LOGW(TAG, "WiFi disconnected - stopping OTA server");
|
||||
ESP_LOGW(TAG, "WiFi disconnected - stopping services");
|
||||
mqtt_client_stop();
|
||||
ota_server_stop();
|
||||
break;
|
||||
|
||||
@ -71,6 +106,35 @@ static void ota_progress_handler(int percent)
|
||||
ESP_LOGI(TAG, "OTA Progress: %d%%", percent);
|
||||
}
|
||||
|
||||
// Task to simulate sensor readings
|
||||
static void sensor_simulation_task(void *pvParameters)
|
||||
{
|
||||
while (1) {
|
||||
// Wait for MQTT connection
|
||||
if (mqtt_client_is_connected()) {
|
||||
// Simulate moisture sensor readings with some variation
|
||||
test_moisture_1 += (esp_random() % 5) - 2; // +/- 2
|
||||
test_moisture_2 += (esp_random() % 5) - 2; // +/- 2
|
||||
|
||||
// Keep values in range
|
||||
if (test_moisture_1 < 0) test_moisture_1 = 0;
|
||||
if (test_moisture_1 > 100) test_moisture_1 = 100;
|
||||
if (test_moisture_2 < 0) test_moisture_2 = 0;
|
||||
if (test_moisture_2 > 100) test_moisture_2 = 100;
|
||||
|
||||
// Publish sensor data
|
||||
mqtt_publish_moisture(1, test_moisture_1);
|
||||
mqtt_publish_moisture(2, test_moisture_2);
|
||||
|
||||
ESP_LOGI(TAG, "Published moisture: Sensor1=%d%%, Sensor2=%d%%",
|
||||
test_moisture_1, test_moisture_2);
|
||||
}
|
||||
|
||||
// Update every 10 seconds
|
||||
vTaskDelay(10000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
void print_chip_info(void)
|
||||
{
|
||||
esp_chip_info_t chip_info;
|
||||
@ -83,55 +147,38 @@ void print_chip_info(void)
|
||||
(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);
|
||||
ESP_LOGI(TAG, "Plant Watering System 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);
|
||||
}
|
||||
// Print MQTT configuration
|
||||
ESP_LOGI(TAG, "MQTT Broker: %s", CONFIG_MQTT_BROKER_URL);
|
||||
ESP_LOGI(TAG, "MQTT Username: %s", CONFIG_MQTT_USERNAME);
|
||||
|
||||
// Initialize WiFi manager
|
||||
ESP_ERROR_CHECK(wifi_manager_init());
|
||||
wifi_manager_register_callback(wifi_event_handler);
|
||||
|
||||
// TEMPORARY: Clear stored credentials to force use of new ones
|
||||
// wifi_manager_clear_credentials();
|
||||
// ESP_LOGI(TAG, "Cleared stored WiFi credentials");
|
||||
|
||||
// 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'", ssid);
|
||||
wifi_manager_set_credentials(ssid, 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'", ssid);
|
||||
wifi_manager_set_credentials(ssid, password);
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Found stored credentials - SSID: '%s'", stored_ssid);
|
||||
}
|
||||
*/
|
||||
// Initialize MQTT client
|
||||
ESP_ERROR_CHECK(mqtt_client_init());
|
||||
mqtt_client_register_callbacks(mqtt_connected_callback,
|
||||
mqtt_disconnected_callback,
|
||||
mqtt_data_callback);
|
||||
|
||||
// Start WiFi connection
|
||||
esp_err_t ret = wifi_manager_start();
|
||||
@ -139,42 +186,23 @@ void app_main(void)
|
||||
ESP_LOGE(TAG, "Failed to start WiFi manager");
|
||||
}
|
||||
|
||||
// Main loop with RGB LED blinking
|
||||
int color_index = 0;
|
||||
bool led_on = false;
|
||||
// Create sensor simulation task
|
||||
xTaskCreate(sensor_simulation_task, "sensor_sim", 4096, NULL, 5, NULL);
|
||||
|
||||
// Main loop - monitor system status
|
||||
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;
|
||||
ESP_LOGI(TAG, "System Status - WiFi: %s, MQTT: %s, Free heap: %d bytes",
|
||||
wifi_manager_is_connected() ? "Connected" : "Disconnected",
|
||||
mqtt_client_is_connected() ? "Connected" : "Disconnected",
|
||||
esp_get_free_heap_size());
|
||||
|
||||
// Print pump states
|
||||
if (mqtt_client_is_connected()) {
|
||||
ESP_LOGI(TAG, "Pump States - Pump1: %s, Pump2: %s",
|
||||
test_pump_1 ? "ON" : "OFF",
|
||||
test_pump_2 ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
// 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);
|
||||
vTaskDelay(30000 / portTICK_PERIOD_MS); // Every 30 seconds
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user