192
main/service/control.c
Normal file
192
main/service/control.c
Normal file
@@ -0,0 +1,192 @@
|
||||
#include "webserver.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
#include "cJSON.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_timer.h"
|
||||
|
||||
static const char *TAG = "CONTROL";
|
||||
|
||||
// --- GPIO 핀 정의 ---
|
||||
#define GPIO_12V_SWITCH CONFIG_GPIO_SW_12V
|
||||
#define GPIO_5V_SWITCH CONFIG_GPIO_SW_5V
|
||||
#define GPIO_POWER_TRIGGER CONFIG_GPIO_TRIGGER_POWER
|
||||
#define GPIO_RESET_TRIGGER CONFIG_GPIO_TRIGGER_RESET
|
||||
|
||||
// --- 상태 변수, 뮤텍스 및 타이머 핸들 ---
|
||||
static bool status_12v_on = false;
|
||||
static bool status_5v_on = false;
|
||||
static SemaphoreHandle_t state_mutex;
|
||||
static esp_timer_handle_t power_trigger_timer;
|
||||
static esp_timer_handle_t reset_trigger_timer;
|
||||
|
||||
/**
|
||||
* @brief 타이머 만료 시 GPIO를 다시 HIGH로 설정하는 콜백 함수
|
||||
*/
|
||||
static void trigger_off_callback(void* arg)
|
||||
{
|
||||
gpio_num_t gpio_pin = (int) arg;
|
||||
gpio_set_level(gpio_pin, 1); // 핀을 다시 HIGH로 복구
|
||||
ESP_LOGI(TAG, "GPIO %d trigger finished.", gpio_pin);
|
||||
}
|
||||
|
||||
static void update_gpio_switches()
|
||||
{
|
||||
gpio_set_level(GPIO_12V_SWITCH, status_12v_on);
|
||||
gpio_set_level(GPIO_5V_SWITCH, status_5v_on);
|
||||
ESP_LOGI(TAG, "Switches updated: 12V=%s, 5V=%s", status_12v_on ? "ON" : "OFF", status_5v_on ? "ON" : "OFF");
|
||||
}
|
||||
|
||||
static esp_err_t control_get_handler(httpd_req_t *req)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
xSemaphoreTake(state_mutex, portMAX_DELAY);
|
||||
cJSON_AddBoolToObject(root, "load_12v_on", status_12v_on);
|
||||
cJSON_AddBoolToObject(root, "load_5v_on", status_5v_on);
|
||||
xSemaphoreGive(state_mutex);
|
||||
|
||||
char *json_string = cJSON_Print(root);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_send(req, json_string, strlen(json_string));
|
||||
|
||||
free(json_string);
|
||||
cJSON_Delete(root);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t control_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[128];
|
||||
int ret, remaining = req->content_len;
|
||||
|
||||
if (remaining >= sizeof(buf)) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "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_408(req);
|
||||
}
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[ret] = '\0';
|
||||
ESP_LOGI(TAG, "Received JSON: %s", buf);
|
||||
|
||||
cJSON *root = cJSON_Parse(buf);
|
||||
if (root == NULL) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON format");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
bool state_changed = false;
|
||||
xSemaphoreTake(state_mutex, portMAX_DELAY);
|
||||
|
||||
cJSON *item_12v = cJSON_GetObjectItem(root, "load_12v_on");
|
||||
if (cJSON_IsBool(item_12v)) {
|
||||
status_12v_on = cJSON_IsTrue(item_12v);
|
||||
state_changed = true;
|
||||
}
|
||||
|
||||
cJSON *item_5v = cJSON_GetObjectItem(root, "load_5v_on");
|
||||
if (cJSON_IsBool(item_5v)) {
|
||||
status_5v_on = cJSON_IsTrue(item_5v);
|
||||
state_changed = true;
|
||||
}
|
||||
|
||||
if (state_changed) {
|
||||
update_gpio_switches();
|
||||
}
|
||||
xSemaphoreGive(state_mutex);
|
||||
|
||||
cJSON *power_trigger = cJSON_GetObjectItem(root, "power_trigger");
|
||||
if (cJSON_IsTrue(power_trigger)) {
|
||||
ESP_LOGI(TAG, "Triggering GPIO %d LOW for 3 seconds...", GPIO_POWER_TRIGGER);
|
||||
gpio_set_level(GPIO_POWER_TRIGGER, 0);
|
||||
esp_timer_stop(power_trigger_timer); // Stop timer if it's already running
|
||||
ESP_ERROR_CHECK(esp_timer_start_once(power_trigger_timer, 3000000)); // 3초
|
||||
}
|
||||
|
||||
cJSON *reset_trigger = cJSON_GetObjectItem(root, "reset_trigger");
|
||||
if (cJSON_IsTrue(reset_trigger)) {
|
||||
ESP_LOGI(TAG, "Triggering GPIO %d LOW for 3 seconds...", GPIO_RESET_TRIGGER);
|
||||
gpio_set_level(GPIO_RESET_TRIGGER, 0);
|
||||
esp_timer_stop(reset_trigger_timer); // Stop timer if it's already running
|
||||
ESP_ERROR_CHECK(esp_timer_start_once(reset_trigger_timer, 3000000)); // 3초
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void control_module_init(void)
|
||||
{
|
||||
state_mutex = xSemaphoreCreateMutex();
|
||||
|
||||
gpio_config_t switch_conf = {
|
||||
.pin_bit_mask = (1ULL << GPIO_12V_SWITCH) | (1ULL << GPIO_5V_SWITCH),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&switch_conf);
|
||||
update_gpio_switches();
|
||||
|
||||
gpio_config_t trigger_conf = {
|
||||
.pin_bit_mask = (1ULL << GPIO_POWER_TRIGGER) | (1ULL << GPIO_RESET_TRIGGER),
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_DISABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE,
|
||||
};
|
||||
gpio_config(&trigger_conf);
|
||||
gpio_set_level(GPIO_POWER_TRIGGER, 1);
|
||||
gpio_set_level(GPIO_RESET_TRIGGER, 1);
|
||||
|
||||
const esp_timer_create_args_t power_timer_args = {
|
||||
.callback = &trigger_off_callback,
|
||||
.arg = (void*) GPIO_POWER_TRIGGER,
|
||||
.name = "power_trigger_off"
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&power_timer_args, &power_trigger_timer));
|
||||
|
||||
const esp_timer_create_args_t reset_timer_args = {
|
||||
.callback = &trigger_off_callback,
|
||||
.arg = (void*) GPIO_RESET_TRIGGER,
|
||||
.name = "reset_trigger_off"
|
||||
};
|
||||
ESP_ERROR_CHECK(esp_timer_create(&reset_timer_args, &reset_trigger_timer));
|
||||
|
||||
ESP_LOGI(TAG, "Control module initialized");
|
||||
}
|
||||
|
||||
void register_control_endpoint(httpd_handle_t server)
|
||||
{
|
||||
control_module_init();
|
||||
httpd_uri_t get_uri = {
|
||||
.uri = "/api/control",
|
||||
.method = HTTP_GET,
|
||||
.handler = control_get_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &get_uri);
|
||||
|
||||
httpd_uri_t post_uri = {
|
||||
.uri = "/api/control",
|
||||
.method = HTTP_POST,
|
||||
.handler = control_post_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &post_uri);
|
||||
|
||||
ESP_LOGI(TAG, "Registered /api/control endpoints (GET, POST)");
|
||||
}
|
||||
175
main/service/datalog.c
Normal file
175
main/service/datalog.c
Normal file
@@ -0,0 +1,175 @@
|
||||
#include "datalog.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include "esp_littlefs.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
static const char* TAG = "DATALOG";
|
||||
static const char* LOG_FILE_PATH = "/littlefs/datalog.csv";
|
||||
static const char* TEMP_LOG_FILE_PATH = "/littlefs/datalog.tmp";
|
||||
#define MAX_LOG_SIZE (1024 * 1024)
|
||||
|
||||
void datalog_init(void)
|
||||
{
|
||||
ESP_LOGI(TAG, "Initializing DataLog with LittleFS");
|
||||
|
||||
esp_vfs_littlefs_conf_t conf = {
|
||||
.base_path = "/littlefs",
|
||||
.partition_label = "littlefs",
|
||||
.format_if_mount_failed = true,
|
||||
.dont_mount = false,
|
||||
};
|
||||
|
||||
esp_err_t ret = esp_vfs_littlefs_register(&conf);
|
||||
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
if (ret == ESP_FAIL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to mount or format filesystem");
|
||||
}
|
||||
else if (ret == ESP_ERR_NOT_FOUND)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to find LittleFS partition");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to initialize LittleFS (%s)", esp_err_to_name(ret));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
size_t total = 0, used = 0;
|
||||
ret = esp_littlefs_info(NULL, &total, &used);
|
||||
if (ret != ESP_OK)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to get LittleFS partition information (%s)", esp_err_to_name(ret));
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Partition size: total: %d, used: %d", total, used);
|
||||
}
|
||||
|
||||
// Check if file exists
|
||||
FILE* f = fopen(LOG_FILE_PATH, "r");
|
||||
if (f == NULL)
|
||||
{
|
||||
ESP_LOGI(TAG, "Log file not found, creating new one.");
|
||||
FILE* f_write = fopen(LOG_FILE_PATH, "w");
|
||||
if (f_write == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create log file.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add header
|
||||
fprintf(f_write, "timestamp,voltage,current,power\n");
|
||||
fclose(f_write);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Log file found.");
|
||||
fclose(f);
|
||||
}
|
||||
}
|
||||
|
||||
void datalog_add(uint32_t timestamp, float voltage, float current, float power)
|
||||
{
|
||||
char new_line[100];
|
||||
int new_line_len = snprintf(new_line, sizeof(new_line), "%lu,%.3f,%.3f,%.3f\n", timestamp, voltage, current, power);
|
||||
|
||||
struct stat st;
|
||||
long size = 0;
|
||||
if (stat(LOG_FILE_PATH, &st) == 0)
|
||||
{
|
||||
size = st.st_size;
|
||||
}
|
||||
|
||||
if (size + new_line_len <= MAX_LOG_SIZE)
|
||||
{
|
||||
FILE* f = fopen(LOG_FILE_PATH, "a");
|
||||
if (f == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to open log file for appending.");
|
||||
return;
|
||||
}
|
||||
fputs(new_line, f);
|
||||
fclose(f);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Log file is full. Rotating log file.");
|
||||
FILE* f_read = fopen(LOG_FILE_PATH, "r");
|
||||
if (f_read == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Could not open log for reading");
|
||||
return;
|
||||
}
|
||||
|
||||
FILE* f_write = fopen(TEMP_LOG_FILE_PATH, "w");
|
||||
if (f_write == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Could not open temp file for writing");
|
||||
fclose(f_read);
|
||||
return;
|
||||
}
|
||||
|
||||
long size_to_remove = (size + new_line_len) - MAX_LOG_SIZE;
|
||||
char line[256];
|
||||
|
||||
// Keep header
|
||||
if (fgets(line, sizeof(line), f_read) != NULL)
|
||||
{
|
||||
fputs(line, f_write);
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "Could not read header");
|
||||
fclose(f_read);
|
||||
fclose(f_write);
|
||||
return;
|
||||
}
|
||||
|
||||
long bytes_skipped = 0;
|
||||
while (fgets(line, sizeof(line), f_read) != NULL)
|
||||
{
|
||||
bytes_skipped += strlen(line);
|
||||
if (bytes_skipped >= size_to_remove)
|
||||
{
|
||||
fputs(line, f_write);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
while (fgets(line, sizeof(line), f_read) != NULL)
|
||||
{
|
||||
fputs(line, f_write);
|
||||
}
|
||||
|
||||
fputs(new_line, f_write);
|
||||
|
||||
fclose(f_read);
|
||||
fclose(f_write);
|
||||
|
||||
if (remove(LOG_FILE_PATH) != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to remove old log file");
|
||||
}
|
||||
else if (rename(TEMP_LOG_FILE_PATH, LOG_FILE_PATH) != 0)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to rename temp file");
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGI(TAG, "Log file rotated successfully.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* datalog_get_path(void)
|
||||
{
|
||||
return LOG_FILE_PATH;
|
||||
}
|
||||
10
main/service/datalog.h
Normal file
10
main/service/datalog.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef MAIN_SERVICE_DATALOG_H_
|
||||
#define MAIN_SERVICE_DATALOG_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void datalog_init(void);
|
||||
void datalog_add(uint32_t timestamp, float voltage, float current, float power);
|
||||
const char* datalog_get_path(void);
|
||||
|
||||
#endif /* MAIN_SERVICE_DATALOG_H_ */
|
||||
135
main/service/monitor.c
Normal file
135
main/service/monitor.c
Normal file
@@ -0,0 +1,135 @@
|
||||
//
|
||||
// Created by shinys on 25. 8. 18..
|
||||
//
|
||||
|
||||
|
||||
#include "monitor.h"
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "cJSON.h"
|
||||
#include "esp_netif.h"
|
||||
#include "esp_wifi_types_generic.h"
|
||||
#include "ina226.h"
|
||||
#include "webserver.h"
|
||||
#include "wifi.h"
|
||||
#include "datalog.h"
|
||||
|
||||
#define INA226_SDA CONFIG_GPIO_INA226_SDA
|
||||
#define INA226_SCL CONFIG_GPIO_INA226_SCL
|
||||
|
||||
ina226_t ina;
|
||||
i2c_master_bus_handle_t bus_handle;
|
||||
i2c_master_dev_handle_t dev_handle;
|
||||
|
||||
// Timer callback function to read sensor data
|
||||
static void sensor_timer_callback(void *arg)
|
||||
{
|
||||
// Generate random sensor data
|
||||
float voltage = 0;
|
||||
float current = 0;
|
||||
float power = 0;
|
||||
|
||||
ina226_get_bus_voltage(&ina, &voltage);
|
||||
ina226_get_power(&ina, &power);
|
||||
ina226_get_current(&ina, ¤t);
|
||||
|
||||
// Get system uptime
|
||||
int64_t uptime_us = esp_timer_get_time();
|
||||
uint32_t uptime_sec = (uint32_t)(uptime_us / 1000000);
|
||||
uint32_t timestamp = (uint32_t)time(NULL);
|
||||
|
||||
datalog_add(timestamp, voltage, current, power);
|
||||
|
||||
// Create JSON object with sensor data
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(root, "type", "sensor_data");
|
||||
cJSON_AddNumberToObject(root, "voltage", voltage);
|
||||
cJSON_AddNumberToObject(root, "current", current);
|
||||
cJSON_AddNumberToObject(root, "power", power);
|
||||
cJSON_AddNumberToObject(root, "timestamp", timestamp);
|
||||
cJSON_AddNumberToObject(root, "uptime_sec", uptime_sec);
|
||||
|
||||
// Push data to WebSocket clients
|
||||
push_data_to_ws(root);
|
||||
}
|
||||
|
||||
static void status_wifi_callback(void *arg)
|
||||
{
|
||||
wifi_ap_record_t ap_info;
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
if (wifi_get_current_ap_info(&ap_info) == ESP_OK) {
|
||||
cJSON_AddStringToObject(root, "type", "wifi_status");
|
||||
cJSON_AddBoolToObject(root, "connected", true);
|
||||
cJSON_AddStringToObject(root, "ssid", (const char *)ap_info.ssid);
|
||||
cJSON_AddNumberToObject(root, "rssi", ap_info.rssi);
|
||||
} else {
|
||||
cJSON_AddBoolToObject(root, "connected", false);
|
||||
}
|
||||
|
||||
push_data_to_ws(root);
|
||||
}
|
||||
|
||||
ina226_config_t ina_config = {
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.i2c_addr = 0x40,
|
||||
.timeout_ms = 100,
|
||||
.averages = INA226_AVERAGES_16,
|
||||
.bus_conv_time = INA226_BUS_CONV_TIME_1100_US,
|
||||
.shunt_conv_time = INA226_SHUNT_CONV_TIME_1100_US,
|
||||
.mode = INA226_MODE_SHUNT_BUS_CONT,
|
||||
.r_shunt = 0.01f,
|
||||
.max_current = 8
|
||||
};
|
||||
|
||||
static void init_ina226()
|
||||
{
|
||||
i2c_master_bus_config_t bus_config = {
|
||||
.i2c_port = I2C_NUM_0,
|
||||
.sda_io_num = (gpio_num_t) INA226_SDA,
|
||||
.scl_io_num = (gpio_num_t) INA226_SCL,
|
||||
.clk_source = I2C_CLK_SRC_DEFAULT,
|
||||
.glitch_ignore_cnt = 7,
|
||||
.flags.enable_internal_pullup = false,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_new_master_bus(&bus_config, &bus_handle));
|
||||
|
||||
i2c_device_config_t dev_config = {
|
||||
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
|
||||
.device_address = 0x40,
|
||||
.scl_speed_hz = 400000,
|
||||
};
|
||||
ESP_ERROR_CHECK(i2c_master_bus_add_device(bus_handle, &dev_config, &dev_handle));
|
||||
|
||||
ESP_ERROR_CHECK(ina226_init(&ina, dev_handle, &ina_config));
|
||||
}
|
||||
|
||||
static esp_timer_handle_t sensor_timer;
|
||||
static esp_timer_handle_t wifi_status_timer;
|
||||
|
||||
void init_status_monitor()
|
||||
{
|
||||
init_ina226();
|
||||
datalog_init();
|
||||
|
||||
// Timer configuration
|
||||
const esp_timer_create_args_t sensor_timer_args = {
|
||||
.callback = &sensor_timer_callback,
|
||||
.name = "sensor_reading_timer" // Optional name for debugging
|
||||
};
|
||||
|
||||
const esp_timer_create_args_t wifi_timer_args = {
|
||||
.callback = &status_wifi_callback,
|
||||
.name = "wifi_status_timer" // Optional name for debugging
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_create(&sensor_timer_args, &sensor_timer));
|
||||
ESP_ERROR_CHECK(esp_timer_create(&wifi_timer_args, &wifi_status_timer));
|
||||
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(sensor_timer, 1000000)); // 1sec
|
||||
ESP_ERROR_CHECK(esp_timer_start_periodic(wifi_status_timer, 1000000 * 5)); // 5s in microseconds
|
||||
}
|
||||
25
main/service/monitor.h
Normal file
25
main/service/monitor.h
Normal file
@@ -0,0 +1,25 @@
|
||||
//
|
||||
// Created by shinys on 25. 8. 18..
|
||||
//
|
||||
|
||||
#ifndef ODROID_REMOTE_HTTP_MONITOR_H
|
||||
#define ODROID_REMOTE_HTTP_MONITOR_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "esp_http_server.h"
|
||||
|
||||
// 버퍼에 저장할 데이터의 개수
|
||||
#define SENSOR_BUFFER_SIZE 100
|
||||
|
||||
// 단일 센서 데이터를 저장하기 위한 구조체
|
||||
typedef struct {
|
||||
float voltage;
|
||||
float current;
|
||||
float power;
|
||||
uint32_t timestamp; // 데이터를 읽은 시간 (부팅 후 ms)
|
||||
} sensor_data_t;
|
||||
|
||||
void init_status_monitor();
|
||||
|
||||
#endif //ODROID_REMOTE_HTTP_MONITOR_H
|
||||
249
main/service/setting.c
Normal file
249
main/service/setting.c
Normal file
@@ -0,0 +1,249 @@
|
||||
#include "webserver.h"
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
#include "nconfig.h"
|
||||
#include "wifi.h"
|
||||
#include "system.h"
|
||||
#include "esp_netif.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
static const char *TAG = "webserver";
|
||||
|
||||
static esp_err_t setting_get_handler(httpd_req_t *req)
|
||||
{
|
||||
wifi_ap_record_t ap_info;
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
char mode_buf[16];
|
||||
if (nconfig_read(WIFI_MODE, mode_buf, sizeof(mode_buf)) == ESP_OK) {
|
||||
cJSON_AddStringToObject(root, "mode", mode_buf);
|
||||
} else {
|
||||
cJSON_AddStringToObject(root, "mode", "sta"); // Default to sta
|
||||
}
|
||||
|
||||
char net_type_buf[16];
|
||||
if (nconfig_read(NETIF_TYPE, net_type_buf, sizeof(net_type_buf)) == ESP_OK) {
|
||||
cJSON_AddStringToObject(root, "net_type", net_type_buf);
|
||||
} else {
|
||||
cJSON_AddStringToObject(root, "net_type", "dhcp"); // Default to dhcp
|
||||
}
|
||||
|
||||
// Add baudrate to the response
|
||||
char baud_buf[16];
|
||||
if (nconfig_read(UART_BAUD_RATE, baud_buf, sizeof(baud_buf)) == ESP_OK) {
|
||||
cJSON_AddStringToObject(root, "baudrate", baud_buf);
|
||||
}
|
||||
|
||||
if (wifi_get_current_ap_info(&ap_info) == ESP_OK) {
|
||||
cJSON_AddBoolToObject(root, "connected", true);
|
||||
cJSON_AddStringToObject(root, "ssid", (const char *)ap_info.ssid);
|
||||
cJSON_AddNumberToObject(root, "rssi", ap_info.rssi);
|
||||
|
||||
esp_netif_ip_info_t ip_info;
|
||||
cJSON* ip_obj = cJSON_CreateObject();
|
||||
if (wifi_get_current_ip_info(&ip_info) == ESP_OK) {
|
||||
char ip_str[16];
|
||||
esp_ip4addr_ntoa(&ip_info.ip, ip_str, sizeof(ip_str));
|
||||
cJSON_AddStringToObject(ip_obj, "ip", ip_str);
|
||||
esp_ip4addr_ntoa(&ip_info.gw, ip_str, sizeof(ip_str));
|
||||
cJSON_AddStringToObject(ip_obj, "gateway", ip_str);
|
||||
esp_ip4addr_ntoa(&ip_info.netmask, ip_str, sizeof(ip_str));
|
||||
cJSON_AddStringToObject(ip_obj, "subnet", ip_str);
|
||||
}
|
||||
|
||||
esp_netif_dns_info_t dns_info;
|
||||
char dns_str[16];
|
||||
if (wifi_get_dns_info(ESP_NETIF_DNS_MAIN, &dns_info) == ESP_OK) {
|
||||
esp_ip4addr_ntoa(&dns_info.ip.u_addr.ip4, dns_str, sizeof(dns_str));
|
||||
cJSON_AddStringToObject(ip_obj, "dns1", dns_str);
|
||||
}
|
||||
if (wifi_get_dns_info(ESP_NETIF_DNS_BACKUP, &dns_info) == ESP_OK) {
|
||||
esp_ip4addr_ntoa(&dns_info.ip.u_addr.ip4, dns_str, sizeof(dns_str));
|
||||
cJSON_AddStringToObject(ip_obj, "dns2", dns_str);
|
||||
}
|
||||
cJSON_AddItemToObject(root, "ip", ip_obj);
|
||||
|
||||
} else {
|
||||
cJSON_AddBoolToObject(root, "connected", false);
|
||||
}
|
||||
|
||||
const char *json_string = cJSON_Print(root);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_send(req, json_string, HTTPD_RESP_USE_STRLEN);
|
||||
cJSON_Delete(root);
|
||||
free((void*)json_string);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t wifi_scan(httpd_req_t *req)
|
||||
{
|
||||
wifi_ap_record_t *ap_records;
|
||||
uint16_t count;
|
||||
|
||||
wifi_scan_aps(&ap_records, &count);
|
||||
|
||||
cJSON *root = cJSON_CreateArray();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
cJSON *ap_obj = cJSON_CreateObject();
|
||||
cJSON_AddStringToObject(ap_obj, "ssid", (const char *)ap_records[i].ssid);
|
||||
cJSON_AddNumberToObject(ap_obj, "rssi", ap_records[i].rssi);
|
||||
cJSON_AddStringToObject(ap_obj, "authmode", auth_mode_str(ap_records[i].authmode));
|
||||
cJSON_AddItemToArray(root, ap_obj);
|
||||
}
|
||||
|
||||
if (count > 0)
|
||||
free(ap_records);
|
||||
|
||||
|
||||
const char *json_string = cJSON_Print(root);
|
||||
httpd_resp_set_type(req, "application/json");
|
||||
httpd_resp_send(req, json_string, HTTPD_RESP_USE_STRLEN);
|
||||
cJSON_Delete(root);
|
||||
free((void*)json_string);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t setting_post_handler(httpd_req_t *req)
|
||||
{
|
||||
char buf[512];
|
||||
int received = httpd_req_recv(req, buf, sizeof(buf) - 1);
|
||||
|
||||
if (received <= 0) {
|
||||
if (received == HTTPD_SOCK_ERR_TIMEOUT) httpd_resp_send_408(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
buf[received] = '\0';
|
||||
|
||||
cJSON *root = cJSON_Parse(buf);
|
||||
if (root == NULL) {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
cJSON *mode_item = cJSON_GetObjectItem(root, "mode");
|
||||
cJSON *net_type_item = cJSON_GetObjectItem(root, "net_type");
|
||||
cJSON *ssid_item = cJSON_GetObjectItem(root, "ssid");
|
||||
cJSON *baud_item = cJSON_GetObjectItem(root, "baudrate");
|
||||
|
||||
if (mode_item && cJSON_IsString(mode_item)) {
|
||||
const char* mode = mode_item->valuestring;
|
||||
ESP_LOGI(TAG, "Received mode switch request: %s", mode);
|
||||
|
||||
if (strcmp(mode, "sta") == 0 || strcmp(mode, "apsta") == 0) {
|
||||
if (strcmp(mode, "apsta") == 0) {
|
||||
cJSON *ap_ssid_item = cJSON_GetObjectItem(root, "ap_ssid");
|
||||
cJSON *ap_pass_item = cJSON_GetObjectItem(root, "ap_password");
|
||||
|
||||
if (ap_ssid_item && cJSON_IsString(ap_ssid_item)) {
|
||||
nconfig_write(AP_SSID, ap_ssid_item->valuestring);
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "AP SSID required for APSTA mode");
|
||||
cJSON_Delete(root);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (ap_pass_item && cJSON_IsString(ap_pass_item)) {
|
||||
nconfig_write(AP_PASSWORD, ap_pass_item->valuestring);
|
||||
} else {
|
||||
nconfig_delete(AP_PASSWORD); // Open network
|
||||
}
|
||||
}
|
||||
|
||||
wifi_switch_mode(mode);
|
||||
httpd_resp_sendstr(req, "{\"status\":\"mode_switch_initiated\"}");
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid mode");
|
||||
}
|
||||
} else if (net_type_item && cJSON_IsString(net_type_item)) {
|
||||
const char* type = net_type_item->valuestring;
|
||||
ESP_LOGI(TAG, "Received network config: %s", type);
|
||||
|
||||
if (strcmp(type, "static") == 0) {
|
||||
cJSON *ip_item = cJSON_GetObjectItem(root, "ip");
|
||||
cJSON *gw_item = cJSON_GetObjectItem(root, "gateway");
|
||||
cJSON *sn_item = cJSON_GetObjectItem(root, "subnet");
|
||||
cJSON *d1_item = cJSON_GetObjectItem(root, "dns1");
|
||||
cJSON *d2_item = cJSON_GetObjectItem(root, "dns2");
|
||||
|
||||
const char* ip = cJSON_IsString(ip_item) ? ip_item->valuestring : NULL;
|
||||
const char* gw = cJSON_IsString(gw_item) ? gw_item->valuestring : NULL;
|
||||
const char* sn = cJSON_IsString(sn_item) ? sn_item->valuestring : NULL;
|
||||
const char* d1 = cJSON_IsString(d1_item) ? d1_item->valuestring : NULL;
|
||||
const char* d2 = cJSON_IsString(d2_item) ? d2_item->valuestring : NULL;
|
||||
|
||||
if (ip && gw && sn && d1) {
|
||||
nconfig_write(NETIF_TYPE, "static");
|
||||
nconfig_write(NETIF_IP, ip);
|
||||
nconfig_write(NETIF_GATEWAY, gw);
|
||||
nconfig_write(NETIF_SUBNET, sn);
|
||||
nconfig_write(NETIF_DNS1, d1);
|
||||
if (d2) nconfig_write(NETIF_DNS2, d2); else nconfig_delete(NETIF_DNS2);
|
||||
|
||||
wifi_use_static(ip, gw, sn, d1, d2);
|
||||
httpd_resp_sendstr(req, "{\"status\":\"static_config_applied\"}");
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Missing static IP fields");
|
||||
}
|
||||
} else if (strcmp(type, "dhcp") == 0) {
|
||||
nconfig_write(NETIF_TYPE, "dhcp");
|
||||
wifi_use_dhcp();
|
||||
httpd_resp_sendstr(req, "{\"status\":\"dhcp_config_applied\"}");
|
||||
}
|
||||
} else if (ssid_item && cJSON_IsString(ssid_item)) {
|
||||
cJSON *pass_item = cJSON_GetObjectItem(root, "password");
|
||||
if (cJSON_IsString(pass_item)) {
|
||||
nconfig_write(WIFI_SSID, ssid_item->valuestring);
|
||||
nconfig_write(WIFI_PASSWORD, pass_item->valuestring);
|
||||
nconfig_write(NETIF_TYPE, "dhcp"); // Default to DHCP on new connection
|
||||
|
||||
httpd_resp_sendstr(req, "{\"status\":\"connection_initiated\"}");
|
||||
wifi_disconnect();
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
wifi_connect();
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Password required");
|
||||
}
|
||||
} else if (baud_item && cJSON_IsString(baud_item)) {
|
||||
const char* baudrate = baud_item->valuestring;
|
||||
ESP_LOGI(TAG, "Received baudrate set request: %s", baudrate);
|
||||
nconfig_write(UART_BAUD_RATE, baudrate);
|
||||
change_baud_rate(strtol(baudrate, NULL, 10));
|
||||
httpd_resp_sendstr(req, "{\"status\":\"baudrate_updated\"}");
|
||||
} else {
|
||||
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid payload");
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void register_wifi_endpoint(httpd_handle_t server)
|
||||
{
|
||||
httpd_uri_t status = {
|
||||
.uri = "/api/setting",
|
||||
.method = HTTP_GET,
|
||||
.handler = setting_get_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &status);
|
||||
|
||||
httpd_uri_t set = {
|
||||
.uri = "/api/setting",
|
||||
.method = HTTP_POST,
|
||||
.handler = setting_post_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &set);
|
||||
|
||||
httpd_uri_t scan = {
|
||||
.uri = "/api/wifi/scan",
|
||||
.method = HTTP_GET,
|
||||
.handler = wifi_scan,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &scan);
|
||||
}
|
||||
102
main/service/webserver.c
Normal file
102
main/service/webserver.c
Normal file
@@ -0,0 +1,102 @@
|
||||
#include "webserver.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sys/param.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_netif.h"
|
||||
#include "driver/uart.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "indicator.h"
|
||||
#include "nconfig.h"
|
||||
#include "monitor.h"
|
||||
#include "wifi.h"
|
||||
#include "datalog.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
static const char *TAG = "WEBSERVER";
|
||||
|
||||
static esp_err_t index_handler(httpd_req_t *req) {
|
||||
extern const unsigned char index_html_start[] asm("_binary_index_html_gz_start");
|
||||
extern const unsigned char index_html_end[] asm("_binary_index_html_gz_end");
|
||||
const size_t index_html_size = (index_html_end - index_html_start);
|
||||
|
||||
httpd_resp_set_hdr(req, "Content-Encoding", "gzip");
|
||||
httpd_resp_set_type(req, "text/html");
|
||||
httpd_resp_send(req, (const char *)index_html_start, index_html_size);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t datalog_download_handler(httpd_req_t *req)
|
||||
{
|
||||
const char *filepath = datalog_get_path();
|
||||
FILE *f = fopen(filepath, "r");
|
||||
if (f == NULL) {
|
||||
ESP_LOGE(TAG, "Failed to open datalog file for reading");
|
||||
httpd_resp_send_404(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
httpd_resp_set_type(req, "text/csv");
|
||||
httpd_resp_set_hdr(req, "Content-Disposition", "attachment; filename=\"datalog.csv\"");
|
||||
|
||||
char buffer[1024];
|
||||
size_t bytes_read;
|
||||
while ((bytes_read = fread(buffer, 1, sizeof(buffer), f)) > 0) {
|
||||
if (httpd_resp_send_chunk(req, buffer, bytes_read) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "File sending failed!");
|
||||
fclose(f);
|
||||
httpd_resp_send_chunk(req, NULL, 0);
|
||||
httpd_resp_send_500(req);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
fclose(f);
|
||||
httpd_resp_send_chunk(req, NULL, 0);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// HTTP 서버 시작
|
||||
void start_webserver(void) {
|
||||
httpd_handle_t server = NULL;
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.stack_size = 1024 * 8;
|
||||
config.max_uri_handlers = 10;
|
||||
|
||||
if (httpd_start(&server, &config) != ESP_OK) {
|
||||
return ;
|
||||
}
|
||||
|
||||
// Index page
|
||||
httpd_uri_t index = {
|
||||
.uri = "/",
|
||||
.method = HTTP_GET,
|
||||
.handler = index_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &index);
|
||||
|
||||
httpd_uri_t datalog_uri = {
|
||||
.uri = "/datalog.csv",
|
||||
.method = HTTP_GET,
|
||||
.handler = datalog_download_handler,
|
||||
.user_ctx = NULL
|
||||
};
|
||||
httpd_register_uri_handler(server, &datalog_uri);
|
||||
|
||||
register_wifi_endpoint(server);
|
||||
register_ws_endpoint(server);
|
||||
register_control_endpoint(server);
|
||||
init_status_monitor();
|
||||
}
|
||||
17
main/service/webserver.h
Normal file
17
main/service/webserver.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by shinys on 25. 8. 18..
|
||||
//
|
||||
|
||||
#ifndef ODROID_REMOTE_HTTP_WEBSERVER_H
|
||||
#define ODROID_REMOTE_HTTP_WEBSERVER_H
|
||||
#include "cJSON.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "system.h"
|
||||
|
||||
void register_wifi_endpoint(httpd_handle_t server);
|
||||
void register_ws_endpoint(httpd_handle_t server);
|
||||
void register_control_endpoint(httpd_handle_t server);
|
||||
void push_data_to_ws(cJSON *data);
|
||||
esp_err_t change_baud_rate(int baud_rate);
|
||||
|
||||
#endif //ODROID_REMOTE_HTTP_WEBSERVER_H
|
||||
178
main/service/ws.c
Normal file
178
main/service/ws.c
Normal file
@@ -0,0 +1,178 @@
|
||||
//
|
||||
// Created by shinys on 25. 8. 18..
|
||||
//
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "webserver.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_http_server.h"
|
||||
#include "esp_log.h"
|
||||
#include "nconfig.h"
|
||||
#include "driver/uart.h"
|
||||
|
||||
#define UART_NUM UART_NUM_1
|
||||
#define BUF_SIZE (4096)
|
||||
#define RD_BUF_SIZE (BUF_SIZE)
|
||||
#define UART_TX_PIN CONFIG_GPIO_UART_TX
|
||||
#define UART_RX_PIN CONFIG_GPIO_UART_RX
|
||||
|
||||
static const char *TAG = "ws-uart";
|
||||
|
||||
static int client_fd = -1;
|
||||
|
||||
struct status_message
|
||||
{
|
||||
cJSON *data;
|
||||
};
|
||||
|
||||
QueueHandle_t status_queue;
|
||||
|
||||
// Status task
|
||||
static void status_task(void *arg)
|
||||
{
|
||||
httpd_handle_t server = (httpd_handle_t)arg;
|
||||
struct status_message msg;
|
||||
|
||||
while (1) {
|
||||
if (xQueueReceive(status_queue, &msg, portMAX_DELAY)) {
|
||||
if (client_fd <= 0) continue;
|
||||
|
||||
char *json_string = cJSON_Print(msg.data);
|
||||
httpd_ws_frame_t ws_pkt;
|
||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||
ws_pkt.payload = (uint8_t *)json_string;
|
||||
ws_pkt.len = strlen(json_string);
|
||||
ws_pkt.type = HTTPD_WS_TYPE_TEXT;
|
||||
esp_err_t err = httpd_ws_send_frame_async(server, client_fd, &ws_pkt);
|
||||
free(json_string);
|
||||
cJSON_Delete(msg.data);
|
||||
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
// try close...
|
||||
httpd_ws_frame_t close_frame = {
|
||||
.final = true,
|
||||
.fragmented = false,
|
||||
.type = HTTPD_WS_TYPE_CLOSE,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
httpd_ws_send_frame_async(server, client_fd, &close_frame);
|
||||
client_fd = -1;
|
||||
}
|
||||
}
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
|
||||
// UART task
|
||||
static void uart_read_task(void *arg) {
|
||||
httpd_handle_t server = (httpd_handle_t)arg;
|
||||
|
||||
uint8_t data[RD_BUF_SIZE];
|
||||
while (1) {
|
||||
int len = uart_read_bytes(UART_NUM, data, RD_BUF_SIZE, 10 / portTICK_PERIOD_MS);
|
||||
if (len > 0 && client_fd != -1) {
|
||||
httpd_ws_frame_t ws_pkt;
|
||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||
ws_pkt.payload = data;
|
||||
ws_pkt.len = len;
|
||||
ws_pkt.type = HTTPD_WS_TYPE_BINARY;
|
||||
|
||||
esp_err_t err = httpd_ws_send_frame_async(server, client_fd, &ws_pkt);
|
||||
if (err != ESP_OK)
|
||||
{
|
||||
// try close...
|
||||
httpd_ws_frame_t close_frame = {
|
||||
.final = true,
|
||||
.fragmented = false,
|
||||
.type = HTTPD_WS_TYPE_CLOSE,
|
||||
.payload = NULL,
|
||||
.len = 0
|
||||
};
|
||||
|
||||
httpd_ws_send_frame_async(server, client_fd, &close_frame);
|
||||
client_fd = -1;
|
||||
}
|
||||
}
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
|
||||
// 웹소켓 처리 핸들러
|
||||
static esp_err_t ws_handler(httpd_req_t *req) {
|
||||
if (req->method == HTTP_GET) {
|
||||
ESP_LOGI(TAG, "Accept websocket connection");
|
||||
client_fd = httpd_req_to_sockfd(req);
|
||||
xQueueReset(status_queue);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
httpd_ws_frame_t ws_pkt;
|
||||
uint8_t buf[BUF_SIZE];
|
||||
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
|
||||
ws_pkt.payload = buf;
|
||||
ws_pkt.type = HTTPD_WS_TYPE_BINARY;
|
||||
|
||||
esp_err_t ret = httpd_ws_recv_frame(req, &ws_pkt, BUF_SIZE);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGI(TAG, "웹소켓 프레임 수신 실패");
|
||||
return ret;
|
||||
}
|
||||
|
||||
uart_write_bytes(UART_NUM, (const char *)ws_pkt.payload, ws_pkt.len);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void register_ws_endpoint(httpd_handle_t server)
|
||||
{
|
||||
size_t baud_rate_len;
|
||||
|
||||
nconfig_get_str_len(UART_BAUD_RATE, &baud_rate_len);
|
||||
char buf[baud_rate_len];
|
||||
nconfig_read(UART_BAUD_RATE, buf, baud_rate_len);
|
||||
|
||||
uart_config_t uart_config = {
|
||||
.baud_rate = strtol(buf, NULL, 10),
|
||||
.data_bits = UART_DATA_8_BITS,
|
||||
.parity = UART_PARITY_DISABLE,
|
||||
.stop_bits = UART_STOP_BITS_1,
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
// .source_clk = UART_SCLK_APB,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(uart_param_config(UART_NUM, &uart_config));
|
||||
ESP_ERROR_CHECK(uart_set_pin(UART_NUM, UART_TX_PIN, UART_RX_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
||||
ESP_ERROR_CHECK(uart_driver_install(UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 0, NULL, ESP_INTR_FLAG_IRAM));
|
||||
|
||||
httpd_uri_t ws = {
|
||||
.uri = "/ws",
|
||||
.method = HTTP_GET,
|
||||
.handler = ws_handler,
|
||||
.user_ctx = NULL,
|
||||
.is_websocket = true
|
||||
};
|
||||
httpd_register_uri_handler(server, &ws);
|
||||
|
||||
status_queue = xQueueCreate(10, sizeof(struct status_message));
|
||||
|
||||
xTaskCreate(uart_read_task, "uart_read_task", 1024*6, server, 8, NULL);
|
||||
xTaskCreate(status_task, "status_task", 4096, server, 7, NULL);
|
||||
}
|
||||
|
||||
void push_data_to_ws(cJSON *data)
|
||||
{
|
||||
struct status_message msg;
|
||||
msg.data = data;
|
||||
if (xQueueSend(status_queue, &msg, 10) != pdPASS)
|
||||
{
|
||||
ESP_LOGW(TAG, "Queue full");
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t change_baud_rate(int baud_rate)
|
||||
{
|
||||
return uart_set_baudrate(UART_NUM, baud_rate);
|
||||
}
|
||||
Reference in New Issue
Block a user