daily pastebin goal
37%
SHARE
TWEET

twitmaslights_uno

a guest Oct 29th, 2013 137 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  Twitmaslights
  3.  
  4.  Control an Adafruit NeoPixel strip via Twitter.
  5.  
  6.  created 27 October 2013
  7.  by Ralph Crutzen for the @twitmaslights project
  8. */
  9.  
  10. #include <SPI.h>
  11. #include <WiFi.h>
  12. #include <TextFinder.h>
  13. #include <Adafruit_NeoPixel.h>
  14.  
  15. #define PIN 6
  16.  
  17. // Parameter 1 = number of pixels in strip
  18. // Parameter 2 = pin number (most are valid)
  19. // Parameter 3 = pixel type flags, add together as needed:
  20. //   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
  21. //   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
  22. //   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
  23. //   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
  24. Adafruit_NeoPixel strip = Adafruit_NeoPixel(49, PIN, NEO_GRB + NEO_KHZ800);
  25.  
  26. char ssid[] = ""; //  your network SSID (name)
  27. char pass[] = "";    // your network password (use for WPA, or use as key for WEP)
  28.  
  29. int status = WL_IDLE_STATUS;
  30. // if you don't want to use DNS (and reduce your sketch size)
  31. // use the numeric IP instead of the name for the server:
  32. //IPAddress server(74,125,232,128);  // numeric IP for Google (no DNS)
  33. char server[] = "twitmaslights.crutzen.eu";
  34.  
  35. // Initialize the Ethernet client library
  36. // with the IP address and port of the server
  37. // that you want to connect to (port 80 is default for HTTP):
  38. WiFiClient client;
  39.  
  40. char tweet[141] = "", oldTweet[141] = "";
  41. char user[141];
  42. String hashtag = "#twitmaslights:";
  43. char color[7];
  44.  
  45. void setup() {
  46.   strip.begin();
  47.   strip.show(); // Initialize all pixels to 'off'
  48.   //Initialize serial and wait for port to open:
  49.   Serial.begin(9600);
  50.  
  51.   // check for the presence of the shield:
  52.   if (WiFi.status() == WL_NO_SHIELD) {
  53.     Serial.println("WiFi shield not present");
  54.     // don't continue:
  55.     while(true);
  56.   }
  57.  
  58.   // attempt to connect to Wifi network:
  59.   while (status != WL_CONNECTED) {
  60.     Serial.print("Attempting to connect to SSID: ");
  61.     Serial.println(ssid);
  62.     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:    
  63.     status = WiFi.begin(ssid, pass);
  64.  
  65.     // wait 10 seconds for connection:
  66.     delay(10000);
  67.   }
  68.   Serial.println("Connected to wifi");
  69.   printWifiStatus();
  70. }
  71.  
  72. void loop() {
  73.   int i;
  74.   TextFinder finder(client);
  75.  
  76.   Serial.println("\nStarting connection to server...");
  77.  
  78.   // if you get a connection, report back via serial:
  79.   if (client.connect(server, 80)) {
  80.     Serial.println("connected to server");
  81.     // Make a HTTP request:
  82.     client.println("GET /index.php HTTP/1.1");
  83.     client.print("Host: ");
  84.     client.println(server);
  85.     client.println("User-Agent: Arduino/1.0");
  86.     client.println("Connection: close");
  87.     client.println();
  88.   }  
  89.  
  90.   // if there are incoming bytes available
  91.   // from the server, look for the right tags:
  92.   while (client.connected()) {
  93.     if (client.available()) {
  94.       if ( (finder.getString("<tweet>", "</tweet>", tweet, 141) != 0) &&
  95.            (finder.getString("<user>", "</user>",   user,  141) != 0) ) {
  96.         for (i = 0; i < 141; i++)
  97.           if (oldTweet[i] != tweet[i])
  98.             break;
  99.         if (i != 141) {
  100.           //Serial.println(tweet);
  101.           foundTweet(tweet, user);
  102.           for (i = 0; i < 141; i++)
  103.             oldTweet[i] = tweet[i];
  104.           break;
  105.         }
  106.         else {
  107.           Serial.println("No new tweets :-(");
  108.         }
  109.       }
  110.     }
  111.   }
  112.  
  113.   // if the server's disconnected, stop the client:
  114.   if (!client.connected()) {
  115.     Serial.println("Disconnecting from server.");
  116.     client.flush();
  117.     client.stop();
  118.   }
  119.  
  120.   // wait to reconnect
  121.   Serial.println("Wait...");
  122.   delay (30000); // Max. 30000
  123. }
  124.  
  125. void printWifiStatus() {
  126.   // print the SSID of the network you're attached to:
  127.   Serial.print("SSID: ");
  128.   Serial.println(WiFi.SSID());
  129.  
  130.   // print your WiFi shield's IP address:
  131.   IPAddress ip = WiFi.localIP();
  132.   Serial.print("IP Address: ");
  133.   Serial.println(ip);
  134.  
  135.   // print the received signal strength:
  136.   long rssi = WiFi.RSSI();
  137.   Serial.print("signal strength (RSSI):");
  138.   Serial.print(rssi);
  139.   Serial.println(" dBm");
  140. }
  141.  
  142. void foundTweet(char* tweet, char* user) {
  143.   int i;
  144.  
  145.   Serial.println("New tweet!");
  146.   Serial.print("Tweet: ");
  147.   Serial.println(tweet);
  148.   Serial.print("From: ");
  149.   Serial.println(user);
  150.  
  151.   String sTweet(tweet);
  152.   int iBeginHashtag = sTweet.indexOf(hashtag);
  153.   if (iBeginHashtag == -1) {
  154.     Serial.println("Hashtag not found or without colon");
  155.   }
  156.   else {
  157.     // Get color code from tweet
  158.     sTweet.substring(iBeginHashtag + hashtag.length()).toCharArray(color,7);
  159.     Serial.println(color);
  160.     for (i=0; i<6; i++)
  161.       if (((color[i] < 'A') || (color[i] > 'F')) &&
  162.           ((color[i] < 'a') || (color[i] > 'f')) &&
  163.           ((color[i] < '0') || (color[i] > '9')))
  164.         break;
  165.     if (i == 6) {
  166.       char red[] = {color[0],color[1]};
  167.       char green[] = {color[2],color[3]};
  168.       char blue[] = {color[4],color[5]};
  169.       byte r = hexToByte(red);
  170.       byte g = hexToByte(green);
  171.       byte b = hexToByte(blue);
  172.       if (b == 255) b = 254; // Fixes bug in (modified for Aldi) AdaFruit_Neopixel library
  173.       for(uint16_t i=0; i<strip.numPixels(); i++) {
  174.         strip.setPixelColor(i, strip.Color(r, g, b));
  175.       }
  176.       strip.show();
  177.     }
  178.     else {
  179.       Serial.println("No valid color code");
  180.     }
  181.   }  
  182. }
  183.  
  184. byte hexToByte(char hexString[]) {
  185.  
  186.   // https://github.com/benrugg/Arduino-Hex-Decimal-Conversion
  187.  
  188.   byte decValue = 0;
  189.   char nextChar;
  190.  
  191.   for (int i = 0; i < 2; i++) {
  192.    
  193.     nextChar = char(hexString[i]);
  194.     if (nextChar >= 48 && nextChar <= 57) nextChar = map(nextChar, 48, 57, 0, 9);
  195.     if (nextChar >= 65 && nextChar <= 70) nextChar = map(nextChar, 65, 70, 10, 15);
  196.     if (nextChar >= 97 && nextChar <= 102) nextChar = map(nextChar, 97, 102, 10, 15);
  197.     nextChar = constrain(nextChar, 0, 15);
  198.    
  199.     decValue = (decValue * 16) + nextChar;
  200.   }
  201.  
  202.   return decValue;
  203. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top