Reputation: 11
What I’m trying to achieve is controlling the Arduino over the serial interface. It works only to a certain degree.
I am using an Arduino Uno R3 and a Mac with Mojave (10.14.6), Apache version 2.4.34 and PHP version 7.1.23.
I started out with this simple project:
https://www.instructables.com/id/Control-LED-Using-Serial-Monitor/,
and it works fine. I am able to turn the led off and on by typing 1 and 2 into the serial input of the Arduino-app on my Mac. Now I want to do the same from a web server set up on the same Mac. I have tried two different ways of doing this. Either by accessing the serial interface through the PhpSerial library found here:
https://www.phpclasses.org/package/3679-PHP-Communicate-with-a-serial-port.html,
or by echoing the signal (1 or 2) in command line. Neither works. In a PHP-file, I have put the following code:
<?php
include 'PhpSerial.php';
$serial = new PhpSerial;
$serial->deviceSet("/dev/cu.usbmodem14101");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
if(isset($_GET["submit"])){
$serial->deviceOpen();
if($_GET["led"] == "ON")
$serial->sendMessage(1);
if($_GET["led"] == "OFF")
$serial->sendMessage(2);
}
?>
It results in the led doing some random blinking, which I interpret is the Arduino or the serial being set up, but the led is not turned on an off when I try to do so through a <form>
element in the PHP file. Can it be that the serial is set up wrongly? Now I thought to work around by running shell commands in PHP. By running
screen /dev/cu.usbmodem14101
in terminal (manually, not through PHP), I am able to access the Arduino through the serial interface, and successfully turning the led on and off, just as I did in the serial interface of the Arduino-app on the Mac. I’m not able however to find out how to use the screen command from PHP. So I thought I could echo the commands to the Arduino over the serial interface like this
echo 1 > /dev/cu.usbmodem14101
This yields the same result as with the PhpSerial. The led does some random blinking, but it will not turn on an off as I send ones and twos to the serial. As I can’t manage to get it working in terminal, I have not tried running the commands in PHP yet.
What have I missed?
Edit:
I’ve made some progress. By using cat
and putting the commands for turning the led on and off in separate files, I can concatenate the commands to the serial port. I have a file on.txt
containing only the number 1, and another file off.txt
containing the number 2. By running the commands
cat on.txt > /dev/cu.usbmodem14101
and
cat off.txt > /dev/cu.usbmodem14101
I am able to tur on and off the led. I can also put the text files in the root folder of the web page project, and run the commands from php
<?php
if(isset($_GET["submit"])){
if($_GET["led"] == "ON")
exec("cat on.txt > /dev/cu.usbmodem14101");
if($_GET["led"] == "OFF")
exec("cat off.txt > /dev/cu.usbmodem14101");
}
?>
<h1>Turn LED on or off</h1>
<p>LED turned <?php echo $_GET["led"]; ?></p>
<form action="led.php" method="GET">
<input type="radio" name="led" value="ON" <?php if($_GET["led"]=="ON") echo "checked"; ?> > On <br />
<input type="radio" name="led" value="OFF" <?php if($_GET["led"]=="OFF") echo "checked"; ?> > Off <br />
<input type="submit" name="submit" value="Execute">
</form>
Now I accomplish what I tried to do, but are there other commands I could run to direct the 1 and the 2 directly to serial port, not using the text files?
Upvotes: 1
Views: 180
Reputation: 11
After more testing it turnes out the connection to the Arduino over the serial port only works when the serial is being monitored. When I worked with the cat
solution mentioned in my edit, I monitored the serial in a separate terminal window with the command
cat /dev/cu.usbmodem14101
I did not do this with the initial testing with
echo 1 > /dev/cu.usbmodem14101
The sceen command I used is monitoring serial, as well as another solution I found and that worked
sudo cu -s 9600 -l /dev/cu.usbmodem14101
So the conclusion, it seems to me, is that I have to run the cat /dev/cu.usbmodem14101
command in a separate terminal window. Then I can output to serial port in any prefered way, from an other terminal window, through the PhpSerial library or simply exec("echo 1 > /dev/cu.usbmodem14101");
in PHP. The code I now use is the following:
<?php
if(isset($_GET["submit"])){
if($_GET["led"] == "ON")
exec("echo 1 > /dev/cu.usbmodem14101");
if($_GET["led"] == "OFF")
exec("echo 2 > /dev/cu.usbmodem14101");
}
?>
Upvotes: 0