I am using the below code taken from GTT’s Code Examples and noticed a glitch.
#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[] = "BS_0ip-_b5Oe5a5vDw6VRfzg8qCiwBnY";
char ssid[] = "proietti office";
char pass[] = "Mariano3565";
//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();
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
}
}
When you use WiFi.begin(ssid, pass); Blynk.config(auth, server, port); the OTA does not work. the network port does not register the sketch so hence you cannot then use OTA. When you use Blynk.begin(auth, ssid, pass); the network port registers the sketch and you can use the OTA.
Any ideas why this is?
Yes it does, I use this approach all the time.
If you’re using a Windows PC and the Arduino IDE then at some point you will come across an issue where some or all of your OTA Network ports don’t appear in the port list of the IDE.
The simple way to fix this is to download Bonjour Browser, install it and run it.
clicking the refresh button in the top right hand corner will do a Bonjour discovery, which is enough to allow the ports to be visible in the IDE again.
Pete.
Ok did what you said but it still does not work.
Do your devices appear in the Bonjour Browser results window?
Pete.
No they dont. They appear only when I change it to 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.
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:
- Your Wi-Fi connection routine is after
ArduinoOTA.begin()
or
- 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