Python Interface for Arduino Uno Using a Netgear WGT634u Running OpenWrt

Posted by Max Power | Posted in Arduino, Netgear WGT634u | Posted on 29-11-2011-05-2008

0

Edit:  I abandoned this method in favor of a simpler method.  I am using a FTDI to communicate and just executing serial commands with PHP.  You can see how I am doing this in this post.  I left this up thinking that it may be useful to someone else.  However, keep in mind it may not be complete.

In this post I will show you how I was able to communicate with my Arduino Uno connected to my Netgear WGT634u running OpenWrt Backfire.  This is a baby step toward being able to control the Arduino Uno from a website hosted by the Netgear WGT634u.

Setup

First we will install Python (python_2.6.4-3_brcm47xx.ipk) :

opkg update
opkg install python

Next we will install the PySerial module (pyserial_2.4-1_brcm47xx.ipk):

opkg update
opkg install pyserial

Now we need to edit the PySerial module to disable DTR, I discus this a bit (here):

cd /usr/local/lib/pythonX.Y/site-packages/serial
rm serialwin32.pyc
vi serialwin32.py

Change the following line:

self._dtrState = win32.DTR_CONTROL_ENABLE

to:

self._dtrState = win32.DTR_CONTROL_DISABLE

Now lets create a sample script and test it. First make a folder to put the python script in:

mkdir /python

Create the sample script:

vi /python/arduino_com.py
import sys
import arduino
 
ser = arduino.Serial('/dev/ttyACM0',9600)
 
if (len(sys.argv) >; 1):
print sys.argv[1]
ser.write(sys.argv[1])

Now lets test it.. To test the script I am using the LED example included with the Arduino software. Using your computer, upload the PhysicalPixel sketch from File > Examples > Communication > PhysicalPixel to the Arduino. After you upload the sketch, plug it into the WGT634u and lets test it out. In this example when we send a “H” the LED on the Arduino will light and when we send a “L” the LED will go off.

Turn the LED on:

/python/arduino_com.py H

Turn the LED off:

/python/arduino_com.py L

You can call you script from a webserver using cgi. Place the following file in your bin-cgi folder:

#!C:\Python27\python.exe -u
#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable() # for troubleshooting
import arduino # For serial com (modified pyserial)
 
print "Content-type: text/html"
print
 
print """
Sample CGI Script

Sample CGI Script

"""
 
form = cgi.FieldStorage()
arg = form.getvalue("arg")
 
print """
str value: %s
""" % cgi.escape(arg)
 
ser = arduino.Serial('\dev\ttyACM0',9600)
ser.write(arg)

Visit it with your web browser using http://localhost:8080/cgi-bin/arduino_com.py?arg=H to turn the light on and http://localhost:8080/cgi-bin/arduino_com.py?arg=L to turn it off.

You can also call it from a webserver using PHP:

#button {
	font-weight: bold;
	border: 2px solid #fff;
}

Write a comment