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

Blynk + Photon Serial connection with other MCU

nonblynkrelated
android

#1

I’m sorry I came in from the back door:confounded:

It’s been a year I am not working with Blynk nor Photon !
I’m working in a project that read 5 analog sensors trough a PSoC 5LP from Cypress.
Then PSoC 5LP send 5 character buffers of 6 element each through the serial port towards the photon serial port 1.
I am using Blink as GUI so that 5 labeled values display each of those analog reading.
The Blynk app get disconnected periodically and reconnected.
Labeled values just display only the the first two values the others three are shown with no format ( Format is ###,##).
I suspect that something is happening with the time stamp I am using.
I would appreciate very much any help.

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_printserial

char auth[] = "745cf23fe70543bcbfcd68673d82e3ff" ;
char Lbs_1[6]; // 1th sensor array
char Lbs_2[6]; // 2th sensor array
char Lbs_3[6]; // 3th sensor array
char Lbs_4[6]; // 4th sensor array
char Lbs_5[6]; // 5th sensor array

BlynkTimer timer;
int Email_1=0;
const unsigned long SendEmail=17000L;
unsigned long lastEmail; // Global initialize to 0

const unsigned long SendLbs_1=1000L;
unsigned long lastLbs_1; // Global initialize to 0

const unsigned long SendLbs_2=1000L;
unsigned long lastLbs_2; // Global initialize to 0

const unsigned long SendLbs_3=1000L;
unsigned long lastLbs_3; // Global initialize to 0

const unsigned long SendLbs_4=1000L;
unsigned long lastLbs_4; // Global initialize to 0

const unsigned long SendLbs_5=1000L;
unsigned long lastLbs_5; // Global initialize to 0


void sendPressure_1()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_1[i]=Serial1.read(); 
           if (Lbs_1[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_1[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V7,Lbs_1);
      
}



void sendPressure_2()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_2[i]=Serial1.read(); 
           if (Lbs_2[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_2[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V8,Lbs_2);
      
}


void sendPressure_3()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_3[i]=Serial1.read(); 
           if (Lbs_3[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_3[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V9,Lbs_3);
      
}


void sendPressure_4()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_4[i]=Serial1.read(); 
           if (Lbs_4[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_4[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V10,Lbs_4);
      
}


void sendPressure_5()

{
    int i=0;
    while( Serial1.available() && i<7)
        {
           Lbs_5[i]=Serial1.read(); 
           if (Lbs_5[i]=='\0;'     )
           {
               
               break;
           }
           i++;
            
        }

      i=0;
      Lbs_5[6]='\0';
      
      
      /* Send it over the Blynk server                              */
     Blynk.virtualWrite (V11,Lbs_5);
      
}


void SEmail()
{
    
   while( Serial1.available()>0) 
    
    {
      Email_1=Serial1.read();  
      if (Email_1==0xAA)   
      {
       Blynk.email("Subjet","Lbs_1>250");
       Email_1 =0; break;
      }   
    } 
          
        
        
    
    
    
    
    
}



void setup() {

// Debug Console
Serial1.begin(57600);
delay(5000);// Allow board to settle
Blynk.begin (auth);
}

void loop() {

unsigned long topLoop_1 = millis();
//unsigned long topLoop_2 = millis();
//unsigned long topLoop_3 = millis();
//unsigned long topLoop_4 = millis();
//unsigned long topLoop_5 = millis();

Blynk.run();

if ((topLoop_1-lastLbs_1 )>=SendLbs_1)

{
    lastLbs_1=topLoop_1;
    sendPressure_1();
}


if ((topLoop_1-lastLbs_2 )>=SendLbs_2)

{
    lastLbs_2=topLoop_1;
    sendPressure_2();
}

if ((topLoop_1-lastLbs_3 )>=SendLbs_3)

{
    lastLbs_3=topLoop_1;
    sendPressure_3();
}

if ((topLoop_1-lastLbs_4 )>=SendLbs_4)

{
    lastLbs_4=topLoop_1;
    sendPressure_4();
}

if ((topLoop_1-lastLbs_5 )>=SendLbs_5)

{
    lastLbs_5=topLoop_1;
    sendPressure_5();
}
}

#3

Welcome back. I fixed your post for you. it is three backticks, fore and aft, not commas :wink:

Blynk - FTFC

Your void loop() is full of code… all trying to run thousands of times a second. You should be using timed functions for your if() functions instead. Since you use a Photon, I suspect you need whatever Particle timer is required instead of BlynkTimer…


#4

Hi Gunner

Thank for your answer and fixing my post.

I believed that " if ((topLoop_1-lastLbs_1 )>=SeendLbs_1 run only when the if is true !!! .:confounded:


#5

The code contained in the curly brackets of the if statement will only run when the if statement evaluates as true, but the if statement has to be tested each time the void loop runs.
You have 5 of these if statements in your void loop, plus you’re defining an unsigned long variable each cycle of the loop - not to mention the other 4 that are currently commented out.

All of this can be done much better using timers that are declared in your void setup.

Pete.


#6

Hi Gunner and Peter

I changed completely my approach for this code.
From external MCU through serial port I’m sending an array of 30 character which contain the reading of 5 sensors towards the photon serial input.
Each read looks like ###,##
Once I received that 30 char array I store it in Lbs[30]
From there using pointers I generate five Lbsx[6] arrays with x=1,2,3,4,5,
Then using Blynk.virtualWrite ( Vx,Lbsx); I am sending those Lsbx[6] to the Blynk server.
I am using 5 Labeled values set to PUSH to display those readings.
It does not work, just I get reading on Virtual pin V1, V3, and V7 and it is only garbage
I am not realize what is wrong, please let me know.
Tanks in advance.
Please see attached code


// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_printserial

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
char Lbs[30]; //  Sensors array where incoming 30 char are received
char Lbs1[6]; // 1th sensor array
char Lbs2[6]; // 2th sensor array
char Lbs3[6]; // 3th sensor array
char Lbs4[6]; // 4th sensor array
char Lbs5[6]; // 5th sensor array



BlynkTimer timer;

const unsigned long SendEmail=17000L;
unsigned long lastEmail; // Global initialize to 0

const unsigned long SendLbs=1000L;
unsigned long lastLbs; // Global initialize to 0




void sendPressure()

{
    int i=0; 
    int k;
    
    
    while( Serial1.available() >0)
        
        {  
            
        
          
        for (i=0 ;i<30;i++)  
         
         { 
               if(i==30)
                {break;} 
             Lbs[i]=Serial1.read(); 
          
             
         }
           
         
        }
          
        for(k=0;k<6;k++)  

             { Lbs1[k]= (*(Lbs+k));}
      
   
      
      
         for(k=6;k<12;k++)  

             { Lbs2[k]= (*(Lbs+k));}
      
   
      
         for(k=12;k<18;k++)  

             { Lbs3[k]= (*(Lbs+k));}
      
    
      
       
          for(k=18;k<24;k++)  

             { Lbs4[k]= (*(Lbs+k));}
      
       
     
      
      
         for(k=24;k<30;k++)  

             { Lbs5[k]= (*(Lbs+k));}
      
        
       /* Send it over the Blynk server                              */
     Blynk.virtualWrite ( V7,Lbs1);  
         
       /* Send it over the Blynk server                              */
     Blynk.virtualWrite ( V8,Lbs2);
       
      
          /* Send it over the Blynk server                              */
     Blynk.virtualWrite ( V9,Lbs3);
      
         /* Send it over the Blynk server                              */
     Blynk.virtualWrite ( V10,Lbs4); 
       /* Send it over the Blynk server                              */
     Blynk.virtualWrite ( V11,Lbs5); 
      
     
     
     
      
       
}


void setup() {

// Debug Console
Serial1.begin(57600);
delay(5000);// Allow board to settle
Blynk.begin (auth);
}

void loop() {

unsigned long topLoop = millis();


Blynk.run();

if ((topLoop-lastLbs )>=SendLbs)

{
    lastLbs=topLoop;
    sendPressure();
}

}

#7

Once again, you haven’t formatted your code correctly. You should go back and edit your post and fix that…

Blynk - FTFC

You still haven’t cleared-out your void loop by using a non-blocking timer to call your sendPressure function.

I have to say that your approach of using serial communication between two devices will be difficult to get working reliably in a way that works well with Blynk. Without any RTS/CTS control, your receiving board will be waiting for your sending device to send the data. If this wait is too long then you’ll get a Blynk disconnection.

What code are you running on your transmitting device, and what baud rate is that using to send the data?

Pete.


#8

Hi Peter
I’m so sorry for the mistake posting the code, what happens is that I am struggling with this code :disappointed_relieved:

I am using a PSoC 5LP like data acquisition MCU and transmitter device @57600 baud. towards Photon.
Photon has no flow control so what you are telling me about serial communication with Blynk worries me.

.
My goal using Blynk and Photon is getting Cloud connectivity to display different physical parameters on an app like Blynk. I do not intend to control motors or led or actuator, just display values.
On the other hand, I do not have a coding background for another type of app different from Blynk…

Here is the code

Array example :
148.99148.59147.70148.280.00 ---- I am sending it to the photon serial1 and I assume that it is stored in Lbs[ ].
The last sensor is not connected so it gives 0.00

Thank you very much again.


// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_printserial

char auth[] = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx” ;
char Lbs[30]; // Sensors array where incoming 30 char are received
char Lbs1[6]; // 1th sensor array
char Lbs2[6]; // 2th sensor array
char Lbs3[6]; // 3th sensor array
char Lbs4[6]; // 4th sensor array
char Lbs5[6]; // 5th sensor array

BlynkTimer timer;

const unsigned long SendEmail=17000L;
unsigned long lastEmail; // Global initialize to 0

const unsigned long SendLbs=1000L;
unsigned long lastLbs; // Global initialize to 0

void sendPressure()

{
int i=0;
int k;

while( Serial1.available() >0)
    
    {  
        
    
      
    for (i=0 ;i<30;i++)  
     
     { 
           if(i==30)
            {break;} 
         Lbs[i]=Serial1.read(); 
      
         
     }
       
     
    }
      
    for(k=0;k<6;k++)  

         { Lbs1[k]= (*(Lbs+k));}
  

  
  
     for(k=6;k<12;k++)  

         { Lbs2[k]= (*(Lbs+k));}
  

  
     for(k=12;k<18;k++)  

         { Lbs3[k]= (*(Lbs+k));}
  

  
   
      for(k=18;k<24;k++)  

         { Lbs4[k]= (*(Lbs+k));}
  
   
 
  
  
     for(k=24;k<30;k++)  

         { Lbs5[k]= (*(Lbs+k));}
  
    
   /* Send it over the Blynk server                              */
 Blynk.virtualWrite ( V7,Lbs1);  
     
   /* Send it over the Blynk server                              */
 Blynk.virtualWrite ( V8,Lbs2);
   
  
      /* Send it over the Blynk server                              */
 Blynk.virtualWrite ( V9,Lbs3);
  
     /* Send it over the Blynk server                              */
 Blynk.virtualWrite ( V10,Lbs4); 
   /* Send it over the Blynk server                              */
 Blynk.virtualWrite ( V11,Lbs5); 

}

void setup() {

// Debug Console
Serial1.begin(57600);
delay(5000);// Allow board to settle
Blynk.begin (auth);
}

void loop() {

unsigned long topLoop = millis();

Blynk.run();

if ((topLoop-lastLbs )>=SendLbs)

{
lastLbs=topLoop;
sendPressure();
}

}



#9

So you’ve posted different receiving code twice, but no code for the sending device, so I can’t work out what the sent data stream looks like.
it appears that you’re starting to listen to the incoming serial data stream at some random point in time and that you’re expecting this to correspond with the start of the serial data stream. in reality, the first inbound character you see could be any point ion the transmitted data string, so you have a 1 in 30 chance of it being the first character. Probably less if there are terminating characters at the end of the string.

if you have no flow control then you’ll need to wait for a known character, an initiation or terminating character, in the string to know when to begin using the received data. If there are non (seems unlikely, but I’m unclear if this is your code on the sending device or not) then you may need to look for the data from the last sensor, but that wont work if there’s ever a possibility that the other sensors will read zero at some point.

You still need to fix your void loop by adding timer.run and getting rid of the If statement.

Pete.


#10

Continuing with our instructive conversation, the code running over the exterior microcontroller is a UART sending an array of 31 char where the first one is a ‘A’ followed by 30 char representing the 5 sensor readings.
This array is send every 2 seconds approx.

I have tried two different codes using your suggestions (I hope), but non of them works.
In both cases the Blynk app seem to be keeping the Photon on line.
I am attaching both codes.
Maybe I am doing something wrong but I am not realize what’s that, even I am wondering If I should post on Particle Photon forum also.

Thank you very much for helping me.



// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_printserial

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf" ;
char Lbs[31]; 

char Lbs1[6];
char Lbs2[6];
char Lbs3[6];
char Lbs4[6];
char Lbs5[6];









BlynkTimer timer;



void sendPressure()

{  int i=0;
   
   
      while( Serial1.available()>0)
     
      {
              
           while( Lbs[0] !='A') 
          { Lbs[0]=Serial1.read();}
        
               for( i=0; i<31; i++)
              { 

                 Lbs[i]=Serial1.read();
           
                 if(i==30) 
             
                   {break;} 
              }//1
            
       }//2




            for( i=1; i<7; i++)
          { Lbs1[i]=Lbs[i];} 
  
  
         for( i=7; i<13; i++)
          { Lbs2[i]=Lbs[i];} 
          
            for( i=13; i<19; i++)
          { Lbs3[i]=Lbs[i];} 
  
              for( i=19; i<25; i++)
          { Lbs4[i]=Lbs[i];} 
  
               for( i=25; i<31; i++)
          { Lbs5[i]=Lbs[i];} 
  
     

      Blynk.virtualWrite ( V7,Lbs1); 
      Blynk.virtualWrite ( V8,Lbs2); 
      Blynk.virtualWrite ( V9,Lbs3); 
      Blynk.virtualWrite ( V10,Lbs4); 
      Blynk.virtualWrite ( V11,Lbs5); 
}     

void setup() {

// Debug Console
Serial1.begin(57600);
delay(5000);// Allow board to settle
Blynk.begin (auth);
timer.setInterval(1000L,sendPressure);
}

void loop() {



Blynk.run();
timer.run(); // Initiates BlynkTimer
sendPressure();



}


The other code is:

   

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>
#define BLYNK_printserial

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
char Lbs[31]; 

char Lbs1[6];
char Lbs2[6];
char Lbs3[6];
char Lbs4[6];
char Lbs5[6];









BlynkTimer timer;



void sendPressure()

{  int i=0;
   
   
      while( Serial1.available()>0)
     
      {
              
           while( Lbs[0] !='A') 
          { Lbs[0]=Serial1.read();}
        
               for( i=0; i<31; i++)
              { 

                 Lbs[i]=Serial1.read();
           
                 if(i==30) 
             
                   {break;} 
              }//1
            
       }//2
             

   
        for( i=1; i<7; i++)
          { Lbs1[i]=Lbs[i];} 
  
  
         for( i=7; i<13; i++)
          { Lbs2[i]=Lbs[i];} 
          
            for( i=13; i<19; i++)
          { Lbs3[i]=Lbs[i];} 
  
              for( i=19; i<25; i++)
          { Lbs4[i]=Lbs[i];} 
  
               for( i=25; i<31; i++)
          { Lbs5[i]=Lbs[i];} 
  }// endfunction
       
    
void sendLbs1()
{
 Blynk.virtualWrite ( V7,Lbs1);

}

void sendLbs2()
{
 Blynk.virtualWrite ( V8,Lbs2);

}

void sendLbs3()
{
 Blynk.virtualWrite ( V9,Lbs3);

}


void sendLbs4()
{
 Blynk.virtualWrite ( V10,Lbs4);

}

void sendLbs5()
{
 Blynk.virtualWrite ( V11,Lbs5);

}


void setup() {

// Debug Console
Serial1.begin(57600);
delay(5000);// Allow board to settle
Blynk.begin (auth);
timer.setInterval(1000L,sendPressure);
timer.setInterval(1000L,sendLbs1);
timer.setInterval(1000L,sendLbs2);
timer.setInterval(1000L,sendLbs3);
timer.setInterval(1000L,sendLbs4);
timer.setInterval(1000L,sendLbs5);




}

void loop() {



Blynk.run();
timer.run(); // Initiates BlynkTimer


sendPressure();
sendLbs1();
sendLbs2();
sendLbs3();
sendLbs4();
sendLbs5();



}
  
     











#11

Sharing that piece of information earlier would have been very useful!

You’ve already declared a timer that will run sendPressure(); every 1000 milliseconds, so sendPressure(); should not be in your void loop.

You need to think clearly about what you’re trying to achieve with the code in your sendPressure function.
It needs to read each incoming character and check if it’s “A”. If it isn’t “A” then do nothing - just wait for the next incoming character. If it is an “A” then you need to read-in the next 30 characters into your array, or into a string that you later parse.
I’m not familiar with the hardware you’re using, but if it has a free serial port then you should be using this to do some debug prints of your data to the serial monitor so you can see what your incoming data looks like (generally easier if you’ve read your data into a 30 character long string which you can push out to the serial monitor).
If you don’t have a free serial port then maybe use software serial and a FTDI adapter.
You could conceivably use a terminal widget in Blynk to display the debug data, but I don’t think your Blynk skills are good enough to do that yet. In fact, I’d remove the Blynk stuff from the code and get it working using a serial monitor then add the Blynk bits back in later.

This clearly isn’t a Blynk related problem and you’d be better off googling how to parse an incoming data stream for example code that you can modify.

Pete.


#12

Hi Peter

The following is a code that prints over Real Term the 5 sensor readings preceded by the character #0 which is 'A"
The following is a sequence of readings getting from the MCU and sent to Photon.
This is coming out of Photon’s TX pin to a FTDI serial to USB board.

image

This is the code I wrote for that:

// This #include statement was automatically added by the Particle IDE.
//#include <blynk.h>



// This #include statement was automatically added by the Particle IDE.


/* Comment this out to disable prints and save space */
////////#define BLYNK_PRINT Serial1

/////////////#include <blynk.h>**



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ;
char Lbs[32]; 
//////////////////BlynkTimer timer



const unsigned long SendLbs = 4000L;
unsigned long lastLbs;// Global initialize to 0




void sendPressure()
{
 
  
 int i=0;


while ( Serial1.available() && i < 31  )
  
      {  
     
     
      Lbs[i] = Serial1.read(); 
      if(  Lbs[i]=='\0'   )
         { break;}
          i++;
     
      }
 
 
  
  for (i=0;i<32;i++)
 {  Serial1.print( Lbs[i]);}
  
} 


void setup()
{
 
  // Debug console
  Serial1.begin(57600);

  delay(5000); // Allow board to settle
 /////////////// Blynk.begin(auth);

 
 
}



void loop()
{
   
  

  
  unsigned long topLoop = millis(); 
  

 if ( (topLoop - lastLbs) >= SendLbs) 
      { lastLbs = topLoop;
         sendPressure();
      }


}

As you can see, there are no Blynk stuff.
I tried to use " timer.setInterval (4000L, sendPressure); " at void setup() and
timer.run(); at void loop(), but non of them seems to be working.

Please also note that using the following snippet to detect character ‘A’ hang up the code.:

 while (Lbs[0]!='A')
    
        {  
             Lbs[0] =  Serial1.read();
        }
          


After that, I rewrote the code I been using for a whole year to read just one sensor and work fantastic using only a labeled value on Blink app.
It’s the following:
.

// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>

// This #include statement was automatically added by the Particle IDE.




 
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial1


//#include <blynk.h>



// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] =   "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
char Pressure[6];
//BlynkTimer timer;

const unsigned long SendPressure = 2000L;
unsigned long lastPressure;// Global initialize to 0


void sendPressure()
{
 
  
 int i=0;

 while ( Serial1.available()) 


  {  
     
    
      Pressure[i] = Serial1.read(); 
   
    
    
      if(i<0x06) 
       
        {i++;}
      
      
     
      
      else if (i>0x06)  
      { break;}
     
     
 }
 
   
 
    Blynk.virtualWrite(V7, Pressure); // send info treceived to Blynk app
    Blynk.virtualWrite(V8, Pressure); /////// added today to test what happens
   
   
   
} 

  
  
void setup()
{
 
  // Debug console
  Serial1.begin(57600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);

 
 
}






void loop()
{
   
  
  Blynk.run();

 
 
 
  unsigned long topLoop = millis(); 
  

 if ( (topLoop - lastPressure) >= SendPressure) 
      { lastPressure = topLoop;
         sendPressure();
      }
 

 
                            
}






The above code is receiving the same 31 character like is depicted on Real Term, just that I am only using 6 character in this case and the same value is showed very well at both labeled values as expected.
If I try to use 12 characters and store them on other Pressurex[i] array and send it to virtual pin V8, the code does not work,

At this point I am wondering also, why I can run the above code just using the Arduino approach for timers and the Blynk stuff does not work?
Please if you know it let me know.
Hope you can help me.
Thank you very much.


#13

Because basic Arduino code sits in the loop running thousands if times a second.

Blynk is a full IoT based interactive library that communicates to a server all the time… thus requires priority timing and changes to “normal” coding routines. Blynk is not the only library that works this way, but it is the one we discuss here :stuck_out_tongue:

I recommend you start of with basic Blynk sketches and get use to how it works… then you will better understand and be able to merge in advanced timing and serial links, etc.

Meanwhile, further pursuing this topic of cross MSU serial communication is not what this forum is about.


#14

Olay, this is going to be my last post on the subject, unless you get the seriual data read working correctly and need sopme help with getting it working with Blynk.

Are you sure abut that??

Looking at your RealTerm screenshot, I think you’re making some incorrect assumptions.
There is clearly an End of Line marker of some sort, as each group of readings begins on a new line. This will be helpful to you, as I’ll explain next.
You’re assuming that the data from each sensor is a fixed width of 6 characters (xxx.xx). This clearly isn’t the case with the last sensor, which I think you said isn’t currently connected, as this is in the format x.xx
I have no idea what it is you’re measuring, and what the potential data range is for each sensor, but if the value of one of the sensors dropped below three leading characters then your approach of slicing the data up into 6 character chunks will fail. If one of the sensors stops working and gives a 4 character 0.00 reading the all of the readings afterwards would be sliced-up incorrectly. As a result, you have to be prepared for the overall length of the serial data string to change depending on what’s happening with the sensors.

To get around this, you have to use both the “A” character and the terminating character (whatever this is, maybe \n).
The only certainty regarding each set of data is that it has two places after the decimal point.

My approach would be to listen to the incoming serial data stream one character at a time and check if that character is an “A” (note that this isn’t what your code that hangs is doing). Once I’ve sen an “A” character I’d append each of the following characters into a string (not an array) until I see the End of Line character.
I’d then parse the string (probably from the end, working back towards the beginning) looking for the decimal point characters and using these to work-out the offsets to the actual slices of data that I need.

There are lots of examples on the internet of how to parse strings in similar ways, so a bit of experimentation, helped by serial printing the results along the way, will give you a routine that reliably slices-up your data string into the correct variables.

As your incoming data string is sent at two second intervals, once you’ve received your “A” character you’ll know that the next one will start to arrive approximately two seconds later. You could start a simple timer (you need to include the Blynk library for that to work) for around 1.75 seconds. This way, when you are running the Blynk code, you wont spend too much time in a blocking routine waiting for your next serial data stream, so you shouldn’t get Blynk timeout disconnections.

Pete.


#15

Code almost solved.
I followed your instructions and I have the code running.
But so far the code has a down side: it hangs at random fashion.
According to particle Build (IDE) , the photon is always on line.
When the led breath magenta it mean that,
On the other hand I set the timer like that:

timer.setInterval(1750L, sendPressure);

An here is the code :

I hope you can help me with that !.
Thank you for your help, both of you.


// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>


/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial1

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
BlynkTimer timer;

char Lbs[36]={'\0'};  
char data;


 char* Lbs1;
 char* Lbs2;
 char* Lbs3;
 char* Lbs4;
 char* Lbs5;


void sendPressure()
{
    
  
 int i=0 ;


while ( Serial1.available( )  && i<36 )
  
  
     {   
     
        while (data !='A') {   data =  Serial1.read(); }
    
      
         
         while(1)
         {
                data =  Serial1.read();
                
                Lbs[i] = data;
                i++;
                if (i>36)
                { break;}
                
          }
           
       
         
   }     
         
         
 

 
 /* Typycal string received from external mcu:
 
         A123.45-145.78-234.45-200.56-0.00-
 */
 
  /*   Parsing the received string    */
 
 
Lbs1 = strtok (Lbs,"-");



Lbs2 = strtok (NULL, "-");



Lbs3 = strtok (NULL, "-");



Lbs4 = strtok (NULL, "-");



Lbs5 = strtok (NULL, "-");
      

 

 
 Blynk.virtualWrite ( V7,Lbs1);
 Blynk.virtualWrite ( V8,Lbs2); 
 Blynk.virtualWrite ( V9,Lbs3); 
 Blynk.virtualWrite ( V10,Lbs4);           
 Blynk.virtualWrite ( V11,Lbs5);    

          
    

} 

 

void setup()
{
 
  // Debug console
  Serial1.begin(57600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);
  timer.setInterval(1750L, sendPressure);
  
    /*VIRTUAL PINs SET TO PUSH */                            
}



void loop()
{
   
  
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  
 }








#16

@PeteKnight
I did what you ask me , why I do not deserve an answer !


#17

I’d suggested you begin your timer once you’ve received an incoming data stream, so that you won’t be waiting for upto two seconds for the next data stream.
Instead, you’re simply going and checking the data stream every 1.75 seconds, but this isn’t synchronised with the data stream, so that’s probably why you’re getting timeouts.

I don’t sit at my PC 24/7 waiting for comments or questions from other community members!

Pete.


#18

I think I am doing what you suggested me :
This version works OK 2, 3, 4 , 5 minutes and hangs up.
If I am doing wrong please give an example about how timer should be set up.
Please teach me, you are the Master.

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
BlynkTimer timer;

char Lbs[36]={'\0'};  
char data;


static char* Lbs1;
static  char* Lbs2;
static char* Lbs3;
static char* Lbs4;
static char* Lbs5;


void sendPressure()
{
    
  
 int i=0 ;


//while ( Serial1.available( )  && i<36 )
 while ( Serial1.available( )   ) 
  
     {   
     
        while (data !='A') {   data =  Serial1.read(); }
    
      
         
         while(1)
         {
                data =  Serial1.read();
                
                Lbs[i]=data;
                i++;
                if (i>36)
                { break;}
                
          }
           
       
         
   }     
         
         
 

 
 /* Typycal string received from external mcu:
 
         A123.45-145.78-234.45-200.56-0.00-
 */
 
  /*   Parsing the received string    */
 
 
Lbs1 = strtok (Lbs,"-");



Lbs2 = strtok (NULL, "-");



Lbs3 = strtok (NULL, "-");



Lbs4 = strtok (NULL, "-");



Lbs5 = strtok (NULL, "-");
      

 

 
 
          
    

} 



void Virtual_Write(){

 Blynk.virtualWrite ( V7,Lbs1);
 





 Blynk.virtualWrite ( V8,Lbs2); 
 
 




 Blynk.virtualWrite ( V9,Lbs3); 



 Blynk.virtualWrite ( V10,Lbs4);  




 Blynk.virtualWrite ( V11,Lbs5);    


}

 

void setup()
{
 
  // Debug console
  Serial1.begin(57600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);
  timer.setInterval(1750L, sendPressure);
  timer.setInterval(2000L,Virtual_Write);
  
  
   
    /*VIRTUAL PINs SET TO PUSH */                            
}



void loop()
{
   
  
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  
 }








#19

So, imagine that a bus goes past your house every 2 hours, on the hour. But, you don’t have a watch that tells you the time, just a stopwatch.
You could walk to the bus stop at some random time and wait for the next bus. You might have just missed one, in which case you’ll have to wait another 2 hours, or you may be lucky and one may appear immediately. On average, you’ll need to wait an hour for the next bus.

This is the situation you’re in with your code at present. You go and wait for a data stream every 1.75 seconds, but this isnt synchronised in any way with the device that’s sending the data. Occasionally you have to wait too long, and by the time the data has arrived and you’ve processed it, Blynk has become bored and timed-out on you.

Back to the bus analogy - you see the bus going past your window, but it’s too late to run out and catch it. You start your stopwatch and when it says that 1 hour 45 minutes has elapsed you go outside and wait. The bus will be along in less than 15 minutes.

Back to Blynk - when you’ve finished processing your first data stream, starting a timer for 1.75 seconds and going to check for the next data stream when this timer has expired will ensure that you’re never waiting more than .25 seconds for your next bus (aka serial data stream) to come along.
Of course, all of this assumes you live in Switzerland, where the buses run on time. If you live on the UK then you’ll have nothing for three hours then three will come at once, but non of them will stop, even if you’re stood at the bus stop with your arm out :bus: :bus: :bus:

I’m certainly not the master of Blynk timers, as the way that I use Blynk they aren’t needed (and I can clutter-up my void-loop to my heart’s content) but I’d suggest that you look in to the timer.setTimer() function as a way of getting this working for you.
@ Gunner has some great timer examples in his tutorials:

Pete.


#20

Hi My dear Masters

I found the solution to my code thanks to you.
It runs very well and never hangs up.
It’s been three days running and everything seems to be OK.
If you can suggest me some improve I will appreciate it.
Here is the code:


// This #include statement was automatically added by the Particle IDE.
#include <blynk.h>




/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial1

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).

char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" ;
BlynkTimer timer;

char Lbs[36]={'\0'};  
  char data;


 char* Lbs1;
 char* Lbs2;
 char* Lbs3;
 char* Lbs4;
 char* Lbs5;
 int Aflag;
/*



*/

void lookForIni() {
   
 int i=0;  
  
//Keep reading "data" until finding 'A', during 500 ms, after timeout, call " waitForString"

    while (data !='A')  // check for A
        
        
        {   
            if (i>1500)
        
                 { i=0;  break;}
            
          data =  Serial1.read();
          
           if(data='A')
                {Aflag=1;   break ;  }
            
           
            else{Aflag=0;}
           
            i++;
          
        }
           
          
          
           
        
         
}



void waitForString()
{
    
  
 
int i=0;
data=0;

while ( Serial1.available( )  && i<36 )
  
  
     {   
      if (Aflag!=1) // If Aflag is not "1", skip the function.
       { break;}
         
         while(1) // carga el LBS[] array
         {
                data =  Serial1.read();
                
                if (data!='A')
      //------------------------------------          
              {  Lbs[i] = data;
                i++;
                if (i>36)
                {Aflag=0; break;}
                
              }
       //------------------------------------         
          }
           
       
         
   }     
         
         
 

 
 /* Typycal string received from external mcu:
 
         A123.45-145.78-234.45-200.56-0.00-
 */
 
  /*   Parsing the received string    */
 
 
Lbs1 = strtok (Lbs,"-");



Lbs2 = strtok (NULL, "-");



Lbs3 = strtok (NULL, "-");



Lbs4 = strtok (NULL, "-");



Lbs5 = strtok (NULL, "-");
      

 

 
 Blynk.virtualWrite ( V7,Lbs1);
 Blynk.virtualWrite ( V8,Lbs2); 
 Blynk.virtualWrite ( V9,Lbs3); 
 Blynk.virtualWrite ( V10,Lbs4);           
 Blynk.virtualWrite ( V11,Lbs5);    

          
    

} 

 

void setup()
{
    int timerId;
 
  // Debug console
  Serial1.begin(57600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);

 // timer.setInterval(1750L,lookForIni); /*VIRTUAL PINs SET TO PUSH */                            

 // timer.setInterval(2000L, waitForString);


 timer.setInterval(500L,lookForIni); /*VIRTUAL PINs SET TO PUSH */                            

 timer.setInterval(600L, waitForString);
 
 
 
 
}



void loop()
{
   
  
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
  
 }







#21

The whole bus stuff is :ok_hand::ok_hand::ok_hand::grinning: