Blynk app get connected and disconnected cyclically

Hi There
I wrote a Blynk app to start a DC motor and receive a feedback signal which turn on a led on Blynk app.
This is implemented using Particule Photon with Blynk libraries
On Blynk app the start button activate a relay and other relay send back to Blynk a feedback signal…
The code has one module which send the start motor command and other module which receive the the feedback signal to turn on/off a led on Blyink app.

IF I only use the StartM () module every think works OK.
As soon as I use the other module SendFeebk() the code does not work.

What you see on the mobile is that with intervals of app 2 or 3 second the app get connected and disconnected

Hop some help with that.
Thank you in advance

The following is the code:


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

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************

  You can use this sketch as a debug tool that prints all incoming values
  sent by a widget connected to a Virtual Pin 1 in the Blynk App.

  App project setup:
    Slider widget (0...100) on V1
 *************************************************************/

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

#include <blynk.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " e069ade1b8684b8783a6150e6228aabb";

BlynkTimer timer;

unsigned long lastStartMotor;// Global initialize to 0
unsigned long TachoL;// Global initialize to 0
const unsigned long SendStMot  = 2000L;
const unsigned long SendTachoL = 15000L;

// This function will be called every time button is pressed
// Blynk app writes values to the Virtual Pin 6
  int pinValue;
 

BLYNK_WRITE(V6) // Button on Blynk app write to to virtual pin 6.
{
  
  pinValue = param.asInt(); // assigning incoming value from pin V6 to a variable

}

void SendFeebk() {
 
   int feebk;
   int TachoLed;
 
   feebk = Serial1.read();
  
  if (feebk==0xcd)
  
   {TachoL=255;  }
  
   else if (feebk=0xfe)
  
   {TachoL=0;    } 
   
   else{;} 
  
   Blynk.virtualWrite(V4,TachoL); 

}




void StarM() {
  
  
      
     BLYNK_WRITE(V6);
      
      
 
      
    
      
   if (pinValue==0x01)
   
  
    
  { Serial1.write(0xAF);}
   
    else if (pinValue==0)  
    { Serial1.write(0xFF);}
    
    else{;}
    
    }
   
    





void setup()
{
 
  Serial1.begin(57600);

  delay(5000); // Allow board to settle
  Blynk.begin(auth);
   Blynk.email( "Subject", "My Blynk Start motor project with feedback is online.");
  
   
   
}

void loop()
{
    
   
     Blynk.run();
    

  unsigned long topLoop = millis();  
  
  
  if ( (topLoop - lastStartMotor) >= SendStMot )
     { lastStartMotor = topLoop;
        StarM();}
        
  if ( (topLoop - TachoL) >= SendTachoL )
     { TachoL = topLoop;
        SendFeebk();                   }     
      
   
  
}

What are you trying to do here?

Hi Costas Thank you very much.

feebk = Serial1.read(); is reading from the Serial1 the fedback signal that confirm that the start command to turn on the motor was received.
If feebk =1 the led on Blynk app should be turned off ( motor stopped).
If feedk =0 the led should be on ( motor is on).
feedbk is just an input pin from a microcontoller tied to 3.3v .
When the command start motor is received ( 0xAF), that pin is connected to gnd through another relay contact.
That’s all.
Thank you Costas

Try changing the line as indicated below and provide the results.
feebk=1;

I had the same problem working with esp8266 board and Blynk, without feed back every thing works fine. I discovered if feed back is sent very fast (a dozen time in a second) or too many variables sent as feed back would make the Blynk app connection unstable and it keeps disconnecting reconnecting. I have re arranged the code to send the feed back not cyclic but occasionally when there is a change in the state of the signal, or you can setup a timer to send the feed back with some delay, update the feedback in the app every 500ms.

Best Regards

I’ll try later, I cannot get connected to build.particle.io right now.
Regarding your suggestion, are you trying to set a “1” to feebk variable to just verify that it can be send a feed back ?

Thank you

Thank you seaspac

I am waiting for get connected to the site where I am able to change the code.
I’'ll will try your suggestion sending a feed back signal just once when the motor got running.
Also Costas is waiting for provide my feed back for a test he suggested me.

Timers are essential with Blynk, so much so that the Arduino Simple Timer library is incorporated into the Blynk library and renamed Blynk Timer. It’s not related to Blynk’s timer widgets and will run without a connection to Blynk or the internet.

I believe you Particle users have your own timers but @Sony777 seems to have handled the issue correctly with millis().

@Sony777 I’m not a Particle user but I was a little concerned about “reading” the Serial1 port in your sketch as Serial1 is defined for use with BLYNK_PRINT. Hence my hack to remove the “read”.

OK Costas what do you suggest to do? Use serial2 ?
Thank you.

Blynk will run without BLYNK_PRINT so you could comment that out. However it is useful to see some of the feedback from Blynk. Try the “1” hack first as I might be barking up the wrong tree.

Please clarify me where put the feebk=1 ?

It is about replace the feebk = Serial1.read(); line?

Yes.

Ok Costas I did it
It happens exactly the same the Blynk app keep telling me that the device is connected and disconnected
app every two seconds.

And the motor does not respond to the start command from Blynk.

This is not a legit command for Arduino / ESP and I would be surprised if it was for Particle.

@Costas

Costas just for you get more info, the code running on the microcontroller was tested using REALTerm to send and receive command through the serial port.
Becauseof that, I am starting to think that the the Blink app is not receiving the feedback command.
May be is Blynk or also can be the particle Photon.

In StartM you have a call “BLYNK_WRITE(V6)” that usually should be a callback method for the V6 writes, so it is possibly a place for some error. What is changing V6 pin value?

Also the check in the loop should be reworked using BlynkTimer that will call 2 methods with that SendStMot and SendTachoL delays - it’s mostly a recommendation, i’m not sure that it could cause some issues with disconnect.

@vshymanskyy could you also check this code?

Does Blynk Timer work with Particles or should they use their own timers?

Hm, not sure about that, but probably there were at least plans for such functionality.

@Costas@seaspac@BlynkAndroidDev
Hi There
Good news the code is working OK as I expected.
The first mistake was a logical comparison inside an If statement like
" If (TachoL =0xFE)" in place of " If (TachoL ==0xFE)".
This was the key to get working the code.
Nevertheless, I got to possible timer methods which return me a feed back to know that the command sent from the mobile really reach the remote microcontroller app.
The one suggested for all of you, I mean using Blynk timer worked better than the Arduino I originally
posted in a first place.
The Blynk timer gives me a latency or delay of app 5 seconds to power on/off the feed back led.
The Arduino method using millis() give me a big latency and a top of that the feed back led is powered randomly.
I want to thank very much all of you for your help.

1 Like