Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: xyluz on Jan 25, 2018, 04:13 pm

Title: Send Data From Arduion to a PHP Page
Post by: xyluz on Jan 25, 2018, 04:13 pm
I am new to Arduino, but not new to PHP.

I am trying to create a simple connection between PHP and Arduino, so far, I have seen lots of examples of how to connect to Arduino from a PHP web page, but now, what I am trying to do is, when a button is pressed on my Arduino board, a message is sent to PHP (the message will be used to take on different actions).

So far, I have been very unsuccessful at achieving this.

I'll appreciate any help and if possible specific examples.

PS:  I am able to read the port from my terminal... I just don't know how to get PHP to read it. ScreenShot Attached.


MY PHP Code

Code: [Select]

<?php

include "phpserial.php";

$serial = new phpSerial();    
$serial->deviceSet("/dev/cu.usbmodem1411");

$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") 
$serial->confFlowControl("none");
$serial->deviceOpen(); 

var_dump($serial->readPort());
// $serial->sendMessage(chr(10)); // start transmission
// $serial->sendMessage(1);
// $serial->sendMessage("0\r"); //THIS IS WHAT I SEND
// $serial->sendMessage(chr(13)); // end transmission

$serial->deviceClose();


In my Arduino Code, I am using
Code: [Select]
Serial.println("ready!"); to print to the serial port

Thank you.
Title: Re: Send Data From Arduion to a PHP Page
Post by: ieee488 on Jan 25, 2018, 06:57 pm
No matter which language you use, you must wait after you open the serial port before sending anything to the Arduino. 250 ms should be adequate.
Title: Re: Send Data From Arduion to a PHP Page
Post by: PaulS on Jan 29, 2018, 02:22 pm
Opening the port, which resets the Arduino, reading what the Arduino has sent before it has time to send anything, and closing the port doesn't make a lot of sense to me.

You need to use a while loop, after opening the port, to read everything that the Arduino (eventually) sends, until it sends "Go away", or something more (or less) friendly.