0% found this document useful (0 votes)
74 views

Using DHCP Mode With Arduino and Ethernet Shield

This Arduino sketch uses an Ethernet shield to connect to the internet using DHCP and perform a GET request to checkip.dyndns.com to retrieve the client's IP address. It initializes the Ethernet shield using the MAC address, connects to the server when serial input of 'e' is received, sends the GET request, prints the response to the serial monitor, and closes the connection.

Uploaded by

Alex Harijanto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Using DHCP Mode With Arduino and Ethernet Shield

This Arduino sketch uses an Ethernet shield to connect to the internet using DHCP and perform a GET request to checkip.dyndns.com to retrieve the client's IP address. It initializes the Ethernet shield using the MAC address, connects to the server when serial input of 'e' is received, sends the GET request, prints the response to the serial monitor, and closes the connection.

Uploaded by

Alex Harijanto
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Solved problems: Using DHCP Mode with Arduino and Ethernet Shield

Webserver with an Arduino + Ethernet Shield

https://sites.google.com/site/coolembeddedlaboratory/home/softwares/using-dhcp-mode-with-
arduino-and-ethernet-shield

//zoomkat 3-1-13

//simple client checkip test

//for use with IDE 1.0.1 or later

//with DNS, DHCP, and Host

//open serial monitor and send an e to test

//for use with W5100 based ethernet shields

#include <SPI.h>

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address

char serverName[] = "checkip.dyndns.com"; // test web page server

EthernetClient client;

//////////////////////

void setup(){

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");

// no point in carrying on, so do nothing forevermore:

while(true);

Serial.begin(9600);

Serial.println("Better client ip test 3/1/13"); // so I can keep track of what is loaded

Serial.println("Send an e in serial monitor to test"); // what to do to test

void loop(){

// check for serial input

if (Serial.available() > 0) //if something in serial buffer

byte inChar; // sets inChar as a byte

inChar = Serial.read(); //gets byte from buffer

if(inChar == 'e') // checks to see byte is an e

sendGET(); // call sendGET function below when byte is an e

//////////////////////////
void sendGET() //client function to send/receive GET request data.

if (client.connect(serverName, 80)) { //starts client connection, checks for connection

Serial.println("connected");

client.println("GET / HTTP/1.0"); //download text

client.println("Host: checkip.dyndns.com");

client.println(); //end of get request

else {

Serial.println("connection failed"); //error message if no client connect

Serial.println();

while(client.connected() && !client.available()) delay(1); //waits for data

while (client.connected() || client.available()) { //connected or data available

char c = client.read(); //gets byte from ethernet buffer

Serial.print(c); //prints byte to serial monitor

Serial.println();

Serial.println("disconnecting.");

Serial.println("==================");

Serial.println();

client.stop(); //stop client


}

You might also like