Accessing An Arduino Over The Internet

Wouldn’t it be great if you could access information on your Arduino from anywhere in the world? You could read information from sensors, access files or use it to control lights and electronics around your home and this is all possible with an Ethernet shield. An Ethernet shields allows your Arduino to connect to the internet via an Ethernet cable plugged into your router and enables it to communicate with the outside world.

There are two ways in which an Ethernet connected Arduino can be setup:

  1. As a Web Server, the Arduino acts as a web server which can be accessed from the internet. Commands can be sent to the Arduino and it can control things around the house. It can also be used to display information such as sensor readings on a web page or send stores files.
  2. A a Client, the Arduino acts as a web client and posts data to a database stored on a remote server. This is particularly useful for data logging or handling large amounts of information.

This guide will be dealing mainly with the first setup, getting the Arduino to act as a simple web server and serve a web page with some information on it. If this is your first Arduino project then we recommend that you read through our getting started guide to familiarise yourself with the basics and programming the Arduino.

arduino ethernet shield

Arduino Ethernet Shield by Teardown Central

What You Will Need To Access Your Arduino Over The Internet

  • An Arduino (An Uno is used here)
  • Ethernet Shield (Needs to be compatible with the Arduino board)
  • USB A to B Cable
  • Router (Needs to be connected to the internet for external connections)
  • Ethernet Cable (With RJ45 plug)

Understanding The Ethernet Shield

The first step in getting your Ethernet shield communicating with the outside world is to get a basic understanding on how the shield and an Ethernet network functions.

Your Arduino’s Addresses

Every device which is connected to the internet has an address, much like your home address. This identifies the device on the network so that data can be sent to and from the specific device. There are two types of addresses used to identify devices on a network, these are the MAC address  which is unique to every device and is usually loaded when the device is manufactured and the IP address which may be fixed or may change depending on your network configuration.

When you plug your Arduino (or any device for that matter) into your router, your router automatically assigns an IP address to it which remains the same until the device is unplugged or it is switched off. The router manages all of the IP addresses within your home network and allows them to communicate to each other.

Your Router’s Addresses

A router has two IP addresses, an internal IP address and an external IP address. The internal IP address is used to identify the router to all of the devices connected to your home network while the external IP address is used to interact with the devices and servers on the internet.

These two addresses are quite important as you will need to use a different method to communicate with your Arduino over your local network and over the internet. When you access your Arduino over your local network, you can communicate with the Arduino directly through its own IP address whereas when you communicate with it over the internet, you need to access your router through its IP address and the router will then forward your commands to and from the Arduino.

Connect The Hardware

The hardware connections in this example are quite straight forward as we are not using any additional sensors or switches.

Plug the Ethernet shield into your Arduino making sure that the pin headers line up with the connect pins on the board and the shield.

Now plug the Ethernet cable into the Arduino shield and then into your home router. Plug the USB cable into the Arduino and then into your laptop, PC or Mac.

Loading The Arduino Web Server Sketch

The code is quite straight forward, shown below, although some HTML knowledge is advantageous for the actual web page which is served by the Arduino.

#include <Ethernet.h>
#include<SPI.h>

byte mac[] = {0011, 002B, 003C, 004D, 005E, 0066}; //Define your mac address
EthernetServer serve = EthernetServer(55);  //Create a server which listens on port 55


void setup()
{
  Serial.begin(9600);
  if(Ethernet.begin(mac) == 0)                        
  { 
    Serial.println("Configuration failed");
    return;
  }
  else
  {
    Serial.print("Arduino IP:");   //Displays your Arduino's local IP address
    Serial.println(Ethernet.localIP());
  }
  serve.begin();
  Serial.println("Server Running");
}

void loop()
{
  Serial.println("Waiting For Client Connection");
  EthernetClient client = serve.available();  //Looks for incoming client connections
  if(client)
  {
    Serial.print("Connected to client: ");
    while(client.available())
    {             
      char c = client.read();
      Serial.print(c);
    }
    if(client.connected())                        
    {
      Serial.println("Webpage Sent To Client");   //Delivers a web page to the client
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html\n");
      client.println("<center><h1>Connected To Your Arduino</h1></center>");
      client.println("<center><h2>Hello John</h2></center>");   
    }  
    delay(8);
    client.stop();    //Closes the client connection
    Serial.println("Disconnected from client");
  }
  delay(1500);
}

Download the WebServer code here.

The Basic Code Functions

We use the two built in libraries Ethernet.h which lets us use the built in Ethernet communication functions library and SPI.h which defines how the Arduino communicates with and controls the Ethernet shield.

Next we define the mac address for the Ethernet shield. If you have a genuine Arduino Ethernet shield then the mac address will be printed on a sticker on the back of the shield and you can remove this line of code.

Now we use the Ethernet Server function to create a server called serve which listens for an incoming connection on the specified port. You can change the port number if you’d like as long as there is no other communications running on that port already.

The setup method then starts up the connection with the router using the defined mac address and displays the IP address assigned to the Arduino in the serial monitor window.

The loop functions is then repeated which waits for client connections, connects to the client when a request is made and then serves a basic HTML web page to the client. This web page can be customised and can include sensor values, variables and strings etc. The loop function is run every one and a half seconds to check for client connections.

Upload the sketch onto the Arduino and open up the serial monitor on your computer, this is accessed through Tools > Serial Monitor. If your Ethernet shield is connected to your router, when the software is loaded and the Arduino starts up you should see your Arduino’s IP address, Server Running and then Waiting For Client Connection repeated every one and a half seconds.

Connect To Your Arduino Over Your Local Network

If you open up your internet browser on your computer (which also needs to be connected to the router either via WiFi or Ethernet) and type the following address into your URL address bar you should be served with your Arduino’s web page:

http://”Your Arduino IP”:”Port” for example http://192.168.0.5:55

You should then get a client connection notification on the serial monitor and shortly afterwards your Arduino’s served web page should be displayed.

arduino served web page

Accessing Your Arduino From Outside Your Local Network

Finally, we need to configure your router so that you can access your Arduino from anywhere over the internet.

You need to login to your router’s configuration page, first of all, look on the base of your router or in its manual for information on accessing this page. Generally, it is accessed through the routers IP address which is usually the first in the local network range, if your Arduino’s IP address was 192.168.0.5 then it is likely the the router’s is 192.168.0.1. Type this into your browsers URL bar and you should reach your router login page where you will need to supply your router’s username and password, these may still be the defaults such as admin or administrator etc. They can be found on the internet for different manufacturers.

Reserve Your Arduino’s IP Address

Find the LAN settings in your routers configuration settings. Try to reserve your Arduino’s IP address, this is usually a tick box and you can identify your Arduino by the mac address or IP address. This means that your router will always assign that IP address to your Arduino.

Setup Port Forwarding

Now you need to set up port forwarding. Create a user defined port forwarding service which is forwarded to the port you set up in your Arduino sketch, 55 in this case. The port forwarding enables us to communicate with the Arduino from the internet. The router takes the request made from the internet to the router and forwards the request to the Arduino on the correct port inside the local network.

Now look for your routers connection information page. You need to find your routers external IP address, this is different to the internal IP address and is the address that your service provider provided to your router when the router was switched on. This should be displayed somewhere with the connection status or connection information.

Save the settings and close your router page.

Access Your Arduino From The Internet

Your should now be able to access your Arduino from any internet connected device. Simply type in the address as above but this time with the routers external IP and the Arduino’s port:

http://”Router External IP”:”Port” for example http://172.16.5.5:55

You should now be served with your Arduino’s web page.

If you’ve managed to get the webpage to load correctly, now try to connect a sensor or switch and display the readout or status of the switch via your web page. This is the perfect start to your next Internet of Things project.

Did this guide help you? Please let us know any questions or queries you may have in the comments section below. If you enjoyed this project then try building your own Raspberry Pi Cloud Storage or build a home energy meter using your Arduino and start monitoring your energy usage.

sensors connected to arduino ethernet shield

Arduino Ethernet by 37Hz

Share This Guide:

connect your arduino to the internet

Cover Image: Arduino Ethernet by 37Hz. All referenced images used in this post are used and modified under CC BY 2.0
Michael Klements
Michael Klements
Hi, my name is Michael and I started this blog in 2016 to share my DIY journey with you. I love tinkering with electronics, making, fixing, and building - I'm always looking for new projects and exciting DIY ideas. If you do too, grab a cup of coffee and settle in, I'm happy to have you here.

12 COMMENTS

  1. Great guide about accessing arduino over the internet and Easy to understand embedded beginners . Thanks for sharing informative article

  2. Dear Mr michael, Well crafted article. Thank you. I have some queries. Am using an arduino board with builtin ethernet support, arduino etherten board. I have some sensors, whose values are to be ported to the internet. I am trying to configure the arduino as a web server. I am not clear, where the ethernet RJ45 cable from the arduino has to be connected?. Is it to the router or to my computer ethernet socket??
    If I can connect it to my PC, should i use a twisted pair RG45 cable or any regular RG45 cable?
    Can i check IP address of the arduino board by trying to use the cmd prompt in my PC (with ipconfig).

    Thanks
    Áravind

    • Hi Aravind,
      Your Arduino needs to be connected to your router with a regular RG45 ethernet patch cable. The connection to your PC discussed in the guide is your regular USB interface which you use to program it. The easiest way to find your Arduino’s IP address is to open up the Serial monitor on your pc with the Arduino connected and running and it will display the IP address using the example code.

  3. Hi Michael

    This is a fabulous tutorial. Very simple, very well presented. I congratulate you and thank you.

    I’m trying to make an Arduino MQTT to subscribe to my RAK7249 gateway and this has not been successful. I decided that I would try to get an MQTT (MQTT.fx) hosted on a laptop (windows 10) to subscribe to the gateway. Make that go and proceed from there.

    I managed to make that go. Very exciting, very satisfying.

    So now I have moved on to make the Arduino subscribe to the gateway. I have followed the code to the letter, but the Arduino monitor just sits and stares at me, and the Arduino port to the laptop disappears, crashes. I have determined that the “network connection has failed”.

    So I decided that I’d try and get the Arduino to simply act as a server without any subscribing. If I can get that to work, then i think it will be easy to get the Arduino MQTT to work. I found your tutorial.

    I have followed that to the letter but the outcome is the monitor sits and stares at me. Nothing at all is printed on the monitor. The port fails.

    As it turns out the code on the Arduino MQTT is almost exactly the code in ur tutorial. So there is something very basic gone wrong.

    The Arduino IDE does not let me load the programme. It hangs in the middle of that process and says that I’m no longer connected. I reconnect the port but its all dead in the water. I have to reboot the laptop to get it going again …. and then it hangs again.

    Have you any ideas what’s happening, please.

    Many thanks

    John

  4. hello I’m currently interested in you Accessing An Arduino Over The Internet page and was wondering after doing all the steps and turning the Arduino into a web sever, is it possible to to connect multiple other Arduinos up to that web sever by a wireless connection as so to be able to control a multiple number of Arduinos through the web sever.

    • Hi Jack,
      You could probably set up what you have described, but the setup of the other ones will be a bit different. In this example you’re accessing the Arduino running the web server, the other one’s you’ve described will need to access this web server to look at the state of a field and then act accordingly.

  5. correct comment :
    First of all , thank you for your very beneficial tutorial.
    Currently I am on a remote farm irrigation project and I would like to know how to send instructions to the arduino board from a mobile application or website to start the irrigation.
    To simplify the trick for you, for example how to turn on and off an LED connected to the arduino board from a mobile application by accessing it through the internet (not local network).
    Thank you so much .

    • If you don’t have a fixed IP address at home (which is typically the case) then you’ll need to use a third-party service like Twilio to activate it remotely.

      • Hi Michael ,
        Fixed IP will not be a problem I will ask the internet provider to reserve me some external IP but currently I don’t have any idea how a mobile application interacts with an arduino board. in other words how to control an arduino board through a mobile application or website.
        if you have some examples or tutorials please help me.
        Thank you so much .

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest posts

Gweike G2 Pro 30W Fibre Laser Unboxing & Review

I've been using diode and CO2 lasers in my home workshop for quite some time. Combined with a 3D printer, they've been fantastic for...

Khadas Mind 2 Unboxing & Review

Over the past two years, I've tried two of Khadas' single board computers which were aimed at being compact and low power computers for...

Elegoo Mars 5 Ultra Unboxing and Review

The Mars 5 Ultra is the latest revision to Elegoo's successful Mars series of SLA printers. Its product page advertises an impressive set of...

Related posts