2013-05-22 57 views
0

我有一个arduino Uno通过USB连接到我的笔记本电脑。我在Windows 7上运行WAMP webserver。我安装了python 2.7和py serial。我写了一个HTML,其中点击按钮将调用led1.py(python脚本)。 python脚本会和arduino进行通信,然后用户按下另一个按钮来放下Led。 按钮按下调用的python脚本时,LED是越来越上,但随后的HTML页面给了一个错误:Python和arduino串行通信

Internal Server Error;
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, [email protected] and inform them of the time the error occurred, and anything you might have done that may have caused the error. More information about this error may be available in the server error log.

我要去哪里错了?该HTML代码如下:

<html> 
    <head> 
     <title>Sample Web Form</title> 
    </head> 
<body> 

<h1>Fill Out This Form</h1> 

<form action="/cgi-bin/led.py" method="POST"> 
    <input type="submit" name='action' value="LEFT"> 
    <input type="submit" style="background-color:yellow" name='action' value="LEFT"/><br><br><br> 
    <input type="submit" style="background-color:yellow" name='action' value="BACK"/> 

</form> 

</body> 
</html> 

的Python代码如下:

#!python 
import serial 
import time 
keyword =form.getvalue('action') 
arduino = serial.Serial('COM4', baudrate=115200, bytesize=8, parity='N', stopbits=1, timeout=1) 
arduino.open() 
arduino.isOpen() 
time.sleep(5) # waiting the initialization... 
print("initialising") 
while True: 
    if keyword == 'LEFT': 
     arduino.write("H\n") # turns LED ON 
     break 
    elif keyword == 'BACK': 
     arduino.write('L\n') # turns LED OFF 
     break 
    elif break 
arduino.close() #say goodbye to Arduino 

和Arduino的代码非常简单:

int redpin =13; 
int incomingbyte; 

void setup() 
{ 
    Serial.begin(115200); 
    pinMode(redpin,OUTPUT); 
    pinMode(greenpin,OUTPUT); 
    pinMode(fanpin,OUTPUT); 
    } 

void loop() 
{ 
    if(Serial.available()>0) 
    { 
    incomingbyte=Serial.read(); 
    } 
    if(incomingbyte == 'H') 
    { 
    digitalWrite(redpin,HIGH); 
    } 
    if(incomingbyte == 'L') 
    { 
    digitalWrite(redpin,LOW); 
    } 
} 

能否请你告诉我在哪里我错了吗?我是python的新手。 另外我想使用python在同一个HTML页面中显示来自arduino传感器的数据。这怎么可能。我可以有一个完整的HTML和Python的小程序。

+0

“我可以有一个完整的小程序...”你的意思是,你想让别人为你写这个吗?请求时是否免费编写软件?我会指出一些问题。 'form'是未定义的。 'elif break'应该是'else break'。就服务器而言,三个“”中的两个是相同的。 –

+0

如何定义表单?我已经删除了一个输入语句。仍然有错误 –

回答

1

如果python脚本的内容是cgi-bin/led.py的内容,它看起来像这样:

7 print "Content-type: text/html" 
    8 print 
    9 
    10 print """ 
    11 <html> 
    12 
    13 <head><title>Sample CGI Script</title></head> 
    14 
    15 <body> 
    16 
    17 <h3> Sample CGI Script </h3> 
    18 """ 

http://wiki.python.org/moin/CgiScripts

你缺少的python脚本的头。

+0

单击提交按钮后,python脚本正在工作,但控件不会回到HTML。 –

+0

所以python脚本的内容是'cgi-bin/led.py'的内容吗? – User