From 96506c868176c2250b6487a867466d03a501fc7c Mon Sep 17 00:00:00 2001 From: Simon Schmidt Date: Mon, 12 Jul 2021 01:39:24 +0200 Subject: [PATCH] initial commit --- .gitignore | 7 +++ .vscode/extensions.json | 7 +++ README.md | 128 ++++++++++++++++++++++++++++++++++++++- debug_ser.py | 17 ++++++ include/creds_override.h | 4 ++ lib/README | 46 ++++++++++++++ ota_flash.png | Bin 0 -> 3080 bytes platformio.ini | 18 ++++++ src/main.cpp | 120 ++++++++++++++++++++++++++++++++++++ test/README | 11 ++++ 10 files changed, 356 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 debug_ser.py create mode 100644 include/creds_override.h create mode 100644 lib/README create mode 100644 ota_flash.png create mode 100644 platformio.ini create mode 100644 src/main.cpp create mode 100644 test/README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f99e2eb --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch +include/creds.h +blink \ No newline at end of file diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e80666b --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ] +} diff --git a/README.md b/README.md index 55f03cb..4da6d65 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,127 @@ -# OTA_SmartHome_Vortrag +# step by step guide -Step by step guide \ No newline at end of file +__start with [ArduinoOTA basic example](https://github.com/esp8266/Arduino/blob/master/libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino)__ + +```cpp +void setup() { + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + delay(5000); + ESP.restart(); /* reload application */ + } + + /* register callbacks for OTA events */ + ArduinoOTA.onStart([]() { /* ... */ }); + ArduinoOTA.onEnd ([]() { /* ... */ }); + ArduinoOTA.onError([](ota_error_t error) { /* ... */ }); + + /* setup OTA for listening on default UDP-port 8266 */ + ArduinoOTA.begin(); +} + +void loop() { + /* check for incoming OTA update */ + ArduinoOTA.handle(); +} +``` + +___ + +modify __platformio.ini__ + +```ini +; example platformio.ini +[env:esp01_1m] +platform = espressif8266 +board = esp01_1m +framework = arduino + +upload_port = COM6 +monitor_speed = 74880 +``` + +___ + +__open serial port to esp8266 and reset__ + +```bash +# example output after reset +ets Jan 8 2013,rst cause:2, boot mode:(3,7) + +# load binary +load 0x4010f000, len 3460, room 16 +tail 4 +chksum 0xcc +load 0x3fff20b8, len 40, room 4 +tail 4 +chksum 0xc9 +csum 0xc9 +v00040b70 +~ld +``` +___ + +__Boot Messages and Modes, see also [detailed documentation](https://github.com/esp8266/Arduino/blob/master/doc/boards.rst)__ + +at startup the ESP prints out the current boot mode example: +``` +rst cause:2, boot mode:(3,6) +``` + +| rst cause| Description | +|----------|------------------| +| 0 | unknown | +| 1 | normal boot | +| 2 | reset pin | +| 3 | software reset | +| 4 | watchdog reset | + +``` +boot mode:(x,y) +``` + +| x | GPIO15 | GPIO0 | GPIO2 | Mode | +|----------|----------|---------|---------|-------------| +| 1 | 0V | 0V | 3.3V | Uart | +| 3 | 0V | 3.3V | 3.3V | Flash | + +__note:__ __y__ represents the position of the boot file + +___ + +__Upload compiled binary via uart__ + +__note__: close serial port + +```bash +$ pio run -t upload +# or use upload button in vscode +``` + +__open serial port to esp8266 and check Wi-Fi connection__ + +monitor output: +```bash +Ready +IP address: 192.168.10.108 +``` +__modify platformio.ini accordingly__ +```ini +; new platformio.ini for OTA +[env:esp01_1m] +platform = espressif8266 +board = esp01_1m +framework = arduino + +upload_protocol = espota +upload_port = 192.168.10.108 +monitor_speed = 74880 +``` +___ + +Process of updating + +![ota flash layout](ota_flash.png) + +code for updating \ No newline at end of file diff --git a/debug_ser.py b/debug_ser.py new file mode 100644 index 0000000..96a87e9 --- /dev/null +++ b/debug_ser.py @@ -0,0 +1,17 @@ +import serial + +with serial.Serial(port='COM6', timeout=1) as ser: + ser.baudrate = 74880 + + while True: + msg = ser.readline() + if msg: + try: + print(msg.decode(), flush=True, sep='' ,end='') + except: + pass + + if msg == b'ld\r\n': + ser.baudrate = 74880 + if msg == b'[OTA] End\r\n': + ser.baudrate = 115200 diff --git a/include/creds_override.h b/include/creds_override.h new file mode 100644 index 0000000..702c172 --- /dev/null +++ b/include/creds_override.h @@ -0,0 +1,4 @@ +#ifndef STASSID +#define STASSID "your ssid" +#define STAPSK "your password" +#endif diff --git a/lib/README b/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/ota_flash.png b/ota_flash.png new file mode 100644 index 0000000000000000000000000000000000000000..4c641f3236a6e0c5352f69d37f503c2e63810a5c GIT binary patch literal 3080 zcmd5-2~g8l8V_P4l~bh>Kw#AlM}c6JL&6b36afVUYq*a9sZs$GNCINSZK{BTV+#lZ z0?JA_Y>>ln1OsS7%CVwy1PI|2Lb;+qI1-XQ>wk`8jQUe1mH^6NuCOP$@{I9zZ5 z0BSQ8cS1jt`b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S!56r2w*$=&4c7Z>fbOkB-=ug=&z?t(vUb5X`p(#!Sn_KH;` z@`fJuG$C&8EWgWhGw%(#)-zz2?htj92ub<2sKtrgNJ?6wCBU)C!!Oe9G0TUj1E7IX z4MJMp2_J+StJwqbrg2{-$=+q^zHP#Z%ic-pd)dy<2CYZEQ82)($*ajvHZpYf_nter zvG|_%^1Mv7SR#_C^$K!l^dKZ(pUEN0bg{!+iu}qkl+{t0pdP!BLDiILD&Z=6>>oeL zWc7UZO!TjmJcK*(NnHEiHx>R(rmz3Yss4mv?U&LR5_#(37s|myQ*yF9>2k*1iR>8i G-9G``oAn6* literal 0 HcmV?d00001 diff --git a/platformio.ini b/platformio.ini new file mode 100644 index 0000000..424c450 --- /dev/null +++ b/platformio.ini @@ -0,0 +1,18 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:esp01_1m] +platform = espressif8266 +board = esp01_1m +framework = arduino + +upload_protocol = espota +upload_port = 192.168.10.108 +monitor_speed = 74880 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..74f5252 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,120 @@ +#include + +#include +#include +#include +#include "eboot_command.h" +#include "creds.h" + +const char* ssid = STASSID; +const char* password = STAPSK; + +WiFiServer server(80); + +void ota_setup() { + ArduinoOTA.onStart([]() { + Serial.flush(); + Serial.begin(74880); + Serial.flush(); + String type; + if (ArduinoOTA.getCommand() == U_FLASH) { + type = "sketch"; + } else { // U_FS + type = "filesystem"; + } + // NOTE: if updating FS this would be the place to unmount FS using FS.end() + Serial.println("[OTA] Start updating " + type); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\n[OTA] End"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("[OTA] Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + Serial.printf("[OTA] Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) { + Serial.println("[OTA] Auth Failed"); + } else if (error == OTA_BEGIN_ERROR) { + Serial.println("[OTA] Begin Failed"); + } else if (error == OTA_CONNECT_ERROR) { + Serial.println("[OTA] Connect Failed"); + } else if (error == OTA_RECEIVE_ERROR) { + Serial.println("[OTA] Receive Failed"); + } else if (error == OTA_END_ERROR) { + Serial.println("[OTA] End Failed"); + } + }); +} + +void setup() { + Serial.begin(74880); + Serial.println("\n[Program] Booting"); + + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("[Program] Connection Failed! Rebooting..."); + delay(5000); + ESP.restart(); + } + + ota_setup(); + ArduinoOTA.begin(); + server.begin(); + + Serial.printf("[Program] OTA and HTTPServer ready !\r\n"); + Serial.printf("[Program] IP address: "); + Serial.println(WiFi.localIP()); + + Serial.flush(); + Serial.begin(9600); + Serial.flush(); +} + +int value = 0; +void client_handler(WiFiClient* cl) { + String currentLine = ""; + while (cl->connected()) { + if (cl->available()) { + char c = cl->read(); + Serial.write(c); + if (c == '\n') { + if (currentLine.length() == 0) { + cl->println("HTTP/1.1 200 OK"); + cl->println("Content-type:text/html"); + cl->println(); + cl->print("Relais Einschalten
"); + cl->print("Relais Ausschalten
"); + cl->println(); + break; + } else { + currentLine = ""; + } + } else if (c != '\r') { + currentLine += c; + } + if (currentLine.endsWith("GET /H")) { + Serial.println("EIN"); + delay(10); + byte close[] = {0xA0, 0x01, 0x01, 0xA2}; + Serial.write(close, sizeof(close)); + } + if (currentLine.endsWith("GET /L")) { + Serial.println("AUS"); + delay(10); + byte open[] = {0xA0, 0x01, 0x00, 0xA1}; + Serial.write(open, sizeof(open)); + } + } + } + cl->stop(); +} + +void loop() { + ArduinoOTA.handle(); + WiFiClient client = server.available(); + if (client) { + client_handler(&client); + } +} diff --git a/test/README b/test/README new file mode 100644 index 0000000..b94d089 --- /dev/null +++ b/test/README @@ -0,0 +1,11 @@ + +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