Another PHP and Arduino - Sending more than one character to Arduino via serial

I know this has been discussed many times before on here but I can’t seem to find the answer I am looking for:

I have built an X10 system using PHP and Arduino and I can’t seem to get it to work right. I started off by just using the A HouseCode and the Serial.read(); to get a single character but I would like to expand this. So, I have been looking at some of the samples and tutorials to get a string passed from PHP to Arduino but nothing works. Is this even possible? I would like to pass A4 instead of just A to Arduino and have it actually understand what I am sending to it.

I am posting all of my stuff so whomever will help can see exactly what I have going on - such as it is. Am I pretty much total newbie with Arduino.

My sketch (which is based off of the SerialEvent example):

#include <x10.h>

const int rxPin = 3;    // data receive pin
const int txPin = 4;    // data transmit pin
const int zcPin = 2;    // zero crossing pin

boolean a1 = false;
boolean a2 = false;
boolean a3 = false;
boolean a4 = false;

String inputString = "";         // a string to hold incoming data
boolean stringComplete = false;  // whether the string is complete


String a1s,a2s,a3s,a4s;



void setup() {
  // initialize serial:
  Serial.begin(9600);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
  
  x10.begin(rxPin, txPin, zcPin);

  
  a1s = String("A1");
a2s = String("A2");
a3s = String("A3");
a4s = String("A4");
}


void stoptrop(){
   x10.write(OFF);
}

void startop(){
   x10.write(ON);
   delay(10);
   x10.write(ON);
   
}




void firex10(){
    if(inputString == a3s+'\n'){
      x10.beginTransmission(A);
      x10.write(UNIT_3);
      if(a3){  
        stoptrop();   
        a3 = false;
      }else{
        delay(10);
        startop();
        a3 = true;
       }
    }
   
    if(inputString.equals(a4s+'\n')){
      x10.beginTransmission(A);
      x10.write(UNIT_4);
      if(a4){  
        stoptrop();   
        a4 = false;
      }else{
        delay(10);
        startop();
        a4 = true;
       }
    }
    
    x10.endTransmission(); 
   
 }



void loop() {
  // print the string when a newline arrives:
  if (stringComplete) {
   /* Serial.println(inputString); 
*/

   firex10();
   

    // clear the string:
    inputString = "";
    stringComplete = false;
  }
}


 
 
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
     
     
    } 
  }
}

and my PHP:

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

error_reporting(E_ALL); 
ini_set("display_errors", 1);  
$read = "";
$msg ="";
if (isset($_GET['action'])) {
$response = $_GET['action'];

    
sender($response );




}


function sender($response ){
        $serial = new phpSerial();
        $serial->deviceSet("COM3");
        $serial->confBaudRate(9600);
		$serial->confParity("none");
		$serial->confCharacterLength(8);
		$serial->confStopBits(1);
		$serial->confFlowControl("none");
        $serial->deviceOpen();
		//sleep(2);

$serial->sendMessage($response);
$serial->deviceClose();


}

?>

<div><a href="index.php?action=A1">Master Bedroom Lamp</a></div>
<div><a href="index.php?action=A2">Lady Lair Death Lamp</a> </div>
<div><a href="index.php?action=A3">Dining Room Tall Lamp</a> </div>
<div><a href="index.php?action=A4">Bar Lights</a></div>

Any help at all would be greatly appreciated.

Not sure what does and what doesn't work.
I have uploaded my serial string reader library to github. That shows how you can easily read from the serial monitor and get line by line feedback.

I added a example so you know how to use it.
I hope this helps
Best regards
Jantje

I appreciate your help. I did look at it but I can’t figure out how to use it properly - I guess. I can read from the serial monitor just fine - what I can’t do is read from PHP over serial fine … at all.

I took your library and tried to use it but it doesn’t help me get what I need from the PHP.

This is what I did:

#include <x10.h>

const int rxPin = 3;    // data receive pin
const int txPin = 4;    // data transmit pin
const int zcPin = 2;    // zero crossing pin

boolean a1 = false;
boolean a2 = false;
boolean a3 = false;
boolean a4 = false;

#include <SerialStringReader.h>
SerialStringReader mySerialReader;
void setup()
{
	Serial.begin(9600);
	mySerialReader.setup();
        x10.begin(rxPin, txPin, zcPin);
}
      
void stoptrop(){
   x10.write(OFF);
}

void startop(){
   x10.write(ON);
   delay(10);
   x10.write(ON);
   
}
void loop()
{
	mySerialReader.loop();
	if (mySerialReader.messageReceived())
	{
		//Serial.println("You have send a message to Arduino.");
		//Serial.print("And the message is: ");
		Serial.print(mySerialReader.getMessage());
          if(strcmp(mySerialReader.getMessage(),"123")){
               
                x10.beginTransmission(A);
                x10.write(UNIT_4); 
                if(a4){  
                   stoptrop();   
                   a4 = false;
                }else{
                  delay(10);
                   startop();
                   a4 = true;
                }
               Serial.println("I got: ");
            }



	}
}

What am I doing wrong?

jgibbens:
What am I doing wrong?

From a helping perspective you are not clearly explaining what your problem is. Is the message properly send to arduino? is Arduino receiving the (full?) message or only partially. Is arduino sending the required reply? Is the PHP receiving a reply (which must be malformed as you say it doesn't work)?
So break your problem is smaller pieces and test each piece. Then you must find a piece that doesn't work. For instance: I can send characters from PHP but not words or phrases containing spaces ....
I hope this helps you out
Best regards
Jantje

I apologize for not being clear enough: I am sending a 2 character message (for instance: AA) to Arduino via PHP. Now, I don't know what PHP is sending because I can't find any information on the subject but Arduino will work fine if I send AA from the serial monitor and not at all with anything over one character from PHP. Basically I want to send a string from PHP to Arduino. I want Arduino to receive that string (or array of characters - whatever gets the job done).

I was hoping someone here had experience with the subject because I am lost, lol.

OK I figured it out if anyone is interested. I have to listen for both characters:

if (Serial.available() >0) {
            bitOne = Serial.read();
           if (Serial.available() >0) {
           bitTwo = Serial.read();
          }

And then I can do something like:

if(bitOne == 'A' && bitTwo == 'c'){
}

I'm happy to see you have found a solution to your problem.
I have to disappoint you though because you havn't really pinpointed the problem.
You say

jgibbens:
I am sending a 2 character message (for instance: AA) to Arduino via PHP.

This is correct and I think that it is the root cause of your problem. Let me explain
If you only send "1 character messages" you get the whole message with one serial read. This is easy and simple.
If you only send "2 character messages" you get the whole message with 2 serial reads. This is what your new code does.
However your code will block when only one character is send.

The real problem is: what if you want to mix 1, 2 ....n character messages.
The solution to do so is adding a delimiter to the message. There are plenty of delimiters in use; some common are: a space, a semi colon, new line, carriage return ....
Assume we use Carriage return and show it as [cr].
In that case you do not send 2 characters (AA)but 3 (AA[cr])
This way you can send A[cr]AA[cr] but also "This is a stupid message ended with a carriage return.[cr]" and the receiver will know the difference between A.....A and AA as the first will be A[cr]A[cr] and the second AA[cr].
I hope you get the point.

The library I have posted before works with carriage return and/or newline as delimiter. The code you posted should work fine if you send 123[cr]. or 123 [nl] or 123[cr][nl].

Best regards
Jantje