Migrating to the new version

Hi all,

So I have recently realised that the legacy app is being put to sleep and I need to move forward with Blynk 2.0

Do I need to modify my code (pasted below) other than replacing the old token and instead showing the Template ID, Device Name, and AuthTokenat the top of the code?

From what I understand I also need to fill out the comparable pin details into ‘datastream’ on the blynk.console, configure the web dashboard and then recreate/configure the mobile app?

Is that correct? Appreciate the help.

#define BLYNK_PRINT Serial


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



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "authtoken";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "ssid";
char pass[] = "password";



BlynkTimer timer;
BlynkTimer timer2;

const int relayPin = 2; //GPIO pin relay is attached to (D4)
const int btnPin = 14; //pin physical button is attached to. wired between GPIO14 (D5) and GND

int cycleState = LOW;
int btnState = HIGH;

float RunTime = 1;
int DelayTime = 1;

int timerId1;
int timerId2;
int timerId3;

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

int onFlag = 0;

WidgetLED led1(V10);

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the serverformula answer virtual pin write blynk flow rate
  Blynk.syncVirtual(V1, V2, V7);
  ReCnctCount = 0;

}

// When App button is pushed - switch the state. The button should be on virtual pin 7 and set to switch
BLYNK_WRITE(V7) {
  cycleState = param.asInt();

  if (cycleState == HIGH) {
    onFlag = 1;
    onRelay();

  }
  else if (cycleState == LOW) {
    onFlag = 0;
  }
}





void checkPhysicalButton()
{
  //Serial.println("BUTTON CHECK");
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    //Serial.println("BUTTON PRESSED");
    if (btnState != LOW) {

      // Toggle cycleState
      cycleState = !cycleState;
      Blynk.virtualWrite(V7, cycleState);
      if (cycleState == HIGH) {
        onFlag = 1;
        onRelay();
      }
      else if (cycleState == LOW) {
        onFlag = 0;
      }

    }
    btnState = LOW;
  }
  else {
    btnState = HIGH;
  }
}

BLYNK_WRITE (V1) // Time interval for misting 0.5-10 seconds
{
  RunTime = param.asFloat(); // assigning incoming value from pin V1 to a variable

  Serial.println(RunTime);
}



BLYNK_WRITE (V2) // Time interval for delay 5-120 seconds
{
  DelayTime = param.asInt(); // assigning incoming value from pin V2 to a variable

  Serial.println(DelayTime);
}








BLYNK_READ(V30) //Displays Wifi Strength as a % in the an the app (Value display setting widget) - Set to Virtual Pin 30
{
   Blynk.virtualWrite(V30, String("Wifi: ") + map(WiFi.RSSI(), -105, -40, 0, 100) + String('%'));
}









void onRelay()
{
  if (onFlag == 1) {
    timer.deleteTimer(timerId1);//delete timers in case they are still running, that is buttons turned on/off/on too fast
    timer.deleteTimer(timerId2);//delete timers in case they are still running, that is buttons turned on/off/on too fast
    digitalWrite(relayPin, HIGH); //turn relay ON
    led1.on();
    Serial.println("ON");  
    Serial.println((((RunTime/(RunTime+DelayTime)))*11*5.5));     
Blynk.virtualWrite(V8,(((RunTime/(RunTime+DelayTime)))*11*5.5));
Blynk.virtualWrite(V9,"L/Hr");
    timerId1 = timer.setTimeout(RunTime * 1000, offRelay); //run after
  }

}

void offRelay()
{
  digitalWrite(relayPin, LOW);//turn relay OFF
  led1.off();
  Serial.println("OFF");
  timerId2 = timer.setTimeout(DelayTime * 1000, onRelay); //run after
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  pinMode(relayPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(relayPin, LOW); //relay is active HIGH, start with relay OFF

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

  // Setup a function to be called every 100 ms
  timerId3 = timer2.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  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 a moment...");
    timer2.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
  }
  timer.run();
  timer2.run();
}

Hey there, check this out
https://docs.blynk.io/en/blynk-1.0-and-2.0-comparison/migrate-from-1.0-to-2.0

This might help you

It looks like it would also be a good time to sort-out your BlynkTimers.
You only need one timer object, and you would be better using timeout timers instead of creating and deleting all of these temporary timers.

Pete,

Thanks all for your comments.

I didn’t realise I’d have to change so much. For someone like me below the hobbyist level this seems daunting.

With the legacy code, I had an idea, went and did it (with help obviously) and since then I forgot everything I’ve done and why…it just works. That’s what happens if your not even a hobbyist.

Only wish I could continue with the legacy app for a project already made!

You can, for now, but one day it will stop working.

The code changes are minimal, and the rest of the setup is fairly straightforward.
The issues with the timers is down to bad coding in the first instance and if you want to live with what you have then you can.

Pete.

Ok thanks

I’ll start to look into the minor changes first. Get that working first with 2.0

The timers I’ll consider as the next step. But what will be the main issue if I don’t tackle the timers? (If I have no intention to further modify/add to the code later)

BTW do we know when legacy dies? I couldn’t find a date.

Having two timer objects doubles the memory usage, which can cause issues. Understanding exactly how your timers function is much more complex than it you’d used timeouts.
If you aren’t worried about either of these issues then stick with what you have.

No.

Pete.

Thank you

okay, have started to have a go at this and already come across a hurdle

BlynkEdgent.h: No such file or directory

How is this so if I have installed the latest library, restarted the IDE? If I go an Examples>Blynk>BlynkEdget it is there?

What’s have I missed?

Beggining of the code is this

#include <Blynk.h>
#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "xxx"
#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#include <BlynkSimpleEsp32.h>
#define APP_DEBUG
#include "BlynkEdgent.h"

I guess you’ve done something odd, like copying the Edgent_ESP32.ino file from the GitHub site without also copying all of the other Edgent files as well.

The easiest way is to go to File > Examples > Blynk > Blynk.Edgent and open the appropriate example (Edgent_ESP32 in your case).

This will then give you the appropriate tabs at the top of your sketch like this…

Pete.

so instead of editing my code, you suggest I copy my code over to the Edgent_ESP32 Example?

BTW I downloaded the zip from the github and then simply adding with sketch>include library>add .zip library. Then restarted the IDE

If you want to use Edgent then you need all of the supporting .h files, which appear in the tabs in the screenshot I provided.
If you want a simpler solution then do nothing other than adding the Template ID and Device Name lines to the top of your code and using the IoT Auth token.

Pete.

Thanks Pete, as Edgent is not important to me anyway that is what I have just tried to do.
Below is the latest code (I used the example from Blynk_WiFi>ESP32_Wifi).

The code uploaded ok, but just won’t connect and in the console it shows device is offline? What did I miss here? I have tripled checked my credentials, tried various ip addresses with the latest being 45.55.96.146. I have also restarted my router.

#define BLYNK_TEMPLATE_ID "xxx"
#define BLYNK_DEVICE_NAME "xxx"

#define BLYNK_FIRMWARE_VERSION        "0.1.0"
#define BLYNK_PRINT Serial


#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";



BlynkTimer timer;
BlynkTimer timer2;

const int relayPin = 2; //GPIO pin relay is attached to (D2)
const int btnPin = 14; //pin physical button is attached to. wired between GPIO14 (D5) and GND

int cycleState = LOW;
int btnState = HIGH;

float RunTime = 1;
int DelayTime = 1;

int timerId1;
int timerId2;
int timerId3;

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

int onFlag = 0;

WidgetLED led1(V10);

// Every time we connect to the cloud...
BLYNK_CONNECTED() {
  // Request the latest state from the serverformula answer virtual pin write blynk flow rate
  Blynk.syncVirtual(V1, V2, V7);
  ReCnctCount = 0;

}

// When App button is pushed - switch the state. The button should be on virtual pin 7 and set to switch
BLYNK_WRITE(V7) {
  cycleState = param.asInt();

  if (cycleState == HIGH) {
    onFlag = 1;
    onRelay();

  }
  else if (cycleState == LOW) {
    onFlag = 0;
  }
}





void checkPhysicalButton()
{
  //Serial.println("BUTTON CHECK");
  if (digitalRead(btnPin) == LOW) {
    // btnState is used to avoid sequential toggles
    //Serial.println("BUTTON PRESSED");
    if (btnState != LOW) {

      // Toggle cycleState
      cycleState = !cycleState;
      Blynk.virtualWrite(V7, cycleState);
      if (cycleState == HIGH) {
        onFlag = 1;
        onRelay();
      }
      else if (cycleState == LOW) {
        onFlag = 0;
      }

    }
    btnState = LOW;
  }
  else {
    btnState = HIGH;
  }
}

BLYNK_WRITE (V1) // Time interval for misting 0.5-10 seconds
{
  RunTime = param.asFloat(); // assigning incoming value from pin V1 to a variable

  Serial.println(RunTime);
}



BLYNK_WRITE (V2) // Time interval for delay 5-120 seconds
{
  DelayTime = param.asInt(); // assigning incoming value from pin V2 to a variable

  Serial.println(DelayTime);
}








BLYNK_READ(V30) //Displays Wifi Strength as a % in the an the app (Value display setting widget) - Set to Virtual Pin 30
{
   Blynk.virtualWrite(V30, String("Wifi: ") + map(WiFi.RSSI(), -105, -40, 0, 100) + String('%'));
}









void onRelay()
{
  if (onFlag == 1) {
    timer.deleteTimer(timerId1);//delete timers in case they are still running, that is buttons turned on/off/on too fast
    timer.deleteTimer(timerId2);//delete timers in case they are still running, that is buttons turned on/off/on too fast
    digitalWrite(relayPin, HIGH); //turn relay ON
    led1.on();
    Serial.println("ON");  
    Serial.println((((RunTime/(RunTime+DelayTime)))*11*5.5));     
Blynk.virtualWrite(V8,(((RunTime/(RunTime+DelayTime)))*11*5.5));
Blynk.virtualWrite(V9,"L/Hr");
    timerId1 = timer.setTimeout(RunTime * 1000, offRelay); //run after
  }

}

void offRelay()
{
  digitalWrite(relayPin, LOW);//turn relay OFF
  led1.off();
  Serial.println("OFF");
  timerId2 = timer.setTimeout(DelayTime * 1000, onRelay); //run after
}

void setup()
{
  // Debug console
  Serial.begin(115200);

  pinMode(relayPin, OUTPUT);
  pinMode(btnPin, INPUT_PULLUP);
  digitalWrite(relayPin, LOW); //relay is active HIGH, start with relay OFF

  WiFi.begin(ssid, pass);  // Non-blocking if no WiFi available
  Blynk.config(auth, IPAddress(45,55,96,146), 8080);
  Blynk.connect();

  // Setup a function to be called every 100 ms
  timerId3 = timer2.setInterval(100L, checkPhysicalButton);
}

void loop()
{
  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 a moment...");
    timer2.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
  }
  timer.run();
  timer2.run();
}

a snippet from the serial monitor

[5145] Connecting to 45.55.96.146

[10146] Connecting to 45.55.96.146

[15147] Connecting to 45.55.96.146

Starting reconnection timer in a moment…

Attempting reconnection #1

[48144] Connecting to 45.55.96.146

[53145] Connecting to 45.55.96.146

[58146] Connecting to 45.55.96.146

[63147] Connecting to 45.55.96.146

Starting reconnection timer in a moment…

Attempting reconnection #2

[96144] Connecting to 45.55.96.146

[101145] Connecting to 45.55.96.146

[106146] Connecting to 45.55.96.146

[111147] Connecting to 45.55.96.146

Starting reconnection timer in a moment…

I am stuck on this.

Starting reconnection timer in a moment…

Attempting reconnection #4

[192143] Connecting to blynk-cloud.com:8080

[197144] Connecting to blynk-cloud.com:8080

[202145] Connecting to blynk-cloud.com:8080

[207146] Connecting to blynk-cloud.com:8080

Starting reconnection timer in a moment…

Attempting reconnection #5

[240143] Connecting to blynk-cloud.com:8080

[245144] Connecting to blynk-cloud.com:8080

[250145] Connecting to blynk-cloud.com:8080

[255146] Connecting to blynk-cloud.com:8080

Starting reconnection timer in a moment…

Attempting reconnection #6

[288143] Connecting to blynk-cloud.com:8080

[293144] Connecting to blynk-cloud.com:8080

[298145] Connecting to blynk-cloud.com:8080

[303146] Connecting to blynk-cloud.com:8080

Starting reconnection timer in a moment…

Will blynk-cloud.com belongs to blynk legacy, in blynk IOT it’s blynk.cloud

Thanks - but why can’t I ping that?

That’s a type from @John93 it shold say blynk.cloud

However, you shouldn’t be hard-coding an IP address into your Blynk.config command, you should be doing this:

Blynk.config(auth, "blynk.cloud", 8080);

Pete.

1 Like

I fixed it, it was a mistake :sweat_smile:

1 Like