Blynk legacy to Blynk 2.0

Hi calandry86

You will need to add define as the first line, before any include

´´
#define BLYNK_TEMPLATE_ID “T…”
#define BLYNK_DEVICE_NAME “Val…”
#define BLYNK_AUTH_TOKEN “zf-t43---------Tr”
#define BLYNK_FIRMWARE_VERSION “0.0.2”
#define BLYNK_NO_BUILTIN
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
//#define APP_DEBUG

#include <BlynkSimpleEsp8266.h>
#include <ESP8266httpUpdate.h>
#include <TimeLib.h>
#include <WidgetRTC.h>
´´

1 Like

Also, calling a function from your void loop which performs a Blynk.virtualWrite every time the void loop executes is very bad practice…

https://docs.blynk.io/en/legacy-platform/legacy-articles/keep-your-void-loop-clean

Pete.

my bad, I forgot to put a delay(1000) in there.

You are in real hurry. I suggest you to go through the link @PeteKnight provided.

They have clearly mentioned not to use any delays.

Remove this from your void loop. Timer will take
care of that function.

yes yes, i know, don’t use delays, I’m aware, you can see in my code that’s it’s not my first time using timers. Let’s stay on topic please, the problem I want resolved is the connectivity issues. I spoke too quickly about the delay(1000).

The legacy works, Blynk 2.0 doesn’t. That’s the issue. The rest I’ll optimize later.

I’ve added the extra statements suggested, but now I’m getting a different error.

“Error compiling for board Generic ESP8266 Module”

At least it’s a different error, but now it won’t compile.

1 Like
#include <Blynk.h>

#define BLYNK_TEMPLATE_ID "TMPLbrNP_Ylb"
#define BLYNK_DEVICE_NAME "keystudio"
#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "omCOqO8JjxopGXhi8F20DCHFz8csI84d"
#define BLYNK_FIRMWARE_VERSION "0.0.2"
#define BLYNK_NO_BUILTIN
#define BLYNK_PRINT Serial

#include <BlynkSimpleEsp8266.h>
#include <ESP8266httpUpdate.h>
#include <ESP8266WiFi.h>
//#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "christian's house";
char pass[] = "christian";

BlynkTimer timer;

#define sensorA A0 //Sensor is connected to A4

void setup() {
  Blynk.begin(auth, ssid, pass);

  // BLYNK RECONNECT START
  timer.setInterval(30*1000, reconnectBlynk); //run every 30s
  // BLYNK RECONNECT END
}

void loop() {
  
  Blynk.run();

  
  // BLYNK RECONNECT START//
  timer.run(); 
  if(Blynk.connected()) 
  { 
    Blynk.run(); 
  }
  // BLYNK RECONNECT END//
  
   someFunction(); 
}

void someFunction()
{
  Blynk.virtualWrite(V3, analogRead(sensorA));
}

// BLYNK RECONNECT START
void reconnectBlynk() {
  if (!Blynk.connected()) {

    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
}
// BLYNK RECONNECT END

??

What is the MCU you are using ? Nodemcu dev board ? Please provide all the necessary details.

If you are aware of how blynk works, then you have to clean up your code properly. Only then we can rule out few things and concentrate on the other aspects. When we are not sure what is going on when n where we cannot get things straight.

I would suggest you to start with bare min sketch from example folder, compile n upload the code. If the board connects n start working then, the code you have right now are having errors.

If not your board selection may be wrong n many other aspects.

I already sent the link of the wiki page of the board I’m using.

Like I said, the code works on blynk legacy, and stays connected. All that’s different is Blynk 2.0, I just need to know what I have to do differently to transition to the new Blynk.

I’m sending this updated code just so we can stay on topic.

#include <Blynk.h>

#define BLYNK_TEMPLATE_ID "TMPLbrNP_Ylb"
#define BLYNK_DEVICE_NAME "keystudio"
#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "omCOqO8JjxopGXhi8F20DCHFz8csI84d"
#define BLYNK_FIRMWARE_VERSION "0.0.2"
#define BLYNK_NO_BUILTIN
#define BLYNK_PRINT Serial

#include <BlynkSimpleEsp8266.h>
#include <ESP8266httpUpdate.h>
#include <ESP8266WiFi.h>
//#include <TimeLib.h>
#include <WidgetRTC.h>

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "christian's house";
char pass[] = "christian";

BlynkTimer timer;

const int someButton =  2;
#define sensorA A0 //Sensor is connected to A4

void setup() {
  Blynk.begin(auth, ssid, pass);

  // BLYNK RECONNECT START
  timer.setInterval(30*1000, reconnectBlynk); //run every 30s
  // BLYNK RECONNECT END

  pinMode(someButton, INPUT);
}

void loop() {
  
  Blynk.run();

  // BLYNK RECONNECT START//
  timer.run(); 
  if(Blynk.connected()) 
  { 
    Blynk.run(); 
  }
  // BLYNK RECONNECT END//

  if(digitalRead(someButton) == true)
  {
    someFunction();
  }
}

void someFunction()
{
  Blynk.virtualWrite(V3, analogRead(sensorA));
}

// BLYNK RECONNECT START
void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
}
// BLYNK RECONNECT END

So I uncommented the "#include <WidgetRTC.h> and it then compiles like before, but device is still offline.

What does your serial monitor show?

Forgot to ask ! Have you updated the Blynk library? What version are you using now?

Hey there,
First template ID, template name must by the first two lines in you sketch.

Second I don’t know why are you including <Blynk.h>, there’s no need to add it at all.

@calandry86 I think you’ve been slightly confused by the post from @John93

I think what he was trying to say is that these two lines of code…

Need to the the first two lines of code in your sketch.
You don’t need to add-in any additional lines of code, simply move those two lines to the top of your sketch
The issue is explained in more detail in this topic…

At the moment, you still have this line of code at the beginning of your sketch, which (I assume) is causing the issue…

I’m also skeptical about whether this line is needed at all, but if it is going to appear in your sketch then it needs to be moved down so that it is after the Template ID and Device Name lines.

One thing that you’ve not provided is any information about what appears in your serial monitor when you boot your device and it attempts to connect to Blynk.
Your serial monitor is the most powerful debugging tool available to you (and therefore to us as well) and without the information that this provides we are all just guessing about what is actually happening when the device tries snd fsils to connect to Blynk.

Pete.

1 Like

ok great, I’ll try those changes and see if I get better success, if not I’ll send you the serial monitor data. I’ll delete those extra lines too, like the firmware version etc…

1 Like

Also, your void loop should look like this :

void loop()
{
  Blynk.run();
  timer.run();
}

Code finally works, thanks guys!

#define BLYNK_TEMPLATE_ID "TMPLbrNP_Ylb"
#define BLYNK_DEVICE_NAME "keystudio"

#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "omCOqO8JjxopGXhi8F20DCHFz8csI84d"

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

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "christian's house";
char pass[] = "christian";

BlynkTimer timer;

const int someButton =  2;
#define sensorA A0 //Sensor is connected to A4

void setup() {
  Blynk.begin(auth, ssid, pass);

  // BLYNK RECONNECT START
  timer.setInterval(30*1000, reconnectBlynk); //run every 30s
  // BLYNK RECONNECT END

  pinMode(someButton, INPUT);
}

void loop() {
  
  Blynk.run();

  // BLYNK RECONNECT START//
  timer.run(); 
  if(Blynk.connected()) 
  { 
    Blynk.run(); 
  }
  // BLYNK RECONNECT END//

  if(digitalRead(someButton) == true)
  {
    someFunction();
  }
}

void someFunction()
{
  Blynk.virtualWrite(V3, analogRead(sensorA));
}

// BLYNK RECONNECT START
void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
}
// BLYNK RECONNECT END
1 Like

You still haven’t taken onboard the comments about your void loop and the issues that could lead to you flooding the Blynk server sign virtualWrite commands.

Why don’t you simply use a timer to read your analog pin and send the results to Blynk?

Pete.

It just wasn’t the issue I was interested in, the code isn’t even what I’ll be using. Most of the time I use stepper motors, and the timers don’t work well on them, so I just end up disconnecting using delays, then I can use the processor only on pulsing (at 300 microseconds, the CPU really can’t do any other function), then reconnect when the motors stop.

#define BLYNK_TEMPLATE_ID "TMPLbrNP_Ylb"
#define BLYNK_DEVICE_NAME "keystudio"

#define BLYNK_PRINT Serial
#define BLYNK_AUTH_TOKEN "omCOqO8JjxopGXhi8F20DCHFz8csI84d"

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

char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "christian's house";
char pass[] = "christian";

BlynkTimer timer;
BlynkTimer sendSignal;

#define sensorA A0 //Sensor is connected to A4

void setup() {
  Blynk.begin(auth, ssid, pass);

  // BLYNK RECONNECT START
  timer.setInterval(30*1000, reconnectBlynk); //run every 30s
  // BLYNK RECONNECT END

  sendSignal.setInterval(1000, someFunction);
}

void loop() {
  
  Blynk.run();
  sendSignal.run();
}

void someFunction()
{
  Blynk.virtualWrite(V3, analogRead(sensorA));
}

// BLYNK RECONNECT START
void reconnectBlynk() {
  if (!Blynk.connected()) {
    Serial.println("Lost connection");
    if(Blynk.connect()) {
      Serial.println("Reconnected");
    }
    else {
      Serial.println("Not reconnected");
    }
  }
}
// BLYNK RECONNECT END

I’m not surprised, when you use two timer objects, when a single timer object can support up to 16 timers - and then you only service one of those objects in your void loop

If you want to learn how to use timers correctly then you should read this…

If you’re posting code that you want assistance with and

then I think you’re taking the wrong approach to seeking assistance.

Pete.

This forum topic was on connectivity issues from converting from Blynk 1.0 and Blynk 2.0. When people who have similar issues, and looking for answers on forums, they are looking for answers on the topic in question. You’re making them scroll through too many topics to find their answers. That being said, this topic is resolved. The order of where to place the 2 extra #define statements for Blynk 2.0 version mattered for anyone who had similar issues.

If that’s the real name for your SSID, I’d suggest to eliminate spaces and apostrophes (replacing them with “_”) to prevent possible problems with some WiFi devices.