gutzz
gutzz

Reputation: 53

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);

Upvotes: 2

Views: 9059

Answers (3)

Radiotrib
Radiotrib

Reputation: 639

I use this line in my rc.local file to set up the port prior to running anything.

stty -F /dev/ttyUSB0 raw speed 38400 -echo -hupcl

the two additional flags are useful: -echo switches off the OpenWRT/USB habit of echoing every output back on the input -hupcl switches off the autoreset issue by not sending a hup after each transmission

There again YMMV - I am sending and receiving single bytes not strings or complex data

Upvotes: 0

gutzz
gutzz

Reputation: 53

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);

Upvotes: 3

ZnArK
ZnArK

Reputation: 1541

The Arduino has a documented Auto Reset on Serial Connection issue. My first guess would be that you are triggering this with the PHP code (that's why the lights are turning off but you're not receiving any response back).

Try using one of the workarounds from the link above.

Here's a perl code snippet you could use to see if you are actually triggering this issue.

#!/usr/bin/perl
use strict;
use Device::SerialPort;
my $port = Device::SerialPort->new("/dev/ttyUSB0");
$port->databits(8);
$port->baudrate(9600); # <-- match to arduino settings
$port->parity("none");
$port->stopbits(1);
$port->dtr_active(0);  //toggle this to one to trigger reset

Upvotes: 0

Related Questions