Skip to content

Instantly share code, notes, and snippets.

@mwilliams
Created February 12, 2010 01:03
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/302180 to your computer and use it in GitHub Desktop.
Save mwilliams/302180 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[] = { 208, 97, 167, 189 }; // Google
char code[10];
int val = 0;
boolean ipAcquired = false;
int bytesread = 0;
Client client(server, 80);
void setup()
{
Serial.begin(2400);
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("getting ip...");
connectViaDHCP();
}
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);
ConnectToWebserver();
digitalWrite(2, LOW);
delay(1);
}
}
}
}
void ConnectToWebserver(){
uint8_t connectStatus;
if (client.connect()) {
Serial.print(" - Connected");
// Send the HTTP GET to the server
client.print("GET ");
client.print("/rfid/");
client.print(code);
client.print(" HTTP/1.1");
client.println();
client.println("Host: www.ultravoid.com");
client.println();
// Disconnect from the server
client.flush();
client.stop();
} else {
// Connection failed
Serial.println(" - CONNECTION FAILED!");
}
}
void connectViaDHCP()
{
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);
}
else
Serial.println("unable to acquire ip address...");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment