Search code examples
phpserial-portarduinoopenwrt

Serial connection with Arduino, PHP & OpenWrt. Bug?


I'm trying to create a PHP web interface to control Arduino Uno via USB interface on TP-Link MR3420 router using OpenWrt firmware

The weird thing is that my PHP script only get reply from arduino after running a python script that communicate with arduino

I'm sure that my PHP script is working since it was able to turn off leds on arduino but get no reply from arduino

Here is my PHP code:

require("lib/php_serial.class.php");

$serial = new phpSerial;

$serial->deviceSet("/dev/ttyACM0");
$serial->confBaudRate(9600);

$serial->deviceOpen();
$serial->sendMessage($cmd);
sleep(1);
$read = $serial->readPort();
$serial->deviceClose();

Here is my arduino sketch:

int numSerial;
if (Serial.available() > 0)
{
char inByte = Serial.read();
switch (inByte)
{
case 'l':
  numSerial = numberFromSerial();
  if (numSerial >= 0)
  {
    Serial.print("LED Mode: ");
    switch (numSerial)
    {
    case 0:
      ledMode = 0;
      Serial.print("OFF");
      break;
    case 1:
      ledMode = 1;
      Serial.print("TEMP");
      break;
    case 2:
      ledMode = 2;
      Serial.print("KR");
      break;
    }
  }
  else
    Serial.print(ledMode);
  break;
case 't': //Data request
  Serial.print(getTemp());
  break;
}
}

And here is my python code:

    import serial
ser = serial.Serial("/dev/ttyACM0", 9600, timeout=3)
ser.open()
ser.write("t")
print ser.readline()
ser.close()

Please help. Thanks.

Found the real source of the problem!

In phpSerial Class:

$ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);

Replace with:

$ret = $this->_exec("stty -F " . $this->_device . " raw speed " . (int) $rate, $out);


Solution

  • Found the real source of the problem!

    In phpSerial Class:

    $ret = $this->_exec("stty -F " . $this->_device . " " . (int) $rate, $out);
    

    Replace with:

    $ret = $this->_exec("stty -F " . $this->_device . " raw speed " . (int) $rate, $out);