Monthly Archives: January 2012

Arduino, twitter & Python

The second week in the fascinating world of Arduino left us very interesting things. The class was about connecting new kinds of periphals, like buzzers or LCD displays. So, the idea of my classmate Roberto and me was to show the last tweet from twitter in a LCD display. After that, and due to the time limitations of the class, we changed our minds and decided analyze the last tweet in a sense of positive or negative message, and then brigth a blue LED for positive or red LED for negative. But sadly, this also was too much for one and a half hour.

So, what we finally did was to communicate the Arduino device with twitter through serial port connecting to a Python program. The workflow is the next. A Python program in a infinite loop gets the last tweet, then analyzes the positive and negative sentiment (using ViralHeat API) of hte text, after that, it sends a signal to the Arduino device, that it is listening in a specific port. Regardless to the singal received, plus the message, Arduino does the right LED to brigth.

The message is not trivial, and it has to be defined. In our case, we first send the signal for blue, red or both (whether the sentiment was unable to get), followed by a special token as separator, all the characters of the text for a future implementation of the LCD display. The separator token should be a not printable character or some you are sure it is nor possible to use in a twitter message.

So here you can find the Python code fofr the program. As you can see, we used the twython library for connecting with Twitter. Also, if you are using Linux, in order to know the right port to connect, go to the Arduino window → Tool → Serial monitor, and see what it is written in the title of the window. It used to be something like

/dev/ttyACM0

.

import serial
import time
import urllib
import requests

from json import loads
from twython import Twython

# Token, we choose not printable values
SEPARATOR_TOKEN = 666
# Sentimen API
url = "http://www.viralheat.com/api/sentiment/review.json?text=%s&api_key=<KEY>"
# Connect to arduino via serial port
arduino = serial.Serial('/dev/ttyACM0', 9600, timeout=1)

# Write to Arduino
def writeToArduino():
    twitter = Twython()
    public_timeline = twitter.getPublicTimeline()
    last_tweet = public_timeline[-1]
    text = last_tweet["text"]
    # We get the numbers of the each character
    codes = [ord(c) for c in text]]
    # Get the sentiment
    response = requests.get(url % urllib.quote(text))
    sentiment_response = loads(response.content)
    if sentiment_response["mood"].lower() == "negative":
        sentiment = 0
    elif sentiment_response["mood"].lower() == "positive":
        sentiment = 1
    else:
        sentimen = 2
    # Add characters codes and sentiment value
    arduino.write(codes + [SEPARATOR_TOKEN, sentiment])

while True:
    writeToArduino()

And finally the Arduino code.

#define GREEN 8
#define RED 12

const int sepToken = 666;
int val = 0;
int lastVal = 0;

void setup() {
  pinMode(GREEN, OUTPUT);
  pinMode(RED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  if (Serial.available()) {
    val = Serial.read();
    if (lastVal == sepToken) {
      if (val == 0) {
        digitalWrite(GREEN, HIGH);
        digitalWrite(RED, LOW);
      }
      if (val == 0) {
        digitalWrite(GREEN, HIGH);
        digitalWrite(RED, LOW);
      }
      if (val == 0) {
        digitalWrite(GREEN, HIGH);
        digitalWrite(RED, LOW);
      }
    } else {
      // Print "val" to the LCD Display
    }
    Serial.println(val);
    lastVal = val;
  }
}

Leave a Comment

Filed under Tasks

Arduino: First Contact

This week has been, at the end, my first Interactive Exhibit Design class. From the first moment, and working in pairs, we were given an Arduino UNO device, with the corresponding trainer, wires, light-emitting diodes (aka LED), light sensors, one button and the USB cable to connect to the laptop. I cannot say I weren’t excited. I’ve been hearing about Arduino almost 4 years and finally yesterday I could use it.

As a guy with some Computer Sciences Old School background, has been amazing to see the simplicity of Arduino and the magic of the in board microcontroller. I remember the times when I had to pay a lot of attention in order to get my circuit working. If you made a mistake, you should rethink about your wiring, connect the oscilloscope and analyze what the hell was going on. Now, with Arduino, you can layout the components of the circuit and build as much programs on it as you want. For me, it is like magic.

Arduino and its components

Arduino and its components

This first contact was näive. My classmate and I made the tipical first experiment: make a LED to bright. After that, and using the software development kit also provided, and based on Processing, we plug a light sensor, some resistances and build this time a proximity alarm: if you bring your hand closer to the light sensor, this one lose some light, then our code was design to make blink a diode in a inverse way to the light the sensor were receiving. Really funny and didactic.

Our "proximity" detector

Our "proximity" detector

So now, I am exciting again for the next class and to see what Arduino has for us!

2 Comments

Filed under Analysis, Tasks

The Futurible Closet

As the very first assignment of History 9832B: Interactive Exhibit Design, William Turkel proposes a blog post about what it’s called History Appliances. The steps are “easey”: take an common object, do some miracle, think about its way to operate, and connect it to the historical flow. It looks really interesting.

However, and besides of this assignment, I know the course has some readings about totally awesome new tools: Interaction Design and Visualization, Making and Hacking, Coding, Electronics, Physical Computing, Microcontrollers, Desktop Fabrication, Digital Representations and 3D Photography, Scanning,Visualization and Printing. As a computer scientist turned on a humanist, these ones are fields of much interest.

The closet wasn't always what we think it was for

The closet wasn't always what we think it was for

I started thinking about my closet, so is one of the appliances I use every day. Actually almost every day, because the usual chair or couch do the functions of the closet more times that I would like. As a historical appliance, the use of the closet started in the Roman Age. In Herculaneum we find the first closets, but the was used as a place ni which put war weapons and armors, so the latin name is armarĭum —armario in spanish– it comes from arma, that means weapon. Roman people also used it to put in it some portraits made of wax, and books as the main storage systems for libraries. Eventually it became a good place to save any kind of objects, but it wasn’t until the 15th century that the closet was used as today, to store and save clothes.

Said this, I think that would be interesting to have some closet the allows swap your clothes with objects saved in it in any other past or future age. I would like to open my closet tomorrow for put my “folded” clothes in it and find that my closet only has roman papyrus, war armors, or pompous french clothes. And the same thing could happend for the future, who knows what the future closet will have inside? Even our present is the future for our ancestors. In same way, this idea remembers me to the book and movie “Narnia Chronicles: The Lion,The Witch And The Wardrobe”, in which a few kids can enter into a new magical world through the closet, in a different time and space. Kids always have used the closet as a hiding and magical place.

In the other hand, our closets are essentially the same ones that our ancestors used. Not any technological improvements has been carried out. Maybe we put some light bulbs, made decorations or made the doors automatic. But they are, actually, the same appliances from Rome. So, what we could do to enhance our traditional closets? From my point of view, as a bit lazy guy about domestic tasks, personally, I dislike the workflow of the clothes: I wear something, the garment gets dirty, put the garment in the dirty clothes box, wait for the dirt clothes box gets full, carry the dirty clothes box to the wash machine, put the clean clothes in the tumble dryer, and the worst part, do the ironing where necessary, fold the clothes and place them on hangers into the closet in a ordered way. Exactly the same process as people did five centuries ago. I hope to have in the near future some high technological closet why I can just put dirty clothes in it and, in the next day, grab some fresh, clean and folded garments. I think I’m not hoping too much. But I man can dream…

Leave a Comment

Filed under Tasks