From 5443800b40ab017474850baa9ca778f46344621f Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Mon, 21 Jun 2021 09:43:06 +0200 Subject: [PATCH 1/3] reorganize main.cpp --- .vscode/settings.json | 12 ++++++- README.md | 3 +- src/main.cpp | 77 ++++++++++++++++++++++++------------------- 3 files changed, 56 insertions(+), 36 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 1428c38..e9afe7d 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,15 @@ { "files.associations": { - "random": "cpp" + "random": "cpp", + "array": "cpp", + "*.tcc": "cpp", + "deque": "cpp", + "list": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "string_view": "cpp", + "memory": "cpp", + "ranges": "cpp" } } \ No newline at end of file diff --git a/README.md b/README.md index 4a8a391..4e1822d 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,7 @@ https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html ``` ##### platformio.ini -change IP of esp8266 accordingly -IP gets printed on Serial Monitor after first Upload +see templ_platformio_ini ```ini [env:] diff --git a/src/main.cpp b/src/main.cpp index 5e1cc7f..26a2de5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,17 +1,17 @@ #include #include -#include "wifi_credentials.h" #include "PCA9685.h" // Ă„nderung // OTA & WEB +#include "wifi_credentials.h" #include #include #include #include -#include // evtl weghauen ? +#include #include "index.html.gz.h" #include "style.css.gz.h" @@ -21,8 +21,16 @@ const char* ssid = STASSID; const char* password = STAPSK; +void setup_webserver(); +void setup_ota(); +void setup_pwm_pca9685(); +void handleRootGz(); +void handleCssGz(); +void handleNotFound(); + const int led = 13; ESP8266WebServer server(80); +PCA9685 pwmController; void handleRootGz() { const char* dataType = "text/html"; @@ -36,7 +44,6 @@ void handleCssGz() { server.send(200, dataType, (const char*)style_css_gz, style_css_gz_len); } - void handleNotFound() { digitalWrite(led, 1); String message = "File Not Found\n\n"; @@ -83,26 +90,7 @@ void setup_webserver() { Serial.println("HTTP server started"); } - - -PCA9685 pwmController; - -void setup() { - Serial.begin(115200); - Serial.println("Booting"); - WiFi.mode(WIFI_STA); - WiFi.begin(ssid, password); - - Wire.begin(); // Wire must be started first - Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz - - while (WiFi.waitForConnectResult() != WL_CONNECTED) { - Serial.println("Connection Failed! Rebooting..."); - delay(5000); - ESP.restart(); - } - - // setup OTA +void setup_ota() { ArduinoOTA.setPort(8266); ArduinoOTA.setHostname("ESP_Treppenlicht"); ArduinoOTA.setPassword("admin"); @@ -142,21 +130,44 @@ void setup() { } }); ArduinoOTA.begin(); - Serial.println("Ready"); - Serial.print("IP address: "); - Serial.println(WiFi.localIP()); - - setup_webserver(); +} +void setup_pwm_pca9685() { pwmController.resetDevices(); // Software resets all PCA9685 devices on Wire line pwmController.init(B000000); // Address pins A5-A0 set to B000000 pwmController.setPWMFrequency(200); // Default is 200Hz, supports 24Hz to 1526Hz - pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land - - Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4 + pwmController.setChannelPWM(0, 128 << 4); // Set PWM to 128/255, but in 4096 land + Serial.println(pwmController.getChannelPWM(0)); // Should output 2048, which is 128 << 4 +} + + + +void setup() { + Serial.begin(115200); + Serial.println(F("Booting ....")); + + Wire.begin(); // Wire must be started first + Wire.setClock(400000); // Supported baud rates are 100kHz, 400kHz, and 1000kHz + + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("Connection Failed! Rebooting..."); + delay(5000); + ESP.restart(); + } + + Serial.println("Ready"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + setup_ota(); + setup_webserver(); + setup_pwm_pca9685(); } uint32_t t; @@ -164,7 +175,7 @@ uint32_t t; #define TIMEIF_US(_str,_f, _l) t=micros(); _f; t=micros()-t; if(t > _l) { SP_US(_str, t); } void loop() { - TIMEIF_US("OTA", ArduinoOTA.handle(), 20000); + TIMEIF_US("OTA", ArduinoOTA.handle(), 1000); - TIMEIF_US("HTTP", server.handleClient(), 20000); + TIMEIF_US("HTTP", server.handleClient(), 1000); } \ No newline at end of file From 407581451b545de2818712f31bbd3e83f8894f50 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Mon, 21 Jun 2021 09:44:30 +0200 Subject: [PATCH 2/3] d --- .gitignore | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.gitignore b/.gitignore index b3b8747..664297b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,5 @@ .pio .vscode -.vscode/.browse.c_cpp.db* -.vscode/c_cpp_properties.json -.vscode/launch.json -.vscode/ipch platformio.ini include/wifi_credentials.h include/*.gz.h \ No newline at end of file From d0b824963bd79fc436b4b312e49b48081860d8d4 Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Mon, 21 Jun 2021 09:55:03 +0200 Subject: [PATCH 3/3] add webtest.cmd for python testing of webserver --- include/http/index.html | 2 +- include/webtest.cmd | 2 ++ src/main.cpp | 3 +-- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 include/webtest.cmd diff --git a/include/http/index.html b/include/http/index.html index 9bec15e..39f8553 100644 --- a/include/http/index.html +++ b/include/http/index.html @@ -4,7 +4,7 @@ My test page - +

Mozilla is cool

diff --git a/include/webtest.cmd b/include/webtest.cmd new file mode 100644 index 0000000..a5f46f7 --- /dev/null +++ b/include/webtest.cmd @@ -0,0 +1,2 @@ +CD .\include\http +py.exe -m http.server \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 26a2de5..032fe38 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -81,9 +81,8 @@ void setup_webserver() { Serial.print("IP address: "); Serial.println(WiFi.localIP()); - //server.on("/", handleRoot); server.on("/", handleRootGz); - server.on("/styles/style.css", handleCssGz); + server.on("/style.css", handleCssGz); server.onNotFound(handleNotFound); server.begin();