Converting legacy code to Blynk 2.0

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.

You don’t have a timer object declared, and don’t have a timer.run in your void loop.
I think there’s a bug in the 1.0.0 Edgent examples that require you to use a different object name rather than timer - something like myTimer will work.

I don’t understand that statement.

Do you have access to a different WiFi network that you could use? Your ISP may be doing some weird stuff here.

Pete.

Pete, I don’t think it is my code.
I have just now started with a new template, copied the template ID and device name into a clean version of blynk.Edgent, and compiled the program.
Again I get the result where the device tries but fails to connect to the Blynk server.

I have no other way to connect to the internet other than my mobile intenet connection as I am living and travelling on a boat. Of course, I will try again when I move to a different town. But surely such connections are not as sensitive as that?

And if I can get my currently loaded and working devices to connect to Blynk over my mobile connection, surely that eliminates the mobile network.

The only unknown, in my opinion, now seems to be the connection of a new device to the Blynk server. Has anything changed with the Blynk server over the last couple of days which might be causing this?

N.

So, are you provisioning the new device via the app before attempting to connect to the Blynk server?

Pete.

No.
When I load the code into the device, it attempts to connect to the blynk server, and then should pause so that you can activate the dynamic wifi provisioning from the app. In theory.
I cannot get that far. The device does not connect to the blynk server. Therefore it does not appear as a live device on the web dashboard, nor does it appear on the app. It must connect to the blynk server before the next stage can happen.

N.

I think you need to re-read through the provisioning documentation. I’d also recommend flashing your device with the “erase all” option selected, so that all stored WiFi credentials are erased.

Pete.

So the fact that I was successfully flashing last week means that I don’t know how to flash this week.

I have used the ‘erase all’ option and it produced slightly different results, but still doesn’t connect:

[511] Connecting to Puddlenet 2
[10238] Connected to WiFi
[10238] IP: 192.168.64.120
[10440] Current time: Fri Jul  9 08:04:09 2021
[10440] Connecting to blynk.cloud:443
[11626] Ready (ping: 5ms).
[11762] INIT => ERROR
[21765] Restarting after error.
0⸮a⸮⸮,⸮as1s?⸮⸮⸮⸮B+⸮⸮⸮⸮⸮⸮⸮[385] Connecting to Puddlenet 2
[10111] Connected to WiFi
[10111] IP: 192.168.64.120

Yes, I will go back to basics and reread everything.
N.

Your device shouldn’t know the credentials for your WiFi until AFTER you’ve been through the provisioning process. The fact that it is able to connect means that the device is storing your WiFi credentials (or you’ve hard-coded them into your sketch).

Pete.

Yes, the last attempt was with static provisioning. I have been trying every option!

However, I have moved the boat this morning, and come into a large town.
And guess what, everything has worked correctly. I can know see what I expect to see:

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

[556] --------------------------
[574] Product:  Boat Alarm
[589] Hardware: 1.0.0
[601] Firmware: 0.1.0 (build Jul  9 2021 11:13:58)
[628] Device:   ESP8266 @ 80MHz
[645] MAC:      E0:98:06:86:31:43
[663] Flash:    4096K
[675] ESP core: 3.0.1
[687] ESP SDK:  2.2.2-dev(38a443e)
[706] Boot Ver: 31
[716] Boot Mode:1
[726] FW info:  471216/1622016, MD5:4b88b5f0f0688f6a2460fb611807a57f
[916] Free mem: 32064
[917] --------------------------
[917] INIT => WAIT_CONFIG
[1505] AP SSID: Blynk Boat Alarm-5AB92
[1505] AP IP:   192.168.4.1
[1506] AP URL:  blynk.setup

And now I now finalise the process using dynamic provisioning.
I have tried this on three different devices and they are all connecting both statically and dynamically.

So I guess it seems that something with the mobile network provider must have been causing the hiccup.
If this is the case, I find it slightly worrying. I am constantly travelling (and raoming overseas) and need a reliable connection for my boat monitoring.

Anyway, thank you for your patience and persistence. As usual, you have been very helpful.
N.