Compare commits

..

No commits in common. "jedi/dev/wifi" and "stable" have entirely different histories.

13 changed files with 137 additions and 615 deletions

View file

@ -10,4 +10,4 @@ RUN apt remove --purge -y python2 && apt autoremove --purge -y && apt install -y
RUN apt install -y --reinstall python-is-python3 RUN apt install -y --reinstall python-is-python3
USER 1000 USER 1000
WORKDIR /app WORKDIR /app
ENV PATH=/app/modules/sdk/xtensa-lx106-elf/bin:$PATH ENV PATH=/app/modules/sdk/xtensa-lx106-elf/bin:$PATH

View file

@ -1,6 +1,6 @@
PROGRAM=fiatlux PROGRAM=fiatlux
EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=1 -DFLATBUFFERS_PREFER_PRINTF=1 EXTRA_CFLAGS=-O3 -Ibuild/gen -DLWIP_NETIF_HOSTNAME=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 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,9 +11,8 @@
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#include <esp/uart.h> #include <esp/uart.h>
#define PUB_MSG_LEN 16 void user_init(void)
{
void user_init(void) {
uart_set_baud(0, 115200); uart_set_baud(0, 115200);
printf("SDK version: %s\n", sdk_system_get_sdk_version()); printf("SDK version: %s\n", sdk_system_get_sdk_version());
@ -23,18 +22,9 @@ void user_init(void) {
wifi_available_semaphore = xSemaphoreCreateBinary(); wifi_available_semaphore = xSemaphoreCreateBinary();
xTaskCreate(mqtt_task, "mqtt_task", 1024, NULL, 1, NULL);
xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL); xTaskCreate(wifi_task, "wifi_task", 1024, NULL, 1, NULL);
xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL); xTaskCreate(&httpd_task, "httpd_task", 1024, NULL, 2, NULL);
xTaskCreate(&lux_task, "lux_task", 512, NULL, 1, 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); return (head % syslog_buffer_size) - (local_tail % syslog_buffer_size);
} }
extern "C" int syslog_copy_out(char *out, unsigned len, unsigned local_tail) { extern "C" int syslog_copy_out(char *out, int len, unsigned local_tail) {
unsigned cnt = 0; unsigned cnt = 0;
while (cnt < syslog_data_after(local_tail) && cnt < len) { while (cnt < syslog_data_after(local_tail) && cnt < len) {
out[cnt] = syslog_buf[local_tail % syslog_buffer_size + cnt]; 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); unsigned syslog_data_after(unsigned);
int syslog_copy_out(char *, unsigned, unsigned); int syslog_copy_out(char *, int, unsigned);
void syslog_attach(); void syslog_attach();

View file

@ -1,76 +0,0 @@
//
// 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,163 +3,3 @@
// //
#include "mqtt.h" #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,19 +5,10 @@
#ifndef FIRMWARE_MQTT_H #ifndef FIRMWARE_MQTT_H
#define FIRMWARE_MQTT_H #define FIRMWARE_MQTT_H
#include <FreeRTOS.h>
#include <queue.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
void mqtt_task(void *pvParameters);
void beat_task(void *pvParameters);
extern QueueHandle_t publish_queue;
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -24,19 +24,12 @@ extern "C" {
#include <httpd/httpd.h> #include <httpd/httpd.h>
} }
#include "messages.h"
using namespace fiatlux;
#define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS) #define vTaskDelayMs(ms) vTaskDelay((ms) / portTICK_PERIOD_MS)
uint16_t voltage_val;
struct { struct {
bool global; bool global;
bool connection; bool connection;
bool wifi; bool wifi;
bool voltage;
} has_changed; } has_changed;
void websocket_task(void *pvParameter) { void websocket_task(void *pvParameter) {
@ -67,12 +60,11 @@ void websocket_task(void *pvParameter) {
UNLOCK_TCPIP_CORE(); UNLOCK_TCPIP_CORE();
} else } else
syslog("buffer too small -1\n"); syslog("buffer too small -1\n");
vTaskDelayMs(1000);
taskYIELD();
} }
//Global Info //Global Info
if(false && has_changed.global) { if(has_changed.global) {
timeval tv{}; timeval tv{};
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000; size_t uptime = xTaskGetTickCount() * portTICK_PERIOD_MS / 1000;
@ -84,35 +76,40 @@ void websocket_task(void *pvParameter) {
sysparam_get_string("hostname", &hostname); sysparam_get_string("hostname", &hostname);
/* Generate response in JSON format */ /* Generate response in JSON format */
messages::frame<messages::system_info> frame; char response[192];
frame.cmd = messages::SYSTEM_INFO; size_t len = snprintf(response, sizeof(response),
frame.msg.walltime = tv.tv_sec; "{\"walltime\" : \"%d\","
frame.msg.uptime = uptime; "\"uptime\" : \"%d\","
frame.msg.heap = heap; " \"heap\" : \"%d\","
frame.msg.chipid = chip_id; " \"chipid\" : \"%08x\","
frame.msg.flashid = flash_id; " \"flashid\" : \"0x%08x\","
frame.msg.flashsize = flash_size; " \"flashsize\" : \"%u\","
//frame.msg. = ; // hostname " \"hostname\" : \"%s\""
"}", (int) tv.tv_sec, uptime, heap, chip_id, flash_id, flash_size, hostname);
free(hostname); free(hostname);
/*LOCK_TCPIP_CORE(); if(len < sizeof(response)) {
websocket_write(pcb, (uint8_t * ) &frame, sizeof(frame), WS_BIN_MODE); LOCK_TCPIP_CORE();
has_changed.global = false; websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
UNLOCK_TCPIP_CORE();*/ has_changed.global = false;
UNLOCK_TCPIP_CORE();
} else
syslog("buffer too small 0\n");
vTaskDelayMs(1000);
}
taskYIELD(); //Connection Info
} else if(has_changed.connection) { if(has_changed.connection) {
timeval tv{}; timeval tv{};
gettimeofday(&tv, nullptr); gettimeofday(&tv, nullptr);
size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000; size_t connuptime = (xTaskGetTickCount() - connstarttime) * portTICK_PERIOD_MS / 1000;
printf("conn %d: %d.%.%.%d <-> %d.%d.&d.%d \n", pcb->netif_idx, IP2STR(&pcb->local_ip), printf("conn %d: " IPSTR " <-> " IPSTR " \n", pcb->netif_idx, IP2STR(&pcb->local_ip),
IP2STR(&pcb->remote_ip)); IP2STR(&pcb->remote_ip));
char response[192]; char response[192];
size_t size_t len = snprintf(response, sizeof(response),
len = snprintf(response, sizeof(response), "{\"connage\" : \"%d\"," "\"clientip\" : \"" "{\"connage\" : \"%d\","
IPSTR "\"clientip\" : \"" IPSTR "\""
"\"" "}", connuptime, IP2STR(&pcb->remote_ip)); "}", connuptime, IP2STR(&pcb->remote_ip));
if(len < sizeof(response)) { if(len < sizeof(response)) {
LOCK_TCPIP_CORE(); LOCK_TCPIP_CORE();
websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE); websocket_write(pcb, (unsigned char *) response, len, WS_TEXT_MODE);
@ -120,9 +117,10 @@ void websocket_task(void *pvParameter) {
UNLOCK_TCPIP_CORE(); UNLOCK_TCPIP_CORE();
} else } else
syslog("buffer too small 1\n"); syslog("buffer too small 1\n");
vTaskDelayMs(1000);
}
taskYIELD(); if(has_changed.wifi) {
} else if(has_changed.wifi) {
has_changed.wifi = false; has_changed.wifi = false;
uint8_t opmode = sdk_wifi_get_opmode(); uint8_t opmode = sdk_wifi_get_opmode();
const char *opmode_str = "??"; const char *opmode_str = "??";
@ -177,7 +175,7 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 2\n"); syslog("buffer too small 2\n");
} }
taskYIELD(); vTaskDelayMs(1000);
if(opmode == STATION_MODE || opmode == STATIONAP_MODE) { if(opmode == STATION_MODE || opmode == STATIONAP_MODE) {
uint8_t hwaddr[6]; uint8_t hwaddr[6];
@ -204,25 +202,9 @@ void websocket_task(void *pvParameter) {
syslog("buffer too small 3\n"); 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");
} }
//printf("9: %d\n",gpio_read(9));
//printf("10: %d\n",gpio_read(10)); vTaskDelayMs(500);
} }
syslog_detach(); syslog_detach();
@ -230,6 +212,22 @@ void websocket_task(void *pvParameter) {
vTaskDelete(nullptr); 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. * This function is called when websocket frame is received.
* *
@ -239,109 +237,80 @@ void websocket_task(void *pvParameter) {
void websocket_cb(struct tcp_pcb *pcb, char *data, u16_t data_len, 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 /*mode*/) { //mode should be WS_BIN_MODE or WS_TEXT_MODE
messages::response res; uint8_t response[4];
res.cmd = messages::NONE; auto &cmd = (char &) response[0];
res.ret = ERROR; auto &ret = response[1];
res.val = 0; auto &val = (uint16_t &) response[2];
cmd = '0';
ret = ERROR;
val = 0;
bool togl = false; bool togl = false;
if(data[0] == 'R') { switch (data[0]) {
// Restart case 'R': // Restart
res.cmd = (messages::id) 'R'; cmd = 'R';
res.ret = OK; ret = OK;
} else if(data[0] == 'X') { break;
// Clear Config case 'X': // Clear Config
res.cmd = (messages::id) 'X'; cmd = 'X';
res.ret = OK; ret = OK;
} else if(data[0] == 'D') { break;
// Disable LED case 'D': // Disable LED
syslog("G\n"); syslog("G\n");
signal_led(false); signal_led(false);
res.cmd = (messages::id) 'G'; cmd = 'G';
res.ret = OK; ret = OK;
res.val = 1; val = 1;
} else if(data[0] == 'E') { break;
// Enable LED case 'E': // Enable LED
syslog("E\n"); syslog("E\n");
signal_led(true); signal_led(true);
res.cmd = (messages::id) 'G'; cmd = 'G';
res.ret = OK; ret = OK;
res.val = 0; val = 0;
} else if(data[0] == 'F') { break;
togl = !togl; case 'F':
signal_led(togl); togl = !togl;
auto *f = (messages::fw_frame *) data; signal_led(togl);
if(f->seq == 0) { {
system_otaflash_init(); auto *f = (fw_frame *) data;
} if(f->seq == 0) {
uint16_t ack = 0; system_otaflash_init();
res.ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack); }
res.val = htons(ack); uint16_t ack = 0;
res.cmd = (messages::id) 'F'; ret = system_otaflash_chunk(f->data, ntohs(f->len), ntohs(f->seq), ntohl(f->hash), &ack);
} else if(data[0] == 'C') { val = htons(ack);
signal_led(false); }
auto *f = (messages::fw_check *) data; cmd = 'F';
res.ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash)); break;
res.cmd = (messages::id) 'C'; case 'C':
} else if(data[0] == 'S') { signal_led(false);
int8_t en = 0; {
if(data[1] == 'E') auto *f = (fw_check *) data;
en = 1; ret = system_otaflash_verify_and_switch(ntohl(f->len), ntohl(f->hash));
char *ssid = &data[2]; }
size_t ssid_len = strlen(ssid); cmd = 'C';
char *password = &data[3 + ssid_len]; break;
size_t password_len = strlen(password); default:
(void) password_len; printf("[websocket_callback]:\n%.*s\n", (int) data_len, (char *) data);
printf("Unknown command %c\n", data[0]);
sysparam_set_int8("wifi_sta_enable", en); ret = ERROR;
sysparam_set_string("wifi_sta_ssid", ssid); break;
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(); LOCK_TCPIP_CORE();
websocket_write(pcb, (uint8_t * ) & res, sizeof(res), WS_BIN_MODE); websocket_write(pcb, response, 4, WS_BIN_MODE);
UNLOCK_TCPIP_CORE(); UNLOCK_TCPIP_CORE();
if(res.ret == OK) { if(ret == OK) {
if(res.cmd == 'R' || res.cmd == 'C') { // Restart if(cmd == 'R' || cmd == 'C') { // Restart
printf("rebooting now"); printf("rebooting now");
taskYIELD(); vTaskDelay(1000 / portTICK_PERIOD_MS);
vPortEnterCritical(); vPortEnterCritical();
sdk_system_restart(); sdk_system_restart();
} else if(res.cmd == 'X') { // Clear Config } else if(cmd == 'X') { // Clear Config
taskYIELD(); vTaskDelay(1000 / portTICK_PERIOD_MS);
system_clear_config(); system_clear_config();
} }
} }
@ -358,42 +327,13 @@ 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) { extern "C" void httpd_task(void *pvParameters) {
(void) pvParameters; (void) pvParameters;
//while (!uxSemaphoreGetCount(wifi_available_semaphore)) while (!uxSemaphoreGetCount(wifi_available_semaphore))
// vTaskDelay(500 / portTICK_PERIOD_MS); 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); websocket_register_callbacks((tWsOpenHandler) websocket_open_cb, (tWsHandler) websocket_cb);
httpd_init(); httpd_init();
while (1) { vTaskDelete(nullptr);
voltage_val = sdk_system_adc_read();
has_changed.voltage = true;
vTaskDelayMs(1000);
}
} }

View file

@ -17,7 +17,6 @@
<label for="bmenub" class="burger pseudo button">&#9776;</label> <label for="bmenub" class="burger pseudo button">&#9776;</label>
<div class="menu"> <div class="menu">
<a href="/#" class="button icon-picture">Dashboard</a> <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> <a href="/#ota" class="button icon-picture">System</a>
</div> </div>
</nav> </nav>
@ -76,87 +75,6 @@
</article> </article>
</section> </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"> <section id="dashboard">
<h2>Status</h2> <h2>Status</h2>
<div class="flex"> <div class="flex">
@ -294,18 +212,6 @@
return pos; 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 ws;
var retries; var retries;
var series = new TimeSeries(); var series = new TimeSeries();
@ -348,7 +254,6 @@
document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values); document.getElementById("unused_values").innerHTML = JSON.stringify(unused_values);
} else { } else {
var dv = new DataView(evt.data); var dv = new DataView(evt.data);
console.log("[0]",dv.getUint8(0));
var cmd = String.fromCharCode(dv.getUint8(0)); var cmd = String.fromCharCode(dv.getUint8(0));
var val = dv.getUint16(1); var val = dv.getUint16(1);
@ -528,48 +433,6 @@
} }
} }
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) { function update_progress(progressBar, progress) {
var iP = Math.floor(progress); var iP = Math.floor(progress);
var dBar = document.getElementById("progress_bar_" + progressBar); var dBar = document.getElementById("progress_bar_" + progressBar);

View file

@ -32,7 +32,6 @@ char *wifi_ap_ssid = nullptr;
char *wifi_ap_password = nullptr; char *wifi_ap_password = nullptr;
SemaphoreHandle_t wifi_available_semaphore = nullptr; SemaphoreHandle_t wifi_available_semaphore = nullptr;
SemaphoreHandle_t wifi_alive = nullptr;
[[noreturn]] static void dns_task(void *pvParameters) { [[noreturn]] static void dns_task(void *pvParameters) {
char *wifi_ap_ip_addr = nullptr; char *wifi_ap_ip_addr = nullptr;
@ -212,7 +211,7 @@ extern "C" void wifi_task(void *pvParameters) {
if(wifi_sta_enable) { if(wifi_sta_enable) {
printf("try STA Mode: %s %s\n", wifi_sta_ssid, wifi_sta_password); 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.ssid, wifi_sta_ssid);
strcpy((char *) config.password, wifi_sta_password); strcpy((char *) config.password, wifi_sta_password);
config.bssid_set = 0; config.bssid_set = 0;
@ -232,7 +231,7 @@ extern "C" void wifi_task(void *pvParameters) {
wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 && wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 &&
wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) { wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) {
sdk_wifi_station_dhcpc_stop(); sdk_wifi_station_dhcpc_stop();
ip_info info{}; ip_info info;
memset(&info, 0x0, sizeof(info)); memset(&info, 0x0, sizeof(info));
info.ip.addr = ipaddr_addr(wifi_sta_ip_addr); info.ip.addr = ipaddr_addr(wifi_sta_ip_addr);
info.netmask.addr = ipaddr_addr(wifi_sta_netmask); info.netmask.addr = ipaddr_addr(wifi_sta_netmask);
@ -259,6 +258,16 @@ extern "C" void wifi_task(void *pvParameters) {
int8_t wifi_ap_channel = 6; int8_t wifi_ap_channel = 6;
sysparam_get_int8("wifi_ap_channel", &wifi_ap_channel); 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) { if(wifi_ap_channel < 1 || wifi_ap_channel > 14) {
wifi_ap_channel = 6; wifi_ap_channel = 6;
} }
@ -297,13 +306,13 @@ extern "C" void wifi_task(void *pvParameters) {
} }
if(strlen(wifi_ap_ip_addr) >= 7 && strlen(wifi_ap_netmask) >= 7) { 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.ip.addr = ipaddr_addr(wifi_ap_ip_addr);
ap_ip.netmask.addr = ipaddr_addr(wifi_ap_netmask); ap_ip.netmask.addr = ipaddr_addr(wifi_ap_netmask);
IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0); IP4_ADDR(&ap_ip.gw, 0, 0, 0, 0);
sdk_wifi_set_ip_info(1, &ap_ip); 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); strcpy((char *) ap_config.ssid, wifi_ap_ssid);
ap_config.ssid_len = strlen(wifi_ap_ssid); ap_config.ssid_len = strlen(wifi_ap_ssid);
strcpy((char *) ap_config.password, wifi_ap_password); strcpy((char *) ap_config.password, wifi_ap_password);
@ -348,38 +357,5 @@ extern "C" void wifi_task(void *pvParameters) {
xSemaphoreGive(wifi_available_semaphore); xSemaphoreGive(wifi_available_semaphore);
//monitor loop connection here //monitor loop connection here
uint8_t status = 0; vTaskDelete(nullptr);
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,7 +13,6 @@ extern "C" {
#endif #endif
extern SemaphoreHandle_t wifi_available_semaphore; extern SemaphoreHandle_t wifi_available_semaphore;
extern SemaphoreHandle_t wifi_alive;
void wifi_task(void *pvParameters); void wifi_task(void *pvParameters);

1
pcb/.gitignore vendored
View file

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