OTA and Blynk.begin()

Does your serial monitor show that you are getting an IP address that’s within the range used by your network, and that you are connecting successfully to Blynk?

Is your OTA Hostname unique?

Pete.

Yes it does

There is actually nothing wrong with the sketch it functions perfectly using both forms of the connection management

I was facing the same issue. Later i made few changes in the setup part and i got it working.
If you cannot see the ip address on the com port, restart the IDE. And the OTA updates wont work with wifimanager if you have in your sketch.ESP8266 doesnt have enough memory i guess.

ArduinoOTA.onStart([]() {
    Serial.println("Start OTA");
  });

  ArduinoOTA.onEnd([]() {
    Serial.println("End OTA");
  });

  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
  });

  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });

  ArduinoOTA.begin();

  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

SFIFFS 4M(3M SPIFFS)
Give this a try and give us a update on it.

I actually had a working OTA with wifimanager, but if add server field in to the wifimanager(as i am on local server) the esp will simply crash, reset itself and crash all over again. But on cloud we use only auth field. And it works some time. I did not understand what was causing the crash.

@Madhukesh tried your code but still does not show up the app in the network ports using the Blynk.config version.

#define BLYNK_PRINT Serial // This prints to Serial Monitor
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

#include <ESP8266mDNS.h>  // For OTA
#include <WiFiUdp.h>  // For OTA
#include <ArduinoOTA.h>  // For OTA

char auth[] = "";
char ssid[] = "";
char pass[] = "";
char server[] = "blynk-cloud.com";  // URL for Blynk Cloud Server
int port = 8080;

int DeviceLED = 2;
int ReCnctFlag;  // Reconnection Flag
int ReCnctCount = 0;  // Reconnection counter

BlynkTimer timer;



void setup() 
{
  Serial.begin(115200);
  pinMode(2, OUTPUT);

  WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
  Blynk.config(auth, server, port);
  //Blynk.begin(auth, ssid, pass);
  Blynk.connect();

/////////////////////////////////////////////////////////////////////////
  ArduinoOTA.onStart([]() 
  {
    Serial.println("Start OTA");
  });

  ArduinoOTA.onEnd([]()
  {
  Serial.println("End OTA");
  });

  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
  });

  ArduinoOTA.onError([](ota_error_t error)
  {
  Serial.printf("Error[%u]: ", error);
  if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });

  ArduinoOTA.begin();

  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
/////////////////////////////////////////////////////////////////////////  

  timer.setInterval(1000L, UpTime);

  ArduinoOTA.setHostname("Loss of connection test2");  // For OTA
  ArduinoOTA.begin();  // For OTA
}



BLYNK_CONNECTED() {
  Serial.println("Connected");
  ReCnctCount = 0;
}

void UpTime() {
  Blynk.virtualWrite(V0, millis() / 1000);  // Send UpTime seconds to App
  Serial.print("UpTime: ");
  Serial.println(millis() / 1000);  // Send UpTime seconds to Serial
  digitalWrite(DeviceLED, !digitalRead(DeviceLED));  // Blink onboard LED
}



void loop() {
  timer.run();
  ArduinoOTA.handle();  // For OTA

  if (Blynk.connected()) {  // If connected run as normal
    Blynk.run();
  } else if (ReCnctFlag == 0) {  // If NOT connected and not already trying to reconnect, set timer to try to reconnect in 30 seconds
    ReCnctFlag = 1;  // Set reconnection Flag
    Serial.println("Starting reconnection timer in 30 seconds...");
    timer.setTimeout(30000L, []() {  // Lambda Reconnection Timer Function
      ReCnctFlag = 0;  // Reset reconnection Flag
      ReCnctCount++;  // Increment reconnection Counter
      Serial.print("Attempting reconnection #");
      Serial.println(ReCnctCount);
      Blynk.connect();  // Try to reconnect to the server
    });  // END Timer Function
  }
}

Did you restart the Arduino IDE ? If you are on windows this is a real time pain… The IDE needs to be restarted to get the ip address of the esp’s to show up…

Hey what about python ? Have you installed it ??

Yes restarted ide and even rebooted my computer. I dont know python doding hence have not installed it

OTA won’t work at all without Python installed.

Pete.

1 Like

Google on how to install python. And see if thats solves the issue.

Yes… that is why i suddenly realised. Because once we install we forget about it. And when someone asks about it we tell all the possible ways to solve except python.

But using Blynk.begin(auth, ssid, pass); it does?

Blynk.begin is responsible for connecting your modules to the cloud. And later maintains a stable connection with blynk. It has nothing to do with OTA.

Please install Python.
And OTA is nothing to do with Blynk.

Then you must have Python 2.7 installed.

Pete.

@proietti today I’ve been having some issues with OTA that were very similar to yours. I added OTA into a non-Blynk sketch and couldn’t get the network port to show-up, either in the IDE or Bonjour Browser.

So, I flashed a sketch to the device that I knew worked before, and that didn’t work either. I realised that the only thing that had changed since the last time I used this working sketch was updating the Arduino core.

After quite a lot of head-scratching, searching and experimenting with the OTA example sketches I realised that in both sketches I had ArduinoOTA.begin() before I called my Wi-Fi connection function.

Swapping this around, and putting ArduinoOTA.begin() after my Wi-Fi connection function solved the problem and the port showed-up immediately.

You’ve not actually shared the code that you were using with Blynk.begin, but I think that one of two things are happening:

  1. Your Wi-Fi connection routine is after ArduinoOTA.begin() or
  2. Your Wi-Fi connection routine isn’t sophisticated enough and is failing to create a connection first time around. The actual Wi-Fi connection is then taking place in the void loop, so ArduinoOTA.begin() is actually being called before the Wi-Fi connection is established.

Looking at your code, I think it’s the second scenario that we’re seeing.

With this line un-commented, only one attempt will be made at connecting to Wi-Fi. In my experience that’s not enough, and you’d normally have a loop to keep retying every 500ms or so until you do connect. Of course this will create a blocking process, which is what yo’re trying to avoid.
Maybe a series of tries at connecting before exiting a loop is the best alternative, with maybe the ArduinoOTA.begin() only called if a successful Wi-Fi connection is created?

Hope this helps.

Pete.

Thanks for that I will try that tomorrow and let you know

Ok I have now got it working. @PeteKnight you were right the Wi-Fi connection routine was not connecting first time round and hence then skipped the setup again were the ArduinoOTA.begin() is placed. So I removed the ArduinoOTA.begin() & ArduinoOTA.setHostname() into the BLYNK_CONNECTED() function and it now works. Below the code snipped for others to use.

void setup() 
{
  Serial.begin(115200);
  
  WiFi.begin(ssid, pass);
  Blynk.config(auth, server, port);
  //Blynk.begin(auth, ssid, pass);
  Blynk.connect();
  
  pinMode(2, OUTPUT);
  
  timer.setInterval(1000L, UpTime);

  //ArduinoOTA.setHostname("Loss of connection test");  // For OTA
  //ArduinoOTA.begin();  // For OTA
}

BLYNK_CONNECTED() 
{
  Serial.println("Cconnected");
  ReCnctCount = 0;
  rtc.begin();
  ArduinoOTA.setHostname("Loss of connection test");
  ArduinoOTA.begin();  // For OTA
}
3 Likes

Excellent!
This is obviously caused by a change in the ESP core and it would be nice if they fixed this behaviour at some point in the future. In the meantime at least we now understand what’s causing this behaviour and how to work around it.

Pete.

Could you please advise:

  • was this rtc.begin() declared? My Arduino IDE gives a mistake.
  • pinMode(2, OUTPUT) - what is it used for (in the code)?

@John1 i suggest that you start a new “Need help with my project” topic and provide ALL of the information that is requested when you do that.

If you are using Blynk IoT then I’d suggest you read the documentation about BLYNK_WRITE(InternalPinUTC)

This isn’t a Blynk specific function. A quick Google search will show you what it means in the Arduino/C++ coding language.

Pete.