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!


Friday, April 2, 2010

Programming Languages I've Learned in Order

James Tauber did this (http://jtauber.com/blog/2008/09/28/programming_languages_i%27ve_learned_in_order/) and I decided it would be fun to do it too, so here goes my quick list:
  • Javascript
  • java
  • VisualBasic
  • PHP
  • ASP.net
  • Python
  • C/C++ (learned at the same time)
  • Ruby
Ones in bold are ones I've used recently

I have to say, learning C/C++ so late really gave me an appreciation for all the stuff that is automatically done in other languages.

EDIT:
jtauber made a site for this sort of thing, it's pretty fun:
See mine at:

Thursday, March 4, 2010

Project SIKULI is da bomb

Definitely one of the most exciting projects i've seen come out recently has got to be SIKULI.

Basically, it works on the fact that you take screenshots and make a script to automate your UI.

Check out the video to really get a grasp of what it can do:
http://groups.csail.mit.edu/uid/sikuli/

It's really limitless. Today I used it to open up a bunch of putty tabs and sign me into our development server. It's so easy to use even a cave man can do it that even non-programmers could get the hang of it.

Maybe I can get it to automate some things in nethack for me, bwaahahaha.
Anyhow, I'm going to demonstrate the script I used to actually post this blog message:


Yes seriously. It's really that easy.

Happy hacking!

Wednesday, March 3, 2010

IE and jquery.

So today I found an interesting pitfall that I didn't previously know existed.
Apparently, when you're making a list of key-value pairs as such:
var options = {
url:'/Ajax/derpa.php',
success: succeed,
};


...IE doesn't like the trailing ',' on the last parameter, and will completely bug out. Being that IE6 (shudder) doesn't have a good set of debugging tools (that I know of), it took me a while to figure out that the comma was actually the problem.

So to fix it, just take out the trailing comma, as such:
var options = {
url:'/Ajax/reminderThankYou.php',
success: succeed
}


Hope that helps!

Happy Coding!

Sunday, February 28, 2010

A Beginner's First Look at Pinax

Before my laborious morning commute to Manhattan the other day, I decided that I would download a copious amount of .pdfs, documentation, and other files in order to learn about python and maybe try out some new python technology. This is where Pinax comes in.

Pinax is a technology based on Django, which in turn is a technology based on Python. Of course Python is a dynamic programming language that is easy to use and especially easy to read. Django is similar to Ruby on Rails, in the fact that it is used to make an easy to use MVC architecture to your web applications. On top of this architecture is Pinax, which takes all of the projects you might find yourself writing over and over again, and throws it into a single command that your run and like magic you're up and running. One of the neatest of these prebuilt projects is the social website project, which basically builds something similar to facebook in a matter of seconds. It's so easy to use it's mind blowing.


But what if I want to customize it?

This is where I had some trouble. And hopefully, this post will help you avoid some of the things that aren't so obvious to a newbie.

I first created a basic project following the documentation provided on pinaxproject.com. But where do I go from here? There are docs on how to customize tabs, but that's it.

The first thing I wanted to do obviously is change the styles. I did some poking around on the internets and some readme file, to find out that you need to create a folder in

pinaxenvironment_path/lib/python2.6/site-packages/pinax/templates/

In this new folder you make whatever changes to templates you want, then whene you're done you have to change your 'pinax_theme' setting in your settings.py to the foldername that you created. By default it is 'default'.

Doing this wasn't really the first thing I thought of, as a newbie.

Next, I wanted to add some of my own application to my default pinax project. I did what I would normally do in Django, create the new app inside the project folder, write my models, views, templates, etc, add to my settings under installed apps. Everything was going great except that my new application couldn't see any of the media from my pinax app, nor could it remember whether or not I was logged in! Thankfully, the people at #pinax on FreeNode were very helpful, and James Tauber answered my issue, which was that I needed to use RequestContext for my views.

All In all, I've only had those two problems so far with Pinax. I sometimes wish there were more documentation on their site. I still have questions.

How can I add and remove specific apps from pinax? What if I wanted just the blog application? I've tried to add and remove applications, but some are dependent on one another and at the time of me downloading the docs, there were no and may still not be any good documentation on how to do this.

I think something like a tutorial of an example site that can be made using pinax would be great. The preset sites are awesome, and they are very useful, but I think that a tutorial outlining how to make tweaks to your piinax site, subtracting certain features or adding features from your own django creations or from other pinax projects would be very useful. I have a very vague idea of what it's like to develop 'in' pinax, and I'd like to see a more concrete example to make sure that I'm going about it in the right manner.

All in all, Pinax is a really exciting technology and I will definitely be using it extensively for any future clients. It's powerful, and the default projects felt really stable, and although changing the theme wasn't initially apparent to me, it was easy to do that, too.

If you're a newbie to python web development and you're curious about these new technologies, then I hope this helped you out.

Also, if you're interested in what pinax can do, check out their website! Or check out cloud27, a project made with Pinax which is really neat!

Happy hacking!

-Sean

Thursday, February 11, 2010

Vim macro saves the day!!

So I had to work on a php script at work. Urg, php isn't exactly my language of choice, but I decided to make the best of it and at least try to have some fun, which I did, while learning something in the process. Here's the scoop:

I had to take something like this:
$output.="blah blah blah blah blah";
and turn it into something like this:
$output.="blah blah blah blah blah\n";
Unfortunately for me, there was a TON of lines like this.
After attending a great talk at nycpython by Justin Lilly on editor efficiency, I decided to try out a 'vim macro' for the first time.

Here's how I solved this problem:
  1. type 'qq' to start recording my macro
  2. type '$' to get to the end of the line
  3. hit 'h' twice to move the cursor left twice
  4. hit 'i' to go into insert mode
  5. type '\n' to put in my line break
  6. hit the esc key to get out of insert mode
  7. hit 'j' to move down to the next line
  8. hit 'q' to stop recording the macro
  9. type '20@q' to execute the macro 20 times
So this means that in order to create the macro, the syntax is:
qq commands q
..and then to execute it multiple times:
x@q
...where 'x' is the number of times you want to execute it.

I'm sure that there are many ways to do this, being that vim is such a powerful editing tool.
Anyways, the macro is definitely a tool I see myself using in the future, as it cut down the monotony and time needed to accomplish this annoying task.

Happy coding,
Sean

Wednesday, February 10, 2010

Lots of new stuff!

Lots of new stuff going on!

I'm an intern right now at we-care.com, doing web/application development, scripting, analytics, debugging, testing, and anything else geek related. It's a blast! I get to learn and use a lot of neat tools like sed and awk

Also, I joined a couple of cool meet-up groups like Django nyc and python nyc, each of which has a lot of really smart people involved, lot of great ideas coming out of those groups. Lot's to learn from them!!

I was able to meet Steve Holden of the Python Software Foundation, as well as Jakob Kaplan Moss, one of the founders of the Django open source project, got to hear some interesting stories and have a few beers, thanks to our sponsor, HUGE inc.

Pycon is coming up, unfortunately I can't go, being a student requires that you be too poor to do anything like that, haha. Fortunately, there's going to be a lot of coverage from people at django nyc and python nyc, so there's still a lot to look forward to.

And to top it all off, I made it to the very last level for the first time in nethack, I got all the way to the Astral plane and got demolished. It was my first time getting even remotely far in that game, which was pretty cool, although it kinda sucked that I didn't ascend. Oh well, much to learn from that I guess.

Well, back to work, hopefully I'll have a cool project to work on soon that I can make a couple posts about. I've got a few ideas floating around but I need to find a bit of time to get them rolling.