Biggest Challenge (for me): Understanding the difference between Local IP and Public IP.
Materials: Arduino, Ethernet Shield, LED, 220 ohm resistor
The setup: Attach the Ethernet Shield to the Arduino and plug it into your router or access point. The LED is connected to pin 2.
The Code: The Arduino acts as a server, serving up a simple web page with a checkbox on it. When the box is checked, the LED turns on when it is unchecked the LED turns off.
The code (in the textbox below) is just copy and pasted from the (awesome) Starting Electronics website. My goal is to expand on the code in order to make a remote control car drive over the Internet.
The results: Type in your arduinos IP address followed by a colon and 8081 (this is the port you are using). See below.
Local IP vs. Public IP:
There is a ton of information available on the different IP classes and communication standards that can be found by Googling the heading for this section. I will explain it the way I understand it so far, the way that seems to work for me.
An IP address is a way that devices both within a network and outside of a network can communicate. A LOCAL IP (for example 192.168.0.15), is an address that only devices within a network can understand. This number is completely unique from the PUBLIC IP address which devices from outside of the network can understand.
Why make the distinction between Public and Local IP's?
Let's say you have two computers and an Arduino all connected to the same network (it's usually more, if you consider wi-fi enabled smartphones, network printers, etc). Traffic from all three devices all go through the router which needs a way to distinguish which device is asking for what. (It's kind of like needy children, which are the devices on a network, all asking for different things; the parent, which is the router, has to understand who needs what). Hence, all the device that communicate with the router from within the network, have a unique local IP address so the router can make a distinction and act on it accordingly.
However, all the devices within the network have the SAME public IP address. This is different then the local address and will never start with the numbers, 10, 172, or 192 because those are reserved for the local addresses. The reason why all the public IP's are the same within a network would warrant a blog post in itself, but How-To-Geek has a wonderful article.
How do Local IP's and Public IP's work together?
I will break this down into outgoing and incoming traffic:
1) Outgoing - When devices within a network ask for something outside of a local network, (say you do a google search on migration patterns of Alaskan Salmon), your router transfers the request to a server outside the network. Your router uses the Public IP address to distinguish your network from the billions of other networks on the Internet. Then, when the information you requested is found it needs to be transferred back to the right device using your Public IP and loaded in a web browser.
2) Incoming - In order to view the results of the web search on the migration patters of Alaskan Salmon your router needs to direct the webpage to the right place within a network. In reality, outside traffic does not directly reach your device. It reaches the router. The router is responsible for letting traffic through and forwarding it to your device. When the results of the websearch come back to the router, your router is by default set-up to allow traffic to be forwarded to your devices. Using your Local IP, the router knows to direct the web traffic to your, say laptop, rather than your smartphone or network printer. With today's modern infrastructure all this work happens relatively quickly and seamlessly.
How does all this relate to the Arduino?
Just like any other device on your local network, an Ethernet connected Arduino is assigned a local IP. There are a number of ways to find out your Arduinos IP, the easiest is probably to run the sketch "DhcpAdressPrinter" example sketch that is included with the Arduino IDE.
If you are simply looking for a way to turn an LED on and off from a device within the network you don't have to worry about anything else. However, if you would like to access the Ethernet shield and the Arduino from outside of your network you need to set up what is called "Port Forwarding" on your router.
Port Forwarding:
A port is your routers way of passing information to the right device. However, with ports, the same device on a network could use different ports for different applications. The specifics of Port Forwarding is an article for another day.
What is important here is making sure that your router allows the request from the Internet to reach your Arduino in order to perform an action (such as light up an LED). In order to communicate to your Arduino from the internet, you need to put into your browser the Public IP address of the network the Arduino is connected to followed by the port that you designate for the Arduino, for example; 127.xxx.xx.xx:8081. If you don't designate a port, or use the wrong port, your router will automatically block the request from reaching the Arduino.
In order to set up Port Forwarding, you need to log into your router using its Local IP address and find the option that says Port Forwarding. Different routers have their own interface but generally have the same set of options. The screenshot for my Netgear Router on the port is below.
As you can see in the picture, below the Active Forwarding Rules, I set up port 8081 as my forward port that targets my Arduino with its local IP address.
Once the port forwarding is configured you should be able to access your Arduino from anywhere by typing your Public IP followed by the port. One way to test if it works is by disabling wi-fi on your smartphone and putting in the correct address in the browser.