Saturday, May 28, 2011

Communicating with the Arduino


Hello internet, I did something kinda neat and figured you might want to give it a go.

I have a sparkfun arduino kit, and I was bored so I had an idea to communicate to my arduino via USB. Fortunately they provide a lot of good examples.


So how do you do it? Well, start off with this arduino example: http://arduino.cc/en/Tutorial/PhysicalPixel

Set up your board so that you have an led that will go on and off (the above example covers how to do this).
Now, upload this code to the arduino:

const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}

void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
}


It's basically the same code as in the example. What it does is will
read from the Serial port, if the byte is an H, it will turn the ledPin
on, if it's L, it will turn the ledPin off.


In my example, I have the ledPin on pin 9, make sure you change
yours accordingly.


So, now you upload that code to your arduino. You can test it out
with the Serial monitor built into the driver for the arduino.

Yay! it works.


Now this post would be no fun if it ended there (nor would it be
fun if we didn't use python).


So, lets create a python script that listens on a port and writes
whatever data you send to it, to the arduino!


First, you need pySerial: http://pyserial.sourceforge.net/

You also need to create the script, here's what I used (we
basically stripped down some code that my friend Mike
Romano has to create the socket):

(http://pastebin.com/XxRct6zU)

import socket
import serial

def server():
srv=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srv.bind(('',7777))
ser = serial.Serial('COM3', 9600, timeout=1)

while 1:
srv.listen(5)
conn, addr = srv.accept()
print 'Connected Peer',addr
data = conn.recv(1024)
conn.close()
ser.write(str(data))
print str(data)
server()

So, what this code does is listen on port 7777, and sends whatever
data it receives to COM3 (yours may be different) to serial port 9600.
That's the port that the arduino uno looks on for serial data.


Awesome!

Of course my friend Mike Romano wouldn't be satisfied unless
he took it even one step further, he used some of the code from
his JeevesEngine (https://github.com/romanomj/JeevesEngine) so that he could send an email from his phone
that will execute a script, hereby turning the light on or off. Now
we can turn a light on or off in my room from anywhere with a
3G connection! Cool!


Hopefully some cool stuff can come from it!


Enjoy!