byte NUL = 0x00;
byte START_HEADER = 0x01;
byte START_TEXT = 0x02;
byte END_TRANSMISSION = 0x04;
byte ESC = 0x1B;
byte FILL = 0x30;
byte ROTATE = 0x61;
byte SLOW = 0x15;
byte FAST = 0x19;
byte TIME = 0x13;
byte CALL_STRING = 0x10;
byte CALL_SIZE = 0x1A;
byte SIZE_LARGE = 0x33;
byte SIZE_SMALL = 0x31;
#include <SPI.h>
#include <Ethernet.h>
char response[32];
long pollingInterval = 3000; // in milliseconds
boolean failed = false;
byte mac[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
IPAddress server(XXX,XXX,XXX,XXX); //your server's IP here
EthernetClient client;
void setup () {
delay(5000);
Serial.begin(9600);
Serial.println("Allocating Memory...");
allocateMemory();
Serial.println();
Serial.println("Setting Text...");
writeText();
Serial.println();
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
while(true);
delay(1000);
}
}
void loop() {
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /~path/to/reddit.php");
client.println();
delay(1000);
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
failed = true;
}
if (!failed) {
delay(1000);
if (client.available()) {
int i = 0;
memset( &response, 0, 32 );
do {
response[i] = client.read();
i++;
delay(1);
}
while (client.available());
Serial.print("response length: ");
Serial.println(i);
response[i] = '\0';
}
Serial.print("Response from server: ");
Serial.println(response);
client.stop();
writeRedditString(response);
}
failed = false;
delay(pollingInterval);
if (client.connect(server, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /~path/to/reddit_change.php");
client.println();
delay(1000);
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
failed = true;
}
if (!failed) {
delay(1000);
if (client.available()) {
int i = 0;
memset( &response, 0, 32 );
do {
response[i] = client.read();
i++;
delay(1);
}
while (client.available());
Serial.print("response length: ");
Serial.println(i);
response[i] = '\0';
}
Serial.print("Response from server: ");
Serial.println(response);
client.stop();
writeRedditChangeString(response);
}
failed = false;
delay(pollingInterval);
}
void allocateMemory () {
// See page 41 of manual.
Serial.write(NUL); // Start frame sync chars
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL); // end frame sync chars
Serial.write(START_HEADER); // start of header
Serial.write("Z00"); // all sign types, all addresses
Serial.write(START_TEXT); //start of text
Serial.write("E"); // write special functions command
Serial.write("$AAU0400FF001BL002000002BL00200000"); // allocate memory for text file "A" and two strings, "1" and "2"
Serial.write(END_TRANSMISSION); // End of transmission
}
void writeText() {
Serial.write(NUL); // Start frame sync chars
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL); // end frame sync chars
Serial.write(START_HEADER); // start of header
Serial.write("Z00"); // all sign types, all addresses
Serial.write(START_TEXT); //start of text
Serial.write("AA"); // write command, text file A
Serial.write(ESC);
Serial.write(FILL);
Serial.write(ROTATE);
Serial.write(CALL_SIZE);
Serial.write(SIZE_LARGE);
Serial.write(" Reddit: ");
Serial.write(CALL_STRING);
Serial.write("1"); // String File
Serial.write(CALL_SIZE);
Serial.write(SIZE_SMALL);
Serial.write(" (");
Serial.write(CALL_STRING);
Serial.write("2"); // String Files
Serial.write(")");
Serial.write(END_TRANSMISSION); // End of transmission
}
void writeRedditString (String str1) {
Serial.write(NUL); // Start frame sync chars
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL); // end frame sync chars
Serial.write(START_HEADER); // start of header
Serial.write("Z00"); // all sign types, all addresses
Serial.write(START_TEXT); //start of text
Serial.write("G1"); // write string, string file 1
Serial.print(str1); // data
Serial.write(END_TRANSMISSION); // End of transmission
}
void writeRedditChangeString (String str1) {
Serial.write(NUL); // Start frame sync chars
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL);
Serial.write(NUL); // end frame sync chars
Serial.write(START_HEADER); // start of header
Serial.write("Z00"); // all sign types, all addresses
Serial.write(START_TEXT); //start of text
Serial.write("G2"); // write string, string file 2
Serial.print(str1); // data
Serial.write(END_TRANSMISSION); // End of transmission
}