<!DOCTYPE html> | |||||
<html> | |||||
<head> | |||||
<title>Ich bin ein komrpimierter Test</title> | |||||
I bims der Head | |||||
</head> | |||||
<body> | |||||
I bims der Body | |||||
</body> | |||||
</html> |
<div class="terminal"> | <div class="terminal"> | ||||
<input type="button" id="clear_term" value="clear" onclick="clearTerminal();"> | <input type="button" id="clear_term" value="clear" onclick="clearTerminal();"> | ||||
<input type="checkbox" id="scroll" name="scroll" value="scroll"> | |||||
<input type="checkbox" id="scroll" name="scroll" value="scroll" checked> | |||||
<label for="scroll"> autoscroll </label> | <label for="scroll"> autoscroll </label> | ||||
<textarea id="term"> waiting for log messages ... </textarea> | |||||
<textarea id="term">waiting for log messages ... </textarea> | |||||
</div> | </div> | ||||
</body> | </body> |
console.log(xhr.responseText); | console.log(xhr.responseText); | ||||
terminal = document.getElementById("term"); | terminal = document.getElementById("term"); | ||||
autoscroll = document.getElementById("scroll"); | autoscroll = document.getElementById("scroll"); | ||||
terminal.innerHTML += xhr.responseText + '\n'; | |||||
terminal.innerHTML += xhr.responseText; | |||||
if(autoscroll.checked) | if(autoscroll.checked) | ||||
terminal.scrollTop = terminal.scrollHeight; | terminal.scrollTop = terminal.scrollHeight; | ||||
} | } |
#term { | #term { | ||||
font-size: large; | font-size: large; | ||||
width: 100%; | width: 100%; | ||||
height: 10em; | |||||
height: 20em; | |||||
} | } | ||||
#clear_term { | #clear_term { |
logf("cant mount filesystem, EXIT !\n\r"); | logf("cant mount filesystem, EXIT !\n\r"); | ||||
return false; | return false; | ||||
} | } | ||||
logf("[HTTPServer] LittleFS mounted !\n\r"); | |||||
logf("[HTTPServer] root:\n\r"); | |||||
logf("LittleFS mounted !\n\r"); | |||||
logf("root:\n\r"); | |||||
this->listRoot(); | this->listRoot(); | ||||
logf("\n\r"); | logf("\n\r"); | ||||
// add static root file handler for http | // add static root file handler for http | ||||
this->serveStatic("/", LittleFS, "/"); | this->serveStatic("/", LittleFS, "/"); | ||||
this->begin(); | |||||
Serial.printf("Server active on Port 80 !\n\r"); | |||||
return true; | |||||
} | |||||
void HTTPServer::logf(const char *format, ...) { | |||||
va_list args; | |||||
va_start(args, format); | |||||
char temp[64]; | |||||
size_t len = vsnprintf(temp, sizeof(temp), format, args); | |||||
Serial.print(log_prefix); | |||||
Serial.write(temp, len); | |||||
va_end(args); | |||||
} | |||||
void HTTPServer::logt(const char *format, ...) { | |||||
va_list args; | |||||
va_start(args, format); | |||||
// append logging string to local buffer | |||||
if(tbuf_head < TBUF_LEN) { | |||||
size_t len = vsnprintf(tbuf+tbuf_head, TBUF_LEN-tbuf_head, format, args); | |||||
tbuf_head += len; | |||||
} | |||||
va_end(args); | |||||
} | |||||
void HTTPServer::start_apps() { | |||||
// application handler | // application handler | ||||
this->on("/app=terminal", HTTP_POST, [this]() { | this->on("/app=terminal", HTTP_POST, [this]() { | ||||
String log_msg = "terminal: millis: "; | |||||
log_msg += millis(); | |||||
send(200, "text/plain", log_msg); | |||||
if(tbuf_head) { | |||||
send(200, "text/plain", tbuf); | |||||
tbuf_head = 0; | |||||
} | |||||
}); | }); | ||||
this->begin(); | |||||
Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); | |||||
return true; | |||||
} | } |
#pragma once | #pragma once | ||||
#include <vector> | |||||
#include <ESP8266WebServer.h> | #include <ESP8266WebServer.h> | ||||
#include <stdarg.h> | #include <stdarg.h> | ||||
#include "filesys.h" | #include "filesys.h" | ||||
// debug log <ESP8266WebServer.h> | // debug log <ESP8266WebServer.h> | ||||
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0) | // #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0) | ||||
#define LOG_STR "[HTTPServer]" | |||||
#define TBUF_LEN 256 | |||||
class HTTPServer : public ESP8266WebServer { | class HTTPServer : public ESP8266WebServer { | ||||
private: | private: | ||||
const char* rootDir = "/"; | const char* rootDir = "/"; | ||||
const char* log_prefix = "[HTTPServer] "; | |||||
size_t tbuf_head = 0; | |||||
char tbuf[TBUF_LEN]; | |||||
void listRoot() { | void listRoot() { | ||||
ls(rootDir); | ls(rootDir); | ||||
} | } | ||||
void logf(const char *format, ...) { | |||||
va_list args; | |||||
va_start(args, format); | |||||
Serial.print(LOG_STR); | |||||
Serial.printf(format, args); | |||||
va_end(args); | |||||
} | |||||
public: | public: | ||||
HTTPServer(const int _port, const char* _rootDir) : | HTTPServer(const int _port, const char* _rootDir) : | ||||
~HTTPServer() | ~HTTPServer() | ||||
{ | { | ||||
Serial.printf("[HTTPServer] shut down ...\n\r"); | Serial.printf("[HTTPServer] shut down ...\n\r"); | ||||
} | |||||
} | |||||
bool start(); | bool start(); | ||||
void start_apps(); | |||||
void logf(const char *format, ...); | |||||
void logt(const char *format, ...); | |||||
}; | }; |
const char* ssid = STASSID; | const char* ssid = STASSID; | ||||
const char* password = STAPSK; | const char* password = STAPSK; | ||||
// port 80, root directory of server '/' | |||||
HTTPServer httpServer(80, "/"); | |||||
uint32_t _t=0; | |||||
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us") | |||||
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); } | |||||
void timerCallback(void *pArg) | void timerCallback(void *pArg) | ||||
{ | { | ||||
stairs.task(); | stairs.task(); | ||||
} | } | ||||
// port 80, root directory of server '/' | |||||
HTTPServer httpServer(80, "/"); | |||||
uint32_t _t=0; | |||||
#define SP_US(_str,_a) Serial.print(_str); Serial.print(" took: "); Serial.print(_a); Serial.println("us") | |||||
#define TIMEIF_US(_f, _l, _str) _t=micros(); _f; _t=micros()-_t; if(_t > _l) { SP_US(_str, _t); } | |||||
// =============================================== | // =============================================== | ||||
void setup() { | void setup() { | ||||
#ifdef WITH_DEBUGGING_ON | #ifdef WITH_DEBUGGING_ON | ||||
Serial.begin(460800); | Serial.begin(460800); | ||||
ota_setup(); | ota_setup(); | ||||
httpServer.start(); | httpServer.start(); | ||||
httpServer.start_apps(); | |||||
Serial.println("HTTP server started !"); | Serial.println("HTTP server started !"); | ||||
stairs.setup(); | stairs.setup(); | ||||
#include <random> | #include <random> | ||||
uint32_t c = 0; | |||||
void loop() { | void loop() { | ||||
// if(stairs.getState() == 0) { | // if(stairs.getState() == 0) { | ||||
// delay(1000); | // delay(1000); | ||||
// stairs.setDirection(!stairs.getDirection()); | // stairs.setDirection(!stairs.getDirection()); | ||||
// stairs.setState(1); | // stairs.setState(1); | ||||
// } | // } | ||||
if(c++%1000000 == 0) | |||||
httpServer.logt("[%d] starting :)\n", c); | |||||
TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA"); | TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA"); | ||||
TIMEIF_US(httpServer.handleClient(), 10000, "HTTP"); | TIMEIF_US(httpServer.handleClient(), 10000, "HTTP"); |