Compare commits

..

6 commits

Author SHA1 Message Date
60766a0c0b stash
Some checks failed
continuous-integration/drone/push Build is failing
2023-02-12 12:19:08 +01:00
56cddf7073 stash 2023-02-12 12:18:01 +01:00
b7ccdfd5d3 Adding flash_docker function 2023-02-12 12:16:35 +01:00
2c36406a2a mqtt verbindet 2023-02-12 12:15:04 +01:00
b7995c0586 stash 2023-02-12 12:13:06 +01:00
6a7babcaf7 add wifi settings tab in web frontend 2023-02-12 12:13:06 +01:00
13 changed files with 616 additions and 138 deletions

View file

@ -1,6 +1,6 @@
PROGRAM=fiatlux
EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=1
EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=1 -DFLATBUFFERS_PREFER_PRINTF=1
EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/rboot-ota extras/mbedtls extras/httpd extras/sntp extras/cpp_support extras/paho_mqtt_c

View file

@ -11,8 +11,9 @@
#include <espressif/esp_common.h>
#include <esp/uart.h>
void user_init(void)
{
#define PUB_MSG_LEN 16
void user_init(void) {
uart_set_baud(0, 115200);
printf("SDK version: %s\n", sdk_system_get_sdk_version());
@ -22,9 +23,18 @@ void user_init(void)
wifi_available_semaphore = xSemaphoreCreateBinary();
xTaskCreate(mqtt_task, "mqtt_task", 1024, NULL, 1, NULL);
xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL);
xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL);
xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, NULL);
wifi_alive = xSemaphoreCreateBinary();
publish_queue = xQueueCreate(3, PUB_MSG_LEN);
xTaskCreate(&beat_task, "beat_task", 256, NULL, 3, NULL);
}

View file

@ -31,7 +31,7 @@ unsigned syslog_data_after(unsigned local_tail) {
return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size);
}
extern "C" int syslog_copy_out(char *out, int len, unsigned local_tail) {
extern "C" int syslog_copy_out(char *out, unsigned len, unsigned local_tail) {
unsigned cnt = 0;
while (cnt < syslog_data_after(local_tail) && cnt < len) {
out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt];

View file

@ -15,7 +15,7 @@ unsigned syslog_current_tail();
unsigned syslog_data_after(unsigned);
int syslog_copy_out(char *, int, unsigned);
int syslog_copy_out(char *, unsigned, unsigned);
void syslog_attach();

76
firmware/messages.h Normal file
View file

@ -0,0 +1,76 @@
//
// Created by jedi on 13.09.22.
//
namespace fiatlux {
namespace messages {
enum id : uint8_t {
NONE = 0,
RESTART,
CLEAR_CONFIG,
FIRMWARE_FRAME,
FIRMWARE_CHECK,
VOLTAGE_INFO,
SYSTEM_INFO,
count
};
const char* id_to_string(id val){
static const char* lookup[] = {
"NONE",
"RESTART",
"CLEAR_CONFIG",
"FIRMWARE_FRAME",
"FIRMWARE_CHECK",
"VOLTAGE_INFO",
"SYSTEM_INFO",
"out of range"
};
if(val < id::count)
return lookup[val];
else
return lookup[id::count];
}
template <typename T>
struct frame {
id cmd;
T msg;
} __attribute__((packed));
struct fw_frame {
char t;
uint8_t reserved[3];
uint16_t seq;
uint16_t len;
uint32_t hash;
uint8_t data[];
} __attribute__((packed));
struct fw_check {
char t;
uint8_t reserved[3];
uint32_t len;
uint32_t hash;
} __attribute__((packed));
struct system_info {
unsigned walltime;
unsigned uptime;
int heap;
uint32_t chipid;
uint32_t flashid;
uint32_t flashsize;
/*
" \"hostname\" : \"%s\""
"}"
*/
} __attribute__((packed));
struct response {
id cmd;
uint8_t ret;
uint16_t val;
} __attribute__((packed));
}
}

View file

@ -3,3 +3,163 @@
//
#include "mqtt.h"
#include "wifi.h"
#include <stdio.h>
#include <string.h>
#include <espressif/esp_common.h>
#include <espressif/user_interface.h>
extern "C" {
#include <paho_mqtt_c/MQTTESP8266.h>
#include <paho_mqtt_c/MQTTClient.h>
}
#include <semphr.h>
/* You can use http://test.mosquitto.org/ to test mqtt_client instead
* of setting up your own MQTT server */
//#define MQTT_HOST ("172.16.0.42")
#define MQTT_HOST ("10.23.42.187")
#define MQTT_PORT 1883
#define MQTT_USER NULL
#define MQTT_PASS NULL
#define PUB_MSG_LEN 16
QueueHandle_t publish_queue;
extern "C" void beat_task(void *pvParameters) {
TickType_t xLastWakeTime = xTaskGetTickCount();
char msg[PUB_MSG_LEN];
int count = 0;
while (1) {
vTaskDelayUntil(&xLastWakeTime, 10000 / portTICK_PERIOD_MS);
printf("beat\r\n");
snprintf(msg, PUB_MSG_LEN, "Beat %d\r\n", count++);
if(xQueueSend(publish_queue, (void *) msg, 0) == pdFALSE) {
printf("Publish queue overflow.\r\n");
}
}
}
static void topic_received(mqtt_message_data_t *md) {
int i;
mqtt_message_t *message = md->message;
printf("Received: ");
for (i = 0; i < md->topic->lenstring.len; ++i)
printf("%c", md->topic->lenstring.data[i]);
printf(" = ");
for (i = 0; i < (int) message->payloadlen; ++i)
printf("%c", ((char *) (message->payload))[i]);
printf("\r\n");
}
static const char *get_my_id(void) {
// Use MAC address for Station as unique ID
static char my_id[13];
static bool my_id_done = false;
int8_t i;
uint8_t x;
if(my_id_done)
return my_id;
if(!sdk_wifi_get_macaddr(STATION_IF, (uint8_t *) my_id))
return NULL;
for (i = 5; i >= 0; --i) {
x = my_id[i] & 0x0F;
if(x > 9) x += 7;
my_id[i * 2 + 1] = x + '0';
x = my_id[i] >> 4;
if(x > 9) x += 7;
my_id[i * 2] = x + '0';
}
my_id[12] = '\0';
my_id_done = true;
return my_id;
}
extern "C" void mqtt_task(void *pvParameters) {
int ret = 0;
struct mqtt_network network;
mqtt_client_t client = mqtt_client_default;
char mqtt_client_id[20];
uint8_t mqtt_buf[100];
uint8_t mqtt_readbuf[100];
mqtt_packet_connect_data_t data = mqtt_packet_connect_data_initializer;
mqtt_network_new(&network);
memset(mqtt_client_id, 0, sizeof(mqtt_client_id));
strcpy(mqtt_client_id, "ESP-");
strcat(mqtt_client_id, get_my_id());
while (1) {
xSemaphoreTake(wifi_alive, portMAX_DELAY);
printf("%s: started\n\r", __func__);
printf("%s: (Re)connecting to MQTT server %s ... ", __func__,
MQTT_HOST);
ret = mqtt_network_connect(&network, MQTT_HOST, MQTT_PORT);
if(ret) {
printf("error 1: %d\n\r", ret);
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
printf("done\n\r");
mqtt_client_new(&client, &network, 5000, mqtt_buf, 100,
mqtt_readbuf, 100);
data.willFlag = 0;
data.MQTTVersion = 3;
data.clientID.cstring = mqtt_client_id;
data.username.cstring = MQTT_USER;
data.password.cstring = MQTT_PASS;
data.keepAliveInterval = 10;
data.cleansession = 0;
printf("Send MQTT connect ... ");
ret = mqtt_connect(&client, &data);
if(ret) {
printf("error 2: %d\n\r", ret);
mqtt_network_disconnect(&network);
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
printf("done\r\n");
mqtt_subscribe(&client, "/esptopic", MQTT_QOS1, topic_received);
xQueueReset(publish_queue);
while (1) {
char msg[PUB_MSG_LEN - 1] = "\0";
while (xQueueReceive(publish_queue, (void *) msg, 0) ==
pdTRUE) {
printf("got message to publish\r\n");
mqtt_message_t message;
message.payload = msg;
message.payloadlen = PUB_MSG_LEN;
message.dup = 0;
message.qos = MQTT_QOS1;
message.retained = 0;
ret = mqtt_publish(&client, "/beat", &message);
if(ret != MQTT_SUCCESS) {
printf("error while publishing message: %d\n", ret);
break;
}
}
ret = mqtt_yield(&client, 1000);
if(ret == MQTT_DISCONNECTED)
break;
}
printf("Connection dropped, request restart\n\r");
mqtt_network_disconnect(&network);
taskYIELD();
}
}

View file

@ -5,10 +5,19 @@
#ifndef FIRMWARE_MQTT_H
#define FIRMWARE_MQTT_H
#include <FreeRTOS.h>
#include <queue.h>
#ifdef __cplusplus
extern "C" {
#endif
void mqtt_task(void *pvParameters);
void beat_task(void *pvParameters);
extern QueueHandle_t publish_queue;
#ifdef __cplusplus
}
#endif

View file

@ -24,12 +24,19 @@ extern "C" {
#include <httpd/httpd.h>
}
#include "messages.h"
using namespace fiatlux;
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
uint16_t voltage_val;
struct {
bool global;
bool connection;
bool wifi;
bool voltage;
} has_changed;
void websocket_task(void *pvParameter) {
@ -60,11 +67,12 @@ void websocket_task(void *pvParameter) {
UNLOCK_TCPIP_CORE();
} else
syslog("buffer too small -1\n");
vTaskDelayMs(1000);
taskYIELD();
}
//Global Info
if(has_changed.global) {
if(false && has_changed.global) {
timeval tv{};
gettimeofday(&tv, nullptr);
size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
@ -76,40 +84,35 @@ void websocket_task(void *pvParameter) {
sysparam_get_string("hostname", &hostname);
/* Generate response in JSON format */
char response[192];
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);
has_changed.global = false;
UNLOCK_TCPIP_CORE();
} else
syslog("buffer too small 0\n");
vTaskDelayMs(1000);
}
messages::frame<messages::system_info> frame;
frame.cmd = messages::SYSTEM_INFO;
frame.msg.walltime = tv.tv_sec;
frame.msg.uptime = uptime;
frame.msg.heap = heap;
frame.msg.chipid = chip_id;
frame.msg.flashid = flash_id;
frame.msg.flashsize = flash_size;
//frame.msg. = ; // hostname
//Connection Info
if(has_changed.connection) {
free(hostname);
/*LOCK_TCPIP_CORE();
websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE);
has_changed.global = false;
UNLOCK_TCPIP_CORE();*/
taskYIELD();
} else if(has_changed.connection) {
timeval tv{};
gettimeofday(&tv, nullptr);
size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
printf("conn %d: %d.%.%.%d <-> %d.%d.&d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
IP2STR(&pcb->remote_ip));
char response[192];
size_t len = snprintf(response, sizeof(response),
"{\"connage\" : \"%d\","
"\"clientip\" : \"" IPSTR "\""
"}", connuptime, IP2STR(&pcb->remote_ip));
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);
@ -117,10 +120,9 @@ void websocket_task(void *pvParameter) {
UNLOCK_TCPIP_CORE();
} else
syslog("buffer too small 1\n");
vTaskDelayMs(1000);
}
if(has_changed.wifi) {
taskYIELD();
} else if(has_changed.wifi) {
has_changed.wifi = false;
uint8_t opmode = sdk_wifi_get_opmode();
const char *opmode_str = "??";
@ -175,7 +177,7 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 2\n");
}
vTaskDelayMs(1000);
taskYIELD();
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
uint8_t hwaddr[6];
@ -202,9 +204,25 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 3\n");
}
taskYIELD();
} else if(has_changed.voltage) {
has_changed.voltage = false;
messages::response res;
res.val = voltage_val;
res.ret = OK;
res.cmd = (messages::id) 'V';
LOCK_TCPIP_CORE();
websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE);
UNLOCK_TCPIP_CORE();
taskYIELD();
} else {
//taskYIELD();
//printf("no change a\n");
vTaskDelay(50);
//printf("no change b\n");
}
vTaskDelayMs(500);
//printf("9: %d\n",gpio_read(9));
//printf("10: %d\n",gpio_read(10));
}
syslog_detach();
@ -212,22 +230,6 @@ void websocket_task(void *pvParameter) {
vTaskDelete(nullptr);
}
struct fw_frame {
char t;
uint8_t reserved[3];
uint16_t seq;
uint16_t len;
uint32_t hash;
uint8_t data[];
} __attribute__((packed));
struct fw_check {
char t;
uint8_t reserved[3];
uint32_t len;
uint32_t hash;
} __attribute__((packed));
/**
* This function is called when websocket frame is received.
*
@ -237,80 +239,109 @@ struct fw_check {
void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len,
uint8_t /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE
uint8_t response[4];
auto &cmd = (char &) response[0];
auto &ret = response[1];
auto &val = (uint16_t &) response[2];
cmd = '0';
ret = ERROR;
val = 0;
messages::response res;
res.cmd = messages::NONE;
res.ret = ERROR;
res.val = 0;
bool togl = false;
switch (data[0]) {
case 'R': // Restart
cmd = 'R';
ret = OK;
break;
case 'X': // Clear Config
cmd = 'X';
ret = OK;
break;
case 'D': // Disable LED
syslog("G\n");
signal_led(false);
cmd = 'G';
ret = OK;
val = 1;
break;
case 'E': // Enable LED
syslog("E\n");
signal_led(true);
cmd = 'G';
ret = OK;
val = 0;
break;
case 'F':
togl = !togl;
signal_led(togl);
{
auto *f = (fw_frame *) data;
if(f->seq == 0) {
system_otaflash_init();
}
uint16_t ack = 0;
ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
val = htons(ack);
}
cmd = 'F';
break;
case 'C':
signal_led(false);
{
auto *f = (fw_check *) data;
ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
}
cmd = 'C';
break;
default:
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
printf("Unknown command %c\n", data[0]);
ret = ERROR;
break;
if(data[0] == 'R') {
// Restart
res.cmd = (messages::id) 'R';
res.ret = OK;
} else if(data[0] == 'X') {
// Clear Config
res.cmd = (messages::id) 'X';
res.ret = OK;
} else if(data[0] == 'D') {
// Disable LED
syslog("G\n");
signal_led(false);
res.cmd = (messages::id) 'G';
res.ret = OK;
res.val = 1;
} else if(data[0] == 'E') {
// Enable LED
syslog("E\n");
signal_led(true);
res.cmd = (messages::id) 'G';
res.ret = OK;
res.val = 0;
} else if(data[0] == 'F') {
togl = !togl;
signal_led(togl);
auto *f = (messages::fw_frame *) data;
if(f->seq == 0) {
system_otaflash_init();
}
uint16_t ack = 0;
res.ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
res.val = htons(ack);
res.cmd = (messages::id) 'F';
} else if(data[0] == 'C') {
signal_led(false);
auto *f = (messages::fw_check *) data;
res.ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
res.cmd = (messages::id) 'C';
} else if(data[0] == 'S') {
int8_t en = 0;
if(data[1] == 'E')
en = 1;
char *ssid = &data[2];
size_t ssid_len = strlen(ssid);
char *password = &data[3 + ssid_len];
size_t password_len = strlen(password);
(void) password_len;
sysparam_set_int8("wifi_sta_enable", en);
sysparam_set_string("wifi_sta_ssid", ssid);
sysparam_set_string("wifi_sta_password", password);
//sysparam_get_int8("wifi_sta_dhcp", wifi_sta_dhcp);
//sysparam_get_int32("wifi_sta_ip_addr", wifi_sta_ip_addr);
//sysparam_get_int32("wifi_sta_netmask", wifi_sta_netmask);
//sysparam_get_int32("wifi_sta_gateway", wifi_sta_gateway);
res.cmd = (messages::id) 'S';
} else if(data[0] == 'A') {
int8_t en = 0;
uint8_t dns_enable = 0;
uint8_t mdns_enable = 0;
if(data[1] == 'E')
en = 1;
char *ssid = &data[2];
size_t ssid_len = strlen(ssid);
char *password = &data[3 + ssid_len];
size_t password_len = strlen(password);
(void) password_len;
sysparam_set_int8("wifi_ap_enable", en);
sysparam_set_string("wifi_ap_ssid", ssid);
sysparam_set_string("wifi_ap_password", password);
//sysparam_get_int32("wifi_ap_ip_addr", "172.16.0.1");
//sysparam_get_int32("wifi_ap_netmask", "255.255.0.0");
//sysparam_set_int8("wifi_ap_dns", dns_enable);
//sysparam_set_int8("wifi_ap_mdns", mdns_enable);
res.cmd = (messages::id) 'A';
} else {
printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
printf("Unknown command %c\n", data[0]);
res.ret = ERROR;
}
LOCK_TCPIP_CORE();
websocket_write(pcb, response, 4, WS_BIN_MODE);
websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE);
UNLOCK_TCPIP_CORE();
if(ret == OK) {
if(cmd == 'R' || cmd == 'C') { // Restart
if(res.ret == OK) {
if(res.cmd == 'R' || res.cmd == 'C') { // Restart
printf("rebooting now");
vTaskDelay(1000 / portTICK_PERIOD_MS);
taskYIELD();
vPortEnterCritical();
sdk_system_restart();
} else if(cmd == 'X') { // Clear Config
vTaskDelay(1000 / portTICK_PERIOD_MS);
} else if(res.cmd == 'X') { // Clear Config
taskYIELD();
system_clear_config();
}
}
@ -327,13 +358,42 @@ void websocket_open_cb(struct tcp_pcb *pcb, const char *uri) {
}
}
/*const char *gpio_cgi_handler(int iIndex, int iNumParams, char *pcParam[], char *pcValue[]) {
for (int i = 0; i < iNumParams; i++) {
if(strcmp(pcParam[i], "on") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_write(gpio_num, true);
} else if(strcmp(pcParam[i], "off") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_write(gpio_num, false);
} else if(strcmp(pcParam[i], "toggle") == 0) {
uint8_t gpio_num = atoi(pcValue[i]);
gpio_enable(gpio_num, GPIO_OUTPUT);
gpio_toggle(gpio_num);
}
}
return "/index.html";
}*/
extern "C" void httpd_task(void *pvParameters) {
(void) pvParameters;
while (!uxSemaphoreGetCount(wifi_available_semaphore))
vTaskDelay(500 / portTICK_PERIOD_MS);
//while (!uxSemaphoreGetCount(wifi_available_semaphore))
// vTaskDelay(500 / portTICK_PERIOD_MS);
/*tCGI pCGIs[] = {
{"/gpio", (tCGIHandler) gpio_cgi_handler},
};
// register handlers and start the server
http_set_cgi_handlers(pCGIs, sizeof(pCGIs) / sizeof(pCGIs[0]));*/
websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
httpd_init();
vTaskDelete(nullptr);
while (1) {
voltage_val = sdk_system_adc_read();
has_changed.voltage = true;
vTaskDelayMs(1000);
}
}

View file

@ -17,6 +17,7 @@
<label for="bmenub" class="burger pseudo button">&#9776;</label>
<div class="menu">
<a href="/#" class="button icon-picture">Dashboard</a>
<a href="/#wifi" class="button icon-puzzle">Wifi Settings</a>
<a href="/#ota" class="button icon-picture">System</a>
</div>
</nav>
@ -75,6 +76,87 @@
</article>
</section>
<section id="wifi">
<h2>Wifi Settings</h2>
<article class="card">
<header>
<h3>AP Mode</h3>
</header>
<div class="table">
<div class="row">
<label>Enable</label>
<span><input id="ap_toggle" type="checkbox" class="plain"/></span>
</div>
<div class="row">
<label>SSID</label>
<span><input id="ap_ssid" type="text" placeholder="SSID"/></span>
</div>
<div class="row">
<label>Password</label>
<span><input id="ap_pw" type="password" placeholder="Password"/></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 class="row">
<span><input type="reset" class="button"/></span>
<span><input onclick="ap_update();" type="submit" value="Save"></span>
</div>
</div>
</article>
<article class="card">
<header>
<h3>Station Mode <span class="label success">current connection</span></h3>
</header>
<div class="table">
<div class="row">
<label>Eanable</label>
<span><input id="sta_toggle" type="checkbox" class="plain"/></span>
</div>
<div class="row">
<label>SSID</label>
<span><input id="sta_ssid" type="text" placeholder="SSID"/></span>
</div>
<div class="row">
<label>Password</label>
<span><input id="sta_pw" type="password" placeholder="Password"/></span>
</div>
<div class="row">
<label>Use DHCP</label>
<span><input id="sta_shcp" type="checkbox" class="plain"/></span>
</div>
<div class="row">
<label>Static IP</label>
<span><input id="sta_static_ip" type="text" placeholder="192.168.1.2"/></span>
</div>
<div class="row">
<label>Static Netmask</label>
<span><input id="sta_static_netmask" type="text" placeholder="255.255.255.0"/></span>
</div>
<div class="row">
<label>Static Gateway</label>
<span><input id="sta_static_gateway" type="text" placeholder="192.168.1.1"/></span>
</div>
<div class="row">
<span>Sation 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><input type="reset" class="button"/></span>
<span><input onclick="sta_update();" type="submit" value="Save"></span>
</div>
</div>
</article>
</section>
<section id="dashboard">
<h2>Status</h2>
<div class="flex">
@ -212,6 +294,18 @@
return pos;
};
DataView.prototype.setInt8Vec = function (pos, vec) {
for (var i = 0; i < vec.length; i++) {
this.setInt8(pos++, vec[i]);
}
return pos;
};
function colorStringToVec(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)] : null;
}
var ws;
var retries;
var series = new TimeSeries();
@ -254,6 +348,7 @@
document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values);
} else {
var dv = new DataView(evt.data);
console.log("[0]",dv.getUint8(0));
var cmd = String.fromCharCode(dv.getUint8(0));
var val = dv.getUint16(1);
@ -433,6 +528,48 @@
}
}
var sta_toggle = document.getElementById("sta_toggle");
var sta_ssid = document.getElementById("sta_ssid");
var sta_pw = document.getElementById("sta_pw");
function sta_update() {
var en = sta_toggle.checked;
const ssid = sta_ssid.value;
const password = sta_pw.value;
const buffer = new ArrayBuffer(ssid.length + password.length + 4);
const view1 = new DataView(buffer);
var tx_len = 0;
view1.setChar(tx_len++, 'S');
view1.setChar(tx_len++, (en ? "E" : "D"));
tx_len = view1.setString(tx_len, ssid);
tx_len = view1.setString(tx_len, password);
wsWrite(buffer);
}
var ap_toggle = document.getElementById("ap_toggle");
var ap_ssid = document.getElementById("ap_ssid");
var ap_pw = document.getElementById("ap_pw");
function ap_update() {
var en = ap_toggle.checked;
const ssid = ap_ssid.value;
const password = ap_pw.value;
const buffer = new ArrayBuffer(ssid.length + password.length + 4 + 8);
const view1 = new DataView(buffer);
var tx_len = 0;
view1.setChar(tx_len++, 'A');
view1.setChar(tx_len++, (en ? "E" : "D"));
tx_len = view1.setString(tx_len, ssid);
tx_len = view1.setString(tx_len, password);
tx_len = view1.setInt8Vec(tx_len, [192, 168, 111, 1]);
tx_len = view1.setInt8Vec(tx_len, [255, 255, 255, 0]);
wsWrite(buffer);
}
function update_progress(progressBar, progress) {
var iP = Math.floor(progress);
var dBar = document.getElementById("progress_bar_" + progressBar);

View file

@ -32,6 +32,7 @@ char *wifi_ap_ssid = nullptr;
char *wifi_ap_password = nullptr;
SemaphoreHandle_t wifi_available_semaphore = nullptr;
SemaphoreHandle_t wifi_alive = nullptr;
[[noreturn]] static void dns_task(void *pvParameters) {
char *wifi_ap_ip_addr = nullptr;
@ -211,7 +212,7 @@ extern "C" void wifi_task(void *pvParameters) {
if(wifi_sta_enable) {
printf("try STA Mode: %s %s\n", wifi_sta_ssid, wifi_sta_password);
sdk_station_config config;
sdk_station_config config{};
strcpy((char *) config.ssid, wifi_sta_ssid);
strcpy((char *) config.password, wifi_sta_password);
config.bssid_set = 0;
@ -231,7 +232,7 @@ extern "C" void wifi_task(void *pvParameters) {
wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 &&
wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) {
sdk_wifi_station_dhcpc_stop();
ip_info info;
ip_info info{};
memset(&info, 0x0, sizeof(info));
info.ip.addr = ipaddr_addr(wifi_sta_ip_addr);
info.netmask.addr = ipaddr_addr(wifi_sta_netmask);
@ -258,16 +259,6 @@ extern "C" void wifi_task(void *pvParameters) {
int8_t wifi_ap_channel = 6;
sysparam_get_int8("wifi_ap_channel", &wifi_ap_channel);
#if 0
/* AU does not allow channels above 13, although 14 works. */
if(wifi_ap_channel > 13) {
wifi_ap_channel = 13;
}
/* US does not allow channels above 11, although they work. */
if (wifi_ap_channel > 11) {
wifi_ap_channel = 11;
}
#endif
if(wifi_ap_channel < 1 || wifi_ap_channel > 14) {
wifi_ap_channel = 6;
}
@ -306,13 +297,13 @@ extern "C" void wifi_task(void *pvParameters) {
}
if(strlen(wifi_ap_ip_addr) >= 7 && strlen(wifi_ap_netmask) >= 7) {
ip_info ap_ip;
ip_info ap_ip{};
ap_ip.ip.addr = ipaddr_addr(wifi_ap_ip_addr);
ap_ip.netmask.addr = ipaddr_addr(wifi_ap_netmask);
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
sdk_wifi_set_ip_info(1, &ap_ip);
sdk_softap_config ap_config;
sdk_softap_config ap_config{};
strcpy((char *) ap_config.ssid, wifi_ap_ssid);
ap_config.ssid_len = strlen(wifi_ap_ssid);
strcpy((char *) ap_config.password, wifi_ap_password);
@ -357,5 +348,38 @@ extern "C" void wifi_task(void *pvParameters) {
xSemaphoreGive(wifi_available_semaphore);
//monitor loop connection here
vTaskDelete(nullptr);
uint8_t status = 0;
uint8_t retries = 30;
while (1) {
while ((status != STATION_GOT_IP) && (retries)) {
status = sdk_wifi_station_get_connect_status();
printf("%s: status = %d\n\r", __func__, status);
if(status == STATION_WRONG_PASSWORD) {
printf("WiFi: wrong password\n\r");
break;
} else if(status == STATION_NO_AP_FOUND) {
printf("WiFi: AP not found\n\r");
break;
} else if(status == STATION_CONNECT_FAIL) {
printf("WiFi: connection failed\r\n");
break;
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
--retries;
}
if(status == STATION_GOT_IP) {
printf("WiFi: Connected\n\r");
xSemaphoreGive(wifi_alive);
taskYIELD();
}
while ((status = sdk_wifi_station_get_connect_status()) == STATION_GOT_IP) {
xSemaphoreGive(wifi_alive);
taskYIELD();
}
printf("WiFi: disconnected\n\r");
sdk_wifi_station_disconnect();
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}

View file

@ -13,6 +13,7 @@ extern "C" {
#endif
extern SemaphoreHandle_t wifi_available_semaphore;
extern SemaphoreHandle_t wifi_alive;
void wifi_task(void *pvParameters);

1
pcb/.gitignore vendored
View file

@ -16,6 +16,7 @@ _autosave-*
*-save.pro
*-save.kicad_pcb
fp-info-cache
*-backups/
# Netlist files (exported from Eeschema)
*.net