2011-05-21 19:22:43 +02:00
|
|
|
/*
|
|
|
|
DHCP-based IP printer
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-09-02 10:07:14 +02:00
|
|
|
This sketch uses the DHCP extensions to the Ethernet library
|
|
|
|
to get an IP address via DHCP and print the address obtained.
|
|
|
|
using an Arduino Wiznet Ethernet shield.
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-09-02 10:07:14 +02:00
|
|
|
Circuit:
|
|
|
|
Ethernet shield attached to pins 10, 11, 12, 13
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-09-02 10:07:14 +02:00
|
|
|
created 12 April 2011
|
|
|
|
modified 9 Apr 2012
|
|
|
|
by Tom Igoe
|
|
|
|
modified 02 Sept 2015
|
|
|
|
by Arturo Guadalupi
|
2013-10-21 09:58:40 +02:00
|
|
|
|
2015-09-02 10:07:14 +02:00
|
|
|
*/
|
2011-05-21 19:22:43 +02:00
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <Ethernet.h>
|
|
|
|
|
|
|
|
// Enter a MAC address for your controller below.
|
|
|
|
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
2013-10-21 09:58:40 +02:00
|
|
|
byte mac[] = {
|
|
|
|
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
|
|
|
|
};
|
2011-05-21 19:22:43 +02:00
|
|
|
|
|
|
|
// Initialize the Ethernet client library
|
2013-10-21 09:58:40 +02:00
|
|
|
// with the IP address and port of the server
|
2011-05-21 19:22:43 +02:00
|
|
|
// that you want to connect to (port 80 is default for HTTP):
|
2011-08-29 23:36:28 +02:00
|
|
|
EthernetClient client;
|
2011-05-21 19:22:43 +02:00
|
|
|
|
|
|
|
void setup() {
|
2013-10-21 09:58:40 +02:00
|
|
|
// Open serial communications and wait for port to open:
|
2011-05-21 19:22:43 +02:00
|
|
|
Serial.begin(9600);
|
2012-04-02 17:11:46 +02:00
|
|
|
// this check is only needed on the Leonardo:
|
2013-10-21 09:58:40 +02:00
|
|
|
while (!Serial) {
|
2015-09-21 14:44:19 +02:00
|
|
|
; // wait for serial port to connect. Needed for native USB port only
|
2012-04-09 15:51:35 +02:00
|
|
|
}
|
2012-04-02 15:07:58 +02:00
|
|
|
|
2011-05-21 19:22:43 +02:00
|
|
|
// start the Ethernet connection:
|
|
|
|
if (Ethernet.begin(mac) == 0) {
|
|
|
|
Serial.println("Failed to configure Ethernet using DHCP");
|
|
|
|
// no point in carrying on, so do nothing forevermore:
|
2013-10-21 09:58:40 +02:00
|
|
|
for (;;)
|
2011-08-30 21:33:32 +02:00
|
|
|
;
|
2011-05-21 19:22:43 +02:00
|
|
|
}
|
|
|
|
// print your local IP address:
|
2015-09-02 10:07:14 +02:00
|
|
|
printIPAddress();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
|
|
|
|
switch (Ethernet.maintain())
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
//renewed fail
|
|
|
|
Serial.println("Error: renewed fail");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
//renewed success
|
|
|
|
Serial.println("Renewed success");
|
|
|
|
|
|
|
|
//print your local IP address:
|
|
|
|
printIPAddress();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
//rebind fail
|
|
|
|
Serial.println("Error: rebind fail");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
//rebind success
|
|
|
|
Serial.println("Rebind success");
|
|
|
|
|
|
|
|
//print your local IP address:
|
|
|
|
printIPAddress();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
//nothing happened
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void printIPAddress()
|
|
|
|
{
|
2011-05-21 19:22:43 +02:00
|
|
|
Serial.print("My IP address: ");
|
|
|
|
for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
|
|
|
// print the value of each byte of the IP address:
|
2011-08-30 21:33:32 +02:00
|
|
|
Serial.print(Ethernet.localIP()[thisByte], DEC);
|
2013-10-21 09:58:40 +02:00
|
|
|
Serial.print(".");
|
2011-05-21 19:22:43 +02:00
|
|
|
}
|
|
|
|
|
2015-09-02 10:07:14 +02:00
|
|
|
Serial.println();
|
2011-05-21 19:22:43 +02:00
|
|
|
}
|