This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!

Ego Ticker Ethernet Shield (sloppy)

By: MattRichardson on Jan 16th, 2012  |  syntax: C  |  size: 5.15 KB  |  views: 48  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. byte NUL = 0x00;
  2. byte START_HEADER = 0x01;
  3. byte START_TEXT = 0x02;
  4. byte END_TRANSMISSION = 0x04;
  5. byte ESC = 0x1B;
  6. byte FILL = 0x30;
  7. byte ROTATE = 0x61;
  8. byte SLOW = 0x15;
  9. byte FAST = 0x19;
  10. byte TIME = 0x13;
  11. byte CALL_STRING = 0x10;
  12. byte CALL_SIZE = 0x1A;
  13. byte SIZE_LARGE = 0x33;
  14. byte SIZE_SMALL = 0x31;
  15.  
  16. #include <SPI.h>
  17. #include <Ethernet.h>
  18.  
  19. char response[32];
  20. long pollingInterval = 3000; // in milliseconds
  21. boolean failed = false;
  22.  
  23. byte mac[] = {  
  24.   0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
  25. IPAddress server(XXX,XXX,XXX,XXX); //your server's IP here
  26.  
  27.  
  28. EthernetClient client;
  29.  
  30.  
  31. void setup () {
  32.   delay(5000);
  33.   Serial.begin(9600);
  34.   Serial.println("Allocating Memory...");
  35.   allocateMemory();
  36.   Serial.println();
  37.   Serial.println("Setting Text...");
  38.   writeText();
  39.   Serial.println();
  40.  
  41.   if (Ethernet.begin(mac) == 0) {
  42.     Serial.println("Failed to configure Ethernet using DHCP");
  43.     // no point in carrying on, so do nothing forevermore:
  44.     while(true);
  45.  
  46.  
  47.     delay(1000);
  48.  
  49.   }
  50. }
  51.  
  52.  
  53. void loop() {
  54.   if (client.connect(server, 80)) {
  55.     Serial.println("connected");
  56.     // Make a HTTP request:
  57.     client.println("GET /~path/to/reddit.php");
  58.     client.println();
  59.     delay(1000);
  60.   }
  61.   else {
  62.     // if you didn't get a connection to the server:
  63.     Serial.println("connection failed");
  64.     failed = true;
  65.  
  66.   }
  67.   if (!failed) {
  68.     delay(1000);
  69.     if (client.available()) {
  70.       int i = 0;
  71.       memset( &response, 0, 32 );
  72.       do {
  73.         response[i] = client.read();
  74.         i++;
  75.         delay(1);
  76.       }
  77.       while (client.available());
  78.       Serial.print("response length: ");
  79.       Serial.println(i);
  80.       response[i] = '\0';
  81.     }
  82.  
  83.     Serial.print("Response from server: ");
  84.     Serial.println(response);
  85.     client.stop();
  86.  
  87.     writeRedditString(response);
  88.   }
  89.   failed = false;
  90.  
  91.   delay(pollingInterval);
  92.  
  93.  
  94.   if (client.connect(server, 80)) {
  95.     Serial.println("connected");
  96.     // Make a HTTP request:
  97.     client.println("GET /~path/to/reddit_change.php");
  98.     client.println();
  99.     delay(1000);
  100.   }
  101.   else {
  102.     // if you didn't get a connection to the server:
  103.     Serial.println("connection failed");
  104.     failed = true;
  105.  
  106.   }
  107.   if (!failed) {
  108.     delay(1000);
  109.     if (client.available()) {
  110.       int i = 0;
  111.       memset( &response, 0, 32 );
  112.       do {
  113.         response[i] = client.read();
  114.         i++;
  115.         delay(1);
  116.       }
  117.       while (client.available());
  118.       Serial.print("response length: ");
  119.       Serial.println(i);
  120.       response[i] = '\0';
  121.     }
  122.  
  123.     Serial.print("Response from server: ");
  124.     Serial.println(response);
  125.     client.stop();
  126.  
  127.     writeRedditChangeString(response);
  128.   }
  129.   failed = false;
  130.   delay(pollingInterval);
  131. }
  132.  
  133. void allocateMemory () {
  134.   // See page 41 of manual.
  135.   Serial.write(NUL); // Start frame sync chars
  136.   Serial.write(NUL);
  137.   Serial.write(NUL);
  138.   Serial.write(NUL);
  139.   Serial.write(NUL); // end frame sync chars
  140.   Serial.write(START_HEADER); // start of header
  141.   Serial.write("Z00"); // all sign types, all addresses
  142.   Serial.write(START_TEXT); //start of text
  143.   Serial.write("E"); // write special functions command
  144.   Serial.write("$AAU0400FF001BL002000002BL00200000");  // allocate memory for text file "A" and two strings, "1" and "2"
  145.   Serial.write(END_TRANSMISSION); // End of transmission
  146. }
  147.  
  148. void writeText() {
  149.   Serial.write(NUL); // Start frame sync chars
  150.   Serial.write(NUL);
  151.   Serial.write(NUL);
  152.   Serial.write(NUL);
  153.   Serial.write(NUL); // end frame sync chars
  154.   Serial.write(START_HEADER); // start of header
  155.   Serial.write("Z00"); // all sign types, all addresses
  156.   Serial.write(START_TEXT); //start of text
  157.   Serial.write("AA"); // write command, text file A
  158.   Serial.write(ESC);
  159.   Serial.write(FILL);
  160.   Serial.write(ROTATE);
  161.   Serial.write(CALL_SIZE);
  162.   Serial.write(SIZE_LARGE);
  163.   Serial.write("       Reddit: ");
  164.   Serial.write(CALL_STRING);
  165.   Serial.write("1"); // String File
  166.   Serial.write(CALL_SIZE);
  167.   Serial.write(SIZE_SMALL);
  168.   Serial.write(" (");
  169.   Serial.write(CALL_STRING);
  170.   Serial.write("2"); // String Files
  171.   Serial.write(")");
  172.   Serial.write(END_TRANSMISSION); // End of transmission
  173. }
  174.  
  175.  
  176. void writeRedditString (String str1) {
  177.   Serial.write(NUL); // Start frame sync chars
  178.   Serial.write(NUL);
  179.   Serial.write(NUL);
  180.   Serial.write(NUL);
  181.   Serial.write(NUL); // end frame sync chars
  182.   Serial.write(START_HEADER); // start of header
  183.   Serial.write("Z00"); // all sign types, all addresses
  184.   Serial.write(START_TEXT); //start of text
  185.   Serial.write("G1"); // write string, string file 1
  186.   Serial.print(str1); // data
  187.   Serial.write(END_TRANSMISSION); // End of transmission
  188. }
  189.  
  190. void writeRedditChangeString (String str1) {
  191.   Serial.write(NUL); // Start frame sync chars
  192.   Serial.write(NUL);
  193.   Serial.write(NUL);
  194.   Serial.write(NUL);
  195.   Serial.write(NUL); // end frame sync chars
  196.   Serial.write(START_HEADER); // start of header
  197.   Serial.write("Z00"); // all sign types, all addresses
  198.   Serial.write(START_TEXT); //start of text
  199.   Serial.write("G2"); // write string, string file 2
  200.   Serial.print(str1); // data
  201.   Serial.write(END_TRANSMISSION); // End of transmission
  202. }
clone this paste RAW Paste Data