I've got PHP script for control Arduino's diode via website, but I've got a problem.
My Arduino code is:
int green = 8;
int incomingbyte;
void setup()
{
Serial.begin(9600);
pinMode(green,OUTPUT);
}
void loop()
{
if(Serial.available() > 0)
{
incomingbyte = Serial.read();
}
if(incomingbyte == '0'){
digitalWrite(green,HIGH);
}
if(incomingbyte == '1'){
digitalWrite(green,LOW);
}
}
My PHP code is:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (isset($_GET['action'])) {
require("php_serial.class.php");
$serial = new phpSerial();
$serial->deviceSet("COM3");
$serial->confBaudRate(9600);
$serial->deviceOpen();
if ($_GET['action'] == "green1") {
$serial->sendMessage("0\r");
} else if ($_GET['action'] == "green0") {
$serial->sendMessage("1\r");
}
$serial->deviceClose();
}
And my HTML code:
<!DOCTYPE html>
<html>
<head>
<title>ARDUINO</title>
</head>
<body>
<h1> ARDUINO AND PHP COMMUNICATION </h1>
<a href="led.php?action=green1">ON</a></br>
<a href="led.php?action=green0">OFF</a></br>
</body>
</html>
I've got two problems:
Arduino is getting only incomingbyte = 0, so I can turn diode on, but I cannot turn it off. I modified code to set incomingbyte = 1 to turn diode on, but it's not working also. So I think Arduino is getting only incomingbyte = 0.
My website is shuting down after running script. When I click on "ON" or "OFF" script is running and I'm getting white (blank) site. What should I do to stay all the time on my HTML site?
re: 2 Add the html code under your php form handler - so everything is served from the same script, or use
header()
to relocate back to the html page - but then you cannot output errors.
EDIT so to do it the single file way:
<?php
// led.php code in here
error_reporting(E_ALL);
ini_set("display_errors", 1);
if (isset($_GET['action'])) {
// and so on ...
?>
<!--// now show your html form regardless
of whether the form was submitted or not // -->
<!DOCTYPE html>
<html>
<head>
<title>ARDUINO</title>
</head>
<body>
<h1> ARDUINO AND PHP COMMUNICATION </h1>
<a href="?action=green1">ON</a></br>
<a href="?action=green0">OFF</a></br>
</body>
</html>
Edited to try and make the solution a bit clearer. Notice you do not have to add led.php to the links, they are submitted back to the same file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With