PHP + Arduino

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

Arduino:

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:

<?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. :wink:

$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.

Oh. That will complicate things. :frowning:

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

$serial->confCharacterLength(6);

If this is setting the number of bits to send, the value should be 8.

Well, I got it :smiley:

<?php
require("php_serial.class.php");

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

      for ($i=0; $i<strlen($str); $i++){
            $serial->sendMessage($str[$i]);
      }
    $serial->deviceClose();
}
Arduino($_POST['slovo']);

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

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?

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!

Good idea :smiley:

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?

EDIT: Oh stupid me. Arduino is reading integer (or ASCII). So simple if will do a job?

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.

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

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

I tried and I can't get it.

#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.

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.

if(serial_last != serial) {
        serial_last = serial;
        array[i] = serial;
        i++;
      }

Are the answers what you expected?

Ahhh...

#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. :confused:

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

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[x] 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.