This commit is contained in:
parent
23cb4a558b
commit
f4d1b53760
4 changed files with 455 additions and 5 deletions
|
@ -2,9 +2,6 @@ PROGRAM=fiatlux
|
|||
|
||||
EXTRA_CFLAGS=-O3 -Ifsdata
|
||||
|
||||
#Enable debugging
|
||||
#EXTRA_CFLAGS+=-DLWIP_DEBUG=1 -DHTTPD_DEBUG=LWIP_DBG_ON
|
||||
|
||||
EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/mbedtls extras/httpd extras/sntp extras/cpp_support
|
||||
|
||||
LIBS = hal m
|
||||
|
|
|
@ -7,9 +7,232 @@
|
|||
<link rel="stylesheet" href="css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h2>static web page</h2>
|
||||
<nav>
|
||||
<a href="#" class="brand">
|
||||
<span>fiatlux v0.2</span>
|
||||
<span class="label warning" id="status_box">Loading...</span>
|
||||
</a>
|
||||
</nav>
|
||||
<main data-page="dashboard" id="page">
|
||||
<section id="dashboard">
|
||||
<h2>Status</h2>
|
||||
<div class="flex">
|
||||
<div>
|
||||
<article class="card">
|
||||
<header>
|
||||
<h3>System</h3>
|
||||
</header>
|
||||
<div class="table">
|
||||
<div class="row">
|
||||
<span>Chip ID</span>
|
||||
<span><span class="postfill_chipid">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Hostname</span>
|
||||
<span><span class="postfill_hostname">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Firmware Version</span>
|
||||
<span>N/A</span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Flash ID</span>
|
||||
<span><span class="postfill_flashid">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Flash size</span>
|
||||
<span><span class="postfill_flashsize">N/A</span> KiB</span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Free heap</span>
|
||||
<span><span class="postfill_heap">N/A</span> bytes</span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Uptime</span>
|
||||
<span><span class="postfill_uptime">N/A</span> s</span>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
<div>
|
||||
<article class="card">
|
||||
<header>
|
||||
<h3>Network <span class="label success postfill_clientip">current connection</span></h3>
|
||||
</header>
|
||||
<div class="table">
|
||||
<div class="row">
|
||||
<span>Mode</span>
|
||||
<span><span class="postfill_opmode">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Station SSID</span>
|
||||
<span><span class="postfill_stassid">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Station IP</span>
|
||||
<span><span class="postfill_staip">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Station MAC</span>
|
||||
<span><span class="postfill_stamac">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>AP SSID</span>
|
||||
<span><span class="postfill_apssid">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>AP IP</span>
|
||||
<span><span class="postfill_apip">N/A</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>AP MAC</span>
|
||||
<span><span class="postfill_apmac">N/A</span></span>
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
<div>
|
||||
<article class="card">
|
||||
<header>
|
||||
<h3>Power</h3>
|
||||
</header>
|
||||
<div class="table">
|
||||
<div class="row">
|
||||
<span>Input</span>
|
||||
<span><span class="label success">5V</span><span class="label success">12V</span></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span>Output</span>
|
||||
<span><span class="label warning"><span id="out_voltage">11.2</span>V</span></span>
|
||||
</div>
|
||||
</div>
|
||||
<canvas id="chartCanvas" style="height:100px"></canvas>
|
||||
</article>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
<div id="unused_values" style="display:none;"></div>
|
||||
<script type="text/javascript" src="js/smoothie_min.js"></script>
|
||||
<script>
|
||||
var menu = document.getElementById("bmenub");
|
||||
var voltage = document.getElementById("out_voltage");
|
||||
|
||||
var unused_values = {};
|
||||
|
||||
DataView.prototype.setChar = function(pos, char) {
|
||||
this.setInt8(pos++, char.charCodeAt(0));
|
||||
return pos;
|
||||
};
|
||||
|
||||
DataView.prototype.setString = function(pos, str) {
|
||||
for(var i = 0; i < str.length; i++) {
|
||||
this.setInt8(pos++, str.charCodeAt(i));
|
||||
}
|
||||
this.setInt8(pos++, 0);
|
||||
return pos;
|
||||
};
|
||||
|
||||
var ws;
|
||||
var retries;
|
||||
var series = new TimeSeries();
|
||||
|
||||
function setMsg(cls, text) {
|
||||
sbox = document.getElementById('status_box');
|
||||
sbox.className = "label " + cls;
|
||||
sbox.innerHTML = text;
|
||||
console.log(text);
|
||||
}
|
||||
|
||||
function startPolling() {
|
||||
var chart = new SmoothieChart({
|
||||
millisPerPixel: 111, grid: {fillStyle: '#ffffff', strokeStyle: '#ffffff', borderVisible: false},
|
||||
labels: {fillStyle: '#000000'}, maxValue: 1024, minValue: 0
|
||||
});
|
||||
chart.addTimeSeries(series, {lineWidth: 2, strokeStyle: '#03a9f4', fillStyle: '#f1f5fa'});
|
||||
var canvas = document.getElementById("chartCanvas");
|
||||
canvas.width = canvas.clientWidth;
|
||||
canvas.height = canvas.clientHeight;
|
||||
chart.streamTo(canvas, 500);
|
||||
}
|
||||
|
||||
function onMessage(evt) {
|
||||
retries = 0;
|
||||
if (typeof evt.data == 'string') {
|
||||
var data = JSON.parse(evt.data);
|
||||
for (const [key, value] of Object.entries(data)) {
|
||||
const elements = document.querySelectorAll(".postfill_" + key);
|
||||
if (!elements.length)
|
||||
unused_values[key] = value;
|
||||
else
|
||||
for (i = 0; i < elements.length; ++i) {
|
||||
elements[i].innerHTML = value;
|
||||
}
|
||||
}
|
||||
document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values);
|
||||
} else {
|
||||
var dv = new DataView(evt.data);
|
||||
var cmd = String.fromCharCode(dv.getUint8(0));
|
||||
var val = dv.getUint16(1);
|
||||
|
||||
if (cmd === 'G')
|
||||
console.log("LED switched", val);
|
||||
else if (cmd === 'V') {
|
||||
voltage.innerHTML = (val * 13 / 1024).toFixed(2);
|
||||
series.append(new Date().getTime(), val);
|
||||
} else
|
||||
console.log('unknown command', cmd, val);
|
||||
}
|
||||
}
|
||||
|
||||
function wsOpen() {
|
||||
var uri = "/stream"
|
||||
if (ws === undefined || ws.readyState !== 0) {
|
||||
if (retries)
|
||||
setMsg("warning", "WebSocket timeout, retrying..");
|
||||
else
|
||||
setMsg("primary", "Opening WebSocket..");
|
||||
ws = new WebSocket("ws://" + location.host + uri);
|
||||
ws.binaryType = 'arraybuffer';
|
||||
ws.onopen = function (evt) {
|
||||
retries = 0;
|
||||
setMsg("success", "WebSocket is open.");
|
||||
};
|
||||
ws.onerror = function (evt) {
|
||||
console.error(evt);
|
||||
setMsg("error", "WebSocket error!"); /*window.location.reload(true);*/
|
||||
};
|
||||
ws.onmessage = function (evt) {
|
||||
onMessage(evt);
|
||||
};
|
||||
retries = 0;
|
||||
}
|
||||
}
|
||||
|
||||
function buf2hex(buffer) { // buffer is an ArrayBuffer
|
||||
return [...new Uint8Array(buffer)].map(x => x.toString(16).padStart(2, '0')).join('');
|
||||
}
|
||||
|
||||
function wsWrite(data) {
|
||||
console.info(buf2hex(data));
|
||||
if (ws.readyState === 3 || retries++ > 5)
|
||||
wsOpen();
|
||||
else if (ws.readyState === 1)
|
||||
ws.send(data);
|
||||
}
|
||||
|
||||
function gpio() {
|
||||
if (document.getElementById('led-switch').checked)
|
||||
wsWrite('E');
|
||||
else
|
||||
wsWrite('D');
|
||||
}
|
||||
|
||||
window.onload = function () {
|
||||
wsOpen();
|
||||
startPolling();
|
||||
}
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
|
229
firmware/web.cpp
229
firmware/web.cpp
|
@ -23,11 +23,240 @@ extern "C" {
|
|||
#include <httpd/httpd.h>
|
||||
}
|
||||
|
||||
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
|
||||
|
||||
struct {
|
||||
bool global;
|
||||
bool connection;
|
||||
bool wifi;
|
||||
} has_changed;
|
||||
|
||||
void websocket_task(void *pvParameter) {
|
||||
auto *pcb = (struct tcp_pcb *) pvParameter;
|
||||
|
||||
size_t connstarttime = xTaskGetTickCount();
|
||||
has_changed = {true, true, true};
|
||||
|
||||
for (;;) {
|
||||
if(pcb == NULL || pcb->state != ESTABLISHED) {
|
||||
printf("Connection closed, deleting task\n");
|
||||
break;
|
||||
}
|
||||
|
||||
//Global Info
|
||||
if(has_changed.global) {
|
||||
has_changed.global = false;
|
||||
timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
int uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
|
||||
int heap = (int) xPortGetFreeHeapSize();
|
||||
uint32_t chip_id = sdk_system_get_chip_id();
|
||||
uint32_t flash_id = sdk_spi_flash_get_id();
|
||||
uint32_t flash_size = sdk_flashchip.chip_size >> 10;
|
||||
char *hostname = NULL;
|
||||
|
||||
sysparam_get_string("hostname", &hostname);
|
||||
/* Generate response in JSON format */
|
||||
char response[160];
|
||||
size_t len = snprintf(response, sizeof(response),
|
||||
"{\"walltime\" : \"%d\","
|
||||
"\"uptime\" : \"%d\","
|
||||
" \"heap\" : \"%d\","
|
||||
" \"chipid\" : \"%08x\","
|
||||
" \"flashid\" : \"0x%08x\","
|
||||
" \"flashsize\" : \"%u\","
|
||||
" \"hostname\" : \"%s\""
|
||||
"}", (int) tv.tv_sec, uptime, heap, chip_id, flash_id, flash_size, hostname);
|
||||
free(hostname);
|
||||
if(len < sizeof(response)) {
|
||||
LOCK_TCPIP_CORE();
|
||||
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
||||
UNLOCK_TCPIP_CORE();
|
||||
} else
|
||||
printf("buffer too small 1");
|
||||
vTaskDelayMs(2000);
|
||||
}
|
||||
|
||||
//Connection Info
|
||||
if(has_changed.connection) {
|
||||
has_changed.connection = false;
|
||||
timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
int connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
|
||||
|
||||
printf("conn %d: "
|
||||
IPSTR
|
||||
" <-> "
|
||||
IPSTR
|
||||
" \n", pcb->netif_idx, IP2STR(&pcb->local_ip), IP2STR(&pcb->remote_ip));
|
||||
char response[160];
|
||||
size_t len = snprintf(response, sizeof(response),
|
||||
"{\"connage\" : \"%d\","
|
||||
"\"clientip\" : \""
|
||||
IPSTR
|
||||
"\""
|
||||
"}", connuptime, IP2STR(&pcb->remote_ip));
|
||||
if(len < sizeof(response)) {
|
||||
LOCK_TCPIP_CORE();
|
||||
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
||||
UNLOCK_TCPIP_CORE();
|
||||
} else
|
||||
printf("buffer too small 1");
|
||||
vTaskDelayMs(2000);
|
||||
}
|
||||
|
||||
if(has_changed.wifi) {
|
||||
has_changed.wifi = false;
|
||||
uint8_t opmode = sdk_wifi_get_opmode();
|
||||
const char *opmode_str = "??";
|
||||
switch (opmode) {
|
||||
case NULL_MODE:
|
||||
opmode_str = "Null";
|
||||
break;
|
||||
case STATION_MODE:
|
||||
opmode_str = "Station";
|
||||
break;
|
||||
case SOFTAP_MODE:
|
||||
opmode_str = "SoftAP";
|
||||
break;
|
||||
case STATIONAP_MODE:
|
||||
opmode_str = "StationAP";
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
/*struct sockaddr_storage addr;
|
||||
socklen_t addr_len = sizeof(addr);
|
||||
if (getpeername(s, (struct sockaddr *)&addr, &addr_len) == 0) {
|
||||
printf("peer\n");
|
||||
}*/
|
||||
|
||||
|
||||
if(opmode == SOFTAP_MODE || opmode == STATIONAP_MODE) {
|
||||
uint8_t hwaddr[6];
|
||||
sdk_wifi_get_macaddr(SOFTAP_IF, hwaddr);
|
||||
ip_info info;
|
||||
sdk_wifi_get_ip_info(SOFTAP_IF, &info);
|
||||
|
||||
char *apssid = NULL;
|
||||
sysparam_get_string("wifi_ap_ssid", &apssid);
|
||||
|
||||
/* Generate response in JSON format */
|
||||
char response[128];
|
||||
size_t len = snprintf(response, sizeof(response),
|
||||
"{\"opmode\" : \"%s\","
|
||||
" \"apssid\" : \"%s\","
|
||||
" \"apip\" : \""
|
||||
IPSTR
|
||||
"\","
|
||||
" \"apmac\" : \""
|
||||
MACSTR
|
||||
"\""
|
||||
"}", opmode_str, apssid, IP2STR(&info.ip), MAC2STR(hwaddr));
|
||||
free(apssid);
|
||||
if(len < sizeof(response)) {
|
||||
LOCK_TCPIP_CORE();
|
||||
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
||||
UNLOCK_TCPIP_CORE();
|
||||
} else
|
||||
printf("buffer too small 2");
|
||||
}
|
||||
|
||||
vTaskDelayMs(2000);
|
||||
|
||||
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
|
||||
uint8_t hwaddr[6];
|
||||
sdk_wifi_get_macaddr(STATION_IF, hwaddr);
|
||||
ip_info info;
|
||||
sdk_wifi_get_ip_info(STATION_IF, &info);
|
||||
char *stassid = nullptr;
|
||||
sysparam_get_string("wifi_sta_ssid", &stassid);
|
||||
|
||||
/* Generate response in JSON format */
|
||||
char response[128];
|
||||
size_t len = snprintf(response, sizeof(response),
|
||||
"{\"opmode\" : \"%s\","
|
||||
" \"stassid\" : \"%s\","
|
||||
" \"staip\" : \""
|
||||
IPSTR
|
||||
"\","
|
||||
" \"stamac\" : \""
|
||||
MACSTR
|
||||
"\""
|
||||
"}", opmode_str, stassid, IP2STR(&info.ip), MAC2STR(hwaddr));
|
||||
free(stassid);
|
||||
if(len < sizeof(response)) {
|
||||
LOCK_TCPIP_CORE();
|
||||
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
|
||||
UNLOCK_TCPIP_CORE();
|
||||
} else
|
||||
printf("buffer too small 3");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
vTaskDelayMs(500);
|
||||
{
|
||||
uint8_t response[3];
|
||||
uint16_t val = 0;
|
||||
val = sdk_system_adc_read();
|
||||
response[2] = (uint8_t) val;
|
||||
response[1] = val >> 8;
|
||||
response[0] = 'V';
|
||||
websocket_write(pcb, response, 3, WS_BIN_MODE);
|
||||
}
|
||||
vTaskDelayMs(500);
|
||||
}
|
||||
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called when websocket frame is received.
|
||||
*
|
||||
* Note: this function is executed on TCP thread and should return as soon
|
||||
* as possible.
|
||||
*/
|
||||
void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, uint8_t mode) {
|
||||
|
||||
uint8_t response[3];
|
||||
uint16_t val = 0;
|
||||
char cmd = '0';
|
||||
|
||||
switch (data[0]) {
|
||||
default:
|
||||
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
|
||||
printf("Unknown command\n");
|
||||
val = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
response[2] = (uint8_t) val;
|
||||
response[1] = val >> 8;
|
||||
response[0] = cmd;
|
||||
|
||||
websocket_write(pcb, response, 3, WS_BIN_MODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called when new websocket is open and
|
||||
* creates a new websocket_task if requested URI equals '/stream'.
|
||||
*/
|
||||
void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
|
||||
printf("WS URI: %s\n", uri);
|
||||
if(!strcmp(uri, "/stream")) {
|
||||
xTaskCreate(&websocket_task, "websocket_task", 512, (void *) pcb, 3, nullptr);
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" [[noreturn]] void httpd_task(void *pvParameters) {
|
||||
|
||||
while(!uxSemaphoreGetCount( wifi_available_semaphore ))
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
|
||||
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
|
||||
httpd_init();
|
||||
|
||||
for (;;)
|
||||
|
|
1
pcb/.gitignore
vendored
1
pcb/.gitignore
vendored
|
@ -31,5 +31,6 @@ fp-info-cache
|
|||
*.wrl
|
||||
*.step
|
||||
|
||||
*-bak
|
||||
gen/
|
||||
gen.zip
|
||||
|
|
Loading…
Reference in a new issue