Connect for a few seconds - Arduino UNO USB

Hi,

I’m having a problem. I can get my arduino UNO R3 to connect via USB. However, when I touch a button on my android phone, the arduino deconnects from my phone and I get this message from the CMD.

Connecting device at COM3 to blynk-cloud.com:8442...
OpenC0C("\\.\COM3", baud=9600, data=8, parity=no, stop=1) - OK
Connect("blynk-cloud.com", "8442") - OK
InOut() START
DSR is OFF
Received EOF
EVENT_CLOSE
InOut() - STOP
Disconnect() - OK
Connect("blynk-cloud.com", "8442") - OK
InOut() START
DSR is OFF

Here is my code. It seems to be my code the issue. I doesn’t disconnect when using exemples.


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


#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token";
BLYNK_WRITE(V1)
{
  calibration=param.asInt();
  if (calibration==HIGH) {
    calibrer();
  }
  }
BLYNK_WRITE(V2)
{
  int bouton = param.asInt(); // assigning incoming value from pin V1 to a variable
             //Bouton pour redémarrer le calcul de la moyenne sans reset et passer par la calibration a nouveau
    if (bouton==HIGH) {
  totalaverage=0;
  numaverage=0;
    }
}

void calibrer()
{
  digitalWrite(ledPin, HIGH);
  debutcalib=millis();
    // Laisser le capteur à une différence de pression nulle pendant 5 secondes
  while ((fincalib-debutcalib) <= temps_calibration) {
i++;
sensorValue = analogRead(sensorPin);
total=total+sensorValue;
  fincalib=millis();
  }
  calibration=total/i;
  // signal the end of the calibration period
  digitalWrite(ledPin, LOW);
}

void setup()
{
  // Debug console
  SwSerial.begin(9600);
    pinMode(ledPin, OUTPUT);
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  calibrer();
}

void loop()
{
  Blynk.run();

numaverage++;
readIndex=1;
total=0;
debutlecture=micros();
  while ((finlecture-debutlecture) <= 1000000) {                //Valeurs moyenné au 1 sec pour le serveur blynk
  debut=micros();
           //Lecture du sensor
  sensorValue = analogRead(sensorPin);                              //Setup le sensor à 0 pression, une valeur de 0. Si delta_pression<0 alors valeur négative
  Delta_pression=(sensorValue-calibration)*maxsensor*2000/1024;        //, transformation de la valeur analogue en pression Pa
  // Serial.print(Delta_pression);
  total=total+Delta_pression;
  readIndex++;
    finlecture=micros();
  }
  delay(1000);
        // Valeur graphique échantillons numReadings
  PressionSmooth=total/readIndex;
  Blynk.virtualWrite(V6, Delta_pression);
  
        //Valeur moyenne depuis départ
  totalaverage=PressionSmooth+totalaverage;
  average=totalaverage/numaverage;
  Blynk.virtualWrite(V5,average);

      //Fréquence d'échantillonage
frequence=1000000/(finlecture-debut);                                //Calcul de la fréquence d'échantillonage
vitessemax=frequence*60/5;                                        //Vitesse maximale du moteur en RPM pour une belle lecture 
  Blynk.virtualWrite(V3,frequence);                                  //Pour une lecture facile sur le plot


}

Thanks for the help!

You are doing to much in your void loop(), including a delay() (which stops ALL processing for that timeframe) and all that will interrupt Blynk causing the disconnections.

Look through this and other Help Documents to see how to use timers, etc.

http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/how-to-display-any-sensor-data-in-blynk-app


The thing is, I need my sensor to run at a high frequency, at least 100 Hz. I can’t wait for each call of the function (which I read has to be at least 1 seconde) to make my readings and I want my readings to be as continuous as possible.
Is there a way to make it work without the loop?

I put the delay to be sure not to overload the server. I’ll delete it.

Well thanks for the tip! I think I found a good work around :slight_smile:

Could you just take a look?
Is it a good way to have a continuous reading (averaged for every time the print is called)?

Thanks!

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

#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
    
#include <BlynkSimpleStream.h>

//Declarer les fonction de Blynk
BlynkTimer freq;
BlynkTimer moyen;
BlynkTimer lecture;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "auth";
BLYNK_WRITE(V1)
{
  calibration=param.asInt();
  if (calibration==HIGH) {
    calibrer();
  }
  }
BLYNK_WRITE(V2)
{
  int bouton = param.asInt(); // assigning incoming value from pin V1 to a variable
             //Bouton pour redémarrer le calcul de la moyenne sans reset et passer par la calibration a nouveau
    if (bouton==HIGH) {
  totalaverage=0;
  numaverage=0;
    }
}

void calibrer()
{
  digitalWrite(ledPin, HIGH);
  debutcalib=millis();
    // Laisser le capteur à une différence de pression nulle pendant 5 secondes
  while ((fincalib-debutcalib) <= temps_calibration) {
i++;
sensorValue = analogRead(sensorPin);
total=total+sensorValue;
  fincalib=millis();
  }
  calibration=total/i;
  // signal the end of the calibration period
  digitalWrite(ledPin, LOW);
}
void mean()
{
  Blynk.virtualWrite(V5,average);
}

void frequency()
{
 //Fréquence d'échantillonage
frequence=1000000/(fin-debut);                                //Calcul de la fréquence d'échantillonage
vitessemax=frequence*60/5;                                        //Vitesse maximale du moteur en RPM pour une belle lecture 
  Blynk.virtualWrite(V3,frequence);                                  //Pour une lecture facile sur le plot  
}

void lec()
{
  Blynk.virtualWrite(V6, Delta_pression);
        // On reset les valeurs pour la prochaine lecture
  readIndex=1;
  total=0;
}
void setup()
{
  // Debug console
  SwSerial.begin(9600);
    pinMode(ledPin, OUTPUT);
  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);
  Blynk.begin(Serial, auth);

  //Timer pour pas overload serveur
  freq.setInterval(1000L, mean);
  moyen.setInterval(1000L, frequency);
  lecture.setInterval(100L, lec);

  //Calibration du sensor
  calibrer();
 
}

void loop()
{
  Blynk.run();

  debut=micros();
           //Lecture du senso
  sensorValue = analogRead(sensorPin);                              //Setup le sensor à 0 pression, une valeur de 0. Si delta_pression<0 alors valeur négative
  Delta_pression=(sensorValue-calibration)*maxsensor*2000/1024;        //, transformation de la valeur analogue en pression Pa
  // Serial.print(Delta_pression);
  total=total+Delta_pression;
  readIndex++;
  fin=micros();
 lecture.run();

        // Valeur graphique échantillons numReadings
  PressionSmooth=total/readIndex;
  
        //Valeur moyenne depuis départ
  numaverage++;
  totalaverage=PressionSmooth+totalaverage;
  average=totalaverage/numaverage;
  
moyen.run();
freq.run();
}

Perhaps run the Arduino separately (no Blynk) and the ESP with Blynk as a standalone device… and share info between them via serial, I2C, etc.

I am working on something like that to control a stepper motor without worrying about delays or Blynk disconnections…

I am not very good at troubleshooting code over the forum… I usually need to have it running in front of me…

But calling two functions from the void loop() is still the same as having then in the loop :wink: Still all running hundreds/thousands of times a second.

Yep I need it to run very fast (i’m reading the pressure of a running motor).

I continue to test my code and it works great! I have a good frequency of readings and it doesn’t disconnect from the server :slight_smile:

Is it possible to increase the frequency of the Blynk function if I use a local server? Or will it cut the connection also?

Thanks a lot!

Yes, a Local Server will greatly increase throughput… providing your local network is good. But you you can still have disconnection issues or some lag, as WiFi is still a bottleneck… but much better then a full Internet link.

Adding this line may also help with Local Server, at least with Blynk data transmission… no benefit for actual sensor readings as that is mostly the device and code… not sure it is as effective on the Cloud though.

#define BLYNK_MSG_LIMIT 0