Web control, added config 16MB flash
This commit is contained in:
@ -7,29 +7,31 @@
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/ledc.h"
|
||||
#include "cJSON.h"
|
||||
|
||||
// WiFi credentials - CHANGE THESE TO YOUR NETWORK
|
||||
#define WIFI_SSID "YOUR_WIFI_NAME"
|
||||
#define WIFI_PASS "YOUR_WIFI_PASSWORD"
|
||||
#define WIFI_SSID "GL-AXT1800-0c2"
|
||||
#define WIFI_PASS "CR7W25FM8S"
|
||||
#define WIFI_MAXIMUM_RETRY 5
|
||||
|
||||
// Pin definitions (same as before)
|
||||
// Pin definitions
|
||||
#define LED_PIN GPIO_NUM_13
|
||||
#define MOTOR_R_EN GPIO_NUM_18
|
||||
#define MOTOR_L_EN GPIO_NUM_19
|
||||
#define PWM_R_PIN GPIO_NUM_21
|
||||
#define PWM_L_PIN GPIO_NUM_22
|
||||
|
||||
// PWM configuration (same as before)
|
||||
// PWM configuration
|
||||
#define PWM_FREQUENCY 1000
|
||||
#define PWM_RESOLUTION LEDC_TIMER_8_BIT
|
||||
#define PWM_R_CHANNEL LEDC_CHANNEL_0
|
||||
#define PWM_L_CHANNEL LEDC_CHANNEL_1
|
||||
|
||||
static const char* TAG = "WIFI_MOTOR";
|
||||
static const char* TAG = "HTTP_MOTOR";
|
||||
|
||||
// WiFi event group
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
@ -38,7 +40,7 @@ static EventGroupHandle_t s_wifi_event_group;
|
||||
|
||||
static int s_retry_num = 0;
|
||||
|
||||
// Motor control (same as before)
|
||||
// Motor control
|
||||
typedef enum {
|
||||
MOTOR_OFF,
|
||||
MOTOR_EXHAUST,
|
||||
@ -48,7 +50,119 @@ typedef enum {
|
||||
static motor_mode_t current_mode = MOTOR_OFF;
|
||||
static int current_speed = 0;
|
||||
|
||||
// WiFi event handler
|
||||
// HTTP server handle
|
||||
static httpd_handle_t server = NULL;
|
||||
|
||||
// HTML web page for control
|
||||
static const char* html_page =
|
||||
"<!DOCTYPE html>"
|
||||
"<html>"
|
||||
"<head>"
|
||||
" <title>Maxxfan Controller</title>"
|
||||
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
|
||||
" <style>"
|
||||
" body { font-family: Arial, sans-serif; margin: 40px; background: #f0f0f0; }"
|
||||
" .container { max-width: 500px; margin: 0 auto; background: white; padding: 30px; border-radius: 10px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }"
|
||||
" h1 { color: #333; text-align: center; }"
|
||||
" .control-group { margin: 20px 0; padding: 20px; border: 1px solid #ddd; border-radius: 5px; }"
|
||||
" .control-group h3 { margin-top: 0; color: #555; }"
|
||||
" button { padding: 15px 25px; margin: 5px; border: none; border-radius: 5px; cursor: pointer; font-size: 16px; }"
|
||||
" .btn-off { background: #f44336; color: white; }"
|
||||
" .btn-exhaust { background: #ff9800; color: white; }"
|
||||
" .btn-intake { background: #4CAF50; color: white; }"
|
||||
" .btn-off:hover { background: #d32f2f; }"
|
||||
" .btn-exhaust:hover { background: #f57c00; }"
|
||||
" .btn-intake:hover { background: #388e3c; }"
|
||||
" .speed-control { margin: 20px 0; }"
|
||||
" .speed-slider { width: 100%; height: 40px; }"
|
||||
" .status { background: #e3f2fd; padding: 15px; border-radius: 5px; margin: 20px 0; }"
|
||||
" .status h4 { margin: 0 0 10px 0; color: #1976d2; }"
|
||||
" </style>"
|
||||
"</head>"
|
||||
"<body>"
|
||||
" <div class=\"container\">"
|
||||
" <h1>Maxxfan Controller</h1>"
|
||||
" "
|
||||
" <div class=\"status\">"
|
||||
" <h4>Current Status</h4>"
|
||||
" <p><strong>Mode:</strong> <span id=\"mode\">OFF</span></p>"
|
||||
" <p><strong>Speed:</strong> <span id=\"speed\">0</span>%</p>"
|
||||
" </div>"
|
||||
" "
|
||||
" <div class=\"control-group\">"
|
||||
" <h3>Fan Control</h3>"
|
||||
" <button class=\"btn-off\" onclick=\"setFan('off', 0)\">Turn OFF</button>"
|
||||
" <button class=\"btn-exhaust\" onclick=\"setFan('exhaust', 50)\">Exhaust (50%)</button>"
|
||||
" <button class=\"btn-intake\" onclick=\"setFan('intake', 50)\">Intake (50%)</button>"
|
||||
" </div>"
|
||||
" "
|
||||
" <div class=\"control-group\">"
|
||||
" <h3>Speed Control</h3>"
|
||||
" <div class=\"speed-control\">"
|
||||
" <label for=\"speedSlider\">Speed: <span id=\"speedValue\">50</span>%</label><br>"
|
||||
" <input type=\"range\" id=\"speedSlider\" class=\"speed-slider\" min=\"0\" max=\"100\" value=\"50\" oninput=\"updateSpeed(this.value)\">"
|
||||
" </div>"
|
||||
" <button class=\"btn-exhaust\" onclick=\"setFanSpeed('exhaust')\">Set Exhaust Speed</button>"
|
||||
" <button class=\"btn-intake\" onclick=\"setFanSpeed('intake')\">Set Intake Speed</button>"
|
||||
" </div>"
|
||||
" </div>"
|
||||
""
|
||||
" <script>"
|
||||
" let currentSpeed = 50;"
|
||||
" "
|
||||
" function updateSpeed(value) {"
|
||||
" currentSpeed = parseInt(value);"
|
||||
" document.getElementById('speedValue').textContent = value;"
|
||||
" }"
|
||||
" "
|
||||
" function setFan(mode, speed) {"
|
||||
" currentSpeed = speed;"
|
||||
" document.getElementById('speedSlider').value = speed;"
|
||||
" document.getElementById('speedValue').textContent = speed;"
|
||||
" "
|
||||
" fetch('/fan', {"
|
||||
" method: 'POST',"
|
||||
" headers: { 'Content-Type': 'application/json' },"
|
||||
" body: JSON.stringify({ mode: mode, speed: parseInt(speed) })"
|
||||
" })"
|
||||
" .then(response => response.json())"
|
||||
" .then(data => updateStatus(data))"
|
||||
" .catch(error => console.error('Error:', error));"
|
||||
" }"
|
||||
" "
|
||||
" function setFanSpeed(mode) {"
|
||||
" fetch('/fan', {"
|
||||
" method: 'POST',"
|
||||
" headers: { 'Content-Type': 'application/json' },"
|
||||
" body: JSON.stringify({ mode: mode, speed: parseInt(currentSpeed) })"
|
||||
" })"
|
||||
" .then(response => response.json())"
|
||||
" .then(data => updateStatus(data))"
|
||||
" .catch(error => console.error('Error:', error));"
|
||||
" }"
|
||||
" "
|
||||
" function updateStatus(data) {"
|
||||
" document.getElementById('mode').textContent = data.mode.toUpperCase();"
|
||||
" document.getElementById('speed').textContent = data.speed;"
|
||||
" }"
|
||||
" "
|
||||
" function getStatus() {"
|
||||
" fetch('/status')"
|
||||
" .then(response => response.json())"
|
||||
" .then(data => updateStatus(data))"
|
||||
" .catch(error => console.error('Error:', error));"
|
||||
" }"
|
||||
" "
|
||||
" // Update status every 2 seconds"
|
||||
" setInterval(getStatus, 2000);"
|
||||
" "
|
||||
" // Get initial status"
|
||||
" getStatus();"
|
||||
" </script>"
|
||||
"</body>"
|
||||
"</html>";
|
||||
|
||||
// WiFi event handler (same as before)
|
||||
static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void* event_data)
|
||||
{
|
||||
@ -71,66 +185,6 @@ static void event_handler(void* arg, esp_event_base_t event_base,
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_init_sta(void)
|
||||
{
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {
|
||||
.capable = true,
|
||||
.required = false
|
||||
},
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
|
||||
/* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum
|
||||
* number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s", WIFI_SSID);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s", WIFI_SSID);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
}
|
||||
|
||||
void configure_gpio_pins(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Configuring GPIO pins...");
|
||||
@ -230,42 +284,203 @@ void set_motor_speed(motor_mode_t mode, int speed_percent)
|
||||
}
|
||||
}
|
||||
|
||||
// Simple demo task that runs once WiFi is connected
|
||||
void motor_demo_task(void *pvParameters)
|
||||
// HTTP handler for the main web page
|
||||
static esp_err_t root_get_handler(httpd_req_t *req)
|
||||
{
|
||||
ESP_LOGI(TAG, "Motor demo task started - waiting for WiFi...");
|
||||
httpd_resp_set_type(req, "text/html");
|
||||
httpd_resp_send(req, html_page, HTTPD_RESP_USE_STRLEN);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// HTTP handler for fan status (GET /status)
|
||||
static esp_err_t status_get_handler(httpd_req_t *req)
|
||||
{
|
||||
cJSON *json = cJSON_CreateObject();
|
||||
|
||||
const char* mode_str = "off";
|
||||
if (current_mode == MOTOR_EXHAUST) mode_str = "exhaust";
|
||||
else if (current_mode == MOTOR_INTAKE) mode_str = "intake";
|
||||
|
||||
cJSON_AddStringToObject(json, "mode", mode_str);
|
||||
cJSON_AddNumberToObject(json, "speed", current_speed);
|
||||
|
||||
char *json_string = cJSON_Print(json);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_send(req, json_string, strlen(json_string));
|
||||
|
||||
free(json_string);
|
||||
cJSON_Delete(json);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// HTTP handler for fan control (POST /fan)
|
||||
static esp_err_t fan_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[100];
|
||||
int ret, remaining = req->content_len;
|
||||
|
||||
if (remaining >= sizeof(buf)) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Content too long");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ret = httpd_req_recv(req, buf, remaining);
|
||||
if (ret <= 0) {
|
||||
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
||||
httpd_resp_send_err(req, HTTPD_408_REQ_TIMEOUT, "Request timeout");
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[ret] = '\0';
|
||||
|
||||
cJSON *json = cJSON_Parse(buf);
|
||||
if (json == NULL) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *mode_json = cJSON_GetObjectItem(json, "mode");
|
||||
cJSON *speed_json = cJSON_GetObjectItem(json, "speed");
|
||||
|
||||
if (!cJSON_IsString(mode_json) || (!cJSON_IsNumber(speed_json) && !cJSON_IsString(speed_json))) {
|
||||
ESP_LOGE(TAG, "JSON parsing failed - mode: %s, speed: %s",
|
||||
mode_json ? (cJSON_IsString(mode_json) ? mode_json->valuestring : "not_string") : "null",
|
||||
speed_json ? (cJSON_IsNumber(speed_json) ? "number" : (cJSON_IsString(speed_json) ? speed_json->valuestring : "not_number_or_string")) : "null");
|
||||
cJSON_Delete(json);
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing mode or speed");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
const char* mode_str = mode_json->valuestring;
|
||||
int speed;
|
||||
|
||||
// Handle both number and string speed values
|
||||
if (cJSON_IsNumber(speed_json)) {
|
||||
speed = (int)speed_json->valuedouble;
|
||||
} else if (cJSON_IsString(speed_json)) {
|
||||
speed = atoi(speed_json->valuestring);
|
||||
} else {
|
||||
speed = 0;
|
||||
}
|
||||
|
||||
motor_mode_t mode = MOTOR_OFF;
|
||||
if (strcmp(mode_str, "exhaust") == 0) {
|
||||
mode = MOTOR_EXHAUST;
|
||||
} else if (strcmp(mode_str, "intake") == 0) {
|
||||
mode = MOTOR_INTAKE;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "HTTP Request: mode=%s, speed=%d", mode_str, speed);
|
||||
set_motor_speed(mode, speed);
|
||||
|
||||
cJSON_Delete(json);
|
||||
|
||||
// Send response
|
||||
return status_get_handler(req);
|
||||
}
|
||||
|
||||
// Start HTTP server
|
||||
static httpd_handle_t start_webserver(void)
|
||||
{
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.max_uri_handlers = 10;
|
||||
|
||||
ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
|
||||
if (httpd_start(&server, &config) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Registering URI handlers");
|
||||
|
||||
httpd_uri_t root = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = root_get_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &root);
|
||||
|
||||
httpd_uri_t status = {
|
||||
.uri = "/status",
|
||||
.method = HTTP_GET,
|
||||
.handler = status_get_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &status);
|
||||
|
||||
httpd_uri_t fan = {
|
||||
.uri = "/fan",
|
||||
.method = HTTP_POST,
|
||||
.handler = fan_post_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &fan);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Error starting server!");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void wifi_init_sta(void)
|
||||
{
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_create_default_wifi_sta();
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
|
||||
ESP_EVENT_ANY_ID,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_any_id));
|
||||
ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT,
|
||||
IP_EVENT_STA_GOT_IP,
|
||||
&event_handler,
|
||||
NULL,
|
||||
&instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = {
|
||||
.sta = {
|
||||
.ssid = WIFI_SSID,
|
||||
.password = WIFI_PASS,
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK,
|
||||
.pmf_cfg = {
|
||||
.capable = true,
|
||||
.required = false
|
||||
},
|
||||
},
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
|
||||
// Wait for WiFi connection
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT,
|
||||
pdFALSE,
|
||||
pdFALSE,
|
||||
portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "WiFi connected! Starting motor demo...");
|
||||
|
||||
while(1) {
|
||||
ESP_LOGI(TAG, "=== WiFi Motor Demo Sequence ===");
|
||||
|
||||
// Short demo since we have WiFi now
|
||||
set_motor_speed(MOTOR_EXHAUST, 50);
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
|
||||
set_motor_speed(MOTOR_INTAKE, 75);
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
|
||||
set_motor_speed(MOTOR_OFF, 0);
|
||||
vTaskDelay(pdMS_TO_TICKS(5000));
|
||||
}
|
||||
ESP_LOGI(TAG, "connected to ap SSID:%s", WIFI_SSID);
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID:%s", WIFI_SSID);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Starting WiFi + Motor Control!");
|
||||
ESP_LOGI(TAG, "Starting Maxxfan HTTP Controller!");
|
||||
|
||||
// Initialize NVS (needed for WiFi)
|
||||
// Initialize 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());
|
||||
@ -280,8 +495,10 @@ void app_main(void)
|
||||
ESP_LOGI(TAG, "Connecting to WiFi network: %s", WIFI_SSID);
|
||||
wifi_init_sta();
|
||||
|
||||
// Create motor demo task
|
||||
xTaskCreate(motor_demo_task, "motor_demo", 4096, NULL, 5, NULL);
|
||||
// Start HTTP server
|
||||
start_webserver();
|
||||
|
||||
ESP_LOGI(TAG, "Setup complete! Motor controllable via WiFi.");
|
||||
ESP_LOGI(TAG, "=== Maxxfan Controller Ready! ===");
|
||||
ESP_LOGI(TAG, "Open your browser and go to: http://[ESP32_IP_ADDRESS]");
|
||||
ESP_LOGI(TAG, "Check the monitor output above for your IP address");
|
||||
}
|
||||
Reference in New Issue
Block a user