integrate tim's changes
This commit is contained in:
@@ -23,6 +23,11 @@
|
|||||||
<td></td>
|
<td></td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr><td colspan="4">
|
<tr><td colspan="4">
|
||||||
|
<tr>
|
||||||
|
<td>-</td>
|
||||||
|
<td>Battery Voltage</td>
|
||||||
|
<td> <input readonly="" id="voltage"/> </td>
|
||||||
|
</tr>
|
||||||
<button id="commit_btn" onclick="commit_params()" disabled>Save Changes</button>
|
<button id="commit_btn" onclick="commit_params()" disabled>Save Changes</button>
|
||||||
</td></tr>
|
</td></tr>
|
||||||
</table>
|
</table>
|
||||||
@@ -45,8 +50,8 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
let param_values = [];
|
let param_values = [];
|
||||||
const param_names = ["Drive Distance", "TPDF", "Efuse Amt", "Gain", "Offset"];
|
let param_names = [];
|
||||||
const param_units = ["in", "ft", "in", "V", "ms"];
|
const param_units = ["ms", "Per Day", "time", "time", "feet", "inches", "", "", "", "", "","","","","","","","","Amps","Amps","Amps","Amps","Amps","Seconds","","","","Volts","Seconds","Volts","Seconds","Seconds","uSeconds","Volts"];
|
||||||
|
|
||||||
function ge(x) { return document.getElementById(x); }
|
function ge(x) { return document.getElementById(x); }
|
||||||
|
|
||||||
@@ -72,8 +77,12 @@
|
|||||||
ge('in_time').value = date;
|
ge('in_time').value = date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ge('voltage').value = data.battery;
|
||||||
|
|
||||||
|
|
||||||
// Store values (default to empty array if missing)
|
// Store values (default to empty array if missing)
|
||||||
param_values = data.params || [];
|
param_values = data.values || [];
|
||||||
|
param_names = data.names || [];
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error("Error parsing JSON", e);
|
console.error("Error parsing JSON", e);
|
||||||
}
|
}
|
||||||
@@ -97,6 +106,10 @@
|
|||||||
param_names.forEach((name, i) => {
|
param_names.forEach((name, i) => {
|
||||||
let row = table.insertRow(table.rows.length - 1);
|
let row = table.insertRow(table.rows.length - 1);
|
||||||
|
|
||||||
|
let name =(param_names[i] !== undefined && param_names[i] !==null)
|
||||||
|
? param_names[i]
|
||||||
|
: "null";
|
||||||
|
|
||||||
// If the server didn't send a value for this index, show "null"
|
// If the server didn't send a value for this index, show "null"
|
||||||
let val = (param_values[i] !== undefined && param_values[i] !== null)
|
let val = (param_values[i] !== undefined && param_values[i] !== null)
|
||||||
? param_values[i]
|
? param_values[i]
|
||||||
@@ -132,7 +145,7 @@
|
|||||||
// If the user typed "null", we send null; otherwise parse as float
|
// If the user typed "null", we send null; otherwise parse as float
|
||||||
const val = (input.value.toLowerCase() === "null") ? null : parseFloat(input.value);
|
const val = (input.value.toLowerCase() === "null") ? null : parseFloat(input.value);
|
||||||
|
|
||||||
xhr.open("POST", "/sp", true);
|
xhr.open("POST", "http://192.168.4.1/sp", true);
|
||||||
xhr.setRequestHeader("Content-Type", "application/json");
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||||||
xhr.onload = function() {
|
xhr.onload = function() {
|
||||||
if (xhr.status === 200) {
|
if (xhr.status === 200) {
|
||||||
|
|||||||
10
main/main.c
10
main/main.c
@@ -83,20 +83,20 @@ void driveLEDs(led_state_t state) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case LED_STATE_BOOTING:
|
case LED_STATE_BOOTING:
|
||||||
i2c_set_led1(1);
|
i2c_set_led1(0b001);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LED_STATE_START1:
|
case LED_STATE_START1:
|
||||||
i2c_set_led1(0);
|
i2c_set_led1(0b000);
|
||||||
break;
|
break;
|
||||||
case LED_STATE_START2:
|
case LED_STATE_START2:
|
||||||
i2c_set_led1(1);
|
i2c_set_led1(0b001);
|
||||||
break;
|
break;
|
||||||
case LED_STATE_START3:
|
case LED_STATE_START3:
|
||||||
i2c_set_led1(3);
|
i2c_set_led1(0b011);
|
||||||
break;
|
break;
|
||||||
case LED_STATE_START4:
|
case LED_STATE_START4:
|
||||||
i2c_set_led1(7);
|
i2c_set_led1(0b111);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
103
main/webserver.c
103
main/webserver.c
@@ -7,8 +7,10 @@
|
|||||||
CONDITIONS OF ANY KIND, either express or implied.
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "cJSON.h"
|
||||||
#include "esp_ota_ops.h"
|
#include "esp_ota_ops.h"
|
||||||
#include "esp_timer.h"
|
#include "esp_timer.h"
|
||||||
|
#include "power_mgmt.h"
|
||||||
#include "rtc.h"
|
#include "rtc.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
@@ -149,13 +151,96 @@ static esp_err_t st_post_handler(httpd_req_t *req) {
|
|||||||
|
|
||||||
// set parameters id & value
|
// set parameters id & value
|
||||||
static esp_err_t sp_post_handler(httpd_req_t *req) {
|
static esp_err_t sp_post_handler(httpd_req_t *req) {
|
||||||
ESP_LOGI(TAG, "sp_post_handler");
|
char content[128]; // Buffer for incoming JSON
|
||||||
// Send the HTML response
|
size_t recv_size = (req->content_len < sizeof(content)) ? req->content_len : sizeof(content) - 1;
|
||||||
httpd_resp_set_type(req, "text/html");
|
|
||||||
return httpd_resp_send(req, "/sp NOT IMPLEMENTED", HTTPD_RESP_USE_STRLEN);
|
// 1. Receive the data
|
||||||
|
int ret = httpd_req_recv(req, content, recv_size);
|
||||||
|
if (ret <= 0) {
|
||||||
|
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
|
||||||
|
httpd_resp_send_408(req);
|
||||||
|
}
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
content[ret] = '\0'; // Null-terminate the string
|
||||||
|
|
||||||
|
// 2. Parse the JSON
|
||||||
|
cJSON *root = cJSON_Parse(content);
|
||||||
|
if (root == NULL) {
|
||||||
|
ESP_LOGE(TAG, "Failed to parse JSON: %s", content);
|
||||||
|
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON");
|
||||||
|
return ESP_FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Extract the ID and Value
|
||||||
|
cJSON *id_item = cJSON_GetObjectItem(root, "id");
|
||||||
|
cJSON *val_item = cJSON_GetObjectItem(root, "value");
|
||||||
|
|
||||||
|
int param_id = -1;
|
||||||
|
//char param_idstring[32] = "";
|
||||||
|
|
||||||
|
if (cJSON_IsNumber(id_item)) {
|
||||||
|
param_id = id_item->valueint;
|
||||||
|
}
|
||||||
|
if (cJSON_IsString(id_item)){
|
||||||
|
//param_idstring = id_item->valuestring;
|
||||||
|
for (uint8_t i=0; i<NUM_PARAMS; i++) {
|
||||||
|
if (strcmp(id_item->valuestring, get_param_name(i))) {
|
||||||
|
param_id = i;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
double param_val = cJSON_IsNumber(val_item) ? val_item->valuedouble : 0.0f;
|
||||||
|
|
||||||
|
ESP_LOGI(TAG, "Updating Param ID: %d to Value: %.2f", param_id, param_val);
|
||||||
|
|
||||||
|
switch(get_param_type(param_id)) {
|
||||||
|
case PARAM_TYPE_u8:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.u8 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_i8:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.i8 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_u16:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.u16 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_i16:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.i16 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_u32:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.u32 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_i32:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.i32 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_u64:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.u64 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_i64:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.i64 = round(param_val)});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_f32:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.f32 = param_val});
|
||||||
|
break;
|
||||||
|
case PARAM_TYPE_f64:
|
||||||
|
set_param_value_t(param_id, (param_value_t){.f64 = param_val});
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
ESP_LOGW(TAG, "Unknown parameter type for ID %d", param_id);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_Delete(root);
|
||||||
|
|
||||||
|
// 5. Send Success Response
|
||||||
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
return httpd_resp_send(req, "{\"status\":\"ok\"}", HTTPD_RESP_USE_STRLEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static esp_err_t move_post_handler(httpd_req_t *req) {
|
static esp_err_t move_post_handler(httpd_req_t *req) {
|
||||||
ESP_LOGI(TAG, "move_post_handler");
|
ESP_LOGI(TAG, "move_post_handler");
|
||||||
// Send the HTML response
|
// Send the HTML response
|
||||||
@@ -182,7 +267,7 @@ static esp_err_t status_get_handler(httpd_req_t *req) {
|
|||||||
httpd_resp_set_type(req, "application/json");
|
httpd_resp_set_type(req, "application/json");
|
||||||
|
|
||||||
// Start building the JSON string with time
|
// Start building the JSON string with time
|
||||||
head += sprintf(httpBuffer+head, "{\"time\":%lld,\"params\":[", system_rtc_get_raw_time());
|
head += sprintf(httpBuffer+head, "{\"time\":%lld,\" battery\":%f,\" values\":[", system_rtc_get_raw_time(), get_battery_V());
|
||||||
|
|
||||||
for (param_idx_t i = 0; i < NUM_PARAMS; i++) {
|
for (param_idx_t i = 0; i < NUM_PARAMS; i++) {
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
@@ -207,6 +292,14 @@ static esp_err_t status_get_handler(httpd_req_t *req) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
head += sprintf(httpBuffer+head, "], \"names\":[");
|
||||||
|
for (param_idx_t i = 0; i < NUM_PARAMS; i++) {
|
||||||
|
if (i > 0) {
|
||||||
|
head += sprintf(httpBuffer+head, ",");
|
||||||
|
}
|
||||||
|
head += sprintf(httpBuffer+head, "\"%s\"", get_param_name(i));
|
||||||
|
}
|
||||||
|
|
||||||
// Close the JSON array and object
|
// Close the JSON array and object
|
||||||
head += sprintf(httpBuffer+head, "]}");
|
head += sprintf(httpBuffer+head, "]}");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user