implemented browser-esp comm; uart1 not working for some reason

pull/30/head
Ondřej Hruška 8 years ago
parent 521a00ce47
commit c2593c2207
  1. 2
      libesphttpd
  2. 126
      user/user_main.c
  3. 110
      user/web.c
  4. 12
      user/web.h

@ -1 +1 @@
Subproject commit 1a699948bb07d789f827d831f3994fc1b2d962de Subproject commit e5944123376d0d0a4f8547cd27b3aa7bef166f2a

@ -1,3 +1,9 @@
/**
* This is the ESP8266 Remote Terminal project main file.
*
* Front-end URLs and handlers are defined in web.c
*/
/* /*
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42): * "THE BEER-WARE LICENSE" (Revision 42):
@ -7,84 +13,18 @@
* ---------------------------------------------------------------------------- * ----------------------------------------------------------------------------
*/ */
/**
* This is the ESP8266 Remote Terminal project main file.
*/
#include <esp8266.h> #include <esp8266.h>
#include <httpdespfs.h> #include <httpdespfs.h>
#include <cgiwebsocket.h>
#include <captdns.h> #include <captdns.h>
#include <espfs.h> #include <espfs.h>
#include <webpages-espfs.h> #include <webpages-espfs.h>
#include "serial.h" #include "serial.h"
#include "io.h" #include "io.h"
#include "screen.h" #include "screen.h"
#include "web.h"
#define FIRMWARE_VERSION "0.1" #define FIRMWARE_VERSION "0.1"
/**
* Broadcast screen state to sockets
*/
void screen_notifyChange() {
// TODO cooldown / buffering to reduce nr of such events
dbg("Screen notifyChange");
void *data = NULL;
const int bufsiz = 512;
char buff[bufsiz];
for (int i = 0; i < 20; i++) {
httpd_cgi_state cont = screenSerializeToBuffer(buff, bufsiz, &data);
int flg = 0;
if (cont == HTTPD_CGI_MORE) flg |= WEBSOCK_FLAG_MORE;
if (i > 0) flg |= WEBSOCK_FLAG_CONT;
cgiWebsockBroadcast("/ws/update.cgi", buff, (int)strlen(buff), flg);
if (cont == HTTPD_CGI_DONE) break;
}
}
void myWebsocketRecv(Websock *ws, char *data, int len, int flags) {
dbg("Sock RX str: %s, len %d", data, len);
}
/** Socket connected for updates */
void ICACHE_FLASH_ATTR myWebsocketConnect(Websock *ws) {
dbg("Socket connected.");
ws->recvCb=myWebsocketRecv;
}
/**
* Main page template substitution
*
* @param connData
* @param token
* @param arg
* @return
*/
httpd_cgi_state ICACHE_FLASH_ATTR tplScreen(HttpdConnData *connData, char *token, void **arg) {
if (token==NULL) {
// Release data object
screenSerializeToBuffer(NULL, 0, arg);
return HTTPD_CGI_DONE;
}
const int bufsiz = 512;
char buff[bufsiz];
if (streq(token, "screenData")) {
httpd_cgi_state cont = screenSerializeToBuffer(buff, bufsiz, arg);
dbg("Sending buf: %s", buff);
httpdSend(connData, buff, -1);
return cont;
}
return HTTPD_CGI_DONE;
}
#ifdef ESPFS_POS #ifdef ESPFS_POS
CgiUploadFlashDef uploadParams={ CgiUploadFlashDef uploadParams={
.type=CGIFLASH_TYPE_ESPFS, .type=CGIFLASH_TYPE_ESPFS,
@ -105,34 +45,19 @@ CgiUploadFlashDef uploadParams={
#define INCLUDE_FLASH_FNS #define INCLUDE_FLASH_FNS
#endif #endif
/** Routes */
HttpdBuiltInUrl builtInUrls[]={ //ICACHE_RODATA_ATTR
// redirect func for the captive portal
ROUTE_CGI_ARG("*", cgiRedirectApClientToHostname, "esp8266.nonet"),
ROUTE_WS("/ws/update.cgi", myWebsocketConnect),
// TODO add funcs for WiFi management (when web UI is added)
ROUTE_TPL_FILE("/", tplScreen, "term.tpl"),
ROUTE_FILESYSTEM(),
ROUTE_END(),
};
static ETSTimer prHeapTimer; static ETSTimer prHeapTimer;
/** Blink & show heap usage */ /** Blink & show heap usage */
static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) { static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg)
{
static int led = 0; static int led = 0;
static unsigned int cnt = 0; static unsigned int cnt = 0;
if (cnt==5) { if (cnt == 5) {
dbg("HEAP: %ld bytes free", (unsigned long) system_get_free_heap_size()); dbg("HEAP: %ld bytes free", (unsigned long) system_get_free_heap_size());
cnt = 0; cnt = 0;
} }
//cgiWebsockBroadcast("/ws/update.cgi", "HELLO", 5, WEBSOCK_FLAG_NONE);
ioLed(led); ioLed(led);
led = !led; led = !led;
@ -140,14 +65,19 @@ static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) {
} }
//Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done. //Main routine. Initialize stdout, the I/O, filesystem and the webserver and we're done.
void user_init(void) { void user_init(void)
{
serialInit(); serialInit();
banner("ESP8266 Remote Terminal"); printf("\r\n");
banner_info("Version "FIRMWARE_VERSION", built "__DATE__" at "__TIME__); banner("*** ESP8266 Remote Terminal ***");
dbg("!!! TODO (c) and GitHub link here !!!"); banner_info("Firmware (c) Ondrej Hruska, 2017");
banner_info("github.com/MightyPork/esp-vt100-firmware");
//stdoutInit(); banner_info("");
banner_info("Version " FIRMWARE_VERSION ", built " __DATE__ " at " __TIME__);
banner_info("");
banner_info("Department of Measurement, CTU Prague");
banner_info("");
captdnsInit(); captdnsInit();
ioInit(); ioInit();
@ -157,7 +87,7 @@ void user_init(void) {
#ifdef ESPFS_POS #ifdef ESPFS_POS
espFsInit((void*)(0x40200000 + ESPFS_POS)); espFsInit((void*)(0x40200000 + ESPFS_POS));
#else #else
espFsInit((void*)(webpages_espfs_start)); espFsInit((void *) (webpages_espfs_start));
#endif #endif
httpdInit(builtInUrls, 80); httpdInit(builtInUrls, 80);
@ -167,23 +97,27 @@ void user_init(void) {
os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL); os_timer_setfn(&prHeapTimer, prHeapTimerCb, NULL);
os_timer_arm(&prHeapTimer, 1000, 1); os_timer_arm(&prHeapTimer, 1000, 1);
// The terminal screen
screen_init(); screen_init();
info("System ready!"); info("Listening on UART0, 115200-8-N-1!");
} }
// ---- unused funcs removed from sdk to save space --- // ---- unused funcs removed from sdk to save space ---
void user_rf_pre_init() { void user_rf_pre_init()
{
//Not needed, but some SDK versions want this defined. //Not needed, but some SDK versions want this defined.
} }
// вызывается из phy_chip_v6.o // вызывается из phy_chip_v6.o
void ICACHE_FLASH_ATTR chip_v6_set_sense(void) { void ICACHE_FLASH_ATTR chip_v6_set_sense(void)
{
// ret.n // ret.n
} }
// вызывается из phy_chip_v6.o // вызывается из phy_chip_v6.o
int ICACHE_FLASH_ATTR chip_v6_unset_chanfreq(void) { int ICACHE_FLASH_ATTR chip_v6_unset_chanfreq(void)
{
return 0; return 0;
} }

@ -0,0 +1,110 @@
#include <esp8266.h>
#include <httpd.h>
#include <cgiwebsocket.h>
#include <httpdespfs.h>
#include "web.h"
#include "screen.h"
#include "uart_driver.h"
#define URL_WS_UPDATE "/ws/update.cgi"
/* Routes are defined at the bottom of the file */
/**
* Main page template substitution
*
* @param connData
* @param token
* @param arg
* @return
*/
httpd_cgi_state ICACHE_FLASH_ATTR tplScreen(HttpdConnData *connData, char *token, void **arg)
{
if (token == NULL) {
// Release data object
screenSerializeToBuffer(NULL, 0, arg);
return HTTPD_CGI_DONE;
}
const int bufsiz = 512;
char buff[bufsiz];
if (streq(token, "screenData")) {
httpd_cgi_state cont = screenSerializeToBuffer(buff, bufsiz, arg);
httpdSend(connData, buff, -1);
return cont;
}
return HTTPD_CGI_DONE;
}
// --- Sockets ---
/**
* Broadcast screen state to sockets
*/
void ICACHE_FLASH_ATTR screen_notifyChange()
{
// TODO cooldown / buffering to reduce nr of such events
void *data = NULL;
const int bufsiz = 512;
char buff[bufsiz];
for (int i = 0; i < 20; i++) {
httpd_cgi_state cont = screenSerializeToBuffer(buff, bufsiz, &data);
int flg = 0;
if (cont == HTTPD_CGI_MORE) flg |= WEBSOCK_FLAG_MORE;
if (i > 0) flg |= WEBSOCK_FLAG_CONT;
cgiWebsockBroadcast(URL_WS_UPDATE, buff, (int) strlen(buff), flg);
if (cont == HTTPD_CGI_DONE) break;
}
}
/** Socket received a message */
void ICACHE_FLASH_ATTR myWebsocketRecv(Websock *ws, char *data, int len, int flags)
{
dbg("Sock RX str: %s, len %d", data, len);
if (strstarts(data, "STR:")) {
// pass string verbatim
UART_WriteString(UART0, data+4, UART_TIMEOUT_US);
}
else if (strstarts(data, "BTN:")) {
// send button as low ASCII value 1-9
int btnNum = data[4] - '0';
if (btnNum > 0 && btnNum < 10) {
UART_WriteChar(UART0, (unsigned char)btnNum, UART_TIMEOUT_US);
}
}
else if (strstarts(data, "TAP:")) {
// TODO
warn("TODO mouse input handling not implemented!");
}
else {
warn("Bad command.");
}
}
/** Socket connected for updates */
void ICACHE_FLASH_ATTR myWebsocketConnect(Websock *ws)
{
info("Socket connected to "URL_WS_UPDATE);
ws->recvCb = myWebsocketRecv;
}
/** Routes */
HttpdBuiltInUrl builtInUrls[] = { //ICACHE_RODATA_ATTR
// redirect func for the captive portal
ROUTE_CGI_ARG("*", cgiRedirectApClientToHostname, "esp8266.nonet"),
ROUTE_WS(URL_WS_UPDATE, myWebsocketConnect),
// TODO add funcs for WiFi management (when web UI is added)
ROUTE_TPL_FILE("/", tplScreen, "term.tpl"),
ROUTE_FILESYSTEM(),
ROUTE_END(),
};

@ -0,0 +1,12 @@
#ifndef WEB_H
#define WEB_H
#include <esp8266.h>
#include <httpd.h>
extern HttpdBuiltInUrl builtInUrls[];
/** Broadcast screen state to sockets */
void screen_notifyChange();
#endif //WEB_H
Loading…
Cancel
Save