Hello, let me first say that I'm not so great at coding.
So I noticed that getting the Arduino to read data from PHP is pretty well documented. Though getting PHP to read data from the Arduino is not as well written about. I just thought I'd post this code as it works for me.
Arduino
int analogValue = 0; // variable to hold the analog value
void setup() {
// open the serial port at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog input on pin 0:
analogValue = analogRead(0);
// print it out in 1 format:
Serial.println(analogValue); // print as an ASCII-encoded decimal
// delay 1000 milliseconds before the next reading:
delay(1000);
}
This is the standard Arduino serial write example, only modified to send one line of data to the serial port. And the delay has been increased to 1 second.
PHP
<?php
$fp = fopen("/dev/ttyUSB0", "r"); // Open the serial port
while($data==$data)
{
$data = fread($fp, "8"); // Keep getting from the port
sleep(1); // Sleep 1 second
if ($data)
{
print $data; // If you have data, then print it
break; // Break out of the loop
}
}
fclose($fp); // Close the connection to the serial port
?>
I've tested the PHP code extensively and it doesn't seem to work with out the sleep function. Though I'd like to rewrite it with out it, as it kinda slows up loading the page a bit.
Oh yeah, I'm not using the PHP serial class because I can get the same result with this, and I figured: same result + less lines of code = good thing. I know that these are file commands, but to Linux, the serial port is a file anyway, right?
Though getting PHP to read data from the Arduino is not as well written about.
The PHP serial class contains some comments about reading from the serial port on Windoze (basically, it can't). A google search turns up similar (lack of) success stories.
Since most Arduino users are (probably) running Windoze, the lack of how-to-read-from-the-serial-port-using-PHP is understandable.
Two questions, though.
What's with this statement:
while($data==$data)
The Arduino writes one byte, and then the PHP script tries to read 8:
$data = fread($fp, "8"); // Keep getting from the port
Maybe this is why you have to sleep in the PHP script. If the read length matched the write length, maybe you wouldn't need to.
The PHP serial class contains some comments about reading from the serial port on Windoze (basically, it can't). A google search turns up similar (lack of) success stories.
Really? That sucks. Go UNIX-based operating systems! Heh.
What's with this statement:
Haha, I have no idea. Well, I noticed that PHP seemed to be only getting part of the data sent by the Arduino, like something that was 3 bytes would only come in as 1 or 2 bytes. So I thought a loop would force it to keep trying to get the complete data until it does. I know that it's pretty weird, and I'm not sure if it's actually doing what I want, but the code doesn't work with out it.
The Arduino writes one byte, and then the PHP script tries to read 8:
I thought the second argument was the maximum amount of data that it can receive? The data being sent in that example is 3 bytes actually.
Here's some code I worked on this morning. It works with out the sleep function, but sometimes the browser hangs on a refresh.
I'm writing this code is so I can get data from sensors and post it to the web. Here's my first attempt: http://danlab.ath.cx, with a light to frequency converter. Again, I have only half an idea of what's actually going on. Seems to work though.
The "while($data==$data)" statement needs to evaluate whether the two variables contain the same data. Since the same variable is on both sides, the comparison will always be true, so "while(1)" is equivalent, and faster.
The Arduino writes one byte, and then the PHP script tries to read 8:
Actually, that was wrong. The Arduino is writing 3 or 4 bytes (an int and a CR/LF). I'm not sure whether it writes both a CR and an LF or only one of them, and I'm too lazy to look. So, you're right, you need to read 3 or more bytes to clear the buffer. The 8 is the maximum number of bytes to read. But, when you request 8 bytes, it reads until it encounters an end-of-file condition, which can occur before 8 bytes have been read if the serial buffer is empty. If you only want three bytes, which represent a value written by the Arduino, you should only ask for three bytes.
The fgets function reads until it encounters a NULL. The "hanging" can occur if there's no NULL to terminate the string.
The fgets function accepts a 2nd argument - the number of bytes to read. If you know that the data to be read is a fixed length, specify it.
The fgets function reads until it encounters a NULL.
How could I possibly misspell newline? That statement should say that fgets reads until it encounters a newline. Obviously, you can make sure that the Arduino sends a newline, by using println.
However, serial data is not guaranteed to be transmitted. So, adding a reasonable maximum length is a good idea.
well hello i'm want to built a driving robot with Eee 900 (OS: win7 starter) as power source ,wifi acces point and camera(built-in).
I can send commands over wifi adhoc from my E90 to my Eee
so controlling my robot won't be the problem
But i would love to read temperature*, humidity*, front lights status and such things.
even a simple 1 or 0 would do
example:
boolean temp_above_20;
if (temp >= 20){
temp_above_20 true
}
if i would have a bigger budget i would just get a simple wishield and a simple ip camera
Willing to switch back to linux (xandros or Eeebuntu)