Pages: 1 2 [3]   Go Down
Author Topic: Serial communication with PHP  (Read 14478 times)
0 Members and 1 Guest are viewing this topic.
Atlanta, GA
Offline Offline
Newbie
*
Karma: 0
Posts: 25
Do I smell smoke?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Ok - I just went to the Serial.read() function definition on the Arduino site:
http://arduino.cc/en/Serial/Read
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:
 int ch;

  ch = Serial.read();

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

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

Quote
and why doesn't the documentation match up?
I can't help with that one.

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

Bahrain
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduinos rock
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 "<html>
       <head><title>Test MySQL</title></head>
       <body>";


// 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...
?>
</body></html>

arduino code =

http://www.arduino.cc/en/Serial/Print
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 551
Posts: 46238
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Bahrain
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduinos rock
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@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 ?
Logged

Pages: 1 2 [3]   Go Up
Jump to: