Logging with PHP and MySQL without ethernet shield

Hi Folks,

I have Arduino Uno and a temp sensor on it. It is connected through USB serial link to my computer and what I am trying is to log the sensor results with PHP and MySQL. I read a similar approach with ethernet shield and set the arduino as a web server.

My question is, is it possible to do that without ethernet shield. I have written a sketch which works fine and it outputs the data on the serial monitor, but I have no success reading it through PHP (I am using the php serial class from Rémy Sanchez)

Any help or thought are appreciated.

Regards!

My question is, is it possible to do that without ethernet shield.

Yes, but not on Windows. So, what operating system are you using?

It is quite possible to write an application using another language, like C#, to read the serial data and record it in a database, including a MySQL database. I have one now that collects temperature and humidity data, and logs it in a SqlServer database (a Microsoft product). I have other C# and C++ code that interfaces with MySQL, so it would be possible to create a serial port reader that wrote to a MySQL database.

but I have no success reading it through PHP (I am using the php serial class from Rémy Sanchez)

This implies that you are on Windows...

Thanks for the fast reply.

Actually I am on Mac OS and I am using the modifed version of the class which works also with Mac OS.

I have good knowledge on PHP and that's why I would prefer to write it with PHP. Do you think it is possible, and if you have a link to some resources I will appreciate to share it with me.

Thanks

Actually I am on Mac OS and I am using the modifed version of the class which works also with Mac OS.

Then I see no reason why you shouldn't be able to read from the serial port in PHP. If you can't, it might be a coding issue. Coding issues only get resolved when you post the code.

well, I am using regular

Serial.print('text');

statements, but I also encountered that when the serial monitor is turned off, the PHP script is loading all the time and it does not work, I suppose. When the serial monitor is on and it just goes through the code and does nothing.

I will post the whole code in few minutes.

Regards

#include <Wire.h>
void setup()
{
  Serial.begin(115200);
  
  Wire.begin();
}

void loop()
{
 
  Serial.print("Temperature: ");
  Serial.println("75 *F");
  
  delay(1);
   
}

I simplified the code a lot and it works now. I think I found what was wrong. I have set up a long delay of 1000 and it output nothing. Now with no delay or 1 it sends data to the DB.

Thank you very much for the help!

Now I have another question, how could I determine in the sketch when the php script is requesting data ?

I simplified the code a lot and it works now. I think I found what was wrong. I have set up a long delay of 1000 and it output nothing. Now with no delay or 1 it sends data to the DB.

Shortening the delay to 1 millisecond is not what fixed the problem. Something else did.

Now I have another question, how could I determine in the sketch when the php script is requesting data ?

How is the PHP script requesting data? Does it send some data to the serial port saying "Hey, Arduino, wake up. I'm ready to do something!". If not, it should. It probably goes without saying that the message doesn't have to be that long... A simple ">" would do.

If the Arduino sees that there is serial data, and that the character is '>', it should send some data.

I implemented the serialEvent() method and what I understand is the approach I am looking for. php send data to request info from arduino and arduino sends the data back.

Here is the code:

bool get_data = false;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  
 if (get_data) { 

  Serial.println("Temperature: 75 *F");
  
 }
  
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    
    char inChar = (char)Serial.read();
    // add it to the inputString:
    //inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == 'r') {
      //Serial.print(inChar);
      get_data = true;
    }
  }
}

Unfortunately, it sends back nothing and I am stuck again.

What am I doing wrong now?

What happens when you open and send data from the Serial Monitor, instead of the PHP script (which you haven’t shown us)?

the php code is standart from the serial php script: the class is decalared as a CodeIgniter library

$this->phpserial->deviceSet("/dev/tty.usbmodem411");


// We can change the baud rate
$this->phpserial->confBaudRate(115200);     


// Then we need to open it
$this->phpserial->deviceOpen();

// To write into
$this->phpserial->sendMessage("r");

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

$line = $this->phpserial->readPort();

print_r($line);

// If you want to change the configuration, the device must be closed
$this->phpserial->deviceClose();

Neither from the serial monitor nor from php is showing anything

Neither from the serial monitor nor from php is showing anything

Well, at least with the Serial Monitor, you have the option of adding a lot more debug output. Print a message when the serialEvent() handler is called. Echo the character that was received.

By the way, after sending the data, you may want to set get_data to false again, to prevent the continuous sending of data.

What Arduino are you using?

// Then we need to open it
$this->phpserial->deviceOpen();

// To write into
$this->phpserial->sendMessage("r");

Have you modified it to prevent reset when opening the serial port? If not, you need to add a pause after opening the port, before sending data to it, to allow time for the Arduino to reset and be ready to read the data.

Thank you very much for the help.

I am using Arduino UNO

I managed to do what I was looking for with just the following code:

void loop()
{

  
 if (Serial.available() > 0) {
  
   
  inByte = Serial.read();
  
  
  if(inByte == 'r'){
  
     Serial.print("Temperature: 75* F");
     delay(10);

   }
 }
  
}

and it works. If it gets 'r' it outputs the data, but now it works only with serial monitor.

When I execute the php script it ouputs the result not in the browser, but again on the serial monitor :~

Have you modified it to prevent reset when opening the serial port?

How should I prevent that reset ?

Hello friends, I’m new to arduino. I’m trying to make a distance reader in PHP using HC-SR04 ultrasonic sound sensor with a arduino (without ethernet shield). Basically I need to show distance data on the PHP page. Although I tried the following method it didn’t work :confused: . How should I capture and display this sensor data using PHP in windows.

Arduino Code:

/* HC-SR04 Sensor

  • TRIG connection of the sensor attached to digital pin 2
  • ECHO connection of the sensor attached to digital pin 4
    This example code is in the public domain.
    */

const int trigPin = 2;
const int echoPin = 4;

void setup() {
Serial.begin(9600);
}

void loop()
{
long duration, cm;

pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);

cm = microsecondsToCentimeters(duration);

Serial.print(cm);
Serial.print(“cm”);
Serial.println();

delay(1000);
}

long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}

PHP Code:

<?php exec("mode COM32 BAUD=9600 PARITY=N data=8 stop=1 xon=off"); $fp = fopen("com32", "w"); if (!$fp) { echo "Not open"; } else { sleep(10); echo "Port Open"; $string = fgets($fp); echo $string; fclose($fp); } ?>

info:
Port : COM32
PHP version : 5.5.15
Platform : Windows 8.1

Maybe things have changed, but the original windows PHP could not read from a com port.