Go Down

Topic: Serial communication with PHP (Read 31734 times) previous topic - next topic


Honesly, you're blowing my mind.  
I have more experience coding with Visual Basic 6 than I have in any other language/synax.  I'm used to using VB, so I declare almost everything as Integers.  I've never written any code that was so involved as to worry about size or memory constraints.  

On that note, I know I didn't answer your question.  I'm not sure why the integer data type worked here or why I didn't use a lesser data type.  I just did what I was used to.

I'm open to others' input here as well.  Can anyone inform me/us?

Aneal K.

Coding Badly

Ok - I just went to the Serial.read() function definition on the Arduino site:
I just noticed that it reads:
"Returns...the first byte of incoming serial data available (or -1 if no data is available) - int"
Why isn't the return value of this function a byte, instead of an int?
So - why does Serial.read() return an int,

It allows the status and value to be returned in a single function call.  It allows a loop like this...

Code: [Select]
 int ch;

 ch = Serial.read();

 while ( ch != -1 )
   if ( (char) ch == 'x' )
   ch = Serial.read();

Aren't integers on the Arduino defined as 2 bytes long?


and why doesn't the documentation match up?

I can't help with that one.

Also, what is the return value type for Serial.available() - it isn't mentioned in the documentation, either?

uint8_t (aka byte).


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

include "php_serial.class.php";

echo "<html>
      <head><title>Test MySQL</title></head>

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


// Then we need to open it

// To write into
$serial->sendMessage("Hello !");

// Or to read from
$read = $serial->readPort();

echo "here it is";
echo $read;
echo "here it was";


// If you want to change the configuration, the device must be closed

// We can change the baud rate

// etc...

arduino code =



Keep in mind that opening a serial connection resets the Arduino, as does closing the connection, unless you have modified the Arduino.

You should not open and close the connection before and after every send/receive of data.

Also, you need to allow time for the Arduino to reset before you send it data, or that data will be lost.
The art of getting good answers lies in asking good questions.


@Paul : as mentioned, seeeduino clones (like my mega) come with a switch that enables you to toggle between autoreset and manual reset. I guess this is a cheap but nice feature (even a jumper would do) that all arduinos should have, given that communications seem so much easier with this option.
Anyone speak italian ?   :-)

Thanks for the hints on the open/close and reset. The open/close : I think I would prefer to open/close since I intend to use an xbee (or some other wireless system) to get readings only every 15 minutes or so. Unless I am overlooking anything ?

Go Up