Arduino and PHP via serial input byte

I have a PHP script to control an Arduino diode through a website, but I have a problem.

My Arduino Code:

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 is:

 <!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 have two problems:

  • Arduino only receives the input byte = 0, so I can turn on the diode, but I can not turn it off. I modified the code to set the incoming byte = 1 to turn on the diode, but it also does not work. Therefore, I think that Arduino only receives incoming byte = 0.

  • My site goes down after running the script. When I click on the "ON" or "OFF" script, and I get a white (empty) site. What should I do to always stay on my HTML site?

+4
source share
2 answers

re: 2 Add html code to your php form handler - so everything is served using the same script or use

 header() 

to move back to the html page, but you cannot display errors.

EDIT to make it a single file:

 <?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 to make the solution more understandable. Please note that you do not need to add led.php to links, they are sent back to the same file.

+4
source

Hey i just figured out two important changes for ur code ....

1> change

 $serial->sendMessage("0\r"); 

to

 $serial->sendMessage('0'); 

and for sending '1'.

2> enable sleep.here command like this

 $serial = new phpSerial(); $serial->deviceSet("COM3"); $serial->confBaudRate(9600); $serial->deviceOpen(); sleep(2); 

sleep command inserts a delay. when serial port opens php in

 $serial->deviceopen(); 

The arduino team gets an auto-grill. this way when php executes the following commands that arduino will not be able to receive them and therefore will not be able to take action. 2 is the best delay, since 1 will be very short, while others more than 2 will be long. use the php and html code in the same file as in the above message and name it "name.php" without "". it worked for me ....

0
source

Source: https://habr.com/ru/post/1480222/


All Articles