Compare commits

...

10 commits

Author SHA1 Message Date
831d6f665c Ãtest 2021-07-14 22:56:58 +02:00
f9838748b2 Join Mqtt and Docker branch 2021-07-14 22:50:04 +02:00
97339527ee use python3 as default in docker 2021-07-14 22:48:10 +02:00
300cadf627 add Dockerfile for build environment 2021-07-14 22:43:28 +02:00
3098c8f1ab stash 2021-07-14 22:42:06 +02:00
8ffae0b66f keep refactoring 2021-07-14 22:40:49 +02:00
d75262dbea refactoring code 2021-07-14 22:40:03 +02:00
f89f3e1816 basic webconf 2021-07-14 22:37:39 +02:00
0814fc837f add fsdata webdir 2021-07-14 22:35:18 +02:00
9b0f98f7fb add basic build info to README.md 2021-07-14 22:30:10 +02:00
20 changed files with 1733 additions and 8 deletions

View file

@ -4,14 +4,14 @@
all: firmware case pcb all: firmware case pcb
firmware: firmware:
+@make -C firmware all +@make -C firmware html all
clean: clean:
+@make -C firmware clean +@make -C firmware clean
firmware_docker: firmware_docker:
sh -c "docker build -t fiatlux_env docker" sh -c "docker build -t fiatlux_env docker"
sh -c "docker run --volume "$$(pwd)"/firmware:/app/firmware fiatlux_env make -C firmware all" sh -c "docker run --volume "$$(pwd)"/firmware:/app/firmware fiatlux_env make -C firmware html all"
clean_docker: clean_docker:
sh -c "docker build -t fiatlux_env docker" sh -c "docker build -t fiatlux_env docker"

View file

@ -59,4 +59,3 @@ install `docker` on your platform and buil with
```bash ```bash
make firmware_docker -j$(nproc) make firmware_docker -j$(nproc)
```

1
firmware/.gitignore vendored
View file

@ -143,3 +143,4 @@ dkms.conf
*.remove *.remove
firmware/ firmware/
compile_commands.json compile_commands.json
fsdata/fsdata.c

View file

@ -5,7 +5,7 @@ EXTRA_CFLAGS=-O3
#Enable debugging #Enable debugging
#EXTRA_CFLAGS+=-DLWIP_DEBUG=1 -DHTTPD_DEBUG=LWIP_DBG_ON #EXTRA_CFLAGS+=-DLWIP_DEBUG=1 -DHTTPD_DEBUG=LWIP_DBG_ON
EXTRA_COMPONENTS=extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/mbedtls extras/sntp extras/cpp_support EXTRA_COMPONENTS=extras/paho_mqtt_c extras/i2s_dma extras/ws2812_i2s extras/dhcpserver extras/mbedtls extras/httpd extras/sntp extras/cpp_support
LIBS = hal m LIBS = hal m
@ -24,3 +24,5 @@ unittest:
systest: systest:
true true
.NOTPARALLEL: html all

View file

@ -8,13 +8,409 @@
#include "mqtt.h" #include "mqtt.h"
#include "lux.h" #include "lux.h"
#define LED_PIN 2
#define SWITCH_PIN 2
void user_init(void) /* Add extras/sntp component to makefile for this include to work */
{ #include <sntp.h>
#include <time.h>
#include "wifi.h"
#include "web.h"
#include "mqtt.h"
#include "lux.h"
#define SNTP_SERVERS "0.pool.ntp.org", "1.pool.ntp.org", \
"2.pool.ntp.org", "3.pool.ntp.org"
#define vTaskDelayMs(ms) vTaskDelay((ms)/portTICK_PERIOD_MS)
#define UNUSED_ARG(x) (void)x
const gpio_inttype_t int_type = GPIO_INTTYPE_EDGE_NEG;
void sntp_task(void *pvParameters) {
const char *servers[] = {SNTP_SERVERS};
UNUSED_ARG(pvParameters);
/* Wait until we have joined AP and are assigned an IP */
while (sdk_wifi_station_get_connect_status() != STATION_GOT_IP) {
vTaskDelayMs(100);
}
/* Start SNTP */
printf("Starting SNTP... ");
/* SNTP will request an update each 5 minutes */
sntp_set_update_delay(5 * 60000);
/* Set GMT+1 zone, daylight savings off */
const struct timezone tz = {1 * 60, 1};
/* SNTP initialization */
sntp_initialize(&tz);
/* Servers must be configured right after initialization */
sntp_set_servers(servers, sizeof(servers) / sizeof(char *));
printf("DONE!\n");
/* Print date and time each 5 seconds */
while (1) {
vTaskDelayMs(5000);
//time_t ts = time(NULL);
//int t = ts;
//printf("TIME: %d %d %s", t,(int) day_seconds(), ctime(&ts));
}
}
static void dns_task(void *pvParameters) {
char *wifi_ap_ip_addr = NULL;
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
if(!wifi_ap_ip_addr) {
printf("dns: no ip address\n");
vTaskDelete(NULL);
}
ip4_addr_t server_addr;
server_addr.addr = ipaddr_addr(wifi_ap_ip_addr);
#if LWIP_IPV6
int fd = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in6 serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin6_family = AF_INET6;
serv_addr.sin6_port = htons(53);
serv_addr.sin6_flowinfo = 0;
serv_addr.sin6_addr = in6addr_any;
serv_addr.sin6_scope_id = IP6_NO_ZONE;
#else
int fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
struct sockaddr_in serv_addr;
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(53);
#endif
bind(fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
const struct ifreq ifreq0 = {"en0"};
const struct ifreq ifreq1 = {"en1"};
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE,
sdk_wifi_get_opmode() == STATIONAP_MODE ? &ifreq1 : &ifreq0,
sizeof(ifreq0));
for (;;) {
char buffer[96];
struct sockaddr_storage src_addr;
socklen_t src_addr_len = sizeof(src_addr);
ssize_t count = recvfrom(fd, buffer, sizeof(buffer), 0, (struct sockaddr *) &src_addr, &src_addr_len);
/* Drop messages that are too large to send a response in the buffer */
if(count > 0 && count <= sizeof(buffer) - 16) {
size_t qname_len = strlen(buffer + 12) + 1;
uint32_t reply_len = 2 + 10 + qname_len + 16 + 4;
char *head = buffer + 2;
*head++ = 0x80; // Flags
*head++ = 0x00;
*head++ = 0x00; // Q count
*head++ = 0x01;
*head++ = 0x00; // A count
*head++ = 0x01;
*head++ = 0x00; // Auth count
*head++ = 0x00;
*head++ = 0x00; // Add count
*head++ = 0x00;
head += qname_len;
*head++ = 0x00; // Q type
*head++ = 0x01;
*head++ = 0x00; // Q class
*head++ = 0x01;
*head++ = 0xC0; // LBL offs
*head++ = 0x0C;
*head++ = 0x00; // Type
*head++ = 0x01;
*head++ = 0x00; // Class
*head++ = 0x01;
*head++ = 0x00; // TTL
*head++ = 0x00;
*head++ = 0x00;
*head++ = 0x78;
*head++ = 0x00; // RD len
*head++ = 0x04;
*head++ = ip4_addr1(&server_addr);
*head++ = ip4_addr2(&server_addr);
*head++ = ip4_addr3(&server_addr);
*head++ = ip4_addr4(&server_addr);
sendto(fd, buffer, reply_len, 0, (struct sockaddr *) &src_addr, src_addr_len);
}
}
}
const char *wificfg_default_ssid = "fiatlux_%02X%02X%02X";
const char *wificfg_default_password = "fiatlux02";
const char *wificfg_default_hostname = "fiatlux-%02x%02x%02x";
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());
sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM); sdk_wifi_set_sleep_type(WIFI_SLEEP_MODEM);
system_init_config(); system_init_config();
char *wifi_sta_ssid = NULL;
char *wifi_sta_password = NULL;
char *wifi_ap_ssid = NULL;
char *wifi_ap_password = NULL;
/* Default a hostname. */
char *hostname = NULL;
sysparam_get_string("hostname", &hostname);
if(!hostname && wificfg_default_hostname) {
uint8_t macaddr[6];
char name[32];
sdk_wifi_get_macaddr(1, macaddr);
snprintf(name, sizeof(name), wificfg_default_hostname, macaddr[3],
macaddr[4], macaddr[5]);
sysparam_set_string("hostname", name);
}
if(hostname) {
free(hostname);
}
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid);
sysparam_get_string("wifi_ap_password", &wifi_ap_password);
sysparam_get_string("wifi_sta_ssid", &wifi_sta_ssid);
sysparam_get_string("wifi_sta_password", &wifi_sta_password);
int8_t wifi_sta_enable = 1;
int8_t wifi_ap_enable = 1;
sysparam_get_int8("wifi_sta_enable", &wifi_sta_enable);
sysparam_get_int8("wifi_ap_enable", &wifi_ap_enable);
if(!wifi_sta_enable)
wifi_ap_enable = 1;
int8_t wifi_sta_disabled_restarts = 0;
sysparam_get_int8("wifi_sta_disabled_restarts", &wifi_sta_disabled_restarts);
if(wifi_sta_disabled_restarts > 0) {
wifi_sta_enable = 0;
wifi_sta_disabled_restarts--;
sysparam_set_int8("wifi_sta_disabled_restarts", wifi_sta_disabled_restarts);
}
int8_t wifi_ap_disabled_restarts = 0;
sysparam_get_int8("wifi_ap_disabled_restarts", &wifi_ap_disabled_restarts);
if(wifi_ap_disabled_restarts > 0) {
wifi_ap_enable = 0;
wifi_ap_disabled_restarts--;
sysparam_set_int8("wifi_ap_disabled_restarts", wifi_ap_disabled_restarts);
}
/* Validate the configuration. */
if(wifi_sta_enable && (!wifi_sta_ssid || !wifi_sta_password ||
strlen(wifi_sta_ssid) < 1 ||
strlen(wifi_sta_ssid) > 32 ||
!wifi_sta_password ||
strlen(wifi_sta_password) < 8 ||
strlen(wifi_sta_password) >= 64)) {
wifi_sta_enable = 0;
}
if(wifi_ap_enable) {
/* Default AP ssid and password. */
if(!wifi_ap_ssid && wificfg_default_ssid) {
uint8_t macaddr[6];
char ssid[32];
sdk_wifi_get_macaddr(1, macaddr);
snprintf(ssid, sizeof(ssid), wificfg_default_ssid, macaddr[3],
macaddr[4], macaddr[5]);
sysparam_set_string("wifi_ap_ssid", ssid);
sysparam_get_string("wifi_ap_ssid", &wifi_ap_ssid);
if(!wifi_ap_password && wificfg_default_password) {
sysparam_set_string("wifi_ap_password", wificfg_default_password);
sysparam_get_string("wifi_ap_password", &wifi_ap_password);
}
}
printf("ssid: %s\n", wifi_ap_ssid);
/* If the ssid and password are not valid then disable the AP interface. */
if(!wifi_ap_ssid || strlen(wifi_ap_ssid) < 1 || strlen(wifi_ap_ssid) >= 32 ||
!wifi_ap_password || strlen(wifi_ap_password) < 8 || strlen(wifi_ap_password) >= 64) {
printf("len err\n");
wifi_ap_enable = 0;
}
}
int8_t wifi_mode = NULL_MODE;
if(wifi_sta_enable && wifi_ap_enable)
wifi_mode = STATIONAP_MODE;
else if(wifi_sta_enable)
wifi_mode = STATION_MODE;
else if(wifi_ap_enable)
wifi_mode = SOFTAP_MODE;
sdk_wifi_set_opmode(wifi_mode);
if(wifi_sta_enable) {
printf("try STA Mode: %s %s\n", wifi_sta_ssid, wifi_sta_password);
struct sdk_station_config config;
strcpy((char *) config.ssid, wifi_sta_ssid);
strcpy((char *) config.password, wifi_sta_password);
config.bssid_set = 0;
int8_t wifi_sta_dhcp = 1;
sysparam_get_int8("wifi_sta_dhcp", &wifi_sta_dhcp);
if(!wifi_sta_dhcp) {
char *wifi_sta_ip_addr = NULL;
char *wifi_sta_netmask = NULL;
char *wifi_sta_gateway = NULL;
sysparam_get_string("wifi_sta_ip_addr", &wifi_sta_ip_addr);
sysparam_get_string("wifi_sta_netmask", &wifi_sta_netmask);
sysparam_get_string("wifi_sta_gateway", &wifi_sta_gateway);
if(wifi_sta_ip_addr && strlen(wifi_sta_ip_addr) > 4 &&
wifi_sta_netmask && strlen(wifi_sta_netmask) > 4 &&
wifi_sta_gateway && strlen(wifi_sta_gateway) > 4) {
sdk_wifi_station_dhcpc_stop();
struct 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);
info.gw.addr = ipaddr_addr(wifi_sta_gateway);
sdk_wifi_set_ip_info(STATION_IF, &info);
}
if(wifi_sta_ip_addr) free(wifi_sta_ip_addr);
if(wifi_sta_netmask) free(wifi_sta_netmask);
if(wifi_sta_gateway) free(wifi_sta_gateway);
}
sdk_wifi_station_set_config(&config);
}
if(wifi_ap_enable) {
printf("try AP Mode: %s %s\n", wifi_ap_ssid, wifi_ap_password);
/* Read and validate paramenters. */
int8_t wifi_ap_ssid_hidden = 0;
sysparam_get_int8("wifi_ap_ssid_hidden", &wifi_ap_ssid_hidden);
if(wifi_ap_ssid_hidden < 0 || wifi_ap_ssid_hidden > 1) {
wifi_ap_ssid_hidden = 1;
}
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;
}
int8_t wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
sysparam_get_int8("wifi_ap_authmode", &wifi_ap_authmode);
if(wifi_ap_authmode != AUTH_OPEN && wifi_ap_authmode != AUTH_WPA_PSK &&
wifi_ap_authmode != AUTH_WPA2_PSK && wifi_ap_authmode != AUTH_WPA_WPA2_PSK) {
wifi_ap_authmode = AUTH_WPA_WPA2_PSK;
}
int8_t wifi_ap_max_conn = 3;
sysparam_get_int8("wifi_ap_max_conn", &wifi_ap_max_conn);
if(wifi_ap_max_conn < 1 || wifi_ap_max_conn > 8) {
wifi_ap_max_conn = 3;
}
int32_t wifi_ap_beacon_interval = 100;
sysparam_get_int32("wifi_ap_beacon_interval", &wifi_ap_beacon_interval);
if(wifi_ap_beacon_interval < 0 || wifi_ap_beacon_interval > 1000) {
wifi_ap_beacon_interval = 100;
}
/* Default AP IP address and netmask. */
char *wifi_ap_ip_addr = NULL;
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
if(!wifi_ap_ip_addr) {
sysparam_set_string("wifi_ap_ip_addr", "172.16.0.1");
sysparam_get_string("wifi_ap_ip_addr", &wifi_ap_ip_addr);
}
char *wifi_ap_netmask = NULL;
sysparam_get_string("wifi_ap_netmask", &wifi_ap_netmask);
if(!wifi_ap_netmask) {
sysparam_set_string("wifi_ap_netmask", "255.255.0.0");
sysparam_get_string("wifi_ap_netmask", &wifi_ap_netmask);
}
if(strlen(wifi_ap_ip_addr) >= 7 && strlen(wifi_ap_netmask) >= 7) {
struct 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);
struct sdk_softap_config ap_config = {
.ssid_hidden = wifi_ap_ssid_hidden,
.channel = wifi_ap_channel,
.authmode = wifi_ap_authmode,
.max_connection = wifi_ap_max_conn,
.beacon_interval = wifi_ap_beacon_interval,
};
strcpy((char *) ap_config.ssid, wifi_ap_ssid);
ap_config.ssid_len = strlen(wifi_ap_ssid);
strcpy((char *) ap_config.password, wifi_ap_password);
sdk_wifi_softap_set_config(&ap_config);
int8_t wifi_ap_dhcp_leases = 4;
sysparam_get_int8("wifi_ap_dhcp_leases", &wifi_ap_dhcp_leases);
if(wifi_ap_dhcp_leases) {
ip4_addr_t first_client_ip;
first_client_ip.addr = ap_ip.ip.addr + htonl(1);
int8_t wifi_ap_dns = 1;
sysparam_get_int8("wifi_ap_dns", &wifi_ap_dns);
if(wifi_ap_dns < 0 || wifi_ap_dns > 1)
wifi_ap_dns = 1;
dhcpserver_start(&first_client_ip, wifi_ap_dhcp_leases);
dhcpserver_set_router(&ap_ip.ip);
if(wifi_ap_dns) {
dhcpserver_set_dns(&ap_ip.ip);
xTaskCreate(dns_task, "WiFi Cfg DNS", 384, NULL, 2, NULL);
}
}
}
free(wifi_ap_ip_addr);
free(wifi_ap_netmask);
}
if(wifi_sta_ssid) free(wifi_sta_ssid);
if(wifi_sta_password) free(wifi_sta_password);
if(wifi_ap_ssid) free(wifi_ap_ssid);
if(wifi_ap_password) free(wifi_ap_password);
if(wifi_mode != NULL_MODE) {
/* turn off LED */
//gpio_enable(LED_PIN, GPIO_OUTPUT);
//gpio_write(LED_PIN, true);
xTaskCreate(&lux_task, "lux_task", 256, NULL, 1, NULL);
/* initialize tasks */
xTaskCreate(&httpd_task, "httpd_task", 2048, NULL, 3, NULL);
xTaskCreate(&sntp_task, "SNTP", 512, NULL, 1, NULL);
// xTaskCreate(&beat_task, "beat_task", 256, NULL, 3, NULL);
xTaskCreate(&mqtt_task, "mqtt_task", 512, NULL, 4, NULL);
}
>>>>>>> 4b8d354 (basic webconf)
} }

View file

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no">
<link rel="shortcut icon" href="img/favicon.png">
<title>HTTP Server</title>
</head>
<body>
<ul class="navbar">
<li><a href="/">Home</a></li>
<li><a href="websockets">WebSockets</a></li>
<li><a href="about">About</a></li>
</ul>
<div class="grid main">
<h1>404 - Page not found</h1>
<div class="alert alert-error">Sorry, the page you are requesting was not found on this server.</div>
</div>
</body>
</html>

1
firmware/fsdata/fs/css/picnic.min.css vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,62 @@
main {
padding: 4em 2em 2em 2em;
max-width: 960px;
width: 100%;
margin-left: auto;
margin-right: auto;
}
section {
display: none;
}
canvas{
width: 100%;
}
main[data-page="dashboard"] section[id="dashboard"] {
display: block;
}
main[data-page="ota"] section[id="ota"] {
display: block;
}
main[data-page="wifi"] section[id="wifi"] {
display: block;
}
main[data-page="io"] section[id="io"] {
display: block;
}
.table {
width: 100%;
display: table;
}
.table>.row{
display: table-row;
}
.table>.row:nth-child(2n) {
background: rgba(17,17,17,0.05);
}
.table>.row>*{
display: table-cell;
padding: .3em 2.4em .3em .6em;
}
.table>header.row>*{
text-align: left;
font-weight: 900;
color: #fff;
background-color: #0074d9;
}
.table>.row>input{
border: none;
background: none;
font-weight: 900;
}
.plain{
opacity: initial;
width: initial;
}

View file

@ -0,0 +1,477 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>fiatlux v0.2</title>
<link rel="stylesheet" href="css/picnic.min.css">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<nav>
<a href="#" class="brand">
<!--img class="logo" src="/img/basket.png" /-->
<span>fiatlux v0.2</span>
<span class="label warning" id="status_box">Loading...</span>
</a>
<!-- responsive-->
<input id="bmenub" type="checkbox" class="show">
<label for="bmenub" class="burger pseudo button">&#9776;</label>
<div class="menu">
<a href="/" class="button icon-picture" onclick="use_page('dashboard'); return false">Dashboard</a>
<a href="/io" class="button icon-puzzle" onclick="use_page('io'); return false">I/O</a>
<a href="/wifi" class="button icon-puzzle" onclick="use_page('wifi'); return false">Wifi Settings</a>
<a href="/ota" class="button icon-picture" onclick="use_page('ota'); return false">System</a>
</div>
</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>
<article class="card">
<header>
<h3>I/O</h3>
</header>
<div class="table">
<div class="row">
<span>Channel 1-6</span>
<span>SPI Dimmer</span>
</div>
<div class="row">
<span>Channel 7-127</span>
<span>WS2812 via I2S</span>
</div>
</div>
<footer>
<label>
<input type="checkbox" name="onoffswitch" id="led-switch" onclick="gpio()">
<span class="toggle button">Toggle Something</span>
</label>
</footer>
</article>
</div>
</div>
</section>
<section id="ota">
<h2>System</h2>
<article class="card">
<header>
<h3>Firmware Update</h3>
</header>
<div class="table">
<div class="row">
<span><input type="file"/></span>
</div>
<div class="row">
<span><input type="submit" value="Upload"></span>
</div>
</div>
</article>
<article class="card">
<header>
<h3>Restart</h3>
</header>
<div class="table">
<div class="row">
<span><input type="submit" value="Restart" onclick="wsWrite('R')"></span>
</div>
</div>
</article>
<article class="card">
<header>
<h3>Reset Config</h3>
</header>
<div class="table">
<div class="row">
<span><input type="submit" class="warning" value="Reset" onclick="wsWrite('X')"></span>
</div>
</div>
</article>
</section>
<section id="io">
<h2>I/O</h2>
<article class="card">
<header>
<h3>Protocols</h3>
</header>
<div class="table">
<div class="row">
<label>MQTT</label>
<span><input type="checkbox" class="plain"/></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input 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>WS2812 via I2S</label>
<span><input type="checkbox" class="plain"/></span>
<span><input type="color"/></span>
</div>
<div class="row">
<label>APA102C via SPI</label>
<span><input type="checkbox" class="plain"/></span>
<span><input type="color"/></span>
</div>
<div class="row">
<label>6 Channel SPI Dimmer</label>
<span><input type="checkbox" class="plain"/></span>
<span><input type="color"/></span>
</div>
<div class="row">
<label>Binary Output on GPIO4</label>
<span><input type="checkbox" class="plain"/></span>
<span><button class="toggle button">Toggle</button></span>
</div>
<div class="row">
<label>Binary Output on GPIO5</label>
<span><input type="checkbox" class="plain"/></span>
<span><button class="toggle button">Toggle</button></span>
</div>
<div class="row">
<span><input type="reset" class="button"/></span>
<span><input type="submit" value="Save"></span>
</div>
</div>
</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">
<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>
</main>
<div id="unused_values" style="display:none;"></div>
<script type="text/javascript" src="js/smoothie_min.js"></script>
<script>
var section = document.getElementById("page");
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;
};
function use_page(name) {
section.dataset.page = name;
menu.checked = false
}
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);
setInterval(function () {
wsWrite('V');
}, 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");
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.log(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');
}
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);
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);
wsWrite(buffer);
}
window.onload = function () {
wsOpen();
startPolling();
}
</script>
</body>
</html>

File diff suppressed because one or more lines are too long

114
firmware/fsdata/makefsdata Executable file
View file

@ -0,0 +1,114 @@
#!/usr/bin/perl
$incHttpHeader = 1;
open(OUTPUT, "> fsdata.c");
print(OUTPUT "#include \"httpd/fsdata.h\"\n\n");
chdir("fs");
open(FILES, "find . -type f |");
while($file = <FILES>) {
# Do not include files in CVS directories nor backup files.
if($file =~ /(CVS|~)/) {
next;
}
chop($file);
if($incHttpHeader == 1) {
open(HEADER, "> /tmp/header") || die $!;
if($file =~ /404/) {
print(HEADER "HTTP/1.0 404 File not found\r\n");
} else {
print(HEADER "HTTP/1.0 200 OK\r\n");
}
print(HEADER "lwIP/1.4.1 (http://savannah.nongnu.org/projects/lwip)\r\n");
if($file =~ /\.html$/ || $file =~ /\.htm$/ || $file =~ /\.shtml$/ || $file =~ /\.shtm$/ || $file =~ /\.ssi$/) {
print(HEADER "Content-type: text/html\r\n");
} elsif($file =~ /\.js$/) {
print(HEADER "Content-type: application/x-javascript\r\n\r\n");
} elsif($file =~ /\.css$/) {
print(HEADER "Content-type: text/css\r\n\r\n");
} elsif($file =~ /\.ico$/) {
print(HEADER "Content-type: image/x-icon\r\n\r\n");
} elsif($file =~ /\.gif$/) {
print(HEADER "Content-type: image/gif\r\n");
} elsif($file =~ /\.png$/) {
print(HEADER "Content-type: image/png\r\n");
} elsif($file =~ /\.jpg$/) {
print(HEADER "Content-type: image/jpeg\r\n");
} elsif($file =~ /\.bmp$/) {
print(HEADER "Content-type: image/bmp\r\n\r\n");
} elsif($file =~ /\.class$/) {
print(HEADER "Content-type: application/octet-stream\r\n");
} elsif($file =~ /\.ram$/) {
print(HEADER "Content-type: audio/x-pn-realaudio\r\n");
} else {
print(HEADER "Content-type: text/plain\r\n");
}
print(HEADER "\r\n");
close(HEADER);
unless($file =~ /\.plain$/ || $file =~ /cgi/) {
system("cat /tmp/header $file > /tmp/file");
} else {
system("cp $file /tmp/file");
}
} else {
system("cp $file /tmp/file");
}
open(FILE, "/tmp/file");
unlink("/tmp/file");
unlink("/tmp/header");
$file =~ s/\.//;
$fvar = $file;
$fvar =~ s-/-_-g;
$fvar =~ s-\.-_-g;
print(OUTPUT "static const unsigned char data".$fvar."[] = {\n");
print(OUTPUT "\t/* $file */\n\t");
for($j = 0; $j < length($file); $j++) {
printf(OUTPUT "0x%02X, ", unpack("C", substr($file, $j, 1)));
}
printf(OUTPUT "0,\n");
$i = 0;
while(read(FILE, $data, 1)) {
if($i == 0) {
print(OUTPUT "\t");
}
printf(OUTPUT "0x%02X, ", unpack("C", $data));
$i++;
if($i == 10) {
print(OUTPUT "\n");
$i = 0;
}
}
print(OUTPUT "};\n\n");
close(FILE);
push(@fvars, $fvar);
push(@files, $file);
}
for($i = 0; $i < @fvars; $i++) {
$file = $files[$i];
$fvar = $fvars[$i];
if($i == 0) {
$prevfile = "NULL";
} else {
$prevfile = "file" . $fvars[$i - 1];
}
print(OUTPUT "const struct fsdata_file file".$fvar."[] = {{\n$prevfile,\ndata$fvar, ");
print(OUTPUT "data$fvar + ". (length($file) + 1) .",\n");
print(OUTPUT "sizeof(data$fvar) - ". (length($file) + 1) .",\n");
print(OUTPUT $incHttpHeader."\n}};\n\n");
}
print(OUTPUT "#define FS_ROOT file$fvars[$i - 1]\n\n");
print(OUTPUT "#define FS_NUMFILES $i\n");

View file

@ -1,3 +1,103 @@
// //
// Created by jedi on 25.06.21. // Created by jedi on 25.06.21.
// //
<<<<<<< HEAD
=======
#include "lux.h"
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
#include <esp/spi.h>
#include <ws2812_i2s/ws2812_i2s.h>
const int LED_PIN = 2;
const int SWITCH_PIN = 2;
const int cs0 = 15;
const int gpio4 = 4;
const int gpio5 = 5;
static ws2812_pixel_t next_colour(int i) {
ws2812_pixel_t colour = {{0, 0, 0, 0}};
if(i == 8) {
colour.white = 32;
} else {
colour.red = i & 1 ? 32 : 0;
colour.green = i & 2 ? 32 : 0;
colour.blue = i & 4 ? 32 : 0;
}
return colour;
}
void spi_dac(int id, int val) {
int dac_val = (val << 2) & 0x3FFC;
spi_transfer_8(1, ~(0x00));
gpio_write(cs0, 1);
gpio_write(cs0, 0);
spi_transfer_8(1, ~(0x01 << id));
gpio_write(cs0, 1);
gpio_write(cs0, 0);
spi_transfer_16(1, dac_val);
spi_transfer_8(1, ~(0x00));
gpio_write(cs0, 1);
gpio_write(cs0, 0);
spi_transfer_8(1, ~(0x01 << id));
gpio_write(cs0, 1);
gpio_write(cs0, 0);
}
/* This task uses the high level GPIO API (esp_gpio.h) to blink an LED.
*
*/
extern "C" void lux_task(void *pvParameters) {
gpio_enable(9, GPIO_INPUT);
gpio_enable(10, GPIO_INPUT);
ws2812_pixel_t pixels[led_number];
ws2812_i2s_init(led_number, PIXEL_RGBW);
memset(pixels, 0, sizeof(ws2812_pixel_t) * led_number);
gpio_enable(cs0, GPIO_OUTPUT);
gpio_enable(gpio4, GPIO_OUTPUT);
gpio_enable(gpio5, GPIO_OUTPUT);
spi_init(1, SPI_MODE0, SPI_FREQ_DIV_1M, 1, SPI_BIG_ENDIAN, 1);
while (1) {
gpio_write(gpio4, 1);
vTaskDelay(200 / portTICK_PERIOD_MS);
gpio_write(gpio4, 0);
for (int j = 0; j < 64; j++) {
for (int i = 0; i < 8; i++)
spi_dac(i, 64 * j);
//printf("> %d\n", 64*j);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
for (int i = 0; i < 8; i++)
spi_dac(i, 0);
gpio_write(gpio5, 1);
vTaskDelay(200 / portTICK_PERIOD_MS);
gpio_write(gpio5, 0);
for (int c = 8; c >= 0; c--) {
for (int i = 0; i < led_number; i++) {
pixels[i] = next_colour(c);
}
ws2812_i2s_update(pixels, PIXEL_RGBW);
vTaskDelay(200 / portTICK_PERIOD_MS);
}
}
}
>>>>>>> 8d3006e (keep refactoring)

View file

@ -6,9 +6,21 @@
#define FIRMWARE_LUX_H #define FIRMWARE_LUX_H
#ifdef __cplusplus #ifdef __cplusplus
<<<<<<< HEAD
extern "C" { extern "C" {
#endif #endif
=======
constexpr int led_number = 8;
#endif
#ifdef __cplusplus
extern "C" {
#endif
void lux_task(void *pvParameters);
>>>>>>> 8d3006e (keep refactoring)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -3,3 +3,158 @@
// //
#include "mqtt.h" #include "mqtt.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.1.53")
#define MQTT_PORT 1883
#define MQTT_USER NULL
#define MQTT_PASS NULL
QueueHandle_t publish_queue;
#define PUB_MSG_LEN 16
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) {
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: %d\n\r", ret);
taskYIELD();
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: %d\n\r", ret);
mqtt_network_disconnect(&network);
taskYIELD();
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

@ -9,6 +9,9 @@
extern "C" { extern "C" {
#endif #endif
void mqtt_task(void *pvParameters);
void beat_task(void *pvParameters);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -20,6 +20,7 @@ void system_clear_config(){
spiflash_erase_sector(start + i * sdk_flashchip.sector_size); spiflash_erase_sector(start + i * sdk_flashchip.sector_size);
} }
sdk_system_restart(); sdk_system_restart();
<<<<<<< HEAD
} }
void system_init_config(){ void system_init_config(){
@ -34,4 +35,6 @@ void system_init_config(){
} }
sdk_system_restart(); sdk_system_restart();
} }
=======
>>>>>>> 7d1fb53 (refactoring code)
} }

View file

@ -10,10 +10,18 @@ extern "C" {
#endif #endif
void system_clear_config(); void system_clear_config();
<<<<<<< HEAD
void system_init_config(); void system_init_config();
=======
>>>>>>> 7d1fb53 (refactoring code)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
<<<<<<< HEAD
=======
>>>>>>> 7d1fb53 (refactoring code)
#endif //FIRMWARE_SYSTEM_H #endif //FIRMWARE_SYSTEM_H

View file

@ -1,3 +1,350 @@
// //
// Created by jedi on 25.06.21. // Created by jedi on 25.06.21.
<<<<<<< HEAD
// //
=======
//
#include "web.h"
#include "system.h"
#include <string.h>
#include <FreeRTOS.h>
#include <task.h>
extern "C" {
#include <sysparam.h>
#include <lwipopts.h>
}
#include <espressif/esp_common.h>
#include <lwip/tcp.h>
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) {
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) {
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) {
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 = NULL;
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(2000);
//printf("9: %d\n",gpio_read(9));
//printf("10: %d\n",gpio_read(10));
}
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';
uint8_t en = 0;
/*uint8_t ap_disable_if_sta = 0;
uint8_t ssid_hidden = 0;
uint8_t dns_enable = 0;
uint8_t mdns_enable = 0;*/
switch (data[0]) {
case 'V': // ADC
/* This should be done on a separate thread in 'real' applications */
val = sdk_system_adc_read();
cmd = 'V';
break;
case 'R': // Restart
cmd = 'R';
break;
case 'X': // Clear Config
cmd = 'X';
break;
case 'D': // Disable LED
//gpio_write(LED_PIN, true);
val = 0xDEAD;
cmd = 'G';
break;
case 'E': // Enable LED
//gpio_write(LED_PIN, false);
val = 0xBEEF;
cmd = 'G';
break;
case 'S': {
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);
}
cmd = 'S';
break;
case 'A': {
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_set_int8("wifi_ap_disable_if_sta", ap_disable_if_sta);
//sysparam_set_int8("wifi_ap_ssid_hidden", ssid_hidden);
//sysparam_set_int8("wifi_ap_dns", dns_enable);
//sysparam_set_int8("wifi_ap_mdns", mdns_enable);
}
cmd = 'A';
break;
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);
if(data[0] == 'R') { // Restart
vTaskDelay(500 / portTICK_PERIOD_MS);
vPortEnterCritical();
sdk_system_restart();
} else if(data[0] == 'X') { // Clear Config
vTaskDelay(500 / portTICK_PERIOD_MS);
system_clear_config();
}
}
/**
* 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, 2, NULL);
}
}
/*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) {
/*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();
for (;;);
}
>>>>>>> 7d1fb53 (refactoring code)

View file

@ -9,6 +9,11 @@
extern "C" { extern "C" {
#endif #endif
<<<<<<< HEAD
=======
void httpd_task(void *pvParameters);
>>>>>>> 7d1fb53 (refactoring code)
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View file

@ -5,6 +5,7 @@
#ifndef FIRMWARE_WIFI_H #ifndef FIRMWARE_WIFI_H
#define FIRMWARE_WIFI_H #define FIRMWARE_WIFI_H
<<<<<<< HEAD
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -13,4 +14,6 @@ extern "C" {
} }
#endif #endif
=======
>>>>>>> 7d1fb53 (refactoring code)
#endif //FIRMWARE_WIFI_H #endif //FIRMWARE_WIFI_H