Browse Source

updates to http terminal

tags/FSM1.0
Simon Schmidt 3 years ago
parent
commit
66e8ddb2d9
7 changed files with 64 additions and 42 deletions
  1. 0
    12
      data/compress.html
  2. 2
    2
      data/index.html
  3. 1
    1
      data/input.js
  4. 1
    1
      data/style.css
  5. 33
    9
      lib/httpserver/httpserver.cpp
  6. 14
    10
      lib/httpserver/httpserver.h
  7. 13
    7
      src/main.cpp

+ 0
- 12
data/compress.html View File

@@ -1,12 +0,0 @@
<!DOCTYPE html>
<html>

<head>
<title>Ich bin ein komrpimierter Test</title>
I bims der Head
</head>
<body>
I bims der Body
</body>

</html>

+ 2
- 2
data/index.html View File

@@ -62,9 +62,9 @@

<div class="terminal">
<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>
<textarea id="term"> waiting for log messages ... </textarea>
<textarea id="term">waiting for log messages ...&#10;</textarea>
</div>

</body>

+ 1
- 1
data/input.js View File

@@ -17,7 +17,7 @@ xhr.onreadystatechange = function(){
console.log(xhr.responseText);
terminal = document.getElementById("term");
autoscroll = document.getElementById("scroll");
terminal.innerHTML += xhr.responseText + '\n';
terminal.innerHTML += xhr.responseText;
if(autoscroll.checked)
terminal.scrollTop = terminal.scrollHeight;
}

+ 1
- 1
data/style.css View File

@@ -116,7 +116,7 @@ input:checked + .slider:before {
#term {
font-size: large;
width: 100%;
height: 10em;
height: 20em;
}

#clear_term {

+ 33
- 9
lib/httpserver/httpserver.cpp View File

@@ -6,8 +6,8 @@ bool HTTPServer::start() {
logf("cant mount filesystem, EXIT !\n\r");
return false;
}
logf("[HTTPServer] LittleFS mounted !\n\r");
logf("[HTTPServer] root:\n\r");
logf("LittleFS mounted !\n\r");
logf("root:\n\r");
this->listRoot();
logf("\n\r");

@@ -30,15 +30,39 @@ bool HTTPServer::start() {

// add static root file handler for http
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
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;
}

+ 14
- 10
lib/httpserver/httpserver.h View File

@@ -2,6 +2,8 @@

#pragma once

#include <vector>

#include <ESP8266WebServer.h>
#include <stdarg.h>
#include "filesys.h"
@@ -9,24 +11,22 @@
// debug log <ESP8266WebServer.h>
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0)

#define LOG_STR "[HTTPServer]"
#define TBUF_LEN 256


class HTTPServer : public ESP8266WebServer {

private:
const char* rootDir = "/";
const char* log_prefix = "[HTTPServer] ";

size_t tbuf_head = 0;
char tbuf[TBUF_LEN];
void listRoot() {
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:
HTTPServer(const int _port, const char* _rootDir) :
@@ -35,7 +35,11 @@ public:
~HTTPServer()
{
Serial.printf("[HTTPServer] shut down ...\n\r");
}
}
bool start();
void start_apps();
void logf(const char *format, ...);
void logt(const char *format, ...);
};

+ 13
- 7
src/main.cpp View File

@@ -27,6 +27,13 @@ Treppe stairs(16);
const char* ssid = STASSID;
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)
{
@@ -34,16 +41,9 @@ void timerCallback(void *pArg)
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() {
#ifdef WITH_DEBUGGING_ON
Serial.begin(460800);
@@ -78,6 +78,8 @@ void setup() {
ota_setup();

httpServer.start();
httpServer.start_apps();

Serial.println("HTTP server started !");

stairs.setup();
@@ -89,6 +91,8 @@ void setup() {

#include <random>

uint32_t c = 0;

void loop() {
// if(stairs.getState() == 0) {
// delay(1000);
@@ -102,6 +106,8 @@ void loop() {
// stairs.setDirection(!stairs.getDirection());
// stairs.setState(1);
// }
if(c++%1000000 == 0)
httpServer.logt("[%d] starting :)\n", c);
TIMEIF_US(ArduinoOTA.handle(), 10000, "OTA");
TIMEIF_US(httpServer.handleClient(), 10000, "HTTP");

Loading…
Cancel
Save