PHP overflows the arduino buffer

Viewed 73

I have a problem with arduino (uno) buffer overflow. I am trying to create a simple application in which I insert b (blue diode) or r (red diode) or space (space does not turn on any diode) through the PHP page into the form, send it and then the blue or red diode lights up. Arduino connected via USB (COM3) Specifically: I insert "br r b rbrb" into textarea the first 9 characters are displayed by diode light, but after the ninth character arduina buffer is probably filled and other characters are displayed (by diodes) after about 10 seconds. If anyone knew how to fix it I would like to thank you.

BTW if i use serial monitor in arduino app this problem is not and the buffer will not fill so arduino will process the whole string ("br r b rbrb") without jamming on the ninth character

Arduino code:

int red_2=4;
int blue_1=5;

void setup() {
  Serial.begin(9600);

  // set diodes for outputs
  pinMode(red_2, OUTPUT); // red
  pinMode(blue_1, OUTPUT); // blue


  //all diodes going LOW in start
  digitalWrite(red_2, LOW);
  digitalWrite(blue_1, LOW);

}

void loop() {
  char var_characters;


  if (Serial.available() > 0) {
    var_characters = Serial.read();

    if (var_characters == ' ') {
      Serial.println("Reading charakter: 'space' ");
      delay(500);
      digitalWrite(red_2, LOW);
      digitalWrite(blue_1, LOW);
    }

    if (var_characters == 'b') {
      digitalWrite(blue_1, HIGH);
      Serial.print("Reading charakter: ");
      Serial.println(var_characters);
      delay(500);
      digitalWrite(blue_1, LOW);
    }

    if (var_characters == 'r') {
      digitalWrite(red_2, HIGH);
      Serial.print("Reading charakter: ");
      Serial.println(var_characters);
      delay(500);
      digitalWrite(red_2, LOW);
    }
  }
} 

PHP code:

<html>
<head>
    <title>PHP and Arduino</title>
</head>
<body>
    <?php
        exec("mode COM3: BAUD=9600 PARITY=n DATA=8 STOP=1 to=off dtr=off rts=off");
        $port = fopen("COM3", "w+");
        sleep(2);
    ?>

    <center>
        <h1>PHP and Arduino</h1>

        <form method="post">
            Message: <textarea type="text" name="msg_form"></textarea>
            <input type="submit" name="send1">
        </form>

        <br>
    </center>

    <?php
        if (isset($_POST['send1'])) {

            $msg_post = $_POST['msg_form'];

            echo "<center> $msg_post </center>";
            fwrite($port, "$msg_post");
        }

        fclose($port);
    ?>
</body>
</html>
1 Answers

Your delay(500); calls inhibit reading more than 2 characters per second.

There are several ways to improve this behavior.

Keep in mind that the Uno Serial input buffer is very limited (64 char).

If sending e.g. "br r b rbrb" ( 11 characters ) intends a time series of more than 5 seconds, your sender should simply wait long enough before sending the next sequence (easiest manually).

However, your overflow problem description does not really fit (you send less than buffer_size = 64 char ?). Please have a closer look.

Related