Skip to content

Commit

Permalink
Fix example sketch, espota output and failing updater
Browse files Browse the repository at this point in the history
I get 100% success with OTA now
  • Loading branch information
Me No Dev committed Nov 8, 2015
1 parent 83b035d commit e613e42
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cores/esp8266/Updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class UpdaterClass {
}
if(remaining() == 0)
return written;
yield();
delay(1);
available = data.available();
}
return written;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,66 +1,73 @@
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>

const char* host = "esp8266-ota";
const char* ssid = "**********";
const char* pass = "**********";
const uint16_t aport = 8266;
const uint16_t ota_port = 8266;
const char* ota_pass = "1234";

WiFiServer TelnetServer(aport);
WiFiClient Telnet;
WiFiUDP OTA;
WiFiServer MonitorServer(ota_port);
WiFiClient Monitor;

void setup() {
Serial.begin(115200);
Serial.println("");
Serial.println("Arduino OTA Test");
Serial.println("Bare Minimum Arduino OTA");

Serial.printf("Sketch size: %u\n", ESP.getSketchSize());
Serial.printf("Free size: %u\n", ESP.getFreeSketchSpace());

WiFi.begin(ssid, pass);
if(WiFi.waitForConnectResult() == WL_CONNECTED){
MDNS.begin(host);
MDNS.addService("arduino", "tcp", aport);
OTA.begin(aport);
TelnetServer.begin();
TelnetServer.setNoDelay(true);
MDNS.enableArduino(ota_port, true);
OTA.begin(ota_port);
MonitorServer.begin();
MonitorServer.setNoDelay(true);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
} else {
Serial.println("WiFi Connect Failed");
delay(10000);
ESP.reset();
}
}

void loop() {
//OTA Sketch
if (OTA.parsePacket()) {
IPAddress remote = OTA.remoteIP();
String pass = OTA.readStringUntil(' ');
int cmd = OTA.parseInt();
int port = OTA.parseInt();
int size = OTA.parseInt();


if(!pass.equals(String(ota_pass))){
Serial.println("ERROR: Wrong Password");
return;
}

Serial.print("Update Start: ip:");
Serial.print(remote);
Serial.printf(", port:%d, size:%d\n", port, size);
uint32_t startTime = millis();

WiFiUDP::stopAll();

if(!Update.begin(size)){
Serial.println("Update Begin Error");
Update.printError(Serial);
return;
}

WiFiUDP::stopAll();
WiFiClient::stopAll();

WiFiClient client;
if (client.connect(remote, port)) {

uint32_t written;
while(!Update.isFinished()){
written = Update.write(client);
if(written > 0) client.print(written, DEC);
}
Serial.setDebugOutput(false);

if(Update.end()){
client.println("OK");
Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
Expand All @@ -74,28 +81,27 @@ void loop() {
}
}
//IDE Monitor (connected to Serial)
if (TelnetServer.hasClient()){
if (!Telnet || !Telnet.connected()){
if(Telnet) Telnet.stop();
Telnet = TelnetServer.available();
if (MonitorServer.hasClient()){
if (!Monitor || !Monitor.connected()){
if(Monitor) Monitor.stop();
Monitor = MonitorServer.available();
} else {
WiFiClient toKill = TelnetServer.available();
toKill.stop();
MonitorServer.available().stop();
}
}
if (Telnet && Telnet.connected() && Telnet.available()){
while(Telnet.available())
Serial.write(Telnet.read());
if (Monitor && Monitor.connected() && Monitor.available()){
while(Monitor.available())
Serial.write(Monitor.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();
if (Monitor && Monitor.connected()){
Monitor.write((uint8_t *)sbuf, len);
delay(0);
}
free(sbuf);
}
delay(1);
delay(0);
}
8 changes: 5 additions & 3 deletions tools/espota.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
sent = sock2.sendto(message, remote_address)
sock2.close()

logging.info('Waiting for device...\n')
logging.info('Waiting for device...')
try:
sock.settimeout(10)
connection, client_address = sock.accept()
Expand All @@ -78,13 +78,15 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
connection.sendall(chunk)
res = connection.recv(4)
except:
logging.error('\nError Uploading')
sys.stderr.write('\n')
logging.error('Error Uploading')
connection.close()
f.close()
sock.close()
return 1

logging.info('\nWaiting for result...\n')
sys.stderr.write('\n')
logging.info('Waiting for result...')
try:
connection.settimeout(60)
data = connection.recv(32)
Expand Down

0 comments on commit e613e42

Please sign in to comment.