Created
December 29, 2012 20:55
-
-
Save anonymous/4409287 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <SD.h> | |
#include <SoftwareSerial.h> | |
#include <Adafruit_Thermal.h> | |
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; | |
const char host[] = "192.168.1.100"; | |
char cacheFilename[] = "TMP"; | |
const byte printer_RX_Pin = 8; // this is the green wire | |
const byte printer_TX_Pin = 9; // this is the yellow wire | |
const byte SD_Pin = 4; // the SD Card SPI pin | |
bool downloadWaiting = false; | |
bool statusOk = false; | |
unsigned long content_length = 0; | |
EthernetClient client; | |
Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin); | |
void die(unsigned int times) { | |
while(true); | |
} | |
void checkForDownload() { | |
Serial.print("checkForDownload "); | |
Serial.println(millis()); | |
content_length = 0; | |
statusOk = false; | |
unsigned long length = 0; | |
if (SD.exists(cacheFilename)) { | |
if (!SD.remove(cacheFilename)) { | |
die(4); | |
} | |
} | |
File cache = SD.open(cacheFilename, FILE_WRITE); | |
if(client.connect(host, 80)) { | |
client.println("GET /printer.php HTTP/1.1"); | |
client.print("Host: "); client.println(host); | |
client.println("User-Agent: arduino-ethernet"); | |
client.println("Connection: close"); | |
client.println(); | |
bool parsingHeader = true; | |
while(client.connected()) { | |
while(client.available()) { | |
if (parsingHeader) { | |
client.find((char*)"HTTP/1.1 "); | |
char statusCode[] = "000"; | |
client.readBytes(statusCode, 3); | |
statusOk = (strcmp(statusCode, "200") == 0); | |
client.find((char*)"Content-Length: "); | |
char c; | |
while (isdigit(c = client.read())) { | |
content_length = (content_length * 10) + (c - '0'); | |
} | |
client.find((char*)"\n\r\n"); | |
parsingHeader = false; | |
} else { | |
if(length < content_length) { | |
cache.write((byte)client.read()); | |
length++; | |
} else { | |
client.read(); | |
} | |
} | |
} | |
} | |
client.stop(); | |
cache.seek(0); | |
if (statusOk && content_length > 0 && (content_length == length) && (content_length == cache.size())) { | |
downloadWaiting = true; | |
} | |
} else { | |
client.stop(); | |
} | |
cache.close(); | |
} | |
void printFromDownload() { | |
Serial.println("printFromDownload"); | |
File cache = SD.open(cacheFilename); | |
byte b; | |
while (content_length--) { | |
printer.write((byte)cache.read()); | |
} | |
printer.feed(); | |
cache.close(); | |
downloadWaiting = false; | |
} | |
void setup(){ | |
Serial.begin(9600); | |
while (!Serial) { | |
; | |
} | |
delay(1000); | |
Serial.println("Init"); | |
Serial.println("Connect SD"); | |
pinMode(SD_Pin, OUTPUT); | |
if (!SD.begin(SD_Pin)) { | |
Serial.println("SD Error"); | |
die(2); | |
} | |
Serial.println("Connect Ethernet"); | |
if (Ethernet.begin(mac) == 0) { | |
Serial.println("Ethernet Error"); | |
die(3); | |
} | |
Serial.println("Connect Printer"); | |
printer.begin(255); | |
delay(1000); | |
} | |
void loop() { | |
if (downloadWaiting) { | |
printFromDownload(); | |
delay(5000); | |
} else { | |
checkForDownload(); | |
if (!downloadWaiting) { | |
delay(10000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment