public
anonymous / gist:4409287
Created

  • Download Gist
gistfile1.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#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);
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.