Go Down

Topic: PHP + Arduino (Read 7 times) previous topic - next topic

dario111cro

I'm trying with Arduino and PHP. I can turn led on, but I can't turn it off.

Arduino:
Code: [Select]
const int ledPin = 9;
int incomingByte;

void setup() {
 Serial.begin(9600);
 pinMode(ledPin, OUTPUT);
}

void loop() {
 if (Serial.available() > 0) {
   incomingByte = Serial.read();
   
   if (incomingByte == 'L') {
     digitalWrite(ledPin, LOW);
   }
   if (incomingByte == 'H') {
     digitalWrite(ledPin, HIGH);
   }
 }
}


PHP:
Code: [Select]
<?php
include "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("COM7");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(1);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$serial->sendMessage('H');
$serial->deviceClose();
?>


I tried to invert H and L in Arduino code (H was turning off and L was turning on). With inverted code I can't turn it on or off (other words, nothing was working). That tells me that letter "L" is not working. I replaced it with S and now I can turn led on and off. But why letter L doesn't work?

Thank you very much.  ;)

PaulS

Code: [Select]
$serial->deviceOpen();
$serial->sendMessage('H');
$serial->deviceClose();

Every time you open the serial port, the Arduino resets. You need some delay between opening the serial port and sending data, to allow the Arduino time to reboot.

dario111cro

Oh. That will complicate things. :(

Ok. I tried it. Still not working. It looks like I can send some letters and some I can't. Php code:
Code: [Select]
<?php
include "php_serial.class.php";
$serial = new phpSerial;
$serial->deviceSet("COM7");
$serial->confBaudRate(9600);
$serial->confParity("none");
$serial->confCharacterLength(6);
$serial->confStopBits(1);
$serial->confFlowControl("none");
$serial->deviceOpen();
$serial->sendMessage();
sleep(5);
$slovo $_POST["slovo"];
$serial->sendMessage($slovo);
sleep(5);
$serial->deviceClose();

header("Location: led.html");
?>


But, thank you very much.

PaulS

Code: [Select]
$serial->confCharacterLength(6);
If this is setting the number of bits to send, the value should be 8.

dario111cro

Well, I got it :D
Code: [Select]
<?php
require("php_serial.class.php");

function 
Arduino($str)&#123;
      
$serial = new phpSerial();
      
$serial->deviceSet("COM7");
      
$serial->confBaudRate(9600);
      
$serial->confCharacterLength(8);
      
$serial->deviceOpen();

      for (
$i=0$i<strlen($str); $i++)&#123;
            
$serial->sendMessage($str[$i]);
      &
#125;
    
$serial->deviceClose();
&
#125;
Arduino($_POST['slovo']);

header("Location: led.html");
?>


Go Up