Building Arduino Projects for the Internet of Things

(Steven Felgate) #1
CHAPTER 2 ■ INTERNET CONNECTIVITY

// DHCP failed, attempt to connect to Ethernet with static IP
Ethernet.begin(mac, staticIP);
}


// Delay to let Ethernet shield initialize


delay(1000);


// Connection successful
Serial.println("[INFO] Connection Successful");
Serial.print("");
printConnectionInformation();
Serial.println("-----------------------------------------------");
Serial.println("");
}


Once Arduino has successfully connected to the Internet, the Ethernet
printConnectionInformation() function , provided in Listing 2-4 , is called. This function
prints connection information such as IP address, subnet mask, gateway, and DNS to the
Serial Monitor window.


Listing 2-4. Function to Display Connection Information


void printConnectionInformation()
{
// Print Connection Information
Serial.print("[INFO] IP Address: ");
Serial.println(Ethernet.localIP());
Serial.print("[INFO] Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("[INFO] Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("[INFO] DNS: ");
Serial.println(Ethernet.dnsServerIP());
}


Standard Functions


Finally, the code in this third and last section is provided in Listing 2-5. It implements
Arduino’s standard setup() and loop() functions. For this project, you are simply
connecting Arduino to the Internet with no processing thereafter, so the loop() function
will remain empty.

Free download pdf