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 ) 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 :(