Skip to content

Commit e613e42

Browse files
author
Me No Dev
committed
Fix example sketch, espota output and failing updater
I get 100% success with OTA now
1 parent 83b035d commit e613e42

File tree

3 files changed

+42
-34
lines changed

3 files changed

+42
-34
lines changed

cores/esp8266/Updater.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class UpdaterClass {
103103
}
104104
if(remaining() == 0)
105105
return written;
106-
yield();
106+
delay(1);
107107
available = data.available();
108108
}
109109
return written;
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,73 @@
11
#include <ESP8266WiFi.h>
22
#include <ESP8266mDNS.h>
3-
#include <WiFiUdp.h>
43

54
const char* host = "esp8266-ota";
65
const char* ssid = "**********";
76
const char* pass = "**********";
8-
const uint16_t aport = 8266;
7+
const uint16_t ota_port = 8266;
8+
const char* ota_pass = "1234";
99

10-
WiFiServer TelnetServer(aport);
11-
WiFiClient Telnet;
1210
WiFiUDP OTA;
11+
WiFiServer MonitorServer(ota_port);
12+
WiFiClient Monitor;
1313

1414
void setup() {
1515
Serial.begin(115200);
1616
Serial.println("");
17-
Serial.println("Arduino OTA Test");
17+
Serial.println("Bare Minimum Arduino OTA");
1818

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

2222
WiFi.begin(ssid, pass);
2323
if(WiFi.waitForConnectResult() == WL_CONNECTED){
2424
MDNS.begin(host);
25-
MDNS.addService("arduino", "tcp", aport);
26-
OTA.begin(aport);
27-
TelnetServer.begin();
28-
TelnetServer.setNoDelay(true);
25+
MDNS.enableArduino(ota_port, true);
26+
OTA.begin(ota_port);
27+
MonitorServer.begin();
28+
MonitorServer.setNoDelay(true);
2929
Serial.print("IP address: ");
3030
Serial.println(WiFi.localIP());
31+
} else {
32+
Serial.println("WiFi Connect Failed");
33+
delay(10000);
34+
ESP.reset();
3135
}
3236
}
3337

3438
void loop() {
35-
//OTA Sketch
3639
if (OTA.parsePacket()) {
3740
IPAddress remote = OTA.remoteIP();
41+
String pass = OTA.readStringUntil(' ');
3842
int cmd = OTA.parseInt();
3943
int port = OTA.parseInt();
4044
int size = OTA.parseInt();
41-
45+
46+
if(!pass.equals(String(ota_pass))){
47+
Serial.println("ERROR: Wrong Password");
48+
return;
49+
}
50+
4251
Serial.print("Update Start: ip:");
4352
Serial.print(remote);
4453
Serial.printf(", port:%d, size:%d\n", port, size);
4554
uint32_t startTime = millis();
4655

47-
WiFiUDP::stopAll();
48-
4956
if(!Update.begin(size)){
50-
Serial.println("Update Begin Error");
57+
Update.printError(Serial);
5158
return;
5259
}
5360

61+
WiFiUDP::stopAll();
62+
WiFiClient::stopAll();
63+
5464
WiFiClient client;
5565
if (client.connect(remote, port)) {
56-
5766
uint32_t written;
5867
while(!Update.isFinished()){
5968
written = Update.write(client);
6069
if(written > 0) client.print(written, DEC);
6170
}
62-
Serial.setDebugOutput(false);
63-
6471
if(Update.end()){
6572
client.println("OK");
6673
Serial.printf("Update Success: %u\nRebooting...\n", millis() - startTime);
@@ -74,28 +81,27 @@ void loop() {
7481
}
7582
}
7683
//IDE Monitor (connected to Serial)
77-
if (TelnetServer.hasClient()){
78-
if (!Telnet || !Telnet.connected()){
79-
if(Telnet) Telnet.stop();
80-
Telnet = TelnetServer.available();
84+
if (MonitorServer.hasClient()){
85+
if (!Monitor || !Monitor.connected()){
86+
if(Monitor) Monitor.stop();
87+
Monitor = MonitorServer.available();
8188
} else {
82-
WiFiClient toKill = TelnetServer.available();
83-
toKill.stop();
89+
MonitorServer.available().stop();
8490
}
8591
}
86-
if (Telnet && Telnet.connected() && Telnet.available()){
87-
while(Telnet.available())
88-
Serial.write(Telnet.read());
92+
if (Monitor && Monitor.connected() && Monitor.available()){
93+
while(Monitor.available())
94+
Serial.write(Monitor.read());
8995
}
9096
if(Serial.available()){
9197
size_t len = Serial.available();
9298
uint8_t * sbuf = (uint8_t *)malloc(len);
9399
Serial.readBytes(sbuf, len);
94-
if (Telnet && Telnet.connected()){
95-
Telnet.write((uint8_t *)sbuf, len);
96-
yield();
100+
if (Monitor && Monitor.connected()){
101+
Monitor.write((uint8_t *)sbuf, len);
102+
delay(0);
97103
}
98104
free(sbuf);
99105
}
100-
delay(1);
106+
delay(0);
101107
}

tools/espota.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
5353
sent = sock2.sendto(message, remote_address)
5454
sock2.close()
5555

56-
logging.info('Waiting for device...\n')
56+
logging.info('Waiting for device...')
5757
try:
5858
sock.settimeout(10)
5959
connection, client_address = sock.accept()
@@ -78,13 +78,15 @@ def serve(remoteAddr, remotePort, password, filename, command = FLASH):
7878
connection.sendall(chunk)
7979
res = connection.recv(4)
8080
except:
81-
logging.error('\nError Uploading')
81+
sys.stderr.write('\n')
82+
logging.error('Error Uploading')
8283
connection.close()
8384
f.close()
8485
sock.close()
8586
return 1
8687

87-
logging.info('\nWaiting for result...\n')
88+
sys.stderr.write('\n')
89+
logging.info('Waiting for result...')
8890
try:
8991
connection.settimeout(60)
9092
data = connection.recv(32)

0 commit comments

Comments
 (0)