简体   繁体   中英

Python and arduino serial communication

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM