208 lines
6.6 KiB
C
208 lines
6.6 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 "esp_random.h"
|
|
#include "wifi_manager.h"
|
|
#include "ota_server.h"
|
|
#include "plant_mqtt.h"
|
|
#include "sdkconfig.h"
|
|
|
|
static const char *TAG = "MAIN";
|
|
|
|
// Application version
|
|
#define APP_VERSION "2.0.0-mqtt"
|
|
|
|
// 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;
|
|
|
|
// 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);
|
|
}
|
|
|
|
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 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 services");
|
|
mqtt_client_stop();
|
|
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);
|
|
}
|
|
|
|
// 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;
|
|
|
|
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, "Plant Watering System v%s", APP_VERSION);
|
|
|
|
// Print chip information
|
|
print_chip_info();
|
|
|
|
// 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);
|
|
|
|
// 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();
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG, "Failed to start WiFi manager");
|
|
}
|
|
|
|
// Create sensor simulation task
|
|
xTaskCreate(sensor_simulation_task, "sensor_sim", 4096, NULL, 5, NULL);
|
|
|
|
// Main loop - monitor system status
|
|
while (1) {
|
|
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");
|
|
}
|
|
|
|
vTaskDelay(30000 / portTICK_PERIOD_MS); // Every 30 seconds
|
|
}
|
|
} |