Skip to content

Commit

Permalink
bug fixes for ws logs and restart request
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Wilson committed May 26, 2024
1 parent a87f1e9 commit 00d5a73
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"idf.flashType": "UART",
"idf.portWin": "COM4",
"idf.portWin": "COM3",
"idf.adapterTargetName": "esp32s3",
"idf.openOcdConfigs": [
"interface/ftdi/esp32_devkitj_v1.cfg",
Expand Down
3 changes: 2 additions & 1 deletion config-401.cvs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
key,type,encoding,value
main,namespace,,
hostname,data,string,bitaxe
wifissid,data,string,myssid
wifipass,data,string,mypass
stratumurl,data,string,public-pool.io
stratumport,data,u16,21496
stratumuser,data,string,bc1q99n3pu025yyu0jlywpmwzalyhm36tg5u37w20d.bitaxe
stratumuser,data,string,bc1qnp980s5fpp8l94p5cvttmtdqy8rvrq74qly2yrfmzkdsntqzlc5qkc4rkq.bitaxe
stratumpass,data,string,x
asicfrequency,data,u16,490
asicvoltage,data,u16,1166
Expand Down
5 changes: 3 additions & 2 deletions config.cvs.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ wifissid,data,string,myssid
wifipass,data,string,mypass
stratumurl,data,string,public-pool.io
stratumport,data,u16,21496
stratumuser,data,string,1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa.bitaxe
stratumuser,data,string,bc1qnp980s5fpp8l94p5cvttmtdqy8rvrq74qly2yrfmzkdsntqzlc5qkc4rkq.bitaxe
stratumpass,data,string,x
asicfrequency,data,u16,485
asicvoltage,data,u16,1200
Expand All @@ -15,4 +15,5 @@ boardversion,data,string,204
flipscreen,data,u16,1
invertfanpol,data,u16,1
autofanspeed,data,u16,1
fanspeed,data,u16,100
fanspeed,data,u16,100
selftest,data,u16,1
47 changes: 44 additions & 3 deletions main/http_server/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,18 @@ static esp_err_t PATCH_update_settings(httpd_req_t * req)
static esp_err_t POST_restart(httpd_req_t * req)
{
ESP_LOGI(TAG, "Restarting System because of API Request");

// Send HTTP response before restarting
const char* resp_str = "System will restart shortly.";
httpd_resp_send(req, resp_str, HTTPD_RESP_USE_STRLEN);

// Delay to ensure the response is sent
vTaskDelay(1000 / portTICK_PERIOD_MS);

// Restart the system
esp_restart();

// This return statement will never be reached, but it's good practice to include it
return ESP_OK;
}

Expand Down Expand Up @@ -483,19 +493,50 @@ esp_err_t POST_OTA_update(httpd_req_t * req)

void log_to_websocket(const char * format, va_list args)
{
char * log_buffer = (char *) malloc(2048);
vsnprintf(log_buffer, 2048, format, args);
va_list args_copy;
va_copy(args_copy, args);

// Calculate the required buffer size
int needed_size = vsnprintf(NULL, 0, format, args_copy) + 1;
va_end(args_copy);

// Allocate the buffer dynamically
char * log_buffer = (char *) malloc(needed_size);
if (log_buffer == NULL) {
// Handle allocation failure
return;
}

// Format the string into the allocated buffer
va_copy(args_copy, args);
vsnprintf(log_buffer, needed_size, format, args_copy);
va_end(args_copy);

// Prepare the WebSocket frame
httpd_ws_frame_t ws_pkt;
memset(&ws_pkt, 0, sizeof(httpd_ws_frame_t));
ws_pkt.payload = (uint8_t *) log_buffer;
ws_pkt.len = strlen(log_buffer);
ws_pkt.type = HTTPD_WS_TYPE_TEXT;
vprintf(format, args);

// Print to standard output
va_copy(args_copy, args);
vprintf(format, args_copy);
va_end(args_copy);

// Ensure server and fd are valid
if (server == NULL || fd < 0) {
// Handle invalid server or socket descriptor
free(log_buffer);
return;
}

// Send the WebSocket frame asynchronously
if (httpd_ws_send_frame_async(server, fd, &ws_pkt) != ESP_OK) {
esp_log_set_vprintf(vprintf);
}

// Free the allocated buffer
free(log_buffer);
}

Expand Down

0 comments on commit 00d5a73

Please sign in to comment.