Pages: [1]   Go Down
Author Topic: PHP + Arduino  (Read 7229 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Arduino:
Code:
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:
<?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.  smiley-wink
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
$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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh. That will complicate things. smiley-sad

Ok. I tried it. Still not working. It looks like I can send some letters and some I can't. Php code:
Code:
<?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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, I got it smiley-grin
Code:
<?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");
?>
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, as you see that PHP code is sending one character at time. So I need to store all characters in a string. I think it's possible by saving in array with for loop and than converting array into string. I tried, but I can't make it. Actually, I can, but only if i know number of characters. Can someone suggest me something?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Actually, I can, but only if i know number of characters. Can someone suggest me something?
Sure. Send some known end-of-packet marker from PHP. Have the Arduino just keep reading characters until that marker arrives.

This is a packet!
Stop reading when the exclamation point arrives!
Then, do something with the data received!
Make sure to check for room in the array, before adding the new character!
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good idea smiley-grin

I decided for EOT (End Of Transmission). It's number 4 in ASCII. For php side I can use chr. But how can Arduino detect that character?

http://en.wikipedia.org/wiki/End-of-transmission_character


EDIT: Oh stupid me. Arduino is reading integer (or ASCII). So simple if will do a job?
« Last Edit: January 18, 2011, 03:34:40 pm by dario111cro » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
So simple if will do a job?
I recognized each and every one of those words. Comprehending what they mean in that order escapes me.

If you are asking whether EOT/4 can be used as the end-of-packet marker, the answer is yes. I prefer a visual character, for printing purposes, but the marker does not have to be a printable character.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Haha. I read that sentence again.  :o  :smiley

So I meant: Will "if" do a job? xD

I tried and I can't get it.
Code:
#include <LiquidCrystal.h>

const int ledPin = 9;
int serial;
int serial_last;
int stop_bit = false;
int i = 0;
int array[0];
int result;
int check = false;

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

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

void loop() {
  if (Serial.available() > 0) {
    serial = Serial.read();
    if(serial == 4 and !stop_bit){
      stop_bit = true;
    } else {
      if(serial_last != serial) {
        serial_last = serial;
        array[i] = serial;
        i++;
      }
    }
  }
  if(stop_bit)
  {
    if(!check) {
      for (int x=0; x < i; x++){
         result += array[x];
      }
    check = true;
    }
    if(result == 'ON') {
      digitalWrite(ledPin, HIGH);
    } else if(result == 'OFF') {
      digitalWrite(ledPin, LOW);
    } else if (result != 0) {
       lcd.write(result);
       serial = 0;
    }
  }
}

Probably stupid mistake.  ;D

Thank you for helping.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Take pencil and a piece of paper. Write down the values that should be in serial, serial_last, i, and array, when the string "OFF" is sent to the serial port.
Code:
if(serial_last != serial) {
        serial_last = serial;
        array[i] = serial;
        i++;
      }
Are the answers what you expected?
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 41
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ahhh...
Code:
#include <LiquidCrystal.h>

const int ledPin = 9;
int serial;
int stop_bit = false;
int i = 0;
int array[0];
int result;
int check = false;

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

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

void loop() {
  if (Serial.available() > 0) {
    serial = Serial.read();
    if(serial == 4){
      stop_bit = true;
    } else {
      array[i] = serial;
      i++;
    }
  }
  if(stop_bit)
  {
    if(!check) {
      for (int x=0; x < i; x++){
         result += array[x];
      }
    check = true;
    }
    if(result == 'ON') {
      digitalWrite(ledPin, HIGH);
    } else if(result == 'OFF') {
      digitalWrite(ledPin, LOW);
    } else if (result != 0) {
       lcd.write(result);
       serial = 0;
    }
  }
}

Now when I write "dsa" I get only dddddddddddddddd on my lcd. :/

How I can calculate number of bits? That's only thing I neec.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49343
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now when I write "dsa" I get only dddddddddddddddd on my lcd.
Once stop_bit gets set to true, it never gets set back to false.

Once check is set to true, it never gets set back to false.

Once I gets set, it never gets set back to 0.

Once array
  • gets set, it never gets overwritten.

See a pattern? See a solution?

I don't see why the repeating ds happen, though. Fix the issues here, and see if that makes a difference.
Logged

Pages: [1]   Go Up
Jump to: