Advertisement
MattRichardson

Arduino Code for Foursquare Checkins

Jan 19th, 2012
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. // Commands for Alpha Sign Communication Protocol
  2. byte NUL = 0x00;
  3. byte START_HEADER = 0x01;
  4. byte START_TEXT = 0x02;
  5. byte END_TRANSMISSION = 0x04;
  6. byte ESC = 0x1B;
  7. byte FILL = 0x30;
  8. byte ROTATE = 0x61;
  9. byte SLOW = 0x15;
  10. byte FAST = 0x19;
  11. byte TIME = 0x13;
  12. byte CALL_STRING = 0x10;
  13. byte CALL_SIZE = 0x1A;
  14. byte SIZE_LARGE = 0x33;
  15. byte SIZE_SMALL = 0x31;
  16.  
  17. #define STANDBY 0
  18. #define CHECKED_IN 1
  19.  
  20.  
  21. int mode = STANDBY;
  22.  
  23.  
  24. #include <SPI.h>
  25. #include <Ethernet.h>
  26.  
  27. // Enter a MAC address and IP address for your controller below.
  28. // The IP address will be dependent on your local network:
  29. byte mac[] = {
  30.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  31. IPAddress ip(10,0,1, 123);
  32.  
  33. // Initialize the Ethernet server library
  34. // with the IP address and port you want to use
  35. // (port 80 is default for HTTP):
  36. EthernetServer server(80);
  37.  
  38. String response = "";
  39. String formattedResponse = "";
  40. long changeAt = 0;
  41.  
  42. void setup()
  43. {
  44.   // start the Ethernet connection and the server:
  45.   Ethernet.begin(mac, ip);
  46.   server.begin();
  47.   Serial.begin(9600);
  48.   writeText("Check in here on foursquare!                            ");
  49. }
  50.  
  51. void loop()
  52. {
  53.   // listen for incoming clients
  54.   EthernetClient client = server.available();
  55.   if (client) {
  56.     // an http request ends with a blank line
  57.     boolean currentLineIsBlank = true;
  58.     while (client.connected()) {
  59.       if (client.available()) {
  60.         char c = client.read();
  61.         response += c;
  62.         if (c == '\n') {
  63.           if ( (response.indexOf("+++") != -1) && (response.indexOf("&&&") != -1) )
  64.           {
  65.             int first = response.indexOf("+++") + 3;
  66.             int last = response.indexOf("&&&");
  67.             formattedResponse = response.substring(first,last);
  68.             formattedResponse = urlDecode(formattedResponse);
  69.             writeText(formattedResponse);
  70.             mode = CHECKED_IN;
  71.             changeAt = millis() + 60000;
  72.             response = "";
  73.           }
  74.           else {
  75.             response = "";
  76.           }
  77.         }
  78.         if (c == '\n' && currentLineIsBlank) {
  79.           // send a standard http response header
  80.           client.println("HTTP/1.1 200 OK");
  81.           client.println("Content-Type: text/html");
  82.           client.println();
  83.           break;
  84.         }
  85.         if (c == '\n') {
  86.           // you're starting a new line
  87.           currentLineIsBlank = true;
  88.         }
  89.         else if (c != '\r') {
  90.           // you've gotten a character on the current line
  91.           currentLineIsBlank = false;
  92.         }
  93.       }
  94.     }
  95.     // give the web browser time to receive the data
  96.     delay(1);
  97.     // close the connection:
  98.     client.stop();
  99.  
  100.     }
  101.  
  102.     if ( (millis() > changeAt) && (mode == CHECKED_IN) ) {
  103.       writeText("Check in here on foursquare!                            ");
  104.       mode = STANDBY;
  105.     }
  106. }
  107.  
  108. void writeText(String text) {
  109.   Serial.write(NUL); // Start frame sync chars
  110.   Serial.write(NUL);
  111.   Serial.write(NUL);
  112.   Serial.write(NUL);
  113.   Serial.write(NUL); // end frame sync chars
  114.   Serial.write(START_HEADER); // start of header
  115.   Serial.write("Z00"); // all sign types, all addresses
  116.   Serial.write(START_TEXT); //start of text
  117.   Serial.write("AA"); // write command, text file A
  118.   Serial.write(ESC);
  119.   Serial.write(FILL);
  120.   Serial.write(ROTATE);
  121.   Serial.print(text);
  122.   Serial.write(END_TRANSMISSION);
  123. }
  124.  
  125. String urlDecode(String input)
  126. {
  127.  
  128.   input.replace("%20", " ");
  129.   input.replace("+", " ");
  130.   input.replace("%21", "!");
  131.   input.replace("%22", "\"");
  132.   input.replace("%23", "#");
  133.   input.replace("%24", "$");
  134.   input.replace("%25", "%");
  135.   input.replace("%26", "&");
  136.   input.replace("%27", "\'");
  137.   input.replace("%28", "(");
  138.   input.replace("%29", ")");
  139.   input.replace("%30", "*");
  140.   input.replace("%31", "+");
  141.   input.replace("%2C", ",");
  142.   input.replace("%2E", ".");
  143.   input.replace("%2F", "/");
  144.   input.replace("%2C", ",");
  145.   input.replace("%3A", ":");
  146.   input.replace("%3A", ";");
  147.   input.replace("%3C", "<");
  148.   input.replace("%3D", "=");
  149.   input.replace("%3E", ">");
  150.   input.replace("%3F", "?");
  151.   input.replace("%40", "@");
  152.   input.replace("%5B", "[");
  153.   input.replace("%5C", "\\");
  154.   input.replace("%5D", "]");
  155.   input.replace("%5E", "^");
  156.   input.replace("%5F", "-");
  157.   input.replace("%60", "`");
  158.  
  159.   return input;
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement