{"id":832,"date":"2015-03-05T20:29:46","date_gmt":"2015-03-05T20:29:46","guid":{"rendered":"https:\/\/www.courses.tegabrain.com\/SS15\/?page_id=832"},"modified":"2015-03-05T20:30:04","modified_gmt":"2015-03-05T20:30:04","slug":"832-2","status":"publish","type":"page","link":"https:\/\/www.courses.tegabrain.com\/SS15\/?page_id=832","title":{"rendered":"ARDUINO 3A &#8211; Talking to the Cloud"},"content":{"rendered":"<p><strong>03B &#8211; ARDUINO > TWEETING<\/strong><\/p>\n<p>We can then use the flexibility of Processing to connect sensor data to services online such as Twitter. This example shows how to take the potentiometer circuit and code you&#8217;ve just made,\u00a0and connect it to Twitter via Processing. It is set up so that when your potentiometer is turned up to its highest level, the Processing sketch will publish a tweet for you. This example enables you to create things that tweet updates.<\/p>\n<p>TWITTER SETUP:<\/p>\n<p>In\u00a0to use the twitter API you need to register the details as a developer and get authorization keys. You do this as follows:<\/p>\n<p>1. Visit https:\/\/apps.twitter.com\/ and login with your twitter username and password.<br \/>\n2. Click the &#8220;Create a new application&#8221; button&#8221; on the top right.<br \/>\n3. Fill in the form. (You can use any old URL that you have &#8211;\u00a0for example\u00a0your purchase url)<br \/>\n4. Click on &#8220;Keys and Access Tokens&#8221;. You should see the first two of the four things\u00a0that we need: the Consumer key and the Consumer secret.<br \/>\n5. Next to &#8220;Access Level&#8221; click &#8220;Modify Permssions&#8221; and choose the access you want.<br \/>\n6. Return to the &#8220;Keys&#8221; tab and click the button that says &#8220;Create my access token&#8221;.<br \/>\n7. Fill in these four values as strings in the fields indicated in the Processing sketch.<\/p>\n<p>PROCESSING\u00a0SETUP:<\/p>\n<p>1. Download the Twitter4j <a href=\"http:\/\/twitter4j.org\/en\/\">library here.\u00a0<\/a><br \/>\n2. Unzip library and copy the folder the folder named &#8216;libraries&#8217; in the Processing folder<br \/>\n3.\u00a0Rename the whole library folder so that it is called\u00a0&#8216;twitter4j&#8217;<br \/>\n4. Go into this folder and rename the folder called &#8216;lib&#8217; so it is called &#8216;library&#8217; instead.<\/p>\n<p>Update this code with your Twitter oAuth info and compile in Processing. It will tweet every time a byte of 255 is sent in from Arduino.<\/p>\n<pre lang=\"java\">import processing.serial.*; \/\/import serial library\r\nimport twitter4j.conf.*; \/\/import twitter library\r\nimport twitter4j.api.*; \r\nimport twitter4j.*; \r\nimport java.util.*;  \r\n\r\nConfigurationBuilder cb;\r\nQuery query;\r\nTwitter twitter;\r\nSerial myPort;  \/\/ Create object from Serial class\r\nint inByte;\r\n\r\nvoid setup() {\r\n  size(800, 600);      \/\/ window size\r\n  println(Serial.list());\/\/ List all the available serial ports\r\n  \/\/ change the number below to match your port. For me it was my 3rd port (port 2) but check what your consol says.\r\n  String portName = Serial.list()[2];\r\n  myPort = new Serial(this, portName, 9600);\r\n}\r\n\r\nvoid draw() {\r\n  background(#081640);  \r\n  if (inByte==255) {\r\n    println(\"Tweeting\");\r\n    login();\r\n    tweet();\r\n  }\r\n}\r\n\r\nvoid serialEvent(Serial myPort){\r\n    \/\/ get the byte:\r\n  inByte = myPort.read();\r\n  \/\/ print it:\r\n  println(inByte);\r\n}\r\n\r\nvoid login() {\r\n  cb = new ConfigurationBuilder();\r\n  cb.setOAuthConsumerKey(\"YourCosumerKey\");\r\n  cb.setOAuthConsumerSecret(\"YourConsumerSecret\");\r\n  cb.setOAuthAccessToken(\"YourAccessToken\");\r\n cb.setOAuthAccessTokenSecret(\"YourAccessTokenSecret\");\r\n}\r\n\r\nvoid tweet() {\r\n  Twitter twitter = new TwitterFactory(cb.build()).getInstance();\r\n  try {\r\n    Status status = twitter.updateStatus(\"I AM A TWEETING POTENTIOMETER #SUNYSS15\");\r\n    System.out.println(\"Successfully updated the status to [\" + status.getText() + \"].\");\r\n  }\r\n  catch (TwitterException te) {\r\n    println(\"Couldn't connect: \" + te);\r\n  };\r\n}\r\n<\/pre>\n<p>ARDUINO<\/p>\n<p>Use the arduino code and circuit from the first exercise (03A) to send serial data to Processing.<\/p>\n<p><strong>03C &#8211; SEARCHING TWITTER AND PINGING ARDUINO <\/strong><\/p>\n<p>We can work the other way and listen on twitter for particular posts, and when hearing a new post with a particular tag, ping Arduino to trigger something.<br \/>\nPROCESSING CODE:<\/p>\n<pre lang=\"java\">\/\/ Updated 1\/27\/15\r\n\/\/ See http:\/\/twitter4j.org\/en\/code-examples.html for more examples and details.\r\n\r\n\/\/ 1. Visit https:\/\/apps.twitter.com\/ and login with your twitter username and password.\r\n\/\/ 2. Click the \"Create a new application\" button\" on the top right.\r\n\/\/ 3. Fill in the form.\r\n\/\/ 4. Click on \"Keys and Access Tokens\". You should see the first two of the four things\r\n\/\/    that we need: the Consumer key and the Consumer secret. \r\n\/\/ 5. Next to \"Access Level\" click \"Modify Permssions\" and choose the access you want.\r\n\/\/ 6. Return to the \"Keys\" tab and click the button that says \"Create my access token\".\r\n\/\/ 7. Fill in these four values as strings in the cb fields below.\r\n\r\nimport twitter4j.conf.*; \/\/import twitter library files\r\nimport twitter4j.api.*; \r\nimport twitter4j.*; \r\nimport java.util.*;  \r\n\r\nConfigurationBuilder cb;\r\nQuery query;\r\nTwitter twitter;\r\n\r\n\/\/array for tweets\r\nArrayList statuses; \r\n\r\nimport processing.serial.*;\r\n\r\nSerial myPort;\r\n\r\n\/\/size of previous array\r\nint psize;\r\n\/\/size of current array\r\nint csize;\r\n\r\nvoid setup() {\r\n  println(Serial.list());\r\n  myPort = new Serial(this, Serial.list()[2], 9600);\r\n  myPort.buffer(1); \/\/change this to whatever port your computer is using \r\n}\r\n\r\nvoid draw() { \r\n}\r\n\r\nvoid mousePressed() {\r\n  login();\r\n  search();\r\n}\r\nvoid login() {\r\n  cb = new ConfigurationBuilder();\r\n  cb.setOAuthConsumerKey(\"ConsumerKey\");\r\n  cb.setOAuthConsumerSecret(\"ConsumerSecret\");\r\n  cb.setOAuthAccessToken(\"AccessToken\");\r\n  cb.setOAuthAccessTokenSecret(\"AccessTokenSecret\");\r\n}\r\n\r\nvoid search() {\r\n  Twitter twitter = new TwitterFactory(cb.build()).getInstance();\r\n  Query query = new Query(\"#SUNYSS15\"); \/\/search twitter for hashtag\r\n\r\n  try {\r\n    QueryResult result = twitter.search(query);\r\n    statuses = (ArrayList) result.getTweets();\r\n\r\n    for (int i = 0; i < statuses.size (); i++) {\r\n      Status s = (Status) statuses.get(i);\r\n      String user = s.getUser().getScreenName();\r\n      String msg = s.getText();\r\n      Date d = s.getCreatedAt();\r\n      println(\"Tweet by \" + user + \" at \" + d + \": \" + msg);\r\n    };\r\n  }\r\n  catch (TwitterException te) {\r\n    println(\"Couldn't connect: \" + te);\r\n  };\r\n  csize=(statuses.size());  \/\/get current arrayList size\r\n  \/\/if csize> psize, then there is a new tweet\r\n  if(csize>psize){ \/\/if new tweet\r\n    myPort.write(1); \r\n    println(\"1\");\/\/send a 1 to processing\r\n  }else{\r\n    myPort.write(0);\r\n     println(\"0\"); \/\/send a 0 to procssing\r\n  }\r\n \r\n  psize=csize; \/\/set previous size to whatever current size is\r\n}\r\n\r\n<\/pre>\n<p>ARDUINO CODE:<br \/>\nArduino will flash an LED on Pin 13 if a 1 is sent from Processing.<\/p>\n<pre lang=\"java\">int ledPin = 13;      \/\/ select the pin for the LED\r\n\r\nvoid setup() {\r\n  \/\/ put your setup code here, to run once:\r\n  Serial.begin(9600);\r\n  pinMode(ledPin, OUTPUT);\r\n}\r\n\r\nvoid loop() {\r\n  \/\/ put your main code here, to run repeatedly:\r\n  int input = Serial.read();  \/\/ read serial \r\n  if(input==1){\r\n     digitalWrite(ledPin, HIGH);\r\n     delay(1000);\r\n\r\n     digitalWrite(ledPin, LOW); \r\n     delay(1000);\r\n  }else{\r\n    digitalWrite(ledPin, LOW); \r\n  }\r\n\r\n}\r\n<\/pre>\n<p><strong>03D &#8211; CONNECTING ARDUINO TO IFTTT (IF THIS THEN THAT) <\/strong><\/p>\n<p><a href=\"http:\/\/www.instructables.com\/id\/Triggering-IFTTT-from-Arduino-using-Lithouse\/\">Here is a tutorial on how to do this<\/a> using an online data service called Lithouse.<\/p>\n<p>This means you can connect Arduino to trigger various online services &#8211; any that you can use in an IFTTT recipe. Although this tutorial is using a motion sensor, you could replace this with any sensor in your kit. IE. a button, light sensor, heat sensor etc.<\/p>\n<p>It is more difficult to go the other way around and use an IFTTT recipe to trigger an arduino component. But if you need to do this, it could be set up via IFTTT triggering a tweet with a particular hashtag, and having processing then send serial data to arduino (like in the example above).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>03B &#8211; ARDUINO > TWEETING We can then use the flexibility of Processing to connect sensor data to services online such as Twitter. This example shows how to take the potentiometer circuit and code you&#8217;ve just made,\u00a0and connect it to Twitter via Processing. It is set up so that when your potentiometer is turned up [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/832"}],"collection":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=832"}],"version-history":[{"count":2,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/832\/revisions"}],"predecessor-version":[{"id":834,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/832\/revisions\/834"}],"wp:attachment":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=832"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}