Created
July 14, 2011 22:24
-
-
Save neufuture/1083618 to your computer and use it in GitHub Desktop.
RFID Checkin
This file contains hidden or 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
// FourSquare Checkin | |
// EuroRSCG New York | |
// Emerging Technologies Lab | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <EthernetDNS.h> | |
#include <EthernetDHCP.h> | |
#include <Wire.h> | |
#include <SL018.h> | |
#include <EEPROM.h> | |
SL018 rfid; | |
String tag; | |
String lastTag; | |
byte mac[] = { | |
0x90, 0xA2, 0xDA, 0x00, 0x36, 0xBE }; | |
const char* ip_to_str(const uint8_t*); | |
byte serverIp[4]; | |
String apiKey = "1234"; | |
String venueId = "6593555"; | |
char hostName[] = "euro-rscg-nyc.com"; | |
//char hostName[] = "neufuture.com"; | |
int mode = 0; | |
int led[] = { | |
3,5,6,7}; | |
int piezo = 8; | |
int pitch[] = { | |
1568, 1047}; | |
String str; | |
String result; | |
byte c; | |
int romLength; | |
void setup(){ | |
Wire.begin(); | |
Serial.begin(57600); | |
for(int i=0; i<4; i++){ | |
pinMode(led[i], OUTPUT); | |
digitalWrite(led[i], HIGH); | |
delay(500); | |
digitalWrite(led[i], LOW); | |
} | |
Serial.println(""); | |
Serial.println(""); | |
Serial.println("Requesting lease..."); | |
EthernetDHCP.begin(mac); | |
const byte* ipAddr = EthernetDHCP.ipAddress(); | |
const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress(); | |
const byte* dnsAddr = EthernetDHCP.dnsIpAddress(); | |
Serial.println("A DHCP lease has been obtained"); | |
Serial.print("IP: "); | |
Serial.println(ip_to_str(ipAddr)); | |
Serial.print("Gateway: "); | |
Serial.println(ip_to_str(gatewayAddr)); | |
Serial.print("DNS: "); | |
Serial.println(ip_to_str(dnsAddr)); | |
Serial.println(); | |
EthernetDNS.setDNSServer(dnsAddr); | |
EthernetDNS.setDNSServer(EthernetDHCP.dnsIpAddress()); | |
Serial.print("Resolving "); | |
Serial.print(hostName); | |
Serial.println("..."); | |
DNSError err = EthernetDNS.resolveHostName(hostName, serverIp); | |
// Error codes in EthernetDNS.h | |
if (DNSSuccess == err) { | |
Serial.print("Server IP: "); | |
Serial.println(ip_to_str(serverIp)); | |
Serial.println(); | |
} | |
else if (DNSTimedOut == err) { | |
Serial.println("Timed out"); | |
Serial.println(); | |
} | |
else if (DNSNotFound == err) { | |
Serial.println("Does not exist"); | |
Serial.println(); | |
} | |
else { | |
Serial.print("Failed with error code "); | |
Serial.println((int)err, DEC); | |
Serial.println(); | |
} | |
// Set the Venue ID from EEPROM | |
romLength = EEPROM.read(0); | |
for(int i = 0; i < 5; i++){ | |
str += EEPROM.read(i+1); | |
} | |
// venueId = str; | |
Serial.print("Venue ID: "); | |
Serial.println(venueId); | |
Serial.println("Ready to read tags!"); | |
Serial.println(); | |
digitalWrite(led[0], HIGH); | |
} | |
void loop(){ | |
EthernetDHCP.maintain(); | |
rfid.seekTag(); // wait until tag detected | |
if(rfid.available()){ | |
tag = rfid.getTagString(); | |
if(tag != lastTag){ | |
Serial.print("New tag: "); | |
Serial.println(tag); // print tag id | |
if(mode==0) makeRequest(); | |
} | |
lastTag = tag; | |
} | |
} | |
void makeRequest(){ | |
digitalWrite(led[0], LOW); | |
digitalWrite(led[1], HIGH); | |
beep(2, 0); | |
delay(500); | |
Client client(serverIp, 80); | |
if (client.connect()) { | |
Serial.println("Connected"); | |
// Make a HTTP request | |
String request = "GET /nfc_checkin/test.php"; | |
request += "?apiKey=" + apiKey; | |
request += "&tag=" + tag; | |
request += "&venueId=" + venueId; | |
request += " HTTP/1.0"; | |
Serial.print("Requesting: "); | |
Serial.println(request); | |
client.println(request); | |
client.println(); | |
Serial.print("Response length: "); | |
Serial.println(client.available()); | |
result = ""; | |
while (client.available()>0) { | |
char c = client.read(); | |
result += c; | |
} | |
Serial.print(result); | |
if(result.endsWith("success")) { | |
beep(1,1); | |
beep(1,0); | |
blinkLight(2); | |
} | |
else if(result.endsWith("not_active")) blinkLight(3); | |
// if the server's disconnected, stop the client | |
if (!client.connected()) { | |
Serial.println(); | |
Serial.println("Disconnecting"); | |
Serial.println(); | |
client.stop(); | |
} | |
} | |
else { | |
// if you didn't get a connection to the server | |
Serial.println("No connection!"); | |
blinkLight(4); | |
} | |
if(!result.endsWith("not_active") && !result.endsWith("success")) blinkLight(3); | |
digitalWrite(led[1], LOW); | |
digitalWrite(led[0], HIGH); | |
} | |
// format an IP address. | |
const char* ip_to_str(const uint8_t* ipAddr){ | |
static char buf[16]; | |
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); | |
return buf; | |
} | |
void blinkLight(int select){ | |
for(int i=0; i<4; i++){ | |
digitalWrite(led[i], LOW); | |
} | |
for(int i=0;i<2; i++){ | |
digitalWrite(led[select], HIGH); | |
delay(250); | |
digitalWrite(led[select], LOW); | |
delay(250); | |
} | |
} | |
void beep(int num, int type){ | |
for(int i=0; i<num; i++){ | |
tone(8, pitch[type], 200); | |
delay(200); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment