Skip to content

Commit 03baea2

Browse files
committed
ArduinoOTA: forward errors from Update.begin to espota.py
If Update.begin fails, instead of printing “No response from device”, espota.py will print the actual error message from the Updater.
1 parent 01e1c58 commit 03baea2

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

libraries/ArduinoOTA/ArduinoOTA.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <WiFiUdp.h>
66
#include "ArduinoOTA.h"
77
#include "MD5Builder.h"
8+
#include "StreamString.h"
89

910
extern "C" {
1011
#include "osapi.h"
@@ -180,6 +181,7 @@ void ArduinoOTAClass::_onRx(){
180181
_ota_ip = _udp_ota->getRemoteAddress();
181182
_cmd = cmd;
182183
_ota_port = parseInt();
184+
_ota_udp_port = _udp_ota->getRemotePort();
183185
_size = parseInt();
184186
_udp_ota->read();
185187
_md5 = readStringUntil('\n');
@@ -199,12 +201,10 @@ void ArduinoOTAClass::_onRx(){
199201
char auth_req[38];
200202
sprintf(auth_req, "AUTH %s", _nonce.c_str());
201203
_udp_ota->append((const char *)auth_req, strlen(auth_req));
202-
_udp_ota->send(&ota_ip, _udp_ota->getRemotePort());
204+
_udp_ota->send(&ota_ip, _ota_udp_port);
203205
_state = OTA_WAITAUTH;
204206
return;
205207
} else {
206-
_udp_ota->append("OK", 2);
207-
_udp_ota->send(&ota_ip, _udp_ota->getRemotePort());
208208
_state = OTA_RUNUPDATE;
209209
}
210210
} else if (_state == OTA_WAITAUTH) {
@@ -230,12 +230,10 @@ void ArduinoOTAClass::_onRx(){
230230

231231
ota_ip.addr = (uint32_t)_ota_ip;
232232
if(result.equals(response)){
233-
_udp_ota->append("OK", 2);
234-
_udp_ota->send(&ota_ip, _udp_ota->getRemotePort());
235233
_state = OTA_RUNUPDATE;
236234
} else {
237235
_udp_ota->append("Authentication Failed", 21);
238-
_udp_ota->send(&ota_ip, _udp_ota->getRemotePort());
236+
_udp_ota->send(&ota_ip, _ota_udp_port);
239237
if (_error_callback) _error_callback(OTA_AUTH_ERROR);
240238
_state = OTA_IDLE;
241239
}
@@ -245,17 +243,31 @@ void ArduinoOTAClass::_onRx(){
245243
}
246244

247245
void ArduinoOTAClass::_runUpdate() {
246+
ip_addr_t ota_ip;
247+
ota_ip.addr = (uint32_t)_ota_ip;
248+
248249
if (!Update.begin(_size, _cmd)) {
249250
#ifdef OTA_DEBUG
250251
OTA_DEBUG.println("Update Begin Error");
251252
#endif
252253
if (_error_callback) {
253254
_error_callback(OTA_BEGIN_ERROR);
254255
}
256+
257+
StreamString ss;
258+
Update.printError(ss);
259+
_udp_ota->append("ERR: ", 5);
260+
_udp_ota->append(ss.c_str(), ss.length());
261+
_udp_ota->send(&ota_ip, _ota_udp_port);
262+
delay(100);
255263
_udp_ota->listen(*IP_ADDR_ANY, _port);
256264
_state = OTA_IDLE;
257265
return;
258266
}
267+
_udp_ota->append("OK", 2);
268+
_udp_ota->send(&ota_ip, _ota_udp_port);
269+
delay(100);
270+
259271
Update.setMD5(_md5.c_str());
260272
WiFiUDP::stopAll();
261273
WiFiClient::stopAll();

libraries/ArduinoOTA/ArduinoOTA.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ class ArduinoOTAClass
7979
ota_state_t _state;
8080
int _size;
8181
int _cmd;
82-
int _ota_port;
82+
uint16_t _ota_port;
83+
uint16_t _ota_udp_port;
8384
IPAddress _ota_ip;
8485
String _md5;
8586

tools/espota.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def serve(remoteAddr, localAddr, remotePort, localPort, password, filename, comm
9595
sent = sock2.sendto(message.encode(), remote_address)
9696
sock2.settimeout(10)
9797
try:
98-
data = sock2.recv(37).decode()
98+
data = sock2.recv(128).decode()
9999
except:
100100
logging.error('No Answer')
101101
sock2.close()

0 commit comments

Comments
 (0)