BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Converting legacy code to Blynk 2.0

Sorry. Done.
N.

What hardware are you using?

Pete.

Wemos D1 mini.

This code wont work on a D1 Mini, as GPIOs 6 & 7 are reserved, and GPIOs 17-20 don’t exist.

Pete.

My original code from the legacy vesion (which DOES work) is this:

void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, "blynk-cloud.com", 8080);
  pinMode(D3, OUTPUT);  //alarm bell relay
  pinMode(D4, OUTPUT);  //switch 1
  pinMode(D2, OUTPUT);  //switch 2
  pinMode(D1, OUTPUT);  //switch 3
//  pinMode(D6,INPUT_PULLUP);  // Door sensor
//  pinMode(D7,INPUT_PULLUP);  // PIR sensor
  pinMode(D5,INPUT_PULLUP);  // Deck sensor
  
  timer.setInterval(1000L, myTimerPIR);
  timer.setInterval(1000L, myTimerDoor);
  timer.setInterval(1000L, myTimerDeck);
  timer.setInterval(1000L, myTimerBell);
}

For some reason, the legacy code also ran happily with the "D " designations. It appears perhaps that Blynk 2.0 is not happy with them.
The two lines that failed on Blynk 2 are currently marked with ‘//’.

I think I have used the wrong chart for my conversions. Eg D3 in my chart says 18, but I understand should be 0.

I have 4 outputs and 3 inputs on this project? As this project runs in legacy, surely there are enough IO ports in Blynk 2.0?? Many many user-friendly ports are there actually available on this chip?

N.

It’s not the Blynk library that decides that’ it’s the board type you select in the Arduino IDE.
I guess you selected “Generic ESP8266” instead of “LOLIN(WEMOS) D1 RS & Mini”

That’s correct…

Blynk doesn’t have any I/O ports, its the board that has the ports.

The D1 Mini has sufficient suitable GPIO pins for this, but your original choice of pins probably wasn’t as well informed as it could have been, you should read this…

Also, your decision to have 54 timers all being called simultaneously every 1000ms isn’t a great idea…

You should read the “Staggering Timers” section of this post for more info…

Pete.

Sorry, I meant to say IO ports in the D1 mini, but my fingers decided to type something else.

I was just reading the very article about the IO ports of the device when you replied. So I will replan these port allocations.
As regards the timers - the legacy project was from when I was just learning about programming. I was planning to change that part of the code after It got it running on Blynk 2.

So it looks like I need a bit of a re-write on this project. Luckily, most of my others seem to be converting nicely. Well, as nicely as Blynk 2 will allow. Why, when the rest of the world is adding ‘dark mode’, does Blynk decide to take it away? I [used to] run all my projects in dark mode.

Thank you for your time and assistance.

You should also be aware that GPIO2 is the pin that the onboard LED of the D1 Mini is attached to.
It is used in the settings.ini tab of the ESP8266 Edgent example to give feedback during the provisioning process, so you should avoid using it for anything else.

I think aesthetics gets a lower priority than functionality at the moment.

Pete.

Pete, I have resolved the issue with GPIO ports.

I have loaded the code into a NodeMCU ESP12 module. This connects up and runs fine, producing the following info:
[4829] Using Dynamic IP: 192.168.43.105
[4829] CONNECTING_NET => CONNECTING_CLOUD
[5041] Current time: Thu Jul 8 13:32:25 2021
[5041] Connecting to blynk.cloud:443
[6234] Ready (ping: 12ms).
Alarm Start-up: 1
[8529] CONNECTING_CLOUD => RUNNING

However, when I load the code into a second NodeMCu device or a Wemos D1 mini, these devices does not connect to Blynk. I get the following info:
[820] Connecting to WiFi: Puddlenet 2
[4677] Using Dynamic IP: 192.168.43.39
[4677] CONNECTING_NET => CONNECTING_CLOUD
[4889] Current time: Thu Jul 8 13:34:17 2021
[4889] Connecting to blynk.cloud:443
[6167] Redirecting to lon1.blynk.cloud:443
[6175] Current time: Thu Jul 8 13:34:18 2021
[6175] Connecting to lon1.blynk.cloud:443
[7347] Redirecting to lon1.blynk.cloud:443

Any advice you can offer on why this is so? What is upsetting some devices but not others when running the same code?

Where can I find any pages which provide specific information about points to be aware of when programming code for Blynk2 as opposed to legacy Blynk? This sort of information appears to be sparce. I have several D1 mini boards running legacy code, but need to be aware of parts of the code which might not work with Blynk 2.

Thanks again,
N.

Are you using the same template ID for these devices?

On the Blynk documentation pages, and here on the forum.

Pete.

Yes, Pete, I am using the same template ID. I am loading the same .ino file into each device. Now, I cannot get any newly-loaded devices to connect. The only ones I now have working are ones I loaded yesterday. And yes, from the same .ino file with the same template ID.

I have even tried putting a static Authtoken into the code, but that failed, too. This is all so frustrating.

And the documentation is so poor for folk who do not yet know the new system. Just as an example, if I refer to any help documentation for widgets on the blynk2 mobile app, it still refers to ‘energy’ from the legacy version. This is Blynk2’s own documentation!

Why not try cloning the template and using the new template ID in your sketch for the second device?

Pete.

Just Tried your suggestion. NO change.

[770] INIT => CONNECTING_NET
[781] Connecting to WiFi: Puddlenet 2
[4621] Using Dynamic IP: 192.168.43.122
[4621] CONNECTING_NET => CONNECTING_CLOUD
[4832] Current time: Thu Jul 8 20:16:39 2021
[4832] Connecting to blynk.cloud:443
[6027] Redirecting to lon1.blynk.cloud:443

What exactly do these last two lines mean? These two lines keep repeating until I get a time-out.
N.

@nikki-m i assume that you’re in the UK?
The lon1 server is located in London.

What version of the library are you using now, and are you using dynamic provisioning?

Pete.

No, I am currently in the Netherlands. But my internet service comes through my (UK-based) EE mobile contract (connecting to whichever local service is offered to me as I travel through the country).

I am currently using Blynk v1.0.0. I have previously also used the beta 3 version, with the same results.

I am currently using dynamic provisioning, but have tried (also unsuccessfully) using static provisioning.

The version of code I have successfully loaded onto a NodeMCU is able to be successfully reconfigured for wifi. I have tried this several times and it has reconfigured each time. Surely this suggests that the network internet connection is ok.

N.

And what does your latest sketch look like?

Pete.

This sounds very similar to an issue I have trying to migrate a previously functioning sketch to “Blynk2.0”.

If I add the device template and device name defines and a static auth token.

The device will connect to wifi, and then fail to connect to Blynk in the exact same manner as yours.

I assumed my issue had to do with the fact that I’m using an ESP32-CAM and I have been unable to successfully compile a program when using the Espressif driver combined with BlynkEdgent and was also unable to have a legacy program connect to the new Blynk servers when statically provisioned.

Seems like the issue might be different than I thought.

Peter, I have successfully migrated two sketches from legacy to Blynk 2. One runs on a Wemos D1 mini, the other is running on a NoceMCU.

If I copy either sketch onto other devices, they fail today. They were copying successfully yesterday.

Would you like me to provide a full sketch for you to look at? Do I blank out the auth code before posting?

N.

So you aren’t using dynamic provisioning?

It would be useful if you posted a full update on EXACTLY what code you are running and what what process you are following in trying to get them connected.

Pete.

Yes, I am using dynamic provisioning. I meant to say Template ID, not auth code. I think I am getting dementia - possibly caused by brain overload from spending hours on this Blynkin’ issue.

Here is the full version of one of my codes:

// Last updated 5/7/21
/* BLYNK - V1=Volts, V2=Amps, V3=CE, V4=SOC, V5=Sync led, V6=Charging LED, V7=Blynk update time
 * BLYNK - V8=Charging time to go, V12=Gen battery V, V13=Gen batt led
 * Note - BLYNK_WRITE(V7)[see below] changes the Blynk timer period 
 * BMV Tx to Wemos pin Rx - MUST be disconnected to load program
 * MBV Gnd to Wemos pin Gnd
 * Wemos D6(12) to hardware led via resistor (optional) to monitor serial data reading
 * Wemos D7(13) to hardware led via resistor (optional) to monitor serial data reading
 */
 
#define BLYNK_TEMPLATE_ID "TMPLTM0lNg-b"
#define BLYNK_DEVICE_NAME "Battery Monitor"
#define BLYNK_FIRMWARE_VERSION "0.1.0"
#define BLYNK_PRINT Serial
//#define BLYNK_DEBUG
#define APP_DEBUG
#define USE_NODE_MCU_BOARD
#include "BlynkEdgent.h"

unsigned long lastConnectionTime = 0;             
const unsigned long connectionInterval = 2000;  
float BMV_V=0.0, BMV_I=0.0, BMV_SOC=0.0, BMV_CE=0.0;
String label = "", val = "";
int ChecksumBMV;
char buf[45];
bool isFirstConnect = true;
unsigned long blynkPeriod;
int timerID;
int chargeTime;
float inputVal  = 0;
float volts = 0;
float chargingTime;
char hBuffer[4];
char mBuffer[4];
int wholeHours;
int wholeMinutes;
int syncMode = 3;
int lastSyncMode = 3;
int fastInterval = 5;
int medInterval = 900;
int slowInterval = 3600;
WidgetLED ledSync(V5); 
WidgetLED ledCharging(V6); 
WidgetLED ledTrickle(V11);
WidgetLED ledGenBatt(V13); 


void setup() {
Serial.begin(19200);
BlynkEdgent.begin();
timerID = timer.setInterval(60000L, sendBlynk);
}

void loop() {
BlynkEdgent.run();
}



BLYNK_CONNECTED() {
  if (isFirstConnect) {
     Blynk.virtualWrite(V1,0); 
     Blynk.virtualWrite(V2,0);
     Blynk.virtualWrite(V3,0);
     Blynk.virtualWrite(V4,0);
     Blynk.virtualWrite(V7,3);
     isFirstConnect = false;
  }
}

void readBMV() {
   if (millis() - lastConnectionTime > connectionInterval) {

 while (Serial.read() >= 0);    
  label = "";
  val = "";
  
while (label != "Checksum")  {
     label = Serial.readStringUntil('\t');          
     val = Serial.readStringUntil('\r\r\n');

     if (label == "Checksum") {
      if (val != ""){
        ChecksumBMV = val.toInt();
        val = "";
        }
     }

     if (label == "V") {         
      if (val != ""){
        val.toCharArray(buf, sizeof(buf));
        BMV_V = atof(buf);
        label = "";
        val = "";
        }
     }

     if (label == "I")  {          
      if (val != "") {
        val.toCharArray(buf, sizeof(buf));
        BMV_I = atof(buf);
        label = "";
        val = "";
        }
      }  

     if (label == "CE") {      
      if (val != "")  {
        val.toCharArray(buf, sizeof(buf));
        BMV_CE = atof(buf);
        label = "";
        val = "";
        }
     }        

     if (label == "SOC") {       
      if (val != "")  {
        val.toCharArray(buf, sizeof(buf));
        BMV_SOC = atof(buf);
        label = "";
        val = "";
        }
     }
   }
   
lastConnectionTime = millis();
   }
  }

void sendBlynk() {
ledSync.on();
delay(750);
ledSync.off();

Blynk.virtualWrite(V1, BMV_V/1000.00);
Blynk.virtualWrite(V2, BMV_I/1000.00);
Blynk.virtualWrite(V3, BMV_CE/1000.00);
Blynk.virtualWrite(V4, BMV_SOC/10.0);
Serial.println(BMV_V/1000.00);
Serial.println(BMV_I/1000.00);
Serial.println(BMV_CE/1000.00);
Serial.println(BMV_SOC/1000.00);

if (BMV_I>500 && BMV_SOC<1000) {
  ledCharging.on();
   
   chargingTime =(-BMV_CE/1000.00) / (BMV_I/1000.00);
   wholeHours = round(chargingTime*10)/10.0;
   wholeMinutes = round((chargingTime - wholeHours)*600)/10.0;
   
   dtostrf(wholeHours,3,0,hBuffer);
   dtostrf(wholeMinutes,3,0,mBuffer);
   Blynk.virtualWrite(V8,hBuffer, " hrs  ",mBuffer," mins");
}
else {
     ledCharging.off();
     Blynk.virtualWrite(V8, "--");
     Blynk.virtualWrite(V10, " ");
     }

if (BMV_I>500 && BMV_SOC==1000) {
  ledTrickle.on();
  }
else {
  ledTrickle.off();
  }

  inputVal = analogRead (A0);
  volts = inputVal/1023*15.68;
  Serial.print("Battery voltage =  ");
  Serial.println (volts);

  Blynk.virtualWrite(V12,volts);
  ledGenBatt.on();

 if (volts < 12.6) {
  Blynk.setProperty(V13,"color","#D3435C");  // red D3435C
 }
 if (volts >= 12.6 && volts <= 13.6) {
  Blynk.setProperty(V13,"color","#23C48E");  // green  23C48E
 }
 if (volts > 13.6) {
  Blynk.setProperty(V13,"color","#ED9D00");  // yellow  ED9D00
 }
   
}

BLYNK_WRITE(V7)  {
  syncMode = param.asInt();
    if(syncMode == 1) {
       timer.deleteTimer(timerID);
       timerID = timer.setInterval(fastInterval * 1000L, sendBlynk);
       lastSyncMode = syncMode;
    }

    if(syncMode == 2) {
       timer.deleteTimer(timerID);
       timerID = timer.setInterval(medInterval * 1000L, sendBlynk);
       lastSyncMode = syncMode;
    }

    if(syncMode == 3) {
       timer.deleteTimer(timerID);
       timerID = timer.setInterval(slowInterval * 1000L, sendBlynk);
       lastSyncMode = syncMode;
    }

    if(syncMode == 4) {
       sendBlynk();
       syncMode = lastSyncMode;
       Blynk.virtualWrite(V7,lastSyncMode);
    }
}  

This actual code loaded successfully onto a Wemos D1mini last week. Today I have tried loading this (and several other programs) onto both Wemos devices and NodeMCU devices. This code loads onto the device and then my com monitor shows:

[283] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v1.0.0 on ESP8266

[294] --------------------------
[312] Product:  Battery Monitor
[329] Hardware: 1.0.0
[341] Firmware: 0.1.0 (build Jul  8 2021 23:53:23)
[368] Token:    ...KmvK
[381] Device:   ESP8266 @ 80MHz
[398] MAC:      E0:98:06:86:31:43
[416] Flash:    4096K
[428] ESP core: 3.0.1
[440] ESP SDK:  2.2.2-dev(38a443e)
[459] Boot Ver: 6
[469] Boot Mode:1
[479] FW info:  469904/1626112, MD5:ca6449766e36936b8d164315f5c406d0
[669] Free mem: 32544
[669] --------------------------
[669] INIT => CONNECTING_NET
[681] Connecting to WiFi: Puddlenet 2
[10079] Using Dynamic IP: 192.168.64.120
[10079] CONNECTING_NET => CONNECTING_CLOUD
[10190] Current time: Thu Jul  8 21:54:42 2021
[10191] Connecting to blynk.cloud:443
[11427] Redirecting to lon1.blynk.cloud:443
[11439] Current time: Thu Jul  8 21:54:43 2021
[11439] Connecting to lon1.blynk.cloud:443
[12691] Redirecting to lon1.blynk.cloud:443
[12705] Current time: Thu Jul  8 21:54:45 2021
[12705] Connecting to lon1.blynk.cloud:443
[13879] Redirecting to lon1.blynk.cloud:443
[13891] Current time: Thu Jul  8 21:54:46 2021
[13891] Connecting to lon1.blynk.cloud:443
[15080] Redirecting to lon1.blynk.cloud:443
[15092] Current time: Thu Jul  8 21:54:47 2021
[15092] Connecting to lon1.blynk.cloud:443
[16399] Redirecting to lon1.blynk.cloud:443
[16411] Current time: Thu Jul  8 21:54:48 2021
[16411] Connecting to lon1.blynk.cloud:443

Because it is not connecting to a Blynk server it does not appear online and I can proceed no further with configuring the program on the Blynk IOT app on my mobile (Samsung A52 funning android 11).

Again I will re-iterate that this same code hasloaded onto another device and it runs fine, and that device appears online and can be reconfigured for wifi. When any of my working devices connect to Blynk, the following appears on the com monitor [note that the following com printout is from a different program, but the message is the same]:

[400] --------------------------
[418] Product:  B[427] Hold the button to reset configuration...
oat Alarm
[458] Hardware: 1.0.0
[470] Firmware: 0.1.0 (build Jul  8 2021 13:38:16)
[499] Token:    ...JDMJ
[510] Device:   ESP8266 @ 80MHz
[527] MAC:      E0:98:06:85:B3:22
[546] Flash:    4096K
[558] ESP core: 3.0.1
[570] ESP SDK:  2.2.2-dev(38a443e)
[588] Boot Ver: 6
[598] Boot Mode:1
[608] FW info:  470736/1626112, MD5:158e95fa73722a914da2a6f4edcce996
[823] Free mem: 32176
[823] --------------------------
[823] INIT => CONNECTING_NET
[835] Connecting to WiFi: Puddlenet 2
[10149] Using Dynamic IP: 192.168.64.216
[10149] CONNECTING_NET => CONNECTING_CLOUD
[10285] Current time: Thu Jul  8 22:03:29 2021
[10286] Connecting to blynk.cloud:443
[11529] Ready (ping: 18ms).
Alarm Start-up:  1
[13980] CONNECTING_CLOUD => RUNNING

N.