Skip to content

Instantly share code, notes, and snippets.

@versae
Created January 30, 2012 02:41
Show Gist options
  • Save versae/1702145 to your computer and use it in GitHub Desktop.
Save versae/1702145 to your computer and use it in GitHub Desktop.
Send the last tweet and sentiment about the message to Arduino
#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;
}
}
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment