Below is the photo I took in class during my final presentation. I have uploaded the tweet that I sent with the sensor on the Arduino along with a photo of the metadata showing the time the photo was taken.
The last photo shows the Arduino working with Twitter, the "view 1 new tweet" message showing that the tweet successfully connected to Twitter, and the Processing code successfully running to display real time tweets that contain urls of photographs.
Wednesday, December 9, 2015
Tuesday, December 8, 2015
Final Project
As a photographer, I am interested in how I perceive my world around me and how I can represent what I see to other people. Upon viewing my photographs, people often ask me where or when the image was taken. So, for the final project, I taped a sensor connected to the Arduino to the button of a point and shoot camera so that every time someone takes a picture on the camera a tweet is sent to Twitter notifying myself as well as followers of my Twitter account that someone has taken a photograph. I connected my Twitter account, https://twitter.com/Arduino_Photo, with ThingSpeak so I could easily connect Twitter with the Arduino. A classmate, Mckenzie, who is also working with ThingSpeak and Twitter for this project, shared her Arduino code with me which greatly helped me out. Once the sensor on the Arudino connects with ThingSpeak and sends a tweet I can then view what time the tweet hit Twitter on my Twitter account. I can then use the time information from each tweet, and the metadata of each photo containing the time it was taken, to upload images to a site or a blog so people can see that a picture was taken, and they can then in turn see the photograph that was taken at that exact moment.
#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
#include <SPI.h>
// Local Network Settings
//byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xA0, 0xA1 }; // Must be unique on local network, not
// ThingSpeak Settings
char thingSpeakAddress[] = "api.thingspeak.com";
String thingtweetAPIKey = "key";
const char ssid[] = "KSU Guest";
//const char pssk[] = "";
// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;
// Initialize Arduino ESP8266 Client
ESP8266Server server = ESP8266Server(80);
ESP8266Client client;
void setup()
{
// Start Serial for debugging on the Serial Monitor
Serial.begin(9600);
// Start the WiFi
initializeESP8266();
connectESP8266();
delay(1000);
}
void loop()
{
// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}
lastConnected = client.connected();
double volts = pressureSensor();
if (volts > 0.01){
String message = "Another photo.";
//message += volts;
//message += " volts.";
//message += " volts. #arduino #thingspeak";
updateTwitterStatus(message);
}
else{
Serial.println(F("Not sensing anything..."));
}
delay(2000);
}
void updateTwitterStatus(String tsData)
{
if (client.connect(thingSpeakAddress, 80))
{
// Create HTTP POST Data
tsData = "api_key="+thingtweetAPIKey+"&status="+tsData;
client.print("POST /apps/thingtweet/1/statuses/update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData);
lastConnectionTime = millis();
if (client.connected())
{
Serial.println("Connecting to ThingSpeak...");
Serial.println();
failedCounter = 0;
Serial.println("Made a post!");
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();
}
}
else
{
failedCounter++;
Serial.println("Connection to ThingSpeak Failed ("+String(failedCounter, DEC)+")");
Serial.println();
lastConnectionTime = millis();
}
}
void initializeESP8266()
{
int test = esp8266.begin();
if (test != true)
{
Serial.println(F("Error talking to ESP8266."));
errorLoop(test);
}
Serial.println(F("ESP8266 Shield Present"));
}
void connectESP8266()
{
int retVal = esp8266.getMode();
if (retVal != ESP8266_MODE_STA){
retVal = esp8266.setMode(ESP8266_MODE_STA);
if (retVal < 0)
{
Serial.println(F("Error setting mode."));
errorLoop(retVal);
}
}
Serial.println(F("Mode set to station"));
retVal = esp8266.status();
if (retVal <= 0){
Serial.print(F("Connecting to "));
Serial.println(ssid);
retVal = esp8266.connect(ssid);
if (retVal < 0){
Serial.println(F("Error connecting"));
errorLoop(retVal);
}
}
}
void errorLoop(int error){
Serial.print(F("Error: ")); Serial.println(error);
Serial.println(F("Looping forever."));
for (;;)
;
}
double pressureSensor(){
// Read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
double voltage = sensorValue * (5.0 / 1023.0);
// Print out the value read
return voltage;
}
I wanted to take the project one step further and make it so that the user could also find and views pictures that are being tweeted in real time on Twitter. So, with Processing, I was able to make a Twitter search for photographs and was able to print the result in the command line of Processing. The viewer will be able to see the username of the people who made the tweet, the time of the tweet, the tweet itself, and a url of the photo they posted. One can then copy and paste the url of the photo into the web browser to view the images. I can run this Processing sketch at the same time as the Arduino sketch in order to create a more depth in the project as well as the viewer experience.
import java.util.Date;
import twitter4j.conf.*;
import processing.serial.*;
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.*;
import processing.serial.*;
import cc.arduino.*;
// HEY! GET YER OWN KEYS FROM: https://dev.twitter.com/apps/new
static String OAuthConsumerKey = "";
static String OAuthConsumerSecret = "";
static String AccessToken = "";
static String AccessTokenSecret = "";
Twitter myTwitter;
List<Status> tweets;
String myQueryWord = "photo";
long previousIdOfTweetContainingQuery = 0;
//===========================================================================
void setup() {
size(125, 125);
frameRate(10);
background(0);
loginTwitter();
}
//===========================================================================
void loginTwitter() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(OAuthConsumerKey);
cb.setOAuthConsumerSecret(OAuthConsumerSecret);
cb.setOAuthAccessToken(AccessToken);
cb.setOAuthAccessTokenSecret(AccessTokenSecret);
myTwitter = new TwitterFactory(cb.build()).getInstance();
}
void draw() {
try {
println ("Searching.... Current time = " + hour() + ":" + minute() + ":" + second());
Query query = new Query(myQueryWord);
// how many results to fetch
QueryResult result = myTwitter.search(query);
for(Status mostRecentTweetContainingQuery : result.getTweets())
{
long mostRecentTweetContainingQueryId = mostRecentTweetContainingQuery.getId();
if (previousIdOfTweetContainingQuery == 0) {
previousIdOfTweetContainingQuery = mostRecentTweetContainingQueryId;
}
if (mostRecentTweetContainingQueryId != previousIdOfTweetContainingQuery) {
// yay! someone has just tweeted our favorite word!
previousIdOfTweetContainingQuery = mostRecentTweetContainingQueryId;
// tellArduinoToDoSomething(); // HERE IT IS!
// print out the new tweet, for fun.
String user = mostRecentTweetContainingQuery.getUser().getScreenName();
String msg = mostRecentTweetContainingQuery.getText();
Date d = mostRecentTweetContainingQuery.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
}
}
/*
// Indicentally, you can also list all of the most recent tweets containing that search term. Fun!
for (int i=0; i< tweetsContainingQuery.size(); i++) {
Tweet t = (Tweet) tweetsContainingQuery.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
}
println("--------------------");
*/
}
catch (TwitterException te) {
println("Error connecting to Twitter: " + te);
};
delay (12000); // REALLY IMPORTANT, PEOPLE. You're limited to 350 requests per hour, or about once per 11 seconds.
}
import twitter4j.conf.*;
import processing.serial.*;
import twitter4j.conf.*;
import twitter4j.*;
import twitter4j.auth.*;
import twitter4j.api.*;
import java.util.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.*;
import processing.serial.*;
import cc.arduino.*;
// HEY! GET YER OWN KEYS FROM: https://dev.twitter.com/apps/new
static String OAuthConsumerKey = "";
static String OAuthConsumerSecret = "";
static String AccessToken = "";
static String AccessTokenSecret = "";
Twitter myTwitter;
List<Status> tweets;
String myQueryWord = "photo";
long previousIdOfTweetContainingQuery = 0;
//===========================================================================
void setup() {
size(125, 125);
frameRate(10);
background(0);
loginTwitter();
}
//===========================================================================
void loginTwitter() {
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey(OAuthConsumerKey);
cb.setOAuthConsumerSecret(OAuthConsumerSecret);
cb.setOAuthAccessToken(AccessToken);
cb.setOAuthAccessTokenSecret(AccessTokenSecret);
myTwitter = new TwitterFactory(cb.build()).getInstance();
}
void draw() {
try {
println ("Searching.... Current time = " + hour() + ":" + minute() + ":" + second());
Query query = new Query(myQueryWord);
// how many results to fetch
QueryResult result = myTwitter.search(query);
for(Status mostRecentTweetContainingQuery : result.getTweets())
{
long mostRecentTweetContainingQueryId = mostRecentTweetContainingQuery.getId();
if (previousIdOfTweetContainingQuery == 0) {
previousIdOfTweetContainingQuery = mostRecentTweetContainingQueryId;
}
if (mostRecentTweetContainingQueryId != previousIdOfTweetContainingQuery) {
// yay! someone has just tweeted our favorite word!
previousIdOfTweetContainingQuery = mostRecentTweetContainingQueryId;
// tellArduinoToDoSomething(); // HERE IT IS!
// print out the new tweet, for fun.
String user = mostRecentTweetContainingQuery.getUser().getScreenName();
String msg = mostRecentTweetContainingQuery.getText();
Date d = mostRecentTweetContainingQuery.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
}
}
/*
// Indicentally, you can also list all of the most recent tweets containing that search term. Fun!
for (int i=0; i< tweetsContainingQuery.size(); i++) {
Tweet t = (Tweet) tweetsContainingQuery.get(i);
String user = t.getFromUser();
String msg = t.getText();
Date d = t.getCreatedAt();
println("Tweet by " + user + " at " + d + ": " + msg);
}
println("--------------------");
*/
}
catch (TwitterException te) {
println("Error connecting to Twitter: " + te);
};
delay (12000); // REALLY IMPORTANT, PEOPLE. You're limited to 350 requests per hour, or about once per 11 seconds.
}
Overall, I found this project to be quite challenging because I had not worked with the Arduino, or any form of electronics, and programming anything before this project. I did a large amount of Google searching and trial and error in order to find code and insight online, and I got help from classmates as well. I feel my project is interesting and relevant to the art world because it is interactive and enables the viewer to connect with photography through a hands on element, an online Twitter account, and allows the viewer to read and access tweets that contain photographs in real time. Ideally, the project could be placed in a public settings so that a viewer can walk up, examine the project, take a photograph, and then later view the Twitter account as well as the site the images are uploaded to in order to see the photograph they made.
Sunday, December 6, 2015
final progress
Above is a picture of my setup with the Arduino and WiFi shield, and a basic point and shoot camera.
I changed the delay in the Arduino code and that seemed to fix the problem of it sending too many tweets. I am still having an issue with ThingSpeak not letting me spend the same tweet a couple of times in a row. I'm not sure this is something I can get around, it might just be something with ThingSpeak. Nevertheless, I am successfully sending a tweet to Twitter upon pressing the button to take a photo on my camera.
Wednesday, December 2, 2015
final progress
I have successfully tweeted through ThingSpeak from the Arduino sensor, now I need to test how it works when pressing the button to take a picture on camera.
I first did a test with the pressure sensor and was able to tweets the volts coming from the sensor
I then tested sending a tweet upon pressing the sensor with my modified tweet
I am excited that this is working! Now I would like to see how I can connect the camera with the Arduino.
I made a new Twitter account specifically
for this project so I don't have to make my followers on my private account deal with all of these tweets. It is successfully work now, too.
I have learned that ThingSpeak does not like sending the same tweet multiple times because I tried for an hour sending the same tweet. So, I tried changing the tweet and it seems to be working again.
The only problem I am having now is the sensor on the Arduino sends multiple tweets, around 10, when I only press the sensor once. I hope that once I have the sensor taped down the problem will be resolved.
I first did a test with the pressure sensor and was able to tweets the volts coming from the sensor
I am excited that this is working! Now I would like to see how I can connect the camera with the Arduino.
I made a new Twitter account specifically
for this project so I don't have to make my followers on my private account deal with all of these tweets. It is successfully work now, too.
The only problem I am having now is the sensor on the Arduino sends multiple tweets, around 10, when I only press the sensor once. I hope that once I have the sensor taped down the problem will be resolved.
Tuesday, December 1, 2015
wifi shield and thingspeak
I made a ThingSpeak account in order to try to get the Arduino to interact with Twitter though ThinkSpeak. First, I messed around with ThinkSpeak a bit by setting up TimeControl with my Twitter account to see if I could set up a certain time a customized tweet could be sent to my Twitter account through ThingSpeak. It worked, so then I continued with connecting my Twitter account with ThingTweet. Right now, I am thinking about setting up a senor on the Arduino and having ThingSpeak send a customized tweet either my personal or a new Twitter account whenever the sensor is pressed. I would like to attach the senor to the shutter button of a camera and have the senor send a tweet saying "A picture has just been taken!" or something like that to a Twitter account. I think it would be interesting have the Arduino connect to a camera because I could then connect the metadata of the photo and the information from Twitter together in order to figure out what picture was taken at what exact moment.
I successfully got the wifi shield to connect to my computer, but was not successful with trying to connect the senor with code to ThingSpeak in order to send a tweet to my Twitter account.
I successfully got the wifi shield to connect to my computer, but was not successful with trying to connect the senor with code to ThingSpeak in order to send a tweet to my Twitter account.
Subscribe to:
Comments (Atom)
