I've had some trouble over the last few months with getting values back via php. This is on a LAMP setup with an Xbee explorer module plugged into TTYUSB0. Arduino on another XBEE. I just switched to xbee as im getting a little farther along on my long term project. I was having this same issue when I had the arduino plugged directly via USB.
PHP Code:
<?php
/* Script for turning LED on and off based on temperature*/
// Load the serial port class
require("php_serial.class.php");
//Initialize the class
$serial = new phpSerial();
//Specify the serial port to use... in this case COM1
$serial->deviceSet("/dev/ttyUSB0");
//Set the serial port parameters. The documentation says 9600 8-N-1, so
$serial->confBaudRate(9600); //Baud rate: 9600
$serial->confParity("none"); //Parity (this is the "N" in "8-N-1")
$serial->confCharacterLength(8); //Character length (this is the "8" in "8-N-1")
$serial->confStopBits(1); //Stop bits (this is the "1" in "8-N-1")
// Ask Arduino what the Air Temp is.
$serial->deviceOpen();
$serial->sendMessage(chr(10)); // start transmission
$serial->sendMessage(chr(50));
$serial->sendMessage(chr(13)); // end transmission
$read = $serial->readPort(); // waiting for reply
$current_air_temp = $read;
$current_air_temp = (int)$current_air_temp;
$serial->deviceClose();//We're done, so close the serial port again
echo $current_air_temp;
//Dry Remote Arduino Dingle Dongle for GroPI5000 (CO2, Temp, Humidity, Flood Alert)
#include <SHT1x.h>
#define sht15dataPin 10
#define sht15clockPin 11
SHT1x sht1x(sht15dataPin, sht15clockPin);
int CO2Pin = 4; //CO2 Sensor Pin
int CO2SensorValue = 0; //CO2 Sensor Value
int CO2LevelValue = 0; //CO2 Level Value
int FloodValue = 0; //Flood Sensor value
int FloodPin = 5; //Flood Sensor Pin
volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
int hallsensor = 2; //The pin location of the sensor
int count = 0;
void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}
int incomingByte = -1;
int val = 0;
char code[10];
int bytesread = 0;
void setup() // run once, when the sketch starts
{
Serial.begin(9600);
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}
void loop() { // run over and over again
checkSerial();
}
void checkSerial() {
if(Serial.available() > 0) { // if data available
if((val = Serial.read()) == 10) { // check for header start
bytesread = 0;
while(bytesread<1) { // read 1 digit code
if( Serial.available() > 0) {
val = Serial.read();
if(val == 13) { // check for header end
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 1) { // if 1 digit read is complete
incomingByte = int(code[0]);
doLEDS();
}
bytesread = 0;
delay(50); // wait for a second
}
}
}
void doLEDS()
{
if (incomingByte == 50) { // php is asking for the Air Temp
float temp_f;
temp_f = sht1x.readTemperatureF();
Serial.println(temp_f); //printing the result
}
if (incomingByte == 44) { // php is asking for the CO2
CO2SensorValue = analogRead(CO2Pin);
CO2LevelValue = map(CO2SensorValue, 0, 1023, 0, 2000);
Serial.println(CO2LevelValue); //printing the result
}
} //END OF MAIN LOOP
If I run the PHP code It sends a request to arduino for data. arduino does its thing and serial.writes it back. PHP then echos it to the page. no big deal
however if I refresh the page it will return a zero. refresh again 0, refresh again and I get good data. different amounts of refreshes fix it. different amounts of refreshs break it
If I wantch the serial monitor on the arduino I can see it send out the correct data but PHP is somehow sometimes missing it.
Anybody seen anything like this? Ideas?