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");
?>


dario111cro

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?

PaulS

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!

dario111cro

#7
Jan 18, 2011, 09:26 pm Last Edit: Jan 18, 2011, 09:34 pm by dario111cro Reason: 1
Good idea :D

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?

PaulS

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.

dario111cro

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

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

I tried and I can't get it.
Code: [Select]
#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.

PaulS

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: [Select]
if(serial_last != serial) {
       serial_last = serial;
       array[i] = serial;
       i++;
     }

Are the answers what you expected?

dario111cro

Ahhh...
Code: [Select]
#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.

PaulS

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.

Go Up