55#include < WiFiUdp.h>
66#include " ArduinoOTA.h"
77#include " MD5Builder.h"
8+ #include " StreamString.h"
89
910extern " 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
247245void 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 ();
0 commit comments