From 83e5cba4ecb020fd4804ddccba2a8479f6a5c4da Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Thu, 10 Jun 2021 20:58:48 +0200 Subject: [PATCH] websrc to .h converter --- extra_script.py | 20 ++++++++++++++++++++ include/README | 39 --------------------------------------- include/http/index.html | 19 +++++++++++++++++++ include/index.html.h | 1 + platformio.ini | 4 +++- src/main.cpp | 35 ++++++++++------------------------- test/README | 11 ----------- 7 files changed, 53 insertions(+), 76 deletions(-) create mode 100644 extra_script.py delete mode 100644 include/README create mode 100644 include/http/index.html create mode 100644 include/index.html.h delete mode 100644 test/README diff --git a/extra_script.py b/extra_script.py new file mode 100644 index 0000000..6bcff13 --- /dev/null +++ b/extra_script.py @@ -0,0 +1,20 @@ + +import os + +# https://stackoverflow.com/questions/49449430/python-bytes-to-c-array-like-xxd-program/49450004 +def bytes_to_c_arr(data, lowercase=True): + return [format(b, '#04x' if lowercase else '#04X') for b in data] + +src = 'include/http' +out = 'include' + +for f in os.listdir(src): + with open(f'{src}/{f}', 'rb') as f_in: + with open(f'{out}/{f}.h', 'wb') as f_out: + buf = 'const char {}[] = {{{}}};'.format( + f.replace('.', '_'), + ', '.join(bytes_to_c_arr(f_in.read())) + ) + f_out.write(buf.encode(encoding='ascii')) + + diff --git a/include/README b/include/README deleted file mode 100644 index 194dcd4..0000000 --- a/include/README +++ /dev/null @@ -1,39 +0,0 @@ - -This directory is intended for project header files. - -A header file is a file containing C declarations and macro definitions -to be shared between several project source files. You request the use of a -header file in your project source file (C, C++, etc) located in `src` folder -by including it, with the C preprocessing directive `#include'. - -```src/main.c - -#include "header.h" - -int main (void) -{ - ... -} -``` - -Including a header file produces the same results as copying the header file -into each source file that needs it. Such copying would be time-consuming -and error-prone. With a header file, the related declarations appear -in only one place. If they need to be changed, they can be changed in one -place, and programs that include the header file will automatically use the -new version when next recompiled. The header file eliminates the labor of -finding and changing all the copies as well as the risk that a failure to -find one copy will result in inconsistencies within a program. - -In C, the usual convention is to give header files names that end with `.h'. -It is most portable to use only letters, digits, dashes, and underscores in -header file names, and at most one dot. - -Read more about using header files in official GCC documentation: - -* Include Syntax -* Include Operation -* Once-Only Headers -* Computed Includes - -https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/include/http/index.html b/include/http/index.html new file mode 100644 index 0000000..02d6710 --- /dev/null +++ b/include/http/index.html @@ -0,0 +1,19 @@ + + + + + ESP8266 Demo + + + + +

Hello from ESP8266!

+ + + \ No newline at end of file diff --git a/include/index.html.h b/include/index.html.h new file mode 100644 index 0000000..4ce73c5 --- /dev/null +++ b/include/index.html.h @@ -0,0 +1 @@ +const char index_html[] = {0x3c, 0x68, 0x74, 0x6d, 0x6c, 0x3e, 0x0d, 0x0a, 0x0d, 0x0a, 0x3c, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x6d, 0x65, 0x74, 0x61, 0x20, 0x68, 0x74, 0x74, 0x70, 0x2d, 0x65, 0x71, 0x75, 0x69, 0x76, 0x3d, 0x27, 0x72, 0x65, 0x66, 0x72, 0x65, 0x73, 0x68, 0x27, 0x20, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x3d, 0x27, 0x35, 0x27, 0x20, 0x2f, 0x3e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x45, 0x53, 0x50, 0x38, 0x32, 0x36, 0x36, 0x20, 0x44, 0x65, 0x6d, 0x6f, 0x3c, 0x2f, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x62, 0x6f, 0x64, 0x79, 0x20, 0x7b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x62, 0x61, 0x63, 0x6b, 0x67, 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x2d, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x23, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6f, 0x6e, 0x74, 0x2d, 0x66, 0x61, 0x6d, 0x69, 0x6c, 0x79, 0x3a, 0x20, 0x41, 0x72, 0x69, 0x61, 0x6c, 0x2c, 0x20, 0x48, 0x65, 0x6c, 0x76, 0x65, 0x74, 0x69, 0x63, 0x61, 0x2c, 0x20, 0x53, 0x61, 0x6e, 0x73, 0x2d, 0x53, 0x65, 0x72, 0x69, 0x66, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3a, 0x20, 0x23, 0x30, 0x30, 0x30, 0x30, 0x38, 0x38, 0x3b, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x2f, 0x73, 0x74, 0x79, 0x6c, 0x65, 0x3e, 0x0d, 0x0a, 0x3c, 0x2f, 0x68, 0x65, 0x61, 0x64, 0x3e, 0x0d, 0x0a, 0x0d, 0x0a, 0x3c, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0d, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x3c, 0x68, 0x31, 0x3e, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x45, 0x53, 0x50, 0x38, 0x32, 0x36, 0x36, 0x21, 0x3c, 0x2f, 0x68, 0x31, 0x3e, 0x0d, 0x0a, 0x3c, 0x2f, 0x62, 0x6f, 0x64, 0x79, 0x3e, 0x0d, 0x0a, 0x0d, 0x0a, 0x3c, 0x2f, 0x68, 0x74, 0x6d, 0x6c, 0x3e}; \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index cf72ee8..bc659c9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,10 +14,12 @@ board = esp01_1m framework = arduino monitor_speed = 115200 +extra_scripts = pre:extra_script.py + ; stuff for OTA ; https://docs.platformio.org/en/latest/platforms/espressif8266.html#over-the-air-ota-update upload_protocol = espota -upload_port = 192.168.178.75 +upload_port = 192.168.10.94 upload_flags = --port=8266 --auth=admin diff --git a/src/main.cpp b/src/main.cpp index 820f052..4bc46a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ #include +#include + +#include "PCA9685.h" #include #include @@ -11,6 +14,8 @@ #include "wifi_credentials.h" +#include "index.html.h" + const char* ssid = STASSID; const char* password = STAPSK; @@ -19,31 +24,10 @@ ESP8266WebServer server(80); void handleRoot() { digitalWrite(led, 1); - char temp[400]; - int sec = millis() / 1000; - int min = sec / 60; - int hr = min / 60; + // char temp[sizeof(index_html)]; + // snprintf(temp, sizeof(index_html), index_html); - snprintf(temp, 400, - - "\ - \ - \ - ESP8266 Demo\ - \ - \ - \ -

Hello from ESP8266!

\ -

Uptime: %02d:%02d:%02d

\ - \ - \ -", - - hr, min % 60, sec % 60 - ); - server.send(200, "text/html", temp); + server.send(200, "text/html", index_html); digitalWrite(led, 0); } @@ -161,8 +145,9 @@ void setup() { } -unsigned long i = 0; + +unsigned long i = 0; unsigned long a = 0; unsigned long s = 0; float ard_ota_time = 0; diff --git a/test/README b/test/README deleted file mode 100644 index b94d089..0000000 --- a/test/README +++ /dev/null @@ -1,11 +0,0 @@ - -This directory is intended for PlatformIO Unit Testing and project tests. - -Unit Testing is a software testing method by which individual units of -source code, sets of one or more MCU program modules together with associated -control data, usage procedures, and operating procedures, are tested to -determine whether they are fit for use. Unit testing finds problems early -in the development cycle. - -More information about PlatformIO Unit Testing: -- https://docs.platformio.org/page/plus/unit-testing.html