Go Down

Topic: long delay when receiving data from arduino uno (Read 102 times) previous topic - next topic

rfr26604

Jan 17, 2020, 06:07 pm Last Edit: Jan 17, 2020, 08:24 pm by rfr26604
I am having a 6 seconds delay in receiving data from arduino in my php program and specifically
in the line

while(!$buffer=stream_get_line($fp,400,">")) { 1; }

the information I receive is good, as expected, the only problem is the 6 second delay.

Is there any buffer in the way? I am using wamp server, with xampp was the same situation. The OS of my PC is Windows. I am creating a work station to measure backup batteries. The circuit and UI are ok, the only pending problem is this delay.

Here is my php program

Code: [Select]
<?php
/**
*Remember to go to Device Manager> Ports (COM & LPT)>Arduino XXX (COMXX)>right click>Properties>Port Settings>Advanced>uncheck "use FIFO buffers ........."
* In other hand, remember that the Tx speed has to be the same in PhpConnect.php, in Arduino sketch *and in the COM
* properties in Device manager, I selected 115200 b/s.

*/
$output exec("mode COM2: BAUD=115200 PARITY=N data=8 stop=1 XON=off TO=on dtr=off odsr=off octs=off rts=on idsr=off");
$fp fopen("COM2""r+");
if (!
$fp)
{
      exit(
"Unable to establish a connection");
}
// RX form PC**************
$t $_POST['text1'];
// TX to Arduino****************
$writtenBytes fputs($fp$t);
sleep(1); //cuando quite el sleep se arreglo la demora de 1 minuto
// RX from Arduino**************
$j=0;
$dataset1 = [];
while(!
$buffer=stream_get_line($fp,400,">")) { 1; }
// TX to PC***************
$piecesa explode(","$buffer);
foreach (
$piecesa as $value) {  
 
$dataset1[$j] = $value;
 
$j++;
}
$myJSON json_encode($dataset1);
echo 
$myJSON;
fclose($fp);
?>


And here my sketch

Code: [Select]
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//   ARDUINO BATTERY INTERNAL RESISTANCE TESTER
//   Version-1.0

//  "lee" is the model of battery to measure with the voltage differencial method in order to calculate the internal resistance

//    Ri = (V1 - V2)/(I2 - I1)

//   V1 & V2 are measured directly, I1 & I2 are calculated

//   I1 = V1/Rl1

//   I2 = V2/Rl2

//   this method consists of connecting a load for 10 seconds to get a current of 0.2C, in the end
//   of that time, the voltage V1 is obtained, at the same time, connect a second load resistor for
//   3 seconds to get a current from battery equal to 2C, in the end of that time, is obtained V2.
//   Once we have this values, calculate corresponding currents by the formuls

//   I1 = V1/Rl1 y I2 = V2/Rl2

//  and the internal resistance will be

//   Ri = (V1 - V2)/(I2 - I1)

//   these batteries have 5 Ohms of internal resistance new, that's why, when the resistance obtained
//   is lower than 9 Ohms the battery is good, more than that, is bad. 


//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define mosfet1 5
#define mosfet2 6
#define Bat_Pin A0

float Rl1 = 600;  // Load Resistor Value in Ohm
float Rl2 = 60;
float Rl;
float Ri = 0.0; // Internal Resistance
float Vcc1 = 4.9; // Voltage of Arduino 5V pin ( Mesured by Multimeter when Rl1 is connected)
float Vcc2 = 4.78; // Voltage of Arduino 5V pin ( Mesured by Multimeter when Rl2 is connected)
float I1 = 0.0; // Current in Amp when Rl1
float I2 = 0.0; // Current in Amp when Rl2
float V1 = 0.0;  // Battery Voltage when Rl1
float V2 = 0.0;  // Battery Voltage when Rl2
float Voc = 0.0; //  Battery Voltage with connected load
float Vm = 0.0;  // Voltage at lower end of the Resistor load (MOSFET)
float Bat_High; // Battery High Voltage
float Bat_Low; // Discharge Cut Off Voltage
float sample1 = 0;
float sample2 = 0;
int go = 1;
char lee = "";
int bandera = 0;
const byte numChars = 5;
char receivedChars[numChars];   // an array to store the received data
String aStringObject;

boolean newData = false;
//int dataNumber = 0;             // new for this version

void setup() {
  // initialize serial:
  Serial.begin(115200);
  pinMode(mosfet1, OUTPUT);
  pinMode(mosfet2, OUTPUT);
  pinMode(Bat_Pin, INPUT);
  digitalWrite(mosfet1, LOW);  // mosfet1 is off during the start
  digitalWrite(mosfet2, LOW);  // mosfet2 is off during the start
}
//********************************Main Loop Function***********************************************************
void loop() {
  newData == false;
  // if there's any serial available, read it:
  recvWithEndMarker();
  if (newData == true  && lee > 0) {
    medir();
  }
  newData = false;
  lee = 0;
}// end of void loop()
/*********************** recvWithEndMarker ****************************************/
void recvWithEndMarker() {
  digitalWrite(mosfet1, LOW);  // mosfet1 is off during the start
  digitalWrite(mosfet2, LOW);  // mosfet2 is off during the start
  if (Serial.available() > 0) {
    lee = Serial.parseInt();
    newData = true;
  }
}
/*********************** MEDIR **********************************************/
void medir() {
  digitalWrite(mosfet1, LOW); // Turned Off the mosfet1 // No discharge
  digitalWrite(mosfet2, LOW); // Turned Off the mosfet2 // No discharge
  //************ Measuring Battery Voltage OC ***********
  for (int i = 0; i < 100; i++)
  {
    sample1 = sample1 + analogRead(Bat_Pin);
    delay (2);
  }
  sample1 = sample1 / 100;
  Voc = sample1 * 4.96 / 1024.0;
  sample1 = 0;
  delay(100);
  //********************* Checking the different conditions *************
  if (lee == 1) {
    Bat_High = 4.0;
    Bat_Low = 3.0;
    Rl = Rl1;
  }
  if (lee == 2) {
    Bat_High = 2.7;
    Bat_Low = 1.5;
    Rl = Rl2;
  }
  if (Voc < 0.5 ) {
    Serial.print( "Warning Connect the Battery! "); Serial.print(">"); Serial.flush();
    delay(500);
  }
  else if ( Voc > Bat_High) {
    Serial.print( " Warning High-V! ");
    delay(500);
  }
  else if (Voc < Bat_Low && Voc > 0.5) {
    Serial.print( "Warning Low-V! "); Serial.print(">"); Serial.flush();
    delay(500);
  }
  //************ Measuring Battery Voltage with load Rl1 ***********
  digitalWrite(mosfet1, HIGH); // connect load Rl1
  delay(10000); // 10 segundos
  sample1 = analogRead(Bat_Pin); //read the voltage from battery
  //      sample1 = sample1 / 5000;
  V1 = sample1 * Vcc1 / 1024.0;
  sample1 = 0;
  digitalWrite(mosfet1, LOW); // disconnect load Rl1
  //************ Measuring Battery Voltage with load Rl2 ***********
  digitalWrite(mosfet2, HIGH); // connect load Rl2
  delay(3000); // 3 segundos
  sample1 = analogRead(Bat_Pin); //read the voltage from battery
  V2 = sample1 * Vcc2 / 1024.0;
  sample1 = 0;
  digitalWrite(mosfet2, LOW); // disconnect load Rl2
  //*********** Calculating Ri *********************
  I1 = V1 / Rl1;
  I2 = V2 / Rl2;
  float V = V1 - V2;
  float I = I2 - I1;
  Ri = V / I;
  //*********** Showing Ri *********************
  Serial.print(" Voc = "); Serial.print(Voc, 2);
  Serial.print(", V1 = "); Serial.print(V1, 2);
  Serial.print(", V2 = "); Serial.print(V2, 2);
  Serial.print(", I1 = "); Serial.print(I1 * 1000, 0);
  Serial.print(", I2 = "); Serial.print(I2 * 1000, 0);
  Serial.print(", Ri = "); Serial.print(Ri, 2);
  Serial.print(", Ri/2 = "); Serial.print(Ri / 2, 2);
  if (Ri / 2 >= 0 && Ri / 2 <= 9.0) {
    Serial.print( ", Good! "); Serial.print(">");
    Serial.flush();
  }
  if (Ri / 2 > 9.0) {
    Serial.print( ", Bad! "); Serial.print(">");
    Serial.flush();
  }
  newData == false;
}// end medir()


Thank you in advance.

Robin2

You need to post your Arduino program. It would also help if you describe (in English rather than code) how the interaction between your web program and the Arduino is intended to work.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

rfr26604


Robin2

I already modified the post.
For the future, please don't make big changes to earlier Posts after there has been a Reply as if will confuse anyone reading the Thread from top to bottom. Changes to correct typos are fine.

You did not respond to my request for a description of how the system is intended to work.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Robin2

You seem to have several long delay()s in the Arduino program - they are probably the cause of your problem. However its is not obvious what part of the Arduino program is sending data to the web server


Some elements of your code (the function name recvWithEndMarker() and the variable newData) are reminiscent of code in my Tutorial Serial Input Basics however the actual code in your function is something that I deliberately avoided in my tutorial.

...R

 
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up