Python and arduino serial communication

我与影子孤独终老i 提交于 2019-12-13 06:26:45

问题


I have a arduino Uno connected to my laptop through USB. I am running WAMP webserver on windows 7. I have python 2.7 and py serial installed. I wrote a HTML where the buttons when clicked will invoke the led1.py (python script). The python script would communicate with the arduino to put on a led and then the user would press another button to putt off the Led. The buttons when pressed are invoking the python script, the led is getting on, but then the HTML page is giving an error:

Internal Server Error;
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator, admin@localhost 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.

Where am I going wrong? The HTML code is as follows:

    <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>

The Python code is as follows:

#!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

and the Arduino code is very simple:

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);
  }
}

Can you please tell me where am i going wrong?? I am new to python. Also I want to display data from arduino's sensors in the same HTML page using python . How can that be possible. Can I have a complete small program of both HTML and python for this.


回答1:


If the content of the python script is the content of cgi-bin/led.py, it has to look like this:

   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 """

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

You are missing the header in the python script.



来源:https://stackoverflow.com/questions/16684977/python-and-arduino-serial-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!