BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       👉 SKETCH BUILDER

Blynk.Air OTA minimal code

Serg_Grn, Ibrahim_Roshdy, PeteKnight,

Hello friends.
Please help me on this topic.

As the topic instructs the code, the aim is to use the “BlynkSimpleEsp32.h library” to update via Blynk Air without using Blynk.Edgent.

I have 2 problems:

  1. I use BlynkSimpleEsp8266.h library
  2. I don’t know how to write OTA.h code or insert the code you are discussing in my code. (In what position, how to do it).

I take an example of the code used to turn on and off the D4 blue light on this Nodemcu.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN "X8q5lh0pfypriO7b5zAFNEiJoi1_S6zb"

#define BLYNK_FIRMWARE_VERSION        "1.1.1.1"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266Ping.h>


#define WIFI_SSID "HIEU VAI  THANH"                      //Enter Wifi Name
#define WIFI_PASS "passathai96"                    //Enter wifi Password


boolean setBlynk = 0;

void setup() {
  Serial.begin(115200);
  checkConnection();
  checkInternet();
  pinMode(D4,OUTPUT);
}

boolean checkConnection() {
  Serial.print("Check connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.disconnect();
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(WIFI_SSID,WIFI_PASS);

  int count=0;
  while(count < 20){
    if(WiFi.status() == WL_CONNECTED){
      Serial.print("Connected to ");
      Serial.println(WIFI_SSID);
      Serial.print("Web Server IP Address: ");
      Serial.println(WiFi.localIP());
      Serial.println("");
      return true;
    }
    delay(5000);
    Serial.print(".");
    count++;
  }
  Serial.println("Timed out........ hiddenAP = 0;");
  Serial.println("");
  return false;
}


void checkInternet(){
  Serial.println("");
  bool ret = Ping.ping("www.google.com");
  if(ret){
    if(setBlynk == 0){
            Serial.println("ket noi NEW BLYNK NEW");
            Blynk.config(BLYNK_AUTH_TOKEN);
      setBlynk = 1;
    }
      if(!Blynk.connected()){
        Serial.println("Not connected to Blynk server");
        Blynk.connect(); // try to connect to server with default timeout
      }else{
        Serial.println("Connected to Blynk server");
      }
  Serial.println("");
}
}

void loop(){
  Blynk.run();
    if(!Blynk.connected()){
      int count=0;
      while(count < 3){//5 lan chay 6s Blynk.run la 30s
        Blynk.run();//thoi gian chay 6s
        if(Blynk.connected()){
          Serial.println("!Blynk.connected(); Blynk.connected())");Serial.println("");
          delay(500);
          return;
        }
        delay(1000);
        Serial.print(".");
        count++;
      }
      Serial.println("!Blynk.connected(); internetState = 0;");Serial.println("");
      delay(500);
   }
}



BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V4);
}

BLYNK_WRITE(V4) {
  int stateD4 = param.asInt();
  digitalWrite(D4, stateD4);
}

Please help me insert the code to support updating via BlynkAir.
In case you are using ESP32, please write and run on it for testing, I will re-code it myself to match my esp8266.

Thank you very much.

It’s always a good idea to add the #include lines together near the top of your sketch, so those two lines would go there.

The rest of the code can go anywhere, provided it’s not inside an existing function, so the simplest place it to tag it on to the end of your sketch.

Pete.

1 Like

Hello.
I tried to add, tweak the code a bit to fit the ESP8266. As below.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN "X8q5lh0pfypriO7b5zAFNEiJoi1_S6zb"

#define BLYNK_FIRMWARE_VERSION        "1.1.1.2"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266Ping.h>

//ESP32
//#include <Update.h>
//#include <HTTPClient.h>
//ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
//________________________________________________________________
String overTheAirURL = "";

#define WIFI_SSID "HIEU VAI  THANH"                      //Enter Wifi Name
#define WIFI_PASS "passathai96"                    //Enter wifi Password


boolean setBlynk = 0;

void setup() {
  Serial.begin(115200);
  checkConnection();
  checkInternet();
  pinMode(D4,OUTPUT);
}

boolean checkConnection() {
  Serial.print("Check connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.disconnect();
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(WIFI_SSID,WIFI_PASS);

  int count=0;
  while(count < 20){
    if(WiFi.status() == WL_CONNECTED){
      Serial.print("Connected to ");
      Serial.println(WIFI_SSID);
      Serial.print("Web Server IP Address: ");
      Serial.println(WiFi.localIP());
      Serial.println("");
      return true;
    }
    delay(5000);
    Serial.print(".");
    count++;
  }
  Serial.println("Timed out........ hiddenAP = 0;");
  Serial.println("");
  return false;
}


void checkInternet(){
  Serial.println("");
  bool ret = Ping.ping("www.google.com");
  if(ret){
    if(setBlynk == 0){
            Serial.println("ket noi NEW BLYNK NEW");
            Blynk.config(BLYNK_AUTH_TOKEN);
      setBlynk = 1;
    }
      if(!Blynk.connected()){
        Serial.println("Not connected to Blynk server");
        Blynk.connect(); // try to connect to server with default timeout
      }else{
        Serial.println("Connected to Blynk server");
      }
  Serial.println("");
}
}

void loop(){
  Blynk.run();
    if(!Blynk.connected()){
      int count=0;
      while(count < 3){//5 lan chay 6s Blynk.run la 30s
        Blynk.run();//thoi gian chay 6s
        if(Blynk.connected()){
          Serial.println("!Blynk.connected(); Blynk.connected())");Serial.println("");
          delay(500);
          return;
        }
        delay(1000);
        Serial.print(".");
        count++;
      }
      Serial.println("!Blynk.connected(); internetState = 0;");Serial.println("");
      delay(500);
   }
}



BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V4);
}

BLYNK_WRITE(V4) {
  int stateD4 = param.asInt();
  digitalWrite(D4, stateD4);
}

BLYNK_WRITE(InternalPinOTA) {
  overTheAirURL = param.asString();
  HTTPClient http;
  http.begin(overTheAirURL);
  int httpCode = http.GET();
  if (httpCode != HTTP_CODE_OK) {return;}
  int contentLength = http.getSize();
  if (contentLength <= 0) {return; }
  bool canBegin = Update.begin(contentLength);
  if (!canBegin) { return;}
  Client& client = http.getStream();
  int written = Update.writeStream(client);
  if (written != contentLength) {return;}
  if (!Update.end()) {return;}
  if (!Update.isFinished()) {return;}
reboot();
}

void reboot()
{
#if defined(ARDUINO_ARCH_MEGAAVR)
  wdt_enable(WDT_PERIOD_8CLK_gc);
#elif defined(__AVR__)
  wdt_enable(WDTO_15MS);
#elif defined(__arm__)
  NVIC_SystemReset();
#elif defined(ESP8266) || defined(ESP32)
  ESP.restart();
#else
  #error "MCU reset procedure not implemented"
#endif
  for (;;) {}
}
//_________

However, I still have not been able to update, the error appears as detailed image attached.

Do you have a way to test it for me?

Thank you.

Since ESP8266 core version 3.x.x the http.begin command now needs to know the transport method (WiFi) as well as the server url.

Try this…

  WiFiClient my_wifi_client;
  HTTPClient http;
  http.begin(my_wifi_client, overTheAirURL);

Pete.

1 Like

I’m sorry, I’ve revised the code as you instructed, but the update status is still the same as before.

  • Firmware Shipment Process Started 2:04:49 AM Today

  • Request sent 2:04:49 AM Today

  • New Firmware Requested 2:04:49 AM Today

  • Download Failure 2:04:49 AM Today

  • Firmware Shipment Process Started 2:04:22 AM Today

Maybe you should add some serial print commands to the sketch to see how the OTA process is progressing and where it’s failing.

You should also be 100% sure that the new .bin file is correctly compiled for the board you’re using.
I’d also swap to using a three digit version number rather than four.

Pete.

1 Like

Thank you for your response.
And here is my Serial Monitor.

02:43:17.747 → Check connecting to HIEU VAI THANH
02:43:22.978 → .Connected to HIEU VALUES
02:43:22.978 → Web Server IP Address: 192.168.1.21
02:43:22.978 →
02:43:22.978 →
02:43:27.216 → call NEW BLYNK NEW
02:43:27.216 → [32313]
02:43:27.216 → ___ __ __
02:43:27.216 → / _ )/ /_ _____ / /__
02:43:27.216 → / _ / / // / _ / '/
02:43:27.216 → /
//_, /////_
02:43:27.216 → /
_/ v1.1.0 on NodeMCU
02:43:27.216 →
02:43:27.216 → #StandWithUkraine https://bit.ly/swua
02:43:27.216 →
02:43:27.216 →
02:43:27.216 → Not connected to Blynk server
02:43:27.216 → [32323] Connecting to blynk.cloud:80
02:43:27.356 → [32472] Ready (ping: 43ms).
02:43:27.499 →
02:44:48,764 → InternalPinOTA
02:44:48.764 → http://sgp1.blynk.cloud/static/fw_11910610955607970738
-838595071.bin?token=fA9zNJlWe2d8TNpqfIt6H6Mb-W3p01kE
02:44:48.950 → canBegin

update command stopped at this line of code:
bool canBegin = Update.begin(contentLength);
if (!canBegin) {Serial.println(“canBegin”);return;}

Now it’s too late in my country. so I’ll come back later. Thank you for your interest.

I’ve just tested your sketch (after making the changes to the http.begin command and changing to a three digit firmware version (“1.1.2”) and it works fine…

image

Pete.

1 Like

Thank you, Peter.
I’ll check it out and see how it goes with me.
Thanks very much.

Dear Peter.
I tried to run the code again, but still the error.
I hope you can update your full code so I can test it, then I can fix it.
Thank you so much.

  • New Firmware Requested 12:30:31 AM Today

  • Download Failure 12:30:31 AM Today

  • Request sent 12:30:30 AM Today

  • Firmware Shipment Process Started 12:27:18 AM Today

  • Firmware Shipment Process Started 12:27:18 AM Today

  • Request sent 12:27:18 AM Today

  • New Firmware Requested 12:27:18 AM Today

  • Download Failure 12:27:18 AM Today

This was the sketch I used…

#define BLYNK_TEMPLATE_ID "REDACTED"
#define BLYNK_DEVICE_NAME "REDACTED"
#define BLYNK_AUTH_TOKEN "REDACTED"

#define BLYNK_FIRMWARE_VERSION "1.1.3"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266Ping.h>

//ESP32
//#include <Update.h>
//#include <HTTPClient.h>
//ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
//________________________________________________________________
String overTheAirURL = "";

#define WIFI_SSID "REDACTED"                      //Enter Wifi Name
#define WIFI_PASS "REDACTED"                    //Enter wifi Password


boolean setBlynk = 0;

void setup() {
  Serial.begin(74880);
  checkConnection();
  checkInternet();
  pinMode(D4,OUTPUT);
}

boolean checkConnection() {
  Serial.print("Check connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.disconnect();
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(WIFI_SSID,WIFI_PASS);

  int count=0;
  while(count < 20){
    if(WiFi.status() == WL_CONNECTED){
      Serial.print("Connected to ");
      Serial.println(WIFI_SSID);
      Serial.print("Web Server IP Address: ");
      Serial.println(WiFi.localIP());
      Serial.println("");
      return true;
    }
    delay(5000);
    Serial.print(".");
    count++;
  }
  Serial.println("Timed out........ hiddenAP = 0;");
  Serial.println("");
  return false;
}


void checkInternet(){
  Serial.println("");
  bool ret = Ping.ping("www.google.com");
  if(ret){
    if(setBlynk == 0){
            Serial.println("ket noi NEW BLYNK NEW");
            Blynk.config(BLYNK_AUTH_TOKEN);
      setBlynk = 1;
    }
      if(!Blynk.connected()){
        Serial.println("Not connected to Blynk server");
        Blynk.connect(); // try to connect to server with default timeout
      }else{
        Serial.println("Connected to Blynk server");
      }
  Serial.println("");
}
}

void loop(){
  Blynk.run();
    if(!Blynk.connected()){
      int count=0;
      while(count < 3){//5 lan chay 6s Blynk.run la 30s
        Blynk.run();//thoi gian chay 6s
        if(Blynk.connected()){
          Serial.println("!Blynk.connected(); Blynk.connected())");Serial.println("");
          delay(500);
          return;
        }
        delay(1000);
        Serial.print(".");
        count++;
      }
      Serial.println("!Blynk.connected(); internetState = 0;");Serial.println("");
      delay(500);
   }
}



BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V4);
}

BLYNK_WRITE(V4) {
  int stateD4 = param.asInt();
  digitalWrite(D4, stateD4);
}

BLYNK_WRITE(InternalPinOTA) {
 Serial.println("OTA Started"); 
  overTheAirURL = param.asString();
 Serial.print("overTheAirURL = ");  
 Serial.println(overTheAirURL);  
  WiFiClient my_wifi_client;
  HTTPClient http;
  http.begin(my_wifi_client, overTheAirURL);
  int httpCode = http.GET();
 Serial.print("httpCode = ");  
 Serial.println(httpCode);  
  if (httpCode != HTTP_CODE_OK) {return;}
  int contentLength = http.getSize();
 Serial.print("contentLength = ");  
 Serial.println(contentLength);   
  if (contentLength <= 0) {return; }
  bool canBegin = Update.begin(contentLength);
 Serial.print("canBegin = ");  
 Serial.println(canBegin);    
  if (!canBegin) { return;}
  Client& client = http.getStream();
  int written = Update.writeStream(client);
 Serial.print("written = ");  
 Serial.println(written);   
  if (written != contentLength) {return;}
  if (!Update.end()) {return;}
  if (!Update.isFinished()) {return;}
reboot();
}

void reboot()
{
 Serial.println("Rebooting after OTA Update..."); 
#if defined(ARDUINO_ARCH_MEGAAVR)
  wdt_enable(WDT_PERIOD_8CLK_gc);
#elif defined(__AVR__)
  wdt_enable(WDTO_15MS);
#elif defined(__arm__)
  NVIC_SystemReset();
#elif defined(ESP8266) || defined(ESP32)
  ESP.restart();
#else
  #error "MCU reset procedure not implemented"
#endif
  for (;;) {}
}
//_________

Pete.

1 Like

Dear PeteKnight
I used your code and edited the parameter according to my device.

#define BLYNK_TEMPLATE_ID "TMPLhFym36bS"
#define BLYNK_DEVICE_NAME "TEST SWITCH"
#define BLYNK_AUTH_TOKEN "X8q5lh0pfypriO7b5zAFNEiJoi1_S6zb"

#define BLYNK_FIRMWARE_VERSION "1.1.4"

#define BLYNK_PRINT Serial

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266Ping.h>

//ESP32
//#include <Update.h>
//#include <HTTPClient.h>
//ESP8266
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
//________________________________________________________________
String overTheAirURL = "";

#define WIFI_SSID "HIEU VAI  THANH"                      //Enter Wifi Name
#define WIFI_PASS "passathai96"                    //Enter wifi Password


boolean setBlynk = 0;

void setup() {
  Serial.begin(115200);
  checkConnection();
  checkInternet();
  pinMode(D4,OUTPUT);
}

boolean checkConnection() {
  Serial.print("Check connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.disconnect();
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(WIFI_SSID,WIFI_PASS);

  int count=0;
  while(count < 20){
    if(WiFi.status() == WL_CONNECTED){
      Serial.print("Connected to ");
      Serial.println(WIFI_SSID);
      Serial.print("Web Server IP Address: ");
      Serial.println(WiFi.localIP());
      Serial.println("");
      return true;
    }
    delay(5000);
    Serial.print(".");
    count++;
  }
  Serial.println("Timed out........ hiddenAP = 0;");
  Serial.println("");
  return false;
}


void checkInternet(){
  Serial.println("");
  bool ret = Ping.ping("www.google.com");
  if(ret){
    if(setBlynk == 0){
            Serial.println("ket noi NEW BLYNK NEW");
            Blynk.config(BLYNK_AUTH_TOKEN);
      setBlynk = 1;
    }
      if(!Blynk.connected()){
        Serial.println("Not connected to Blynk server");
        Blynk.connect(); // try to connect to server with default timeout
      }else{
        Serial.println("Connected to Blynk server");
      }
  Serial.println("");
}
}

void loop(){
  Blynk.run();
    if(!Blynk.connected()){
      int count=0;
      while(count < 3){//5 lan chay 6s Blynk.run la 30s
        Blynk.run();//thoi gian chay 6s
        if(Blynk.connected()){
          Serial.println("!Blynk.connected(); Blynk.connected())");Serial.println("");
          delay(500);
          return;
        }
        delay(1000);
        Serial.print(".");
        count++;
      }
      Serial.println("!Blynk.connected(); internetState = 0;");Serial.println("");
      delay(500);
   }
}



BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V4);
}

BLYNK_WRITE(V4) {
  int stateD4 = param.asInt();
  digitalWrite(D4, stateD4);
}

BLYNK_WRITE(InternalPinOTA) {
 Serial.println("OTA Started"); 
  overTheAirURL = param.asString();
 Serial.print("overTheAirURL = ");  
 Serial.println(overTheAirURL);  
  WiFiClient my_wifi_client;
  HTTPClient http;
  http.begin(my_wifi_client, overTheAirURL);
  int httpCode = http.GET();
 Serial.print("httpCode = ");  
 Serial.println(httpCode);  
  if (httpCode != HTTP_CODE_OK) {return;}
  int contentLength = http.getSize();
 Serial.print("contentLength = ");  
 Serial.println(contentLength);   
  if (contentLength <= 0) {return; }
  bool canBegin = Update.begin(contentLength);
 Serial.print("canBegin = ");  
 Serial.println(canBegin);    
  if (!canBegin) { return;}
  Client& client = http.getStream();
  int written = Update.writeStream(client);
 Serial.print("written = ");  
 Serial.println(written);   
  if (written != contentLength) {return;}
  if (!Update.end()) {return;}
  if (!Update.isFinished()) {return;}
ESP.restart();
}

However, the error is still the same.

16:21:10.918 -> Check connecting to HIEU VAI  THANH
16:21:16.068 -> .Connected to HIEU VAI  THANH
16:21:16.068 -> Web Server IP Address: 192.168.1.21
16:21:16.068 -> 
16:21:16.068 -> 
16:21:21.068 -> ket noi NEW BLYNK NEW
16:21:21.068 -> [32728] 
16:21:21.068 ->     ___  __          __
16:21:21.068 ->    / _ )/ /_ _____  / /__
16:21:21.068 ->   / _  / / // / _ \/  '_/
16:21:21.068 ->  /____/_/\_, /_//_/_/\_\
16:21:21.068 ->         /___/ v1.1.0 on NodeMCU
16:21:21.068 -> 
16:21:21.068 ->  #StandWithUkraine    https://bit.ly/swua
16:21:21.068 -> 
16:21:21.068 -> 
16:21:21.068 -> Not connected to Blynk server
16:21:21.068 -> [32739] Connecting to blynk.cloud:80
16:21:21.198 -> [32860] Ready (ping: 42ms).
16:21:21.328 -> 
16:22:41.060 -> OTA Started
16:22:41.060 -> overTheAirURL = http://sgp1.blynk.cloud/static/fw_2886935217758816059_-838595071.bin?token=HVQLPULctf_MpnfTM88l1gsd3rJqpITR
16:22:41.201 -> httpCode = 200
16:22:41.201 -> contentLength = 298368
16:22:41.201 -> canBegin = 0
Firmware Shipment Process Started 4:22:40 PM Today
Request sent 4:22:40 PM Today
New Firmware Requested 4:22:40 PM Today
Download Failure 4:22:40 PM Today
Firmware Shipment Process Started 4:22:05 PM Today

Do you think the problem could be caused by this command line library error?

bool canBegin = Update.begin(contentLength);

No. It works perfectly for me.

Pete.

1 Like

After I updated ESP8266 board to 3.0.2 (still use Arduino 1.8.8), the error has changed to this command line.

17:27:15.884 -> overTheAirURL = http://sgp1.blynk.cloud/static/fw_15028989384836929779_-838595071.bin?token=9vUHQAe-qkEJTtyZ2M0RfAqqirmaUt0I
17:27:16.024 -> httpCode = 200
17:27:16.024 -> contentLength = 303360
17:27:16.024 -> canBegin = 1
17:27:16.024 -> written = 0
TEST SWITCH update details
Download Failure 5:27:15 PM Today
Firmware Shipment Process Started 5:27:14 PM Today
Request sent 5:27:14 PM Today
New Firmware Requested 5:27:14 PM Today
Firmware Shipment Process Started 5:26:33 PM Today

Hi Peter. Thank you for your help.
Hi everybody.

I think I solved it, using another update code.

Arduino 1.8.19. ESP8266 board 3.0.2. BlynkSimpleEsp8266.h.

I was able to update via BlynkAir, even with the option of Lower Firmware version or No condition.

And here is the code I used, and the details of the results.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN "X8q5lh0pfypriO7b5zAFNEiJoi1_S6zb"

#define BLYNK_FIRMWARE_VERSION "9.4.0"

#define BLYNK_PRINT Serial



//ESP32
//#include <Update.h>
//#include <HTTPClient.h>

//ESP8266
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <ESP8266Ping.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#include <ESP8266HTTPUpdateServer.h>
#include <WiFiClient.h>

//________________________________________________________________
String overTheAirURL = "";

#define WIFI_SSID "HIEU VAI  THANH"                      //Enter Wifi Name
#define WIFI_PASS "passathai96"                    //Enter wifi Password


boolean setBlynk = 0;

void setup() {
  Serial.begin(115200);
  Serial.print("BLYNK_FIRMWARE_VERSION = ");
  Serial.println(BLYNK_FIRMWARE_VERSION);
  checkConnection();
  checkInternet();
  pinMode(D4,OUTPUT);
}

boolean checkConnection() {
  Serial.print("Check connecting to ");
  Serial.println(WIFI_SSID);
  WiFi.disconnect();
  WiFi.mode(WIFI_AP_STA);
  WiFi.begin(WIFI_SSID,WIFI_PASS);

  int count=0;
  while(count < 20){
    if(WiFi.status() == WL_CONNECTED){
      Serial.print("Connected to ");
      Serial.println(WIFI_SSID);
      Serial.print("Web Server IP Address: ");
      Serial.println(WiFi.localIP());
      Serial.println("");
      return true;
    }
    delay(5000);
    Serial.print(".");
    count++;
  }
  Serial.println("Timed out........ hiddenAP = 0;");
  Serial.println("");
  return false;
}


void checkInternet(){
  Serial.println("");
  bool ret = Ping.ping("www.google.com");
  if(ret){
    if(setBlynk == 0){
            Serial.println("ket noi NEW BLYNK NEW");
            Blynk.config(BLYNK_AUTH_TOKEN);
      setBlynk = 1;
    }
      if(!Blynk.connected()){
        Serial.println("Not connected to Blynk server");
        Blynk.connect(); // try to connect to server with default timeout
      }else{
        Serial.println("Connected to Blynk server");
      }
  Serial.println("");
}
}

void loop(){
  Blynk.run();
    if(!Blynk.connected()){
      int count=0;
      while(count < 3){//5 lan chay 6s Blynk.run la 30s
        Blynk.run();//thoi gian chay 6s
        if(Blynk.connected()){
          Serial.println("!Blynk.connected(); Blynk.connected())");Serial.println("");
          delay(500);
          return;
        }
        delay(1000);
        Serial.print(".");
        count++;
      }
      Serial.println("!Blynk.connected(); internetState = 0;");Serial.println("");
      delay(500);
   }
}



BLYNK_CONNECTED() {
  // Request the latest state from the server
  Blynk.syncVirtual(V4);
}

BLYNK_WRITE(V4) {
  int stateD4 = param.asInt();
  digitalWrite(D4, stateD4);
}

BLYNK_WRITE(InternalPinOTA) {
 Serial.println("OTA Started"); 
  overTheAirURL = param.asString();
 Serial.print("overTheAirURL = ");  
 Serial.println(overTheAirURL);  

 //HTTPClient http;
 //http.begin(overTheAirURL);

 
  WiFiClient my_wifi_client;
  HTTPClient http;
  http.begin(my_wifi_client, overTheAirURL);


t_httpUpdate_return ret = ESPhttpUpdate.update(my_wifi_client, overTheAirURL);
switch(ret) {
    case HTTP_UPDATE_FAILED:
        Serial.println("[update] Update failed.");
        break;
    case HTTP_UPDATE_NO_UPDATES:
        Serial.println("[update] Update no Update.");
        break;
    case HTTP_UPDATE_OK:
        Serial.println("[update] Update ok."); // may not be called since we reboot the ESP
        break;
}
}
21:38:25.739 -> .Connected to HIEU VAI  THANH
21:38:25.739 -> Web Server IP Address: 192.168.1.21
21:38:25.739 -> 
21:38:25.739 -> 
21:38:30.801 -> ket noi NEW BLYNK NEW
21:38:30.801 -> [10165] 
21:38:30.801 ->     ___  __          __
21:38:30.801 ->    / _ )/ /_ _____  / /__
21:38:30.847 ->   / _  / / // / _ \/  '_/
21:38:30.847 ->  /____/_/\_, /_//_/_/\_\
21:38:30.847 ->         /___/ v1.1.0 on ESP8266
21:38:30.847 -> 
21:38:30.847 ->  #StandWithUkraine    https://bit.ly/swua
21:38:30.847 -> 
21:38:30.847 -> 
21:38:30.847 -> Not connected to Blynk server
21:38:30.847 -> [10175] Connecting to blynk.cloud:80
21:38:31.081 -> [10403] Ready (ping: 63ms).
21:38:31.174 -> 
21:39:44.176 -> OTA Started
21:39:44.176 -> overTheAirURL = http://sgp1.blynk.cloud/static/fw_8861998046749586401_-838595071.bin?token=EWvl4L6F4FuusgoQ79c0J8YD_zBXIKl0
21:39:56.983 -> 
21:39:56.983 ->  ets Jan  8 2013,rst cause:2, boot mode:(3,6)
21:39:56.983 -> 
21:39:56.983 -> load 0x4010f000, len 3460, room 16 
21:39:56.983 -> tail 4
21:39:56.983 -> chksum 0xcc
21:39:56.983 -> load 0x3fff20b8, len 40, room 4 
21:39:56.983 -> tail 4
21:39:56.983 -> chksum 0xc9
21:39:56.983 -> csum 0xc9
21:39:56.983 -> v0004ced0
21:39:56.983 -> @cp:B0
21:40:02.268 -> ld
21:40:02.348 -> Check connecting to HIEU VAI  THANH
21:40:07.359 -> .Connected to HIEU VAI  THANH
21:40:07.359 -> Web Server IP Address: 192.168.1.21
21:40:07.359 -> 
21:40:07.359 -> 
21:40:11.559 -> ket noi NEW BLYNK NEW
21:40:11.559 -> [14574] 
21:40:11.559 ->     ___  __          __
21:40:11.559 ->    / _ )/ /_ _____  / /__
21:40:11.559 ->   / _  / / // / _ \/  '_/
21:40:11.559 ->  /____/_/\_, /_//_/_/\_\
21:40:11.559 ->         /___/ v1.1.0 on ESP8266
21:40:11.559 -> 
21:40:11.559 ->  #StandWithUkraine    https://bit.ly/swua
21:40:11.559 -> 
21:40:11.559 -> 
21:40:11.559 -> Not connected to Blynk server
21:40:11.598 -> [14585] Connecting to blynk.cloud:80
21:40:11.729 -> [14717] Ready (ping: 36ms).
21:40:11.846 -> 
21:45:19.424 -> OTA Started
21:45:19.424 -> overTheAirURL = http://sgp1.blynk.cloud/static/fw_14483877767671016810_-838595071.bin?token=4gCroqeGQShaN-HW3uwnraMoguXYDh_O
21:45:34.288 -> 
21:45:34.288 ->  ets Jan  8 2013,rst cause:2, boot mode:(3,6)
21:45:34.288 -> 
21:45:34.288 -> load 0x4010f000, len 3460, room 16 
21:45:34.288 -> tail 4
21:45:34.288 -> chksum 0xcc
21:45:34.288 -> load 0x3fff20b8, len 40, room 4 
21:45:34.288 -> tail 4
21:45:34.288 -> chksum 0xc9
21:45:34.288 -> csum 0xc9
21:45:34.288 -> v0004ced0
21:45:34.288 -> @cp:B0
21:45:39.588 -> ld
21:45:39.638 -> Check connecting to HIEU VAI  THANH
21:45:44.668 -> .Connected to HIEU VAI  THANH
21:45:44.668 -> Web Server IP Address: 192.168.1.21
21:45:44.668 -> 
21:45:44.668 -> 
21:45:48.818 -> ket noi NEW BLYNK NEW
21:45:48.818 -> [14541] 
21:45:48.818 ->     ___  __          __
21:45:48.818 ->    / _ )/ /_ _____  / /__
21:45:48.818 ->   / _  / / // / _ \/  '_/
21:45:48.818 ->  /____/_/\_, /_//_/_/\_\
21:45:48.818 ->         /___/ v1.1.0 on ESP8266
21:45:48.818 -> 
21:45:48.818 ->  #StandWithUkraine    https://bit.ly/swua
21:45:48.858 -> 
21:45:48.858 -> 
21:45:48.858 -> Not connected to Blynk server
21:45:48.858 -> [14552] Connecting to blynk.cloud:80
21:45:48.973 -> [14707] Ready (ping: 38ms).
21:45:49.128 -> 
21:49:32.571 -> OTA Started
21:49:32.571 -> overTheAirURL = http://sgp1.blynk.cloud/static/fw_5800073477836424206_-838595071.bin?token=-J3KrC3e9FTBYDhIhGNUq3wxmTKfOi6Q
21:49:46.868 -> 
21:49:46.868 ->  ets Jan  8 2013,rst cause:2, boot mode:(3,6)
21:49:46.868 -> 
21:49:46.868 -> load 0x4010f000, len 3460, room 16 
21:49:46.908 -> tail 4
21:49:46.908 -> chksum 0xcc
21:49:46.908 -> load 0x3fff20b8, len 40, room 4 
21:49:46.908 -> tail 4
21:49:46.908 -> chksum 0xc9
21:49:46.908 -> csum 0xc9
21:49:46.908 -> v0004ccc0
21:49:46.908 -> @cp:B0
21:49:52.208 -> ld
21:49:52.248 -> BLYNK_FIRMWARE_VERSION = 9.4.1
21:49:52.248 -> Check connecting to HIEU VAI  THANH
21:49:57.248 -> .Connected to HIEU VAI  THANH
21:49:57.248 -> Web Server IP Address: 192.168.1.21
21:49:57.288 -> 
21:49:57.288 -> 
21:50:02.398 -> ket noi NEW BLYNK NEW
21:50:02.398 -> [15505] 
21:50:02.398 ->     ___  __          __
21:50:02.398 ->    / _ )/ /_ _____  / /__
21:50:02.398 ->   / _  / / // / _ \/  '_/
21:50:02.398 ->  /____/_/\_, /_//_/_/\_\
21:50:02.398 ->         /___/ v1.1.0 on ESP8266
21:50:02.398 -> 
21:50:02.398 ->  #StandWithUkraine    https://bit.ly/swua
21:50:02.398 -> 
21:50:02.398 -> 
21:50:02.398 -> Not connected to Blynk server
21:50:02.438 -> [15515] Connecting to blynk.cloud:80
21:50:02.638 -> [15731] Ready (ping: 39ms).
21:50:02.770 -> 

And the error “Firmware type mismatch”, because i used the same code for the next shipping. We must edit just a little before creating the bin file.

1 Like

I have solved the error “Firmware type mismatch”, by typing once the Space key in the space at this line of code.

#define BLYNK_FIRMWARE_VERSION      "22.09.11"

I don’t know the reason for this error, but with such a simple operation, I solved the above error.

@nlqt2022
I am seeing this error “Compilation error: ESP8266Ping.h: No such file or directory”
Can you please guide how to solve

Do you have the ESP8266Ping library installed ?

I would take the code from post #1 of this topic, not one where someone is attempting to add the OTA code into the rest of their sketch.

The ping library isn’t needed for the OTA to work.

Pete.