Created
January 9, 2012 17:32
-
-
Save brendandawes/1584029 to your computer and use it in GitHub Desktop.
Happiness Machine code for Nanode / Arduino
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
// Happiness Machine Arduino / Nanode Code | |
// See www.brendandawes.com | |
#include <EtherCard.h> | |
#include <NewSoftSerial.h> | |
NewSoftSerial Thermal(2, 3); //Soft RX from printer on D2, soft TX out to printer on D3 | |
#define FALSE 0 | |
#define TRUE 1 | |
int printOnBlack = FALSE; | |
int printUpSideDown = FALSE; | |
const int buttonPin = 7; // the number of the pushbutton pin | |
int buttonState = 0; | |
boolean isFetchingData = false; | |
int ledPin = 13; | |
int heatTime = 255; //80 is default from page 23 of datasheet. Controls speed of printing and darkness | |
int heatInterval = 255; //2 is default from page 23 of datasheet. Controls speed of printing and darkness | |
char printDensity = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23. | |
char printBreakTime = 15; //Not sure what the defaut is. Testing shows the max helps darken text. From page 23. | |
#define REQUEST_RATE 5000 // milliseconds | |
// ethernet interface mac address | |
static byte mymac[] = { | |
0x74,0x69,0x69,0x2D,0x30,0x31 }; | |
// remote website name | |
char website[] PROGMEM = "www.brendandawes.com"; | |
byte Ethernet::buffer[700]; | |
static long timer; | |
// called when the client request is complete | |
static void my_result_cb (byte status, word off, word len) { | |
//Serial.print("<<< reply "); | |
//Serial.print(millis() - timer); | |
//Serial.println(" ms"); | |
const char *data = (const char*) Ethernet::buffer + off; | |
char *toPrint = strstr(data, "data="); | |
toPrint += 5; | |
Thermal.print(10, BYTE); | |
Thermal.print(toPrint); | |
Serial.print(toPrint); | |
isFetchingData = false; | |
// CR | |
Thermal.print(10, BYTE); | |
Thermal.print(10, BYTE); | |
Thermal.print(10, BYTE); | |
//Serial.println((const char*) Ethernet::buffer + off); | |
} | |
void setup () { | |
Serial.begin(57600); | |
Thermal.begin(19200); //Setup soft serial for ThermalPrinter control | |
printOnBlack = FALSE; | |
printUpSideDown = FALSE; | |
//Modify the print speed and heat | |
Thermal.print(27, BYTE); | |
Thermal.print(55, BYTE); | |
Thermal.print(7, BYTE); //Default 64 dots = 8*('7'+1) | |
Thermal.print(heatTime, BYTE); //Default 80 or 800us | |
Thermal.print(heatInterval, BYTE); //Default 2 or 20us | |
//Modify the print density and timeout | |
Thermal.print(18, BYTE); | |
Thermal.print(35, BYTE); | |
int printSetting = (printDensity<<4) | printBreakTime; | |
Thermal.print(printSetting, BYTE); //Combination of printDensity and printBreakTime | |
Serial.println("\n[getDHCPandDNS]"); | |
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) | |
Serial.println( "Failed to access Ethernet controller"); | |
if (!ether.dhcpSetup()) | |
Serial.println("DHCP failed"); | |
ether.printIp("My IP: ", ether.myip); | |
// ether.printIp("Netmask: ", ether.mymask); | |
ether.printIp("GW IP: ", ether.gwip); | |
ether.printIp("DNS IP: ", ether.dnsip); | |
if (!ether.dnsLookup(website)) { | |
Serial.println("DNS failed"); | |
} else { | |
ether.printIp("Server: ", ether.hisip); | |
Thermal.print("Hello. I'm connected"); | |
Thermal.print(10, BYTE); | |
Thermal.print(10, BYTE); | |
Thermal.print(10, BYTE); | |
} | |
timer = - REQUEST_RATE; // start timing out right away | |
} | |
void loop () { | |
buttonState = digitalRead(buttonPin); | |
if (ether.dhcpExpired() && !ether.dhcpSetup()) | |
Serial.println("DHCP failed"); | |
ether.packetLoop(ether.packetReceive()); | |
if (buttonState == HIGH && !isFetchingData) { | |
Serial.println("\n>>> Fetching Data..."); | |
isFetchingData = true; | |
ether.browseUrl(PSTR("/php/connectedprinter.php"), "", website, my_result_cb); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment