Help with sending strings with PHP to arduino

Hello,

First of all, forgive me for the long post, but I think I should give all the info before asking my question.

I am very new to PHP but with great help from this forum ( Thanks PaulS :slight_smile: ) I started my PHP project which basically involves Controlling Home equipment, Sending Messages to an LCD at home, Live webfeed over the web.
I have completed the first part of controlling some equipments at home. I made some buttons on a PHP page, and on clicking those buttons a number was sent to my Serial port. On basis of that code some action was done.
This is the code for PHP(very basic):

<html>
<head><title>Control Your Home</title></head>
<h1>WELCOME</h1>
<hr/><hr/>


<body bgcolor="#e4edff">


<?php 			//Start PHP block
exec("mode /dev/ttyS1: BAUD=9600 PARITY=N data=8 stop=1 xon=off"); 
//The following is a HereDoc that creates the form for the website. 
//' action = "$_SERVER[PHP_SELF]" redirects the webpage to itself after the submit button is clicked.
//The buttons are listed in the form of a table. isset() event is checked for the output to occur.
print <<<_HTML_

<form method="post" action = "$_SERVER[PHP_SELF]"> 	
<table border="1">
  <tr>
    <th>Device Name </th>
    <th>Action</th>
  </tr>
<tr>

<td><b>Device One</b></td>
<td><input name="dev1_on" type="submit" value="ON">
<input name="dev1_off" type="submit" value="OFF"></td>
</tr>
<tr>

<td><b>Device Two</b></td>
<td><input name="dev2_on" type="submit" value="ON">
<input name="dev2_off" type="submit" value="OFF"></td>
</tr>

<tr>

<td><b>Device Three</b></td>
<td><input name="dev2_on" type="submit" value="ON">
<input name="dev2_off" type="submit" value="OFF"></td>
</tr>
</table>
</form> 
_HTML_;

if(isset($_POST['dev1_on']))
{
$fp =fopen("/dev/ttyS1", "w");
fwrite($fp, 0);
//fwrite($fp, "yo");
fclose($fp);
}
else if (isset($_POST['dev1_off']))
{
$fp =fopen("/dev/ttyS1", "w");
fwrite($fp, 1);
fclose($fp);
}
else if (isset($_POST['dev2_on']))
{
$fp =fopen("/dev/ttyS1", "w");
fwrite($fp, 2);
fclose($fp);
}
else if (isset($_POST['dev2_off']))
{
$fp =fopen("/dev/ttyS1", "w");
fwrite($fp, 3);
fclose($fp);
}
else if (isset($_POST['dev3_on']))
{
$fp =fopen("/dev/ttyS1", "w");
fwrite($fp, 4);
fclose($fp);
}
else if (isset($_POST['dev3_off']))
{
$fp =fopen("/dev/ttyS1", "w");
fwrite($fp, 5);
fclose($fp);
}

?>
</body>
</html>

This is the corresponding arduino code(This was only for testing, so it is only for two of the inputs):

int ledPin = 13;
int number_in = 0;
void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);

}


void loop() {

  if (Serial.available() > 0) {

    number_in = Serial.read();

  }



  if (number_in ==1) {

    digitalWrite(ledPin, HIGH);
  }
 else if(number_in == 2){
       digitalWrite(ledPin, HIGH);
       delay(500);
        digitalWrite(ledPin, LOW);
       delay(500);
 }
 else
         digitalWrite(ledPin, LOW);
}

Now my problem

Now I want to send strings to my arduino so that I can read them. I thought I could send them in the same way as above because fwrite accepts strings so made the following changes to the above PHP code for the 1st button. but when I read the data on the serial monitor all I get is garbage.

//fwrite($fp, 0);
fwrite($fp, "yo");

This is the corresponding arduino code

int ledPin = 13;
char char_in[30];

void setup() {

  pinMode(ledPin, OUTPUT);

  Serial.begin(9600);

}


void loop() {

  if (Serial.available() > 0) {

  for(int i=0;i<30 && Serial.read();i++)  //Read untill i is less then 30 and there is a non zero serial data
  {
    char_in[i] = Serial.read();
    Serial.println(char_in[i]); // print the read data to serial port for verification
  }
  
  
}
}

I am not too sure about my arduino code for accepting the string as I have not done much serial communication.

What is the problem with this code? Please somebody guide me with this, I want to make it work :(:frowning:

for(int i=0;i<30 && Serial.read();i++) //Read untill i is less then 30 and there is a non zero serial data
{
char_in = Serial.read();[/quote]
If there is serial data, the loop might execute, with i = 0. To see if it will, i is compared to 30, and the next character is read from the serial port. If i is less than 30, and there was a character (that gets thrown away), read and store the next character, even if there is nothing there to read.
Was that what you intended?

Thanks PaulS

That was definitely not what I intended to do :zipper_mouth_face: :zipper_mouth_face: :blush: :blush: :frowning:

I will work on the arduino code again to correct the mistake and see how other people recieve strings serially. I guess I should use serial.peek for this.

Is the PHP part OK???

Every time you open and close the port, the Arduino resets. That's not really conducive to long term control of household appliances, etc.

Hello PaulS,

My final step for this project is going to be removing the bootloader from the atmega chip and burning the program directly and then taking out tha atmega and placing it in a dedicated circuit.

Will the reset problem happen even when i remove the bootloader?

And is the PHP code for sending the string correct if i want to do it this way anyways?

Thanks for your help btw :slight_smile:

Also, if I use dio functions will the arduino reset problem still occur?
Exactly why is it resetting?

Also, if I use dio functions will the arduino reset problem still occur?

Pretty hard to do direct input/output to the serial port without opening the serial port first. That causes a reset.

When you are done writing, you close the port. That causes another reset.

Exactly why is it resetting?

It's the way the board is wired.

Is the auto reset thing associated with arduino or the atmega?
Beacase in the end of the project i am going to remove the bootloader and arduino circuit and use a dedicated circuit and program the atmega without the bootloader.

Is the auto reset thing associated with arduino or the atmega?

The Arduino, and there are ways of circumventing it, with resistors, diodes, and/or capacitors.

Or proper coding on the PC side.

Thanks PaulS. Right now, all the things are going smoothly. As soon as the work is complete, I will switch to a dedicated circuit as I am keeping my board for the prototyping work. So, hopefully then also the things will work out.

Do I have to keep something special in mind when placing an atmega programmed by arduino in a dedicated board or I can do it just like any other programmed atmega?

Once programmed, the chip has no idea where the program came from.

Sorry to bring this topic up again and again, I have one last question.

Is it possible to use same PHP code( i.e. the fopen-fwrite-fclose procedure) on a windows machine to access the serial ports(after entering correct port name, of course) or will I have to use dio functions or PHP-serial. I am asking this because in linux the devices are treated as files so accessing them with fopen-fwrite-fclose procedure seems normal but I don't know much about windows.
I am asking this instead of checking because I do not have access to a windows machine, and would want to know if the code is portable. I have given the code to a friend to check but he doesn't know much about it thats why I am consulting here first.

Is there a permissions issue with accessing serial port on Windows with PHP as with linux.
Anything else I need to know when porting my server to windows?

On windows, PHP-serial can write to the serial port. It can not read from the serial port. Some limitation in Windows.

Thanks PaulS, I wanted to know if fopen,fwrite,fclose procedure would work with windows. I just want to know if my current code is portable.