.pio | |||||
.vscode/.browse.c_cpp.db* | |||||
.vscode/c_cpp_properties.json | |||||
.vscode/launch.json | |||||
.vscode/ipch | |||||
include/creds.h | |||||
blink |
{ | |||||
// See http://go.microsoft.com/fwlink/?LinkId=827846 | |||||
// for the documentation about the extensions.json format | |||||
"recommendations": [ | |||||
"platformio.platformio-ide" | |||||
] | |||||
} |
# OTA_SmartHome_Vortrag | |||||
# step by step guide | |||||
Step by step guide | |||||
__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 |
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 |
#ifndef STASSID | |||||
#define STASSID "your ssid" | |||||
#define STAPSK "your password" | |||||
#endif |
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 <Foo.h> | |||||
#include <Bar.h> | |||||
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 |
; 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 |
#include <Arduino.h> | |||||
#include <ESP8266WiFi.h> | |||||
#include <WiFiUdp.h> | |||||
#include <ArduinoOTA.h> | |||||
#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 <a href=\"/H\">Einschalten</a><br>"); | |||||
cl->print("Relais <a href=\"/L\">Ausschalten</a><br>"); | |||||
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); | |||||
} | |||||
} |
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 |