Using OTA with 3 x ESP8266, when in the IDE they all reset?
Posted: Mon Feb 01, 2016 5:38 pm
Hi,
I can succesfully upload code into the ESP8266 (they are NodeMCU modules), but when ever I open the Arduino IDE, They all start resetting, this can go on for a minute, then they settle down and work OK. I think it may be in the code I am using, its an example from Arduino 'DNS_SD_Arduino_OTA'.
All 3 units have this exact code as shown below, does anything need changing to make each one different ?
I call 'void Setup_OTA ()' once in 'setup' .
Then 'Check_if_we_need_OTA ()' from the Main loop, so this is checked many time a second. I am using Arduino IDE 1.6.5 and ESP8266 1.6.5-947-g39819f0.
Thanks
Regards
Gaz
void Setup_OTA ()
{
Serial.println(F(""));
Serial.printf("Sketch size : %u\n", ESP.getSketchSize());
Serial.printf("Free size : %u\n", ESP.getFreeSketchSpace());
if (WiFi.waitForConnectResult() == WL_CONNECTED)
{
MDNS.begin(host);
MDNS.addService("arduino", "tcp", aport);
OTA.begin(aport);
TelnetServer.begin();
TelnetServer.setNoDelay(true);
}
}
void Check_if_we_need_OTA ()
{
if (OTA.parsePacket())
{
IPAddress remote = OTA.remoteIP();
int cmd = OTA.parseInt();
int port = OTA.parseInt();
int size = OTA.parseInt();
Serial.print(F("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(F("Update Begin Error"));
return;
}
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(F("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);
}
}
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(1);
}
I can succesfully upload code into the ESP8266 (they are NodeMCU modules), but when ever I open the Arduino IDE, They all start resetting, this can go on for a minute, then they settle down and work OK. I think it may be in the code I am using, its an example from Arduino 'DNS_SD_Arduino_OTA'.
All 3 units have this exact code as shown below, does anything need changing to make each one different ?
I call 'void Setup_OTA ()' once in 'setup' .
Then 'Check_if_we_need_OTA ()' from the Main loop, so this is checked many time a second. I am using Arduino IDE 1.6.5 and ESP8266 1.6.5-947-g39819f0.
Thanks
Regards
Gaz
void Setup_OTA ()
{
Serial.println(F(""));
Serial.printf("Sketch size : %u\n", ESP.getSketchSize());
Serial.printf("Free size : %u\n", ESP.getFreeSketchSpace());
if (WiFi.waitForConnectResult() == WL_CONNECTED)
{
MDNS.begin(host);
MDNS.addService("arduino", "tcp", aport);
OTA.begin(aport);
TelnetServer.begin();
TelnetServer.setNoDelay(true);
}
}
void Check_if_we_need_OTA ()
{
if (OTA.parsePacket())
{
IPAddress remote = OTA.remoteIP();
int cmd = OTA.parseInt();
int port = OTA.parseInt();
int size = OTA.parseInt();
Serial.print(F("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(F("Update Begin Error"));
return;
}
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(F("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);
}
}
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(1);
}