CODE ON ARDUINO :
const int green=10;
const int blue=9;
const int red=2;
void setup()
{
pinMode(green,OUTPUT);
pinMode(blue,OUTPUT);
pinMode(red,OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available())
{
light(Serial.read()-'0');
}
delay(500);
}
void light(int n){
switch(n)
{
case 1:
digitalWrite(green,HIGH);
delay(1000);
digitalWrite(green,LOW);
delay(1000);
break;
case 2:
digitalWrite(blue,HIGH);
delay(1000);
digitalWrite(blue,LOW);
delay(1000);
break;
case 3:
digitalWrite(red,HIGH);
delay(1000);
digitalWrite(red,LOW);
delay(1000);
break;
}
}
code on raspberry pi :
lec.py
#!/usr/bin/env python
import socket
import serial
import time
import subprocess
dev =subprocess.check_output('ls /dev/ttyACM*',shell=True)
print dev
try:
ser = serial.Serial(dev.strip(),9600)
print "Arduino Connected"
except:
print "Arduino not connected"
def server():
global ser
while True:
conn, addr = s.accept()
print 'Connection address:', addr
data = conn.recv(BUFFER_SIZE)
if not data: continue
print "received data:", data
if data == '1':
conn.send("Blue light Glowing")
conn.close()
ser.write('1')
time.sleep(1)
elif data == '2':
conn.send(" Red light blowing")
conn.close()
ser.write('2')
time.sleep(1)
elif data == '3':
conn.send(" Green light blowing")
conn.close()
ser.write('3')
time.sleep(1)
elif data == '7':
conn.send('bye bye')
conn.close()
ser.close()
exit(0)
else :
ser.write(data)
aa = ser.readline()
time.sleep(0.1)
print aa
conn.send(aa)
conn.close()
TCP_IP = '192.168.2.100'
TCP_PORT = 5005
BUFFER_SIZE = 20
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
s.bind((TCP_IP, TCP_PORT))
s.listen(5)
print 'server started'
server()
and i have made two php files which goes as follows
file1.php
file 2.php<html>
<form action="welcome.php" method="post">
Enter Command: <input type="text" name="name"><br>
<input type="submit">
</form> </html>
<html>
<body>
Command is <?php echo $_POST["name"]; ?><br>
<?php
$command=$_POST["name"];
$a = "python /home/sid/client.py ";
$b= $a.$command;
$last_line = system($b, $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line;// . '
//<hr />Return value: ' . $retval;
?> </body></html>
and python file for summing everything up
#!/usr/bin/env python
import socket
import sys
TCP_IP = '192.168.2.100'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = str(sys.argv[1])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(MESSAGE)
data = s.recv(BUFFER_SIZE)
s.close()
print "received data:", data
when i run the code which is located on rpi which is lec.py , it works good. and it is waitng for the response to come. but when run the php code from my localhost and go through 2 pages i.e signal.php and welcome.php from my pc .. then nothing happens. whereas it should turn on the lights which are connected to arduino.
please help.