I'm trying to get a web page based serial communication working with an Arduino which is connected to a router running Openwrt, it does work but only when either screen is running or remotely connected via putty, for some reason the php to serial is not starting a session properly? I use ser2net to manage the serial with the following setting
1000:raw:0:/dev/ttyACM0:9600
I have also tried stty with various settings advised on this forum
Update 1
also tried 80:raw:0:/dev/ttyACM0:9600
as setting for ser2net assuming port 80 is web/http port
my php code is
if (isset($_GET['action']))
{
$serial = new phpSerial();
$serial->deviceSet('/dev/ttyACM0');
$serial->confBaudRate(9600);
$serial->confParity('none');
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl('none');
$serial->deviceOpen();
if ($_GET['action'] == "PIN_12_HIGH")
{
$serial->sendMessage("A");
}
if ($_GET['action'] == "PIN_12_LOW")
{
$serial->sendMessage("B");
}
if ($_GET['action'] == "PIN_11_HIGH")
{
$serial->sendMessage("C");
}
if ($_GET['action'] == "PIN_11_LOW")
{
$serial->sendMessage("D");
}
$serial->deviceClose();
}
update 2
if i add sleep(1);
to my php code it seems to solve some of the problems
a few points
- adding sleep to php code allows the commands to reach the arduino after a reset caused by the serial connection, but if i turn on led 1 when i turn led 2 on led 1 gets reset to off. so this would mean a more complicated code in the form of logging an array of choices and sending the whole updated array to arduino, i would like to avoid this and just send 1 command at a time.
- again when screen or putty are active the arduino does not have this reset problem? so the question is what does screen and putty do to keep the connection alive that stty and php serial does'nt?
Update 3
After finding this helpful post i seem to have discovered a possible solution with no reset hacking needed to the Arduino.
Adding cat /dev/ttyACM0 &
to the start up config of the Openwrt router enables serial communication with the Arduino without reseting it on every transmission of data.
problems left:
- I think this code is binding arduino to a session with the router? But im unsure.
- It seems to be eating the return value from the Arduino stopping my php page getting the data return?
- My System Log file contains the missing returned data from Arduino?
Update 4
- As i needed to get this working, i used a capacitor to stop the auto reset from serial communication on the arduino.
- As stated screen and putty dont create this reset problem when communicating with arduino, i tried many different settings with STTY but couldnt find a solution , but i am still trying and keen to discover how screen/putty does this.