From b47f778fc12eb9cf60795bb686f7115b3ed507e0 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Fri, 25 Jun 2021 21:10:07 +0200 Subject: [PATCH 1/9] weitere anpassungen an pwm --- lib/treppe/treppe.cpp | 106 +++++++++++++++++++++--------------------- lib/treppe/treppe.h | 17 +++++-- src/main.cpp | 3 -- 3 files changed, 66 insertions(+), 60 deletions(-) diff --git a/lib/treppe/treppe.cpp b/lib/treppe/treppe.cpp index 4e4bb97..be9be37 100644 --- a/lib/treppe/treppe.cpp +++ b/lib/treppe/treppe.cpp @@ -30,7 +30,7 @@ uint8_t Treppe::softstart_led(uint8_t led, uint16_t startval, uint16_t stopval){ else { current_pwm += stepsize; } - Serial.println((uint16_t)current_pwm); + // Serial.println((uint16_t)current_pwm); pwmController.setChannelPWM(led, (uint16_t)current_pwm); if(current_pwm > stopval - stepsize && current_pwm < stopval + stepsize){ if(stopval == 0) pwmController.setChannelPWM(led, 0); @@ -60,46 +60,40 @@ void Treppe::ledsequence(){ lastbrightness = active_brightness; } status = status_build; // set parameter memory - Serial.print("----Status Changed! onoff: "); - Serial.print(state); - Serial.print(" dir: "); - Serial.println(direction); + Serial.printf("----Status Changed! onoff: %d, dir: %d\n", state, direction); } if(!finish){ // finish == 0 -> action pending - if(!softstart_led(led,lastbrightness, brightness)){ - Serial.print("one LED finished: "); - Serial.print(led); - Serial.print(" last: "); - Serial.print(lastbrightness); - Serial.print(" curr: "); - Serial.println(brightness); - if(direction){ - led++; - if(led >= stairs) { + if(!softstart_led(led,lastbrightness, brightness)){ + Serial.printf("one LED finished: led: %d, last: %d, curr %d\n", + led, lastbrightness, brightness); + + if(direction){ + led++; + if(led >= stairs) + finish = 1; + } + else{ + led--; + if(led < 0) finish = 1; - //lastbrightness = brightness; - } - } - else{ - led--; - if(led < 0){ - //lastbrightness = brightness; - finish = 1; - } - } } + } } } - -void Treppe::task_2() +void Treppe::rampe() { if(state) { + finish = 0; + state = 0; // set parameter memory + } + + if(!finish) { if(direction) { // aufwärts if(tick >= ticks_treppe-1) { // ziel erreicht Serial.println("[Treppe] oberster tick !"); - state = 0; + finish = 1; return; } tick++; // eins hoch @@ -107,17 +101,25 @@ void Treppe::task_2() else { // abwärts if(tick <= 0) { // ziel erreicht Serial.println("[Treppe] unterster tick !"); - state = 0; + finish = 1; return; } tick--; // eins runter } stufe = tick / ticks_pro_stufe; - float new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe); - if(direction) - new_pwm += differenz_pwm_pro_tick; - new_pwm += idle_brightness; + + float new_pwm = 0.0; + if(an_aus) { + new_pwm = differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe); + new_pwm += idle_brightness; + if(direction) new_pwm += differenz_pwm_pro_tick; + } + else { + new_pwm = active_brightness - differenz_pwm_pro_tick * (tick - ticks_pro_stufe*stufe); + new_pwm += idle_brightness; + if(direction) new_pwm -= differenz_pwm_pro_tick; + } pwmController.setChannelPWM(stufe, (uint16_t) new_pwm); Serial.printf("tick %04u, led %02d:%02u, pwm %4.1f\n", @@ -129,12 +131,6 @@ void Treppe::task_2() } } -// if(stufe > stairs || stufe < 0 || tick < 0 || tick > ticks_treppe-1) { -// Serial.println("[Treppe] ERROR, Something went wrong !"); -// state = 0; -// return; -// } - void Treppe::setup(){ Serial.printf("differenz_pwm_pro_tick %f\n", differenz_pwm_pro_tick); @@ -166,30 +162,38 @@ void Treppe::task(){ current_sensor_state[1] = digitalRead(SENSOR2); if(current_sensor_state[0] && !last_sensor_state[0] && state == 0){ + setTick(0); + setAnAus(1); setDirection(1); setState(1); } if(current_sensor_state[1] && !last_sensor_state[1] && state == 0){ + setTick(0); + setAnAus(0); setDirection(0); setState(1); } // first switch - off approach, use timer later if(!current_sensor_state[0] && last_sensor_state[0] && state == 1){ + setTick(ticks_treppe); + setAnAus(1); setDirection(1); setState(0); } if(!current_sensor_state[1] && last_sensor_state[1] && state == 1){ + setTick(ticks_treppe); + setAnAus(1); setDirection(0); setState(0); } last_sensor_state[0] = current_sensor_state[0]; last_sensor_state[1] = current_sensor_state[1]; - ledsequence(); + ledsequence(); } @@ -209,22 +213,20 @@ uint16_t Treppe::setTime(uint16_t _time_per_stair){ return time_per_stair; } -uint8_t Treppe::setDirection(uint8_t _direction){ +void Treppe::setDirection(uint8_t _direction){ switch_direction = _direction; - Serial.println("Treppe: Direction changed!"); + Serial.printf("Treppe: switch_direction=%d!\n", switch_direction); if(finish) Serial.println("apply direction request immediately"); else Serial.println("currently active, dir change afterwards"); // to do: implement state command variable to determine dimm-state - return switch_direction; } -uint8_t Treppe::setState(uint8_t _state){ - if(state == _state) return 1; - else{ - switch_state = _state; - Serial.println("Treppe: State Request changed!"); - if(finish) Serial.println("apply state request immediately"); - else Serial.println("currently active, state changes after activity"); - } - return 0; +void Treppe::setState(uint8_t _state){ + if(state == _state) return; + else { + switch_state = _state; + Serial.printf("Treppe: switch_state=%d!\n", switch_state); + if(finish) Serial.println("apply state request immediately"); + else Serial.println("currently active, state changes after activity"); + } } \ No newline at end of file diff --git a/lib/treppe/treppe.h b/lib/treppe/treppe.h index cd3a612..aba50a9 100644 --- a/lib/treppe/treppe.h +++ b/lib/treppe/treppe.h @@ -20,17 +20,22 @@ private: uint8_t switch_state = 0; uint8_t finish = 1; + // alternative uint32_t tick = 0; uint32_t stufe = 0; + uint8_t an_aus = 0; uint32_t ticks_treppe = 0; uint32_t ticks_pro_stufe = 0; float differenz_pwm_pro_tick = 0.0; + // alternative + // initialize with i2c-Address 0, use Wire Library PCA9685 pwmController; uint8_t softstart_led(uint8_t led, uint16_t startval, uint16_t stopval); void ledsequence(); + void rampe(); public: Treppe(uint8_t _stairs) : stairs(_stairs){ @@ -41,10 +46,8 @@ public: / (float) ticks_pro_stufe; } - void task(); // call periodically - void setup(); - void task_2(); + void task(); // call periodically // Parameter section uint16_t setIdle(uint16_t _idle_brightness); @@ -60,9 +63,13 @@ public: } // Runtime Parameter section - uint8_t setDirection(uint8_t _direction); - uint8_t setState(uint8_t _state); + void setDirection(uint8_t _direction); + void setState(uint8_t _state); + void setAnAus(uint8_t _an_aus) { + an_aus = _an_aus; + } uint8_t getState() { return state;}; + uint8_t getFinished() { return finish;}; uint8_t getDirection() {return direction;}; }; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 9398145..ead5183 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -103,9 +103,6 @@ void setup() { os_timer_setfn(&timer1, timerCallback, &timer_flag); os_timer_arm(&timer1, 20, true); - - stairs.setState(1); - stairs.setDirection(1); } #include From 014aa028d78a1bd43962fcedc2b40525158da26f Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 26 Jun 2021 07:33:12 +0200 Subject: [PATCH 2/9] move http and ota to lib/ for testing, add #pragma once include guards --- .vscode/settings.json | 3 ++- include/ota.h | 8 ++------ {src => lib/httpserver}/filesys.cpp | 0 {include => lib/httpserver}/filesys.h | 5 +---- {src => lib/httpserver}/httpserver.cpp | 0 {include => lib/httpserver}/httpserver.h | 6 +----- src/main.cpp | 2 +- 7 files changed, 7 insertions(+), 17 deletions(-) rename {src => lib/httpserver}/filesys.cpp (100%) rename {include => lib/httpserver}/filesys.h (84%) rename {src => lib/httpserver}/httpserver.cpp (100%) rename {include => lib/httpserver}/httpserver.h (93%) diff --git a/.vscode/settings.json b/.vscode/settings.json index 4eaa233..93ad4be 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -48,5 +48,6 @@ "streambuf": "cpp", "cinttypes": "cpp", "typeinfo": "cpp" - } + }, + "python.pythonPath": "C:\\Users\\simon\\AppData\\Local\\Programs\\Python\\Python37\\python.exe" } \ No newline at end of file diff --git a/include/ota.h b/include/ota.h index 55737d2..14dc4d2 100644 --- a/include/ota.h +++ b/include/ota.h @@ -1,5 +1,4 @@ -#ifndef __OTA_H -#define __OTA_H +#pragma once #include #include @@ -46,7 +45,4 @@ void ota_setup() { }); ArduinoOTA.begin(); -} - - -#endif // __OTA_H \ No newline at end of file +} \ No newline at end of file diff --git a/src/filesys.cpp b/lib/httpserver/filesys.cpp similarity index 100% rename from src/filesys.cpp rename to lib/httpserver/filesys.cpp diff --git a/include/filesys.h b/lib/httpserver/filesys.h similarity index 84% rename from include/filesys.h rename to lib/httpserver/filesys.h index 49b03ea..8f756ea 100644 --- a/include/filesys.h +++ b/lib/httpserver/filesys.h @@ -1,5 +1,4 @@ -#ifndef __FILESYS_H -#define __FILESYS_H +#pragma once #include @@ -12,5 +11,3 @@ void writeFile(const char * path, const char * message); void appendFile(const char * path, const char * message); void renameFile(const char * path1, const char * path2); void deleteFile(const char * path); - -#endif // __FILESYS_H \ No newline at end of file diff --git a/src/httpserver.cpp b/lib/httpserver/httpserver.cpp similarity index 100% rename from src/httpserver.cpp rename to lib/httpserver/httpserver.cpp diff --git a/include/httpserver.h b/lib/httpserver/httpserver.h similarity index 93% rename from include/httpserver.h rename to lib/httpserver/httpserver.h index 0862875..38c5370 100644 --- a/include/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -1,10 +1,8 @@ -#ifndef __HTTPSERVER_H -#define __HTTPSERVER_H +#pragma once #include "filesys.h" #include - class HTTPServer : public ESP8266WebServer { private: const char* rootDir; @@ -44,5 +42,3 @@ public: return false; } }; - -#endif // __HTTPSERVER_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index ead5183..9079023 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,8 +9,8 @@ extern "C" { #include "user_interface.h" } // OTA & WEB -#include "ota.h" #include "wifi_credentials.h" +#include "ota.h" #include "httpserver.h" // BOARD From e8329f258710ff14f75ceb9867ccf7cfe320a3c4 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 26 Jun 2021 09:02:55 +0200 Subject: [PATCH 3/9] moved files, this in on callback for httpserver class --- data/compress.html.gz | Bin 0 -> 591 bytes lib/httpserver/filesys.cpp | 9 +++++++-- lib/httpserver/filesys.h | 4 +++- lib/httpserver/httpserver.cpp | 2 +- lib/httpserver/httpserver.h | 14 ++++++++++++++ test/test_pwm.cpp | 3 +-- 6 files changed, 26 insertions(+), 6 deletions(-) create mode 100644 data/compress.html.gz diff --git a/data/compress.html.gz b/data/compress.html.gz new file mode 100644 index 0000000000000000000000000000000000000000..0ae04ee81c8f09333feddaadc7ee494d45479df7 GIT binary patch literal 591 zcmV-V0hE zoFt}>6WLDJ?$_hk?wYUBLJ?OvwvXTU_&jZ#;Og${$H(sv_b68dTfKb~xMvdqNpyrz zpr~T7y8rg@Zhmuv9tBhta7N=?1-@M~X;+?y3PL%OYR>@496%w@vx|)K4axqbX<@Oe6uF$GsqCnrn}h`4|R;sB$m>tn6a@vXern!ze#A0 z=DQQ=Fe;P1)l29W#f(U8vni|e87Kp6Phhuugwc$@M=@{(;?|a-H%6;&BO&-oR2?Kf0n^eOpvp<=1_q2ozz|JBY@&7bR2=dfbu+ngO*K<36Uk@-;+U>i zDX(hfnjr|!^x0gpfQ;#6W0k*Df)OXhA@_5$8KZ*o5Kr}!>>3t2+7edlr0Xd@S|L<& z;}&94lr>kgiA_6-+lfwnk!X*~S~ca`5}b;y@MVyeqEqkcdK&n4U#m?PLMJ*K28w{9 zyP9u+a0q)YLx4kwuYp{&K`tD~LHkB9UVyTBk$NAv_oD-`dFl5s zDq3`Zmt*u!u{)ET@wy0C57yGbYJ|rK_4$m^S+?mc9Ua@1Q6ytP^p$0AK^@9i&`KdG dVtuWeY9~MSr5X5k!R4E8gI~1gPu(*J001vu9#;SW literal 0 HcmV?d00001 diff --git a/lib/httpserver/filesys.cpp b/lib/httpserver/filesys.cpp index 0e3f42b..b315964 100644 --- a/lib/httpserver/filesys.cpp +++ b/lib/httpserver/filesys.cpp @@ -4,7 +4,7 @@ // deleteFile("/hello.txt"); // writeFile("/hello.txt", "Hello "); // appendFile("/hello.txt", "World!\n\r"); -// readFile("/hello.txt"); +// printFile("/hello.txt"); // listDir("/"); FSInfo fsinfo; @@ -58,7 +58,12 @@ void ls(const char * dirname) { Serial.println(); } -void readFile(const char * path) { +File open(const char * path) { + return LittleFS.open(path, "r"); +} + + +void printFile(const char * path) { Serial.printf("Reading file: %s\n\r", path); File file = LittleFS.open(path, "r"); diff --git a/lib/httpserver/filesys.h b/lib/httpserver/filesys.h index 8f756ea..708c1f4 100644 --- a/lib/httpserver/filesys.h +++ b/lib/httpserver/filesys.h @@ -5,8 +5,10 @@ bool mount_fs(); bool format_fs(); +File open(const char * path); + void ls(const char * dirname); -void readFile(const char * path); +void printFile(const char * path); void writeFile(const char * path, const char * message); void appendFile(const char * path, const char * message); void renameFile(const char * path1, const char * path2); diff --git a/lib/httpserver/httpserver.cpp b/lib/httpserver/httpserver.cpp index ae75c37..36df250 100644 --- a/lib/httpserver/httpserver.cpp +++ b/lib/httpserver/httpserver.cpp @@ -14,4 +14,4 @@ bool HTTPServer::addRootFileHandler() { this->serveStatic("/favicon.png", LittleFS, "/favicon.png"); return true; -} \ No newline at end of file +} diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 38c5370..3a7e848 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -33,6 +33,20 @@ public: this->listRoot(); Serial.printf("\n\r"); + // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 + on("/compress.html", [this]() { + File f = open("/compress.html.gz"); + if (!f) { + Serial.println("Failed to open file for reading"); + } + else + { + Serial.println("Streaming /compress.html.gz to client"); + const String cont = "text/html"; + streamFile(f, cont); + } + }); + if( this->addRootFileHandler() ){ this->begin(); Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); diff --git a/test/test_pwm.cpp b/test/test_pwm.cpp index e03383d..1c2ef4f 100644 --- a/test/test_pwm.cpp +++ b/test/test_pwm.cpp @@ -1,6 +1,5 @@ -#include +// #include #include "treppe.h" - #include Treppe stairs(10); From 10b57ca51625c27ba72f87e48e845dfcc12e7b49 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 26 Jun 2021 09:21:34 +0200 Subject: [PATCH 4/9] further improvments, [this] in lambda fcn callback, compressed gz webfiles via streamFile() --- data/comp.html.gz | Bin 0 -> 135 bytes data/compress.html | 12 ++++++++++++ data/compress.html.gz | Bin 591 -> 139 bytes lib/httpserver/httpserver.h | 28 ++++++++++++++++++++++++++++ src/main.cpp | 20 +------------------- 5 files changed, 41 insertions(+), 19 deletions(-) create mode 100644 data/comp.html.gz create mode 100644 data/compress.html diff --git a/data/comp.html.gz b/data/comp.html.gz new file mode 100644 index 0000000000000000000000000000000000000000..ed3aee40c5ae43ff80d34fcf61a0b012524ddec4 GIT binary patch literal 135 zcmV;20C@i&iwFp1)7D@D|6^}$a4u+cZEOIuQFQTl4v7qKRmdpG&9UR7lt8oH~fz&|dz{)|c002yA + + + + Ich bin ein komrpimierter Test + I bims der Head + + + I bims der Body + + + \ No newline at end of file diff --git a/data/compress.html.gz b/data/compress.html.gz index 0ae04ee81c8f09333feddaadc7ee494d45479df7..4f0b0012302b7a740ce20a5a380f4cdd38bba1c8 100644 GIT binary patch literal 139 zcmV;60CfK!iwFo1($-)A17mM(aB^jHb1rCfZEOIuQFQTl4v7qKRmdpG&9UR7lt8oH~fz&|dz{)|c002yAhE zoFt}>6WLDJ?$_hk?wYUBLJ?OvwvXTU_&jZ#;Og${$H(sv_b68dTfKb~xMvdqNpyrz zpr~T7y8rg@Zhmuv9tBhta7N=?1-@M~X;+?y3PL%OYR>@496%w@vx|)K4axqbX<@Oe6uF$GsqCnrn}h`4|R;sB$m>tn6a@vXern!ze#A0 z=DQQ=Fe;P1)l29W#f(U8vni|e87Kp6Phhuugwc$@M=@{(;?|a-H%6;&BO&-oR2?Kf0n^eOpvp<=1_q2ozz|JBY@&7bR2=dfbu+ngO*K<36Uk@-;+U>i zDX(hfnjr|!^x0gpfQ;#6W0k*Df)OXhA@_5$8KZ*o5Kr}!>>3t2+7edlr0Xd@S|L<& z;}&94lr>kgiA_6-+lfwnk!X*~S~ca`5}b;y@MVyeqEqkcdK&n4U#m?PLMJ*K28w{9 zyP9u+a0q)YLx4kwuYp{&K`tD~LHkB9UVyTBk$NAv_oD-`dFl5s zDq3`Zmt*u!u{)ET@wy0C57yGbYJ|rK_4$m^S+?mc9Ua@1Q6ytP^p$0AK^@9i&`KdG dVtuWeY9~MSr5X5k!R4E8gI~1gPu(*J001vu9#;SW diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 3a7e848..3574b55 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -46,6 +46,34 @@ public: streamFile(f, cont); } }); + on("/comp.html", [this]() { + File f = open("/comp.html.gz"); + if (!f) { + Serial.println("Failed to open file for reading"); + + } + else + { + Serial.println("Streaming /comp.html.gz to client"); + const String cont = "text/html"; + streamFile(f, cont); + } + }); + onNotFound([this]() { + String message = "File Not Found\n\n"; + message += "URI: "; + message += uri(); + message += "\nMethod: "; + message += (method() == HTTP_GET) ? "GET" : "POST"; + message += "\nArguments: "; + message += args(); + message += "\n"; + + for (uint8_t i = 0; i < args(); i++) { + message += " " + argName(i) + ": " + arg(i) + "\n"; + } + send(404, "text/plain", message); + }); if( this->addRootFileHandler() ){ this->begin(); diff --git a/src/main.cpp b/src/main.cpp index 9079023..bffeab4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,8 +34,7 @@ void timerCallback(void *pArg) stairs.task(); } -// HTTP -void handleNotFound(); +// port 80, root directory of server '/' HTTPServer httpServer(80, "/"); uint32_t _t=0; @@ -44,22 +43,6 @@ uint32_t _t=0; // =============================================== -void handleNotFound() { - String message = "File Not Found\n\n"; - message += "URI: "; - message += httpServer.uri(); - message += "\nMethod: "; - message += (httpServer.method() == HTTP_GET) ? "GET" : "POST"; - message += "\nArguments: "; - message += httpServer.args(); - message += "\n"; - - for (uint8_t i = 0; i < httpServer.args(); i++) { - message += " " + httpServer.argName(i) + ": " + httpServer.arg(i) + "\n"; - } - - httpServer.send(404, "text/plain", message); -} void setup() { #ifdef WITH_DEBUGGING_ON @@ -95,7 +78,6 @@ void setup() { ota_setup(); httpServer.start(); - httpServer.onNotFound(handleNotFound); Serial.println("HTTP server started !"); stairs.setup(); From 4e6445fea879e014436828de41b4a38bbd0411fc Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 26 Jun 2021 09:33:37 +0200 Subject: [PATCH 5/9] modify script for new way of serving gz files --- .gitignore | 1 + create_gz_c_arr.py | 38 ++++++++++++++++++++++++++++++++++++++ create_gz_files.py | 41 +++++++++-------------------------------- data/comp.html.gz | Bin 135 -> 0 bytes data/compress.html.gz | Bin 139 -> 0 bytes doku.md | 13 +++++++++---- 6 files changed, 57 insertions(+), 36 deletions(-) create mode 100644 create_gz_c_arr.py delete mode 100644 data/comp.html.gz delete mode 100644 data/compress.html.gz diff --git a/.gitignore b/.gitignore index 437849a..eaa2e0f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .pio .vscode compress +data_gz platformio.ini include/wifi_credentials.h include/*.gz.h \ No newline at end of file diff --git a/create_gz_c_arr.py b/create_gz_c_arr.py new file mode 100644 index 0000000..ca2548c --- /dev/null +++ b/create_gz_c_arr.py @@ -0,0 +1,38 @@ +#!/usr/bin/python3 + +import os, gzip +# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/ + +def convert_to_gzip(src, out, f): + input_file = f'{src}{f}' + output_file = f'{out}{f}.gz.h' + output_charp = f'{f.replace(".", "_")}_gz' + + top = '' + with open(input_file, 'rb') as f_in: + gz = gzip.compress(f_in.read()) + gzlen = len(gz) + + top += f'// filename: {f}.gz.h\n' + top += f'#define {output_charp}_len {gzlen}\n' + top += f'const char {output_charp}[] = ' + top += '{' + + with open(output_file, 'wb') as f_out: + for i, b in enumerate(gz): + if not i%10: + top += '\n ' + top += f'0x{b:02X}, ' + top = top[:-2] + top += '};' + f_out.write(top.encode(encoding='utf-8')) + + +src='data/' +out='compress/' + +if not 'compress' in os.listdir(): + os.mkdir('compress') + +for f in os.listdir(src): + convert_to_gzip(src, out, f) \ No newline at end of file diff --git a/create_gz_files.py b/create_gz_files.py index ca2548c..258fcdb 100644 --- a/create_gz_files.py +++ b/create_gz_files.py @@ -1,38 +1,15 @@ #!/usr/bin/python3 import os, gzip -# https://www.mischianti.org/2020/10/26/web-server-with-esp8266-and-esp32-byte-array-gzipped-pages-and-spiffs-2/ +# https://stackoverflow.com/questions/8156707/gzip-a-file-in-python -def convert_to_gzip(src, out, f): - input_file = f'{src}{f}' - output_file = f'{out}{f}.gz.h' - output_charp = f'{f.replace(".", "_")}_gz' +src_dir='data' +out_dir='data_gz' - top = '' - with open(input_file, 'rb') as f_in: - gz = gzip.compress(f_in.read()) - gzlen = len(gz) +if not 'data_gz' in os.listdir(): + os.mkdir('data_gz') - top += f'// filename: {f}.gz.h\n' - top += f'#define {output_charp}_len {gzlen}\n' - top += f'const char {output_charp}[] = ' - top += '{' - - with open(output_file, 'wb') as f_out: - for i, b in enumerate(gz): - if not i%10: - top += '\n ' - top += f'0x{b:02X}, ' - top = top[:-2] - top += '};' - f_out.write(top.encode(encoding='utf-8')) - - -src='data/' -out='compress/' - -if not 'compress' in os.listdir(): - os.mkdir('compress') - -for f in os.listdir(src): - convert_to_gzip(src, out, f) \ No newline at end of file +for f in os.listdir(src_dir): + with open(f'{src_dir}/{f}', 'rb') as f_in: + with gzip.open(f'{out_dir}/{f}.gz', 'wb') as f_out: + f_out.writelines(f_in) \ No newline at end of file diff --git a/data/comp.html.gz b/data/comp.html.gz deleted file mode 100644 index ed3aee40c5ae43ff80d34fcf61a0b012524ddec4..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 135 zcmV;20C@i&iwFp1)7D@D|6^}$a4u+cZEOIuQFQTl4v7qKRmdpG&9UR7lt8oH~fz&|dz{)|c002yA7lt8oH~fz&|dz{)|c002yA Date: Sat, 26 Jun 2021 10:49:29 +0200 Subject: [PATCH 6/9] simple serial monitor in httpserver --- data/index.html | 7 ++++++ data/input.js | 44 ++++++++++++++++++++++++++++++++----- data/style.css | 20 ++++++++++++++++- lib/httpserver/filesys.cpp | 7 ------ lib/httpserver/filesys.h | 2 ++ lib/httpserver/httpserver.h | 10 ++++++++- 6 files changed, 75 insertions(+), 15 deletions(-) diff --git a/data/index.html b/data/index.html index d9f2860..204f746 100644 --- a/data/index.html +++ b/data/index.html @@ -60,6 +60,13 @@ +
+ + + + +
+ diff --git a/data/input.js b/data/input.js index f1ea08e..69ad112 100644 --- a/data/input.js +++ b/data/input.js @@ -1,8 +1,40 @@ -var slider = document.getElementById("range1"); -var output = document.getElementById("l_pwm"); -output.innerHTML = slider.value; // Display the default slider value +// var slider = document.getElementById("helligkeit"); +// var output = document.getElementById("l_pwm"); +// output.innerHTML = slider.value; // Display the default slider value -// Update the current slider value (each time you drag the slider handle) -slider.oninput = function() { - output.innerHTML = this.value; +// // Update the current slider value (each time you drag the slider handle) +// slider.oninput = function() { +// output.innerHTML = this.value; +// } + + + +let xhr = new XMLHttpRequest(); + +xhr.onreadystatechange = function(){ + if(xhr.readyState == 4) { + if (xhr.status == 200){ + console.log(xhr.responseText); + terminal = document.getElementById("term"); + autoscroll = document.getElementById("scroll"); + terminal.innerHTML += xhr.responseText + '\n'; + if(autoscroll.checked) + terminal.scrollTop = terminal.scrollHeight; + } + else { + console.log("status:", xhr.status); + } + } +} + +function reloadTerminal() { + xhr.open("GET", "/terminal", true); + xhr.send(); + setTimeout(reloadTerminal, 1000); +}; + +reloadTerminal(); + +function clearTerminal() { + document.getElementById("term").innerHTML = ''; } \ No newline at end of file diff --git a/data/style.css b/data/style.css index 1ac52d5..beb3812 100644 --- a/data/style.css +++ b/data/style.css @@ -3,7 +3,7 @@ html { font-family: sans-serif, Arial, Helvetica; background-color: #d4d4d4; height: 100%; - background-image: url('Background.png'); + /* background-image: url('Background.png'); */ background-repeat: repeat; background-size: 150% 150%; } @@ -103,4 +103,22 @@ input:checked + .slider:before { -ms-transform: translateX(26px); transform: translateX(26px); } +*/ +.terminal { + margin:5%; + padding: 1%; + width: 80%; + border: 1px solid black; + border-radius: 5px; +} + +#term { + font-size: large; + width: 100%; + height: 10em; +} + +#clear_term { + margin: 2px; +} \ No newline at end of file diff --git a/lib/httpserver/filesys.cpp b/lib/httpserver/filesys.cpp index b315964..2e2b1aa 100644 --- a/lib/httpserver/filesys.cpp +++ b/lib/httpserver/filesys.cpp @@ -1,12 +1,5 @@ #include "filesys.h" -// listDir("/"); -// deleteFile("/hello.txt"); -// writeFile("/hello.txt", "Hello "); -// appendFile("/hello.txt", "World!\n\r"); -// printFile("/hello.txt"); -// listDir("/"); - FSInfo fsinfo; bool mount_fs() { diff --git a/lib/httpserver/filesys.h b/lib/httpserver/filesys.h index 708c1f4..da4a754 100644 --- a/lib/httpserver/filesys.h +++ b/lib/httpserver/filesys.h @@ -2,6 +2,8 @@ #include +// some usefull wrappers for Filesystem + bool mount_fs(); bool format_fs(); diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 3574b55..7078bff 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -38,6 +38,7 @@ public: File f = open("/compress.html.gz"); if (!f) { Serial.println("Failed to open file for reading"); + send(404, "text/plain", "Failed to open file for reading"); } else { @@ -50,7 +51,7 @@ public: File f = open("/comp.html.gz"); if (!f) { Serial.println("Failed to open file for reading"); - + send(404, "text/plain", "Failed to open file for reading"); } else { @@ -58,7 +59,14 @@ public: const String cont = "text/html"; streamFile(f, cont); } + }); + + on("/terminal", [this]() { + String log_msg = "terminal: millis: "; + log_msg += millis(); + send(200, "text/plain", log_msg); }); + onNotFound([this]() { String message = "File Not Found\n\n"; message += "URI: "; From 9ea541a1b43fa0f9ec12f0b3be931b0a0bab84b7 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sat, 26 Jun 2021 21:11:19 +0200 Subject: [PATCH 7/9] lambda error --- lib/httpserver/httpserver.cpp | 25 ++++++++++++ lib/httpserver/httpserver.h | 72 +++++++++++++++++++++-------------- 2 files changed, 68 insertions(+), 29 deletions(-) diff --git a/lib/httpserver/httpserver.cpp b/lib/httpserver/httpserver.cpp index 36df250..1eeddd3 100644 --- a/lib/httpserver/httpserver.cpp +++ b/lib/httpserver/httpserver.cpp @@ -15,3 +15,28 @@ bool HTTPServer::addRootFileHandler() { return true; } + + +bool HTTPServer::addRootFileHandler_ls() { + logf("addRootFileHandler_ls: %s\n\r", rootDir); + Dir root = LittleFS.openDir(rootDir); + + while (root.next()) { + const String& fName = "/" + root.fileName(); + const String& fName_gz = "/" + root.fileName() + ".gz"; + Serial.printf("Register Callback for: %s @ %s\n\r", + fName.c_str(), fName_gz.c_str()); + + // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 + on(fName, [this, fName, fName_gz]() { + + logf("addRootFileHandler_ls: %s\n\r", rootDir); + File f = open(fName_gz.c_str()); + if (f) + streamFile(f, mime::getContentType(fName)); + else + send(404, "text/plain", "Failed to open file for reading"); + }); + } + return true; +} \ No newline at end of file diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 7078bff..443ba29 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -2,18 +2,30 @@ #include "filesys.h" #include +#include + +#define LOG_STR "[HTTPServer]" + class HTTPServer : public ESP8266WebServer { private: - const char* rootDir; + const char* rootDir = "/"; bool addRootFileHandler(); + bool addRootFileHandler_ls(); bool formatFS() { return format_fs(); } 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) : @@ -33,33 +45,34 @@ public: this->listRoot(); Serial.printf("\n\r"); - // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 - on("/compress.html", [this]() { - File f = open("/compress.html.gz"); - if (!f) { - Serial.println("Failed to open file for reading"); - send(404, "text/plain", "Failed to open file for reading"); - } - else - { - Serial.println("Streaming /compress.html.gz to client"); - const String cont = "text/html"; - streamFile(f, cont); - } - }); - on("/comp.html", [this]() { - File f = open("/comp.html.gz"); - if (!f) { - Serial.println("Failed to open file for reading"); - send(404, "text/plain", "Failed to open file for reading"); - } - else - { - Serial.println("Streaming /comp.html.gz to client"); - const String cont = "text/html"; - streamFile(f, cont); - } - }); + // on("/compress.html", [this]() { + // File f = open("/compress.html.gz"); + // if (!f) { + // Serial.println("Failed to open file for reading"); + // send(404, "text/plain", "Failed to open file for reading"); + // } + // else + // { + // Serial.println("Streaming /compress.html.gz to client"); + // const String cont = "text/html"; + // streamFile(f, cont); + // } + // f.close(); + // }); + // on("/comp.html", [this]() { + // File f = open("/comp.html.gz"); + // if (!f) { + // Serial.println("Failed to open file for reading"); + // send(404, "text/plain", "Failed to open file for reading"); + // } + // else + // { + // Serial.println("Streaming /comp.html.gz to client"); + // const String cont = "text/html"; + // streamFile(f, cont); + // } + // f.close(); + // }); on("/terminal", [this]() { String log_msg = "terminal: millis: "; @@ -67,6 +80,7 @@ public: send(200, "text/plain", log_msg); }); + // default handler onNotFound([this]() { String message = "File Not Found\n\n"; message += "URI: "; @@ -83,7 +97,7 @@ public: send(404, "text/plain", message); }); - if( this->addRootFileHandler() ){ + if( this->addRootFileHandler_ls() ){ this->begin(); Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); return true; From 3c824bfc92a5ee706b7f2619e3b9c5ae6367c594 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sun, 27 Jun 2021 01:56:30 +0200 Subject: [PATCH 8/9] much simpler callback approach due to serveStatic with directories !!! --- data/input.js | 2 +- lib/httpserver/httpserver.cpp | 72 +++++++++++++++---------------- lib/httpserver/httpserver.h | 81 +++-------------------------------- 3 files changed, 43 insertions(+), 112 deletions(-) diff --git a/data/input.js b/data/input.js index 69ad112..d7dd8fd 100644 --- a/data/input.js +++ b/data/input.js @@ -28,7 +28,7 @@ xhr.onreadystatechange = function(){ } function reloadTerminal() { - xhr.open("GET", "/terminal", true); + xhr.open("POST", "/app=terminal", true); xhr.send(); setTimeout(reloadTerminal, 1000); }; diff --git a/lib/httpserver/httpserver.cpp b/lib/httpserver/httpserver.cpp index 1eeddd3..ad21a4a 100644 --- a/lib/httpserver/httpserver.cpp +++ b/lib/httpserver/httpserver.cpp @@ -1,42 +1,42 @@ #include "httpserver.h" +bool HTTPServer::start() { + if(!mount_fs()) { + logf("cant mount filesystem, EXIT !\n\r"); + return false; + } + logf("[HTTPServer] LittleFS mounted !\n\r"); + logf("[HTTPServer] root:\n\r"); + this->listRoot(); + logf("\n\r"); -bool HTTPServer::addRootFileHandler() { + // default handler + this->onNotFound([this]() { + String message = "File Not Found\n\n"; + message += "URI: "; + message += uri(); + message += "\nMethod: "; + message += (method() == HTTP_GET) ? "GET" : "POST"; + message += "\nArguments: "; + message += args(); + message += "\n"; - // //experimental, see doku.md - // server.serveStatic("/compressed", LittleFS, "/compressed.html.gzip"); - - this->serveStatic("", LittleFS, "/index.html"); - this->serveStatic("/", LittleFS, "/index.html"); - this->serveStatic("/#", LittleFS, "/index.html"); - this->serveStatic("/style.css", LittleFS, "/style.css"); - this->serveStatic("/input.js", LittleFS, "/input.js"); - this->serveStatic("/favicon.png", LittleFS, "/favicon.png"); - - return true; -} - - -bool HTTPServer::addRootFileHandler_ls() { - logf("addRootFileHandler_ls: %s\n\r", rootDir); - Dir root = LittleFS.openDir(rootDir); - - while (root.next()) { - const String& fName = "/" + root.fileName(); - const String& fName_gz = "/" + root.fileName() + ".gz"; - Serial.printf("Register Callback for: %s @ %s\n\r", - fName.c_str(), fName_gz.c_str()); - - // https://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 - on(fName, [this, fName, fName_gz]() { - - logf("addRootFileHandler_ls: %s\n\r", rootDir); - File f = open(fName_gz.c_str()); - if (f) - streamFile(f, mime::getContentType(fName)); - else - send(404, "text/plain", "Failed to open file for reading"); + for (uint8_t i = 0; i < args(); i++) { + message += " " + argName(i) + ": " + arg(i) + "\n"; + } + send(404, "text/plain", message); }); - } - return true; + + // add static root file handler for http + this->serveStatic("/", LittleFS, "/"); + + // application handler + this->on("/app=terminal", HTTP_POST, [this]() { + String log_msg = "terminal: millis: "; + log_msg += millis(); + send(200, "text/plain", log_msg); + }); + + this->begin(); + Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); } \ No newline at end of file diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index 443ba29..aead5df 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -1,21 +1,20 @@ #pragma once -#include "filesys.h" #include #include +#include "filesys.h" + +// debug log +// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0) #define LOG_STR "[HTTPServer]" class HTTPServer : public ESP8266WebServer { + private: const char* rootDir = "/"; - bool addRootFileHandler(); - bool addRootFileHandler_ls(); - bool formatFS() { - return format_fs(); - } void listRoot() { ls(rootDir); } @@ -36,73 +35,5 @@ public: Serial.printf("[HTTPServer] shut down ...\n\r"); } - bool start() { - if(!mount_fs()) - return false; - Serial.printf("[HTTPServer] LittleFS mounted !\n\r"); - - Serial.printf("[HTTPServer] root:\n\r"); - this->listRoot(); - Serial.printf("\n\r"); - - // on("/compress.html", [this]() { - // File f = open("/compress.html.gz"); - // if (!f) { - // Serial.println("Failed to open file for reading"); - // send(404, "text/plain", "Failed to open file for reading"); - // } - // else - // { - // Serial.println("Streaming /compress.html.gz to client"); - // const String cont = "text/html"; - // streamFile(f, cont); - // } - // f.close(); - // }); - // on("/comp.html", [this]() { - // File f = open("/comp.html.gz"); - // if (!f) { - // Serial.println("Failed to open file for reading"); - // send(404, "text/plain", "Failed to open file for reading"); - // } - // else - // { - // Serial.println("Streaming /comp.html.gz to client"); - // const String cont = "text/html"; - // streamFile(f, cont); - // } - // f.close(); - // }); - - on("/terminal", [this]() { - String log_msg = "terminal: millis: "; - log_msg += millis(); - send(200, "text/plain", log_msg); - }); - - // default handler - onNotFound([this]() { - String message = "File Not Found\n\n"; - message += "URI: "; - message += uri(); - message += "\nMethod: "; - message += (method() == HTTP_GET) ? "GET" : "POST"; - message += "\nArguments: "; - message += args(); - message += "\n"; - - for (uint8_t i = 0; i < args(); i++) { - message += " " + argName(i) + ": " + arg(i) + "\n"; - } - send(404, "text/plain", message); - }); - - if( this->addRootFileHandler_ls() ){ - this->begin(); - Serial.printf("[HTTPServer] Server active on Port 80 !\n\r"); - return true; - } - Serial.printf("[HTTPServer] Not starting Server, something went wrong !\n\r"); - return false; - } + bool start(); }; From 3d13e761f497a0408c134059ff0dfadaa93ebbf4 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Sun, 27 Jun 2021 02:01:25 +0200 Subject: [PATCH 9/9] update templ.ini and doku --- doku.md | 4 ++++ lib/httpserver/httpserver.h | 2 ++ templ_platformio_ini | 45 ++++++++++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/doku.md b/doku.md index 5ba1485..e1c1ade 100644 --- a/doku.md +++ b/doku.md @@ -75,8 +75,12 @@ PlatformIO Library veraltet !! ### TIMER in OTA unterbrechen !!!!! + + ### further improvments with streamFile gzip encoded Files can be sent this reduces space on fs ! new easier script needed to just convert to .gz maybe measure timings on ESP + +__=> use serveStatic because way simpler !__ diff --git a/lib/httpserver/httpserver.h b/lib/httpserver/httpserver.h index aead5df..0595ac7 100644 --- a/lib/httpserver/httpserver.h +++ b/lib/httpserver/httpserver.h @@ -1,3 +1,5 @@ +// Wrapper for ESP8266WebServer with Filesystem as HTTP source + #pragma once #include diff --git a/templ_platformio_ini b/templ_platformio_ini index 1f5f0c9..7eba75b 100644 --- a/templ_platformio_ini +++ b/templ_platformio_ini @@ -8,23 +8,29 @@ ; Please visit documentation for the other options and examples ; https://docs.platformio.org/page/projectconf.html -[env] +[platformio] +description = ESP8266 Treppenlicht Steuerung +default_envs = ota +; test_dir = test +data_dir = data_gz + +[env:hardware] platform = espressif8266 board = nodemcuv2 framework = arduino -; for http files board_build.filesystem = littlefs board_build.ldscript = eagle.flash.4m1m.ld extra_scripts = pre:create_gz_files.py - monitor_speed = 115200 [env:serial] +extends = env:hardware upload_protocol = esptool upload_speed = 921600 [env:ota] +extends = env:hardware ; OTA => https://docs.platformio.org/en/latest/platforms/espressif8266.html#over-the-air-ota-update upload_protocol = espota upload_port = @@ -33,13 +39,46 @@ upload_flags = --host_port= --auth=admin + [env:debug] +extends = env:hardware ; look at doku.md build_flags = -DWITH_DEBUGGING_ON -Os -g3 -ggdb3 upload_protocol = esptool upload_speed = 921600 +# This file is used compile and run tests located in the `unit` directory. +# For more info, see: +# https://docs.platformio.org/en/latest/plus/unit-testing.html +# https://github.com/ThrowTheSwitch/Unity +# https://github.com/ThrowTheSwitch/Unity/blob/master/docs/UnityAssertionsReference.md +# To prepare coverage data for lcov, add ${coverage.build_flags} to env:test build flags +# To actually generate coverage report: +# $ `pio test` / run the test `program` manually +# $ lcov --include (readlink -f ../espurna)'/*' --capture --directory .pio/build/test/ --output-file test.info +# $ genhtml --ignore-errors source test.info --output-directory out + +[coverage] +build_flags = -lgcov -fprofile-arcs -ftest-coverage + +[env:test] +extends = env:hardware + + +[env:native] +platform = native +test_build_project_src = true +build_flags = + -DMANUFACTURER="PLATFORMIO" + -DDEVICE="TEST" + -std=gnu++11 + -g + -Os + -I lib/treppe + -I lib/PCA9685-Arduino + +