Python 2.7 Internet of Things

Posted on at


For those of you who don't know me I am CPU nation. As a Senior in High School I have taken it upon myself to learn as many programming languages as I can to keep up with modern technology. One such language is Python 2.7. I love python, it allows me to make things I never though I could make.

To begin with you'll need a computer runing Windows 7, an Arduino Uno (you can get these at $50 - $30 at radioshack.com), and some knowledge of Python programming.

The basic idea of the Internet of Things or IoT, is to bring all the devices in your house together. My way of doing that is by port forwarding the computer through the router and connecting the computer to the arduino. Of course it's more difficult that just the wires.

Turn on the (LED) Lights

Lets start at the end. We want the arduino to run a function when we tell it to. So we need the arduino to listen to incoming serial data for a "protocol." In our case, a 1 or 0. The Arduino listens for the comand 1 or 0, if it gets a 1 the light turns on, if it gets a 0, the light turns off. Now on to the Python.

For this project your computer must act as a small server because I'm a cheap boy and didn't want to but the internet shield and such for the arduino. Anyway, using a python socket server we can turn our computer into a local host that listen and writes data. 

Here it is.

import socket
import re
import serial

# Standard socket stuff:
host = '' # do we need socket.gethostname() ?
port = 80
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(1) # don't queue up any requests
ser = serial.Serial('COM5', 9600, timeout=0) #com ports refer to which usb has your arduino
# Loop forever, listening for requests:

while True:
def MainSite():
csock.sendall("""HTTP/1.0 200 OK
Content-Type: text/html

<html>

</html>
""")


def IO():
match = re.match('GET /move\?a=(\w+)(\d+)\sHTTP/1', req)
val = match.group(2)
ser.write(val)
if val == "1":
csock.sendall("""HTTP/1.0 200 OK
Content-Type: text/html

<html>

</html>
""")
elif val == "0":
csock.sendall("""HTTP/1.0 200 OK
Content-Type: text/html

<html>

</html>
""")
def Login():
match = re.match('GET /move\?a=(\w+)(\d+)\sHTTP/1', req)
usr = match.group(1)
group = ["ericedigi", "chloecreative"]
if usr in group:
IO()
else:
MainSite()
csock, caddr = sock.accept()
f = open('log.txt', "a")
f.write("Connection from: " + `caddr`)
req = csock.recv(90000) # get the request, 1kB max
f.write("Request Header Data:")
f.write(req)
# Look in the first line of the request for a move command
# A move command should be e.g. 'http://server/move?a=90'
match = re.match('GET /move\?a=(\w+)(\d+)\sHTTP/1', req)
if match:
angle = match.group(1)
f.write("User: " + angle + "\n")
Login()
else:
MainSite()
# If there was no recognised command then return a 404 (page not found)
csock.close()

There it is. All that does is breakdown the header and send the response through the serial port into the arduino.



About the author

160