Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Twitmaslights
- Control an Adafruit NeoPixel strip via Twitter.
- created 27 October 2013
- by Ralph Crutzen for the @twitmaslights project
- */
- #include <SPI.h>
- #include <WiFi.h>
- #include <TextFinder.h>
- #include <Adafruit_NeoPixel.h>
- #define PIN 6
- // Parameter 1 = number of pixels in strip
- // Parameter 2 = pin number (most are valid)
- // Parameter 3 = pixel type flags, add together as needed:
- // NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
- // NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
- // NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
- // NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
- Adafruit_NeoPixel strip = Adafruit_NeoPixel(49, PIN, NEO_GRB + NEO_KHZ800);
- char ssid[] = ""; // your network SSID (name)
- char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
- int status = WL_IDLE_STATUS;
- // if you don't want to use DNS (and reduce your sketch size)
- // use the numeric IP instead of the name for the server:
- //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
- char server[] = "twitmaslights.crutzen.eu";
- // Initialize the Ethernet client library
- // with the IP address and port of the server
- // that you want to connect to (port 80 is default for HTTP):
- WiFiClient client;
- char tweet[141] = "", oldTweet[141] = "";
- char user[141];
- String hashtag = "#twitmaslights:";
- char color[7];
- void setup() {
- strip.begin();
- strip.show(); // Initialize all pixels to 'off'
- //Initialize serial and wait for port to open:
- Serial.begin(9600);
- // check for the presence of the shield:
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- // don't continue:
- while(true);
- }
- // attempt to connect to Wifi network:
- while (status != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
- status = WiFi.begin(ssid, pass);
- // wait 10 seconds for connection:
- delay(10000);
- }
- Serial.println("Connected to wifi");
- printWifiStatus();
- }
- void loop() {
- int i;
- TextFinder finder(client);
- Serial.println("\nStarting connection to server...");
- // if you get a connection, report back via serial:
- if (client.connect(server, 80)) {
- Serial.println("connected to server");
- // Make a HTTP request:
- client.println("GET /index.php HTTP/1.1");
- client.print("Host: ");
- client.println(server);
- client.println("User-Agent: Arduino/1.0");
- client.println("Connection: close");
- client.println();
- }
- // if there are incoming bytes available
- // from the server, look for the right tags:
- while (client.connected()) {
- if (client.available()) {
- if ( (finder.getString("<tweet>", "</tweet>", tweet, 141) != 0) &&
- (finder.getString("<user>", "</user>", user, 141) != 0) ) {
- for (i = 0; i < 141; i++)
- if (oldTweet[i] != tweet[i])
- break;
- if (i != 141) {
- //Serial.println(tweet);
- foundTweet(tweet, user);
- for (i = 0; i < 141; i++)
- oldTweet[i] = tweet[i];
- break;
- }
- else {
- Serial.println("No new tweets :-(");
- }
- }
- }
- }
- // if the server's disconnected, stop the client:
- if (!client.connected()) {
- Serial.println("Disconnecting from server.");
- client.flush();
- client.stop();
- }
- // wait to reconnect
- Serial.println("Wait...");
- delay (30000); // Max. 30000
- }
- void printWifiStatus() {
- // print the SSID of the network you're attached to:
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your WiFi shield's IP address:
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength:
- long rssi = WiFi.RSSI();
- Serial.print("signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- }
- void foundTweet(char* tweet, char* user) {
- int i;
- Serial.println("New tweet!");
- Serial.print("Tweet: ");
- Serial.println(tweet);
- Serial.print("From: ");
- Serial.println(user);
- String sTweet(tweet);
- int iBeginHashtag = sTweet.indexOf(hashtag);
- if (iBeginHashtag == -1) {
- Serial.println("Hashtag not found or without colon");
- }
- else {
- // Get color code from tweet
- sTweet.substring(iBeginHashtag + hashtag.length()).toCharArray(color,7);
- Serial.println(color);
- for (i=0; i<6; i++)
- if (((color[i] < 'A') || (color[i] > 'F')) &&
- ((color[i] < 'a') || (color[i] > 'f')) &&
- ((color[i] < '0') || (color[i] > '9')))
- break;
- if (i == 6) {
- char red[] = {color[0],color[1]};
- char green[] = {color[2],color[3]};
- char blue[] = {color[4],color[5]};
- byte r = hexToByte(red);
- byte g = hexToByte(green);
- byte b = hexToByte(blue);
- if (b == 255) b = 254; // Fixes bug in (modified for Aldi) AdaFruit_Neopixel library
- for(uint16_t i=0; i<strip.numPixels(); i++) {
- strip.setPixelColor(i, strip.Color(r, g, b));
- }
- strip.show();
- }
- else {
- Serial.println("No valid color code");
- }
- }
- }
- byte hexToByte(char hexString[]) {
- // https://github.com/benrugg/Arduino-Hex-Decimal-Conversion
- byte decValue = 0;
- char nextChar;
- for (int i = 0; i < 2; i++) {
- nextChar = char(hexString[i]);
- if (nextChar >= 48 && nextChar <= 57) nextChar = map(nextChar, 48, 57, 0, 9);
- if (nextChar >= 65 && nextChar <= 70) nextChar = map(nextChar, 65, 70, 10, 15);
- if (nextChar >= 97 && nextChar <= 102) nextChar = map(nextChar, 97, 102, 10, 15);
- nextChar = constrain(nextChar, 0, 15);
- decValue = (decValue * 16) + nextChar;
- }
- return decValue;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement