1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

New Update library, example, upload and more

Proper error handling in the uploading python script
Much faster OTA example sketch with better results
New Update class that simplifies updating the firmware from any source
Updated Esp.updateSketch() to use the new class
This commit is contained in:
John Doe
2015-07-03 13:59:11 +03:00
committed by Ivan Grokhotkov
parent 86cf9b2c4f
commit 6f2069deac
7 changed files with 446 additions and 130 deletions

View File

@ -7,11 +7,12 @@ const char* ssid = "**********";
const char* pass = "**********";
const uint16_t aport = 8266;
WiFiUDP listener;
WiFiServer TelnetServer(aport);
WiFiClient Telnet;
WiFiUDP OTA;
void setup() {
Serial.begin(115200);
Serial.setDebugOutput(true);
Serial.println("");
Serial.println("Arduino OTA Test");
@ -22,29 +23,74 @@ void setup() {
if(WiFi.waitForConnectResult() == WL_CONNECTED){
MDNS.begin(host);
MDNS.addService("arduino", "tcp", aport);
listener.begin(aport);
OTA.begin(aport);
TelnetServer.begin();
TelnetServer.setNoDelay(true);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
}
void loop() {
if (listener.parsePacket()) {
IPAddress remote = listener.remoteIP();
int cmd = listener.parseInt();
int port = listener.parseInt();
int sz = listener.parseInt();
Serial.printf("Starting Update: cmd:%d, port:%d, size:%d\r\n", cmd, port, sz);
WiFiClient cl;
if (!cl.connect(remote, port)) {
Serial.println("Failed to connect");
//OTA Sketch
if (OTA.parsePacket()) {
IPAddress remote = OTA.remoteIP();
int cmd = OTA.parseInt();
int port = OTA.parseInt();
int size = OTA.parseInt();
Serial.print("Update Start: ip:");
Serial.print(remote);
Serial.printf(", port:%d, size:%d\n", port, size);
uint32_t startTime = millis();
if(!Update.begin(size)){
Serial.println("Update Begin Error");
return;
}
listener.stop();
if (!ESP.updateSketch(cl, sz)) {
Serial.println("Update failed");
listener.begin(aport);
WiFiClient client;
if (client.connect(remote, port)) {
Serial.setDebugOutput(true);
while(!Update.isFinished()) Update.write(client);
Serial.setDebugOutput(false);
if(Update.end()){
client.println("OK");
Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
ESP.restart();
} else {
Update.printError(client);
Update.printError(Serial);
}
} else {
Serial.printf("Connect Failed: %u\n", millis() - startTime);
}
}
//IDE Monitor (connected to Serial)
if (TelnetServer.hasClient()){
if (!Telnet || !Telnet.connected()){
if(Telnet) Telnet.stop();
Telnet = TelnetServer.available();
} else {
WiFiClient toKill = TelnetServer.available();
toKill.stop();
}
}
if (Telnet && Telnet.connected() && Telnet.available()){
while(Telnet.available())
Serial.write(Telnet.read());
}
if(Serial.available()){
size_t len = Serial.available();
uint8_t * sbuf = (uint8_t *)malloc(len);
Serial.readBytes(sbuf, len);
if (Telnet && Telnet.connected()){
Telnet.write((uint8_t *)sbuf, len);
yield();
}
free(sbuf);
}
delay(100);
}