Blynk.begin seems to change my long values. without Blynk.begin it work ok

I made a sketch to run on a Photon that reads my smart electricity meter through the P1 port. The sketch works fine, when I connect my computer to serial with USB I get correct readings. (i can check it on the LCD that is on the meter). However, when trying to get the readings on my phone with Blynk I get problems.

I uncommented all parts of Blynk (define, library, blank.start and blynk.begin) one at the time, and at the end, when I uncommented Blynk.begin the output of 4 out of 6 variables (type long) is false. some are 0, some are a wrong number. Only my meter settings High and Low tarif are correct, others are not. When I comment Blynk.begin with // it works back as normal.

Can anyone point me in the right direction to what is causing this? Maybe Blynk.begin needs a little time, or it doesn’t work well with the decomposing of the serial telegram, I don’t know. Printing to terminal also led to only part of the telegram, and then again only mEVHT and mEVLT were readable. rest was rubbish. Apparently there is some sort of max or buffer or something that I exceed when using Blynk.

Here is my code:


#include "blynk/blynk.h"
   
char input; // incoming serial data (byte)
//bool readnextLine = false;
#define BUFSIZE 75
char buffer[BUFSIZE]; //Buffer for serial data to find \n .
int bufpos = 0;
long mEVLT = 0; //Meter reading Electrics - consumption low tariff
long mEVHT = 0; //Meter reading Electrics - consumption high tariff
long mEPVHT = 0; // teruggeleverd hoog
long mEPVLT = 0; //teruggeleverd laag
long mEPV = 0; // teruglevering
long mEAV = 0;  //Meter reading Electrics - Actual generated electricity (Solar panels)
long mG = 0;   //Meter reading Gas
int tarief = 3; //Meter actual rate

long BlynkmEVHT;
long BlynkmEVLT;
long BlynkmEAV;
long BlynkmEPV;
long BlynkmEPVHT;
long BlynkmEPVLT;

#define BLYNK_PRINT Serial


char c;
char auth[] = "813f51a2d6b4492daf2f65217249c185"; 
long teller = 0;

void setup() {
 Blynk.begin(auth);
  Serial.begin(9600);
  delay(1000);
  Serial1.begin(9600);
  delay(2000); 
}

BLYNK_READ(V1) 
{
  Blynk.virtualWrite(V1,BlynkmEVHT);
}

BLYNK_READ(V2) 
{
  Blynk.virtualWrite(V2,BlynkmEVLT);
}

BLYNK_READ(V3) 
{
  Blynk.virtualWrite(V3,BlynkmEPVHT);
}

BLYNK_READ(V4) 
{
   Blynk.virtualWrite(V4,BlynkmEPVLT);
}
BLYNK_READ(V5) 
{
  Blynk.virtualWrite(V5,BlynkmEAV);
}
BLYNK_READ(V6) 
{
  Blynk.virtualWrite(V6,BlynkmEPV);
}
void loop() {
  Blynk.run();
  
  //when this is uncommented, no good values will be put in mEAV, mEPV, mEPVLT and mEPVHT. 
  //serial print values are also wrong. something strange happens, mEPVHT for example gives really different values. others give 0.
  //just mEVHT and mEVLT will give the right readings and show up correctly in Blynk app.
  //when I delete Blynk.run() the sketch runs ok and gives the right values on the serial through USB.

  long tl = 0;
  long tld = 0;
  

    if (Serial1.available()) {
    //delay(10);
    input = Serial1.read();
   
   
    char inChar = (char)input;
    // Fill buffer up to and including a new line (\n)
    
    buffer[bufpos] = input&127;
    bufpos++;
 

    if (input == '\n') { // We received a new line (data up to \n)


      // 1-0:1.8.2 = Electricity consumption high tariff (DSMR v4.0)
      if (sscanf(buffer,"1-0:1.8.2(%ld.%ld*s" , &tl, &tld) ==2 ) {//% eraf, 2x en ==2 ipv >0
        tl *= 1000;
        tl += tld;
        mEVHT = tl;    
      
        if (mEVHT > 0) {

          //mEVHT = 0;
        }
      }
      
 
      
      

        if (sscanf(buffer,"0-0:96.14.0(%ld.%ld*s" , &tl) >0  )   //)
        {
          tarief = tl;
        }


      if (sscanf(buffer,"1-0:1.8.1(%ld.%ld" ,&tl, &tld)==2){
        tl *= 1000;
        tl += tld;
        mEVLT = tl;

        if (mEVLT > 0) {


        }
      }
      
      
 
/////// terug geleverd totaal
// 1-0:2.8.2 = Electricity levering high tariff (DSMR v4.0)
      if (sscanf(buffer,"1-0:2.8.2(%ld.%ld*s" , &tl, &tld)==2 ) {
       tl *= 1000;
        tl += tld;
        mEPVHT = tl;    
   
        if (mEPVHT > 0) {

          //mEVHT = 0;
        }
      }

      // 1-0:2.8.1 = Electricity levering low tariff (DSMR v4.0)
      if (sscanf(buffer,"1-0:2.8.1(%ld.%ld*s" , &tl, &tld)==2 ) {
       tl *= 1000;
        tl += tld;
        mEPVLT = tl;    
       
        if (mEPVLT > 0) {
        }
      }

      ///////// einde teruggeleverd totaal


      // 1-0:1.7.0 = Electricity consumption actual usage (DSMR v4.0)
     if (sscanf(buffer,"1-0:1.7.0(%ld.%ld%*s" , &tl , &tld) >0 ) {
        mEAV = tl * 1000 + tld * 10;
     
        if (mEAV > 0) {

        }
       
      }

////////////
  // 1-0:2.7.0 = Electricity teruglevering actueel (DSMR v4.0)
      if (sscanf(buffer,"1-0:2.7.0(%ld.%ld%*s" ,&tl , &tld) >0 )
      { 
        
       
        mEPV =  tl*1000 + tld*10;
       
        if (mEPV > 0) {

        }
      }

///////////
      // 0-1:24.2.1 = Gas (DSMR v4.0) on Kaifa MA105 meter
      if (strncmp(buffer, "0-1:24.2.1", strlen("0-1:24.2.1")) == 0) {
        if (sscanf(strrchr(buffer, '(') + 1, "%d.%d", &tl, &tld) == 2) {
          mG = (tl*1000)+tld; 

        }
      }

      // Empty buffer again (whole array)
      for (int i=0; i<75; i++)
      { 
        buffer[i] = 0;
      }
      bufpos = 0;

    }
     
    if (input == '!') {   //! is end of telegram, data will be sent/printed to serial
     

      //park values before resetting them
 BlynkmEVHT = mEVHT; 
 BlynkmEVLT = mEVLT;
 BlynkmEAV = mEAV;
 BlynkmEPV = mEPV;
 BlynkmEPVLT = mEPVLT;
 BlynkmEPVHT = mEPVHT;

 printData();
 
      mEVHT=0;
      mEVLT = 0;
      mEPVHT=0;
      mEPVLT = 0;
      mEAV = 0;
      mEPV = 0;
      mG = 0;
      tarief=3;
     
    } //Einde ! detection   

  } //end if serial1.available
}



void printData(){
 
  Serial.print("actueel verbruik: ");Serial.print(BlynkmEAV);Serial.println("W");
  Serial.print("PV teruglevering: ");Serial.print(BlynkmEPV);Serial.println("W");
  Serial.println();
  
  Serial.print("verbruik laag: ");Serial.print(BlynkmEVLT);Serial.println("Wh");
  Serial.print("verbruik hoog: ");Serial.print(BlynkmEVHT);Serial.println("Wh");
  Serial.print("PV laag: ");Serial.print(BlynkmEPVLT);Serial.println("Wh");
  Serial.print("PV hoog: ");Serial.print(BlynkmEPVHT);Serial.println("Wh");
  
  Serial.println("------------------------------------------");
  
}

Thanks Pavel for telling me how to format the code. Didn’t know that.

Any idea on how to fix my problem: more than welcome.
thx

Code snippets should be formatted. Please edit your initial post:

How to do that:


 ``` cpp <--put 3 backticks BEFORE your code starts  ("cpp" means C++ language) 

   //Put your code here
   //..................
   //..................

 ``` <--insert 3 backticks AFTER your code

**This makes your code readable and with highlighted syntax, like this

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

//comment goes here 
void helloWorld() { 
   String message =  "hello" + "world"; 
}
2 Likes