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 0000000..4c641f3 Binary files /dev/null and b/ota_flash.png differ 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