Arduino RFID Controller for Squeezebox - Arduino side
No notes
Syntax:
C
#include <Ethernet.h> byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; byte ip[] = { 192,168,0,250 }; byte server[] = {192,168,0,105}; Client client(server, 80); enum STATE { RFID, RFID_VERIFY, CONNECTING, READING }; static STATE state; #define DEBUG 0 #define FAKE 0 // reset pin for ethernet module, only works on LadyAda shield #define RESET_PIN 7 // RFID enable pin #define RFID_ENABLE 2 int val = 0; char reading[11]; char verify[11]; char current[11]; char target[11]; int bytesread = 0; void setup(){ // rfid is slow as all get out Serial.begin(2400); pinMode(RESET_PIN, OUTPUT); pinMode(RFID_ENABLE, OUTPUT); // sleep for 30 seconds, this gives the squeezebox time to boot up delay(30000); // reset the ethernet board digitalWrite(RESET_PIN, LOW); delay(2); digitalWrite(RESET_PIN, HIGH); delay(1000); Ethernet.begin(mac, ip); Client client(server, 80); if (DEBUG) Serial.println("Finishing setup.."); current[0] = '\0'; reading[0] = '\0'; verify[0] = '\0'; target[0] = '\0'; current[10] = '\0'; reading[10] = '\0'; verify[10] = '\0'; target[10] = '\0'; } void loop(){ if (state == RFID){ // spin a bit to see if the we are ahead of the reader Serial.flush(); digitalWrite(RFID_ENABLE, LOW); int loops = 0; while(loops < 10){ if (Serial.available()) break; loops++; delay(100); } val = Serial.read(); if((val == 10)){ // check for header bytesread = 0; while(bytesread<10){ // read 10 digit code val = Serial.read(); if((val == 10)||(val == 13)){ // if header or stop bytes before the 10 digit reading break; // stop reading } // or if it looks invalid if ((val < 'A' || val > 'Z') && (val < '0' && val > '9')){ break; } reading[bytesread] = val; // add the digit bytesread++; // ready to read next digit } // didn't read a whole tag, clear it if(bytesread != 10){ reading[0] = '\0'; } } else { reading[0] = '\0'; } if (DEBUG) Serial.print("reading: "); if (DEBUG) Serial.println(reading); // this is something is different, verify it if (strcmp(reading, current) != 0){ state = RFID_VERIFY; } bytesread = 0; if (FAKE){ reading[0] = '1'; reading[1] = '\0'; state = RFID_VERIFY; } digitalWrite(RFID_ENABLE, HIGH); delay(250); } else if (state == RFID_VERIFY){ // spin a bit to see if the we are ahead of the reader Serial.flush(); digitalWrite(RFID_ENABLE, LOW); int loops = 0; while(loops < 10){ if (Serial.available()) break; loops++; delay(100); } val = Serial.read(); if((val == 10)){ // check for header bytesread = 0; while(bytesread<10){ // read 10 digit code val = Serial.read(); if((val == 10)||(val == 13)){ // if header or stop bytes before the 10 digit reading break; // stop reading } // or if it looks invalid if ((val < 'A' || val > 'Z') && (val < '0' && val > '9')){ break; } verify[bytesread] = val; // add the digit bytesread++; // ready to read next digit } // didn't read a whole tag, clear it if(bytesread != 10){ verify[0] = '\0'; } } else { verify[0] = '\0'; } if (DEBUG) Serial.print("verify: "); if (DEBUG) Serial.println(verify); // first and second pass the same if (strcmp(reading, verify) == 0){ if (strcmp(reading, current) != 0){ strncpy(target, reading, 10); state = CONNECTING; } } else { state = RFID; } bytesread = 0; if (FAKE){ target[0] = '1'; target[1] = '\0'; state = CONNECTING; } digitalWrite(RFID_ENABLE, HIGH); delay(250); } else if (state == CONNECTING){ if (DEBUG) Serial.println("connecting..."); Ethernet.begin(mac, ip); delay(500); if (client.connect()) { if (DEBUG) Serial.println("connected"); if (strlen(target) == 10){ client.print("GET /tuner/index.php?key="); client.print(target); } else { client.print("GET /tuner/index.php?stop=1"); } client.println(" HTTP/1.0"); client.println("Host: 192.168.0.105"); client.println("User-Agent: AVR ethernet"); client.println("Accept: text/html"); client.println(); strcpy(current, target); state=RFID; client.flush(); client.stop(); } else { if (DEBUG) Serial.println("connection failed"); client.flush(); client.stop(); // reset the board if (DEBUG) Serial.println("resetting!"); // reset the ethernet board digitalWrite(RESET_PIN, LOW); delay(2); digitalWrite(RESET_PIN, HIGH); } delay(500); state = RFID; } // never get into this state, just for debugging else if (state==READING){ if (client.available()) { char c = client.read(); if (DEBUG) Serial.print(c); state=READING; } else if (!client.connected()){ if (DEBUG) Serial.println(); if (DEBUG) Serial.println("disconnecting."); client.stop(); delay(2000); state=RFID; } } }