Blynk.Air OTA minimal code

I hope this topic here is close to the topic that i wanted…

Hi, I am Simon and I am looking for help, to minimize the code of BlynkEdgent, where i just need basic Blynk function, for upload firmware remotely using Blynk Console with my PC.
I just want to set the parameter directly in the code as usual:

#define BLYNK_TEMPLATE_ID "xxxx"
#define BLYNK_TEMPLATE_NAME "yyyy"
#define BLYNK_AUTH_TOKEN "zzzz"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"

And, assign the wifi user and password as usually we did:

char ssid[] = "ssid";
 char pass[] = "password";
 Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass, "blynk.cloud", 80);

The BlynkEdgent sample code is very useful, and i tried it and works for remote uploading using Blynk Shipment. But not really works for my case as i need to pre setup all the parameter before upload to the device. My templeteID, Name, Token, user ID and password are all the same for all devices and only one device will be turns ON at a time.

Is there anyone done this before, can i share code please… i truly need it as i am not good in code modification especially the complexity of BlynkEdgent is out of my reach. TQ in advance!!

without using BlynkEdgent code, online shipment of code is not possible am i right??

@Simon_Cheng Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

You’ve clearly missed the point of the minimal code posted in the first post of this topic.
This minimal code can be added to ANY Blynk sketch to add OTA capabilities (provided the board supports OTA and has a suitable HTTP client available. The original minimal code was written for the ESP32, but there are ESP8266 variants included later).

If you take one of the Blynk non-Edgent example sketches and add the minimal code to it then you will have Blynk.Air OTA capability.

Pete.

1 Like

Thank you @Ibrahim_Roshdy for posting this code, and thank you @PeteKnight for testing and commenting on it.

I have edited @Ibrahim_Roshdy code with @PeteKnight additions and republish here for future Blynkers. See below.

This longtime Blynker now needs Blynk-style remote OTA, and doesn’t require all the rest of Edgent (for now).

My use case: I’m an intense hobbyist building IoT for my own home. Now I’m building a device for my daughter who lives elsewhere - Central America - so I need a remote OTA capability for the first time. Now you know.

Much appreciated. Looks like it works just fine for me.
Thank you both, and thanks Blynk.Community.

I have added the Blynk.disconnect(); and Blynk.connect(); instructions where suggested. I also put the void reboot() at the top so it would compile on my IDE.

I name this file myBlynkAir.h and place it in the folder with my sketch.
I add these two lines AFTER the #include <BlynkSimpleEsp32_SSL.h> statement.

#include "myBlynkAir.h" // My own Blynk OTA setup
#define BLYNK_FIRMWARE_VERSION "0.2.13"

Here’s the updated myBlynkAir.h for your use. All comments welcome.

#include <Update.h>
#include <HTTPClient.h>
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 (;;) {}
}

//________________________________________________________________
String overTheAirURL = "";
BLYNK_WRITE(InternalPinOTA) {
  overTheAirURL = param.asString();
  Blynk.disconnect();  

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


}


//__________________________________________________________

I’d like to ask a general beginner’s “how does this work?” question. Some things like #if I could Google. I understand the 2 “includes…” and “string…” statements go in the declaraions section at the beginning. “void reboot () {…}” is a normal subroutine/function.

What I don’t understand is “BLYNK_WRITE(InternalPinOTA) {. . .}”. Pete later told someone it goes outside all other functions so it must be a function itself, but there’s no declaration (void, int, etc) and I can’t find where it’s called.

Also, in another example from user [nlqt2022] there’s another “function” with the same name (but different param). how does that not conflict?

BLYNK_WRITE() functions are triggered when data is sent from the Blynk server to the device. They are in effect callbacks.
The place you’ll usually see these is when you attach a widget to a virtual pin. In these cases the BLYNK_WRITE(virtual pin number) is triggered and the result allows you to extract the values from the widget.

In this case, this is a special callback associated with the OTA functionality.

Pete.

@wakewatcher i see that you asked a question about the esp826)ping library, then deleted the question.
I assume you asked this because you took the code from post #22 which for some reason included this library. My advice to others who had this problems was…

However, as the original code was for an ESP32, some changes are needed to make it work for an ESP8266 board, so I’d use this as a starting point instead…

Pete.

Hi Pete, Thanks for your previous recent help for this newbie. I used your sketch. I had an initial headache that ESP8266Ping.h didn’t show up in the library manager of IDE 2.3.5 but I got passed that by finding it on github and installed it locally. (I deleted that post when I figured that out.) Anyway it work! Well it worked the first time. In setup I have it Serial.println("Setup Complete Version: " + String(BLYNK_FIRMWARE_VERSION)); and of course it did it with the original when I initially uploaded it to my 8266. And then the first time I shipped an update with the incremented version number it worked! From Version 1.1.4 to 1.1.5. However it only worked once. Every time since then I get I get a “live” status and it never gets to finished.

image

This is what is printed on the serial port. It looks like it’s connected but nothing is downloaded. I’m not sure why it worked the first time but no longer. What have I missed?

05:57:53.471 → Setup Complete Version: 1.1.5
05:57:53.504 →
05:57:53.585 → OTA Started
05:57:53.585 → overTheAirURL = http://ny3.blynk.cloud/static/fw_6523757209194219855_-838595071.bin?token=lMw7St4f-Axhe-hIpuBqSsRZEV-0kNBY
05:57:53.787 → httpCode = 200
05:57:53.787 → contentLength = 309808
05:57:53.787 → canBegin = 1

Difficult to say, because I have no idea if you’ve changed anything other than the version number. Did you change any compiler settings?

As I said before, the ping stuff isn’t needed. It’s simply the ESP8266 version of the HTTP update stuff that’s needed, and the slightly different syntax for the code in BLYNK_WRITE(InternalPinOTA).

What happens if you you do a USB flash, then try to re-do the OTA shipment that worked last time?

It may help if you started a new topic, instead of cluttering up this one.

Pete.