Skip to content

Instantly share code, notes, and snippets.

@mwilliams
Created February 11, 2010 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwilliams/301070 to your computer and use it in GitHub Desktop.
Save mwilliams/301070 to your computer and use it in GitHub Desktop.
#include <Ethernet.h>
#include "Dhcp.h"
#include <string.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 10, 10, 10, 149 }; // Local Rails App
boolean ipAcquired = false;
int val = 0;
char code[10];
int bytesread = 0;
int ledPin = 12;
Client client(server, 3000);
void setup() {
Serial.begin(2400); // RFID reader SOUT pin connected to Serial RX pin at 2400bps
pinMode(2,OUTPUT); // Set digital pin 2 as OUTPUT to connect it to the RFID /ENABLE pin
digitalWrite(2, LOW); // Activate the RFID reader
Serial.println("Trying to get an IP Address.");
int result = Dhcp.beginWithDHCP(mac);
if(result == 1)
{
ipAcquired = true;
byte buffer[6];
Serial.println("ip acquired...");
Dhcp.getMacAddress(buffer);
Serial.print("mac address: ");
printArray(&Serial, ":", buffer, 6, 16);
Dhcp.getLocalIp(buffer);
Serial.print("ip address: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getSubnetMask(buffer);
Serial.print("subnet mask: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getGatewayIp(buffer);
Serial.print("gateway ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDhcpServerIp(buffer);
Serial.print("dhcp server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
Dhcp.getDnsServerIp(buffer);
Serial.print("dns server ip: ");
printArray(&Serial, ".", buffer, 4, 10);
delay(5000);
}
}
void printArray(Print *output, char* delimeter, byte* data, int len, int base)
{
char buf[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
for(int i = 0; i < len; i++)
{
if(i != 0)
output->print(delimeter);
output->print(itoa(data[i], buf, base));
}
output->println();
}
void loop() {
if(Serial.available() > 0) { // if data available from reader
if((val = Serial.read()) == 10) { // check for header
bytesread = 0;
while(bytesread<10) { // read 10 digit code
if( Serial.available() > 0) {
val = Serial.read();
if((val == 10)||(val == 13)) { // if header or stop bytes before the 10 digit reading
break; // stop reading
}
code[bytesread] = val; // add the digit
bytesread++; // ready to read next digit
}
}
if(bytesread == 10) { // if 10 digit read is complete
digitalWrite(2, HIGH);
if (client.connect()) {
Serial.println("connected");
client.println("POST /11llrhx HTTP/1.1");
client.println("Host: www.postbin.org");
client.println("Content-Length: 20");
client.println();
client.print("rfid_code=");
client.println(code);
//client.println("Content-Length: 10");
//client.println();
//client.println(code);
Serial.println("COMPLETED POST");
client.flush();
client.stop();
delay(1000);
} else {
Serial.println("the connection failed");
}
Serial.print("TAG code is: "); // possibly a good TAG
Serial.println(code); // print the TAG code
}
bytesread = 0;
delay(2000); // wait for a second
digitalWrite(2, LOW);
}
}
}
// extra stuff in case
// digitalWrite(2, HIGH); // deactivate RFID reader
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment