Skip to content

Instantly share code, notes, and snippets.

@mwilliams
Created February 12, 2010 00:46
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/302170 to your computer and use it in GitHub Desktop.
Save mwilliams/302170 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
boolean ipAcquired = false;
Client client(server, 80);
void setup()
{
Serial.begin(9600);
Serial.println("getting ip...");
connectViaDHCP();
}
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...");
}
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()
{
for (int i=1; i<=50; i++) {
Serial.print("Connection Attempt ");
Serial.print(i, DEC);
ConnectToWebserver();
Serial.println();
delay(1);
}
// Enter an infinite loop to "stop" the sketch
while (true);
}
void ConnectToWebserver(){
uint8_t connectStatus;
if (client.connect()) {
Serial.print(" - Connected");
// Send the HTTP GET to the server
client.println("GET /rfid/123456 HTTP/1.1");
client.println("Host: www.ultravoid.com");
client.println();
// Disconnect from the server
client.flush();
client.stop();
} else {
// Connection failed
Serial.println(" - CONNECTION FAILED!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment