Serial communication with PHP

Sweet : I have a seeeduino mega, which has a toggle switch for the reset function : auto or manual, so that issue isn't one here.

I downloaded the php serial class mostly because I am interested in reading values from the arduino into a mysql database. My first trial was with the example.php that comes with the class. I made some really small changes.

  • comment out the write line
  • check arduino interface for port name, was /dev/ttyUSB0, so change portname to that string
  • add html header and footer
  • add : echo $serial; after tthe readport() line, this gives me instant feedback, once this works it should easy to direct the output to mysql and then schedule a cron job that calls this php script every 15 minutes or so.

On the arduino, I ran the serial print demo sketch (that prints values in all formats). I verified arduino functionality through serial monitor, than closed that. As you close that, the red tx led on the arduino goes dark.

The first run of the php script failed, the arduino led never gave any sign of life.
I needed to chmod 777 /dev/ttyUSB0, and then I started to get gibberish in my php page, which means success !

Next up : make the arduino respond to a specific call with readings in a specified format. Or maybe I will do one value depending on which call it gets : pc sends A, arduino response is temperature value, pc sends B, response is hunidity etc.

pc code

<?php include "php_serial.class.php"; echo " Test MySQL "; // Let's start the class $serial = new phpSerial; // First we must specify the device. This works on both linux and windows (if // your linux serial device is /dev/ttyS0 for COM1, etc) $serial->deviceSet("/dev/ttyUSB0"); $serial->confBaudRate(9600); // Then we need to open it $serial->deviceOpen(); // To write into $serial->sendMessage("Hello !"); // Or to read from $read = $serial->readPort(); echo "here it is"; echo $read; echo "here it was"; sleep(1); // If you want to change the configuration, the device must be closed $serial->deviceClose(); // We can change the baud rate //$serial->confBaudRate(2400); // etc... ?>

arduino code =