Arduino as web-server with simple web-page

Arduino board can be connected to LAN (Local Area network) with Ethernet-cards, designed  either Arduino shields or connecting by wires. This allows to run network communication programs on Arduino, for instance a small Web-server.

Actduino-to-LAN-boards

Both cards are based on  HanRun’s HR911105A RJ45 connector and Microchip’s ENC28J60 10BASE-T Ethernet Controller. Cards are controlled over Serial Peripheral Interface (SPI).

ENC28G60

Ethernet-shield for Arduino Nano

Ethernet-Shield-and Arduino-Nano  Arduino-shield-forArduino-Nano-pinouts  Ethernet-shield-attached-to-Arduino-Nano

This Ethernet-shield has a voltage regulator on-board to convert 5V to 3.3V – ENC28J60 operates with 3.3V. Optionally this shield can be connected to Arduino by wires with following pins: 5V, GND, D10, D11, D12, D13

EthernetShield-for-Arduino-Nano-connected-by-wires

hint Details of connecting pins

5V Power
GND GND
D10 CS (Chip Select). It is also known as SS (Slave Select).
Some of libraries define pin 10 to CS by default and it can be changed only in their source code. Other libraries allow to redefine the CS pin (e.g. the library for SD cards accepts this pin as an argument in the method “SD.begin()”) however in such cases it’s recommended to setup the pin 10 as OUTPUT: pinMode(10, OUTPUT);
D11 MOSI (Master Output Slave Input).
It also may be marked as MO or SI.
The pin is connected to ATMega’s pin PB3 (MOSI/OC2A/PCINT3)
D12 MISO (Master Input Slave Output).
It also may be marked as MI or SO.
The pin is connected to ATMega’s pin PB4 (MISO/PCINT4).
D13 SCK (Serial Clock).
The pin is connected to ATMega’s pin PB5 (SCK/PCINT5).

Ethernet card connected with wires (not Arduino-shield)

This card need to be powered by 3.3V (from Arduino pin or to 3.3V power adapter). The card  is connected with following pins:

Arduino Nano Ethernet card
3.3V VCC
GND GND
D10 CS
D11 SI
D12 SO
D13 SCK

Setup a Web-server on Arduino

There are several libraries which can connect Arduino to network. Some of them give more features (e.g. support of DHCP) and stability but also need more efforts to learn and make compiled sketch larger.

Easy to use library to start with is the Ethernet library “ETHER_28J60” by Simon Monk. He made this library as a wrapper of the Nuelectronics library “etherShield” to simplify work with it.

hint Install libraries ETHER_28J60 and etherShield

This wrapper library can be downloaded from from github (or from Simon’s archive – but ETHER_28J60.cpp need to be corrected: replace “WProgram.h” with “Arduino.h” in #include directive).

Site Nuelectronics is not available so “etherShield” can be downloaded also from github. There are other versions in this or this github repositories – more features, but file names are started from capital E, so wrapper’s ETHER_28J60.cpp directive #include “etherShield.h” need to be corrected to #include EtherShield.h”.

Both libraries need to be unarchived to folders ETHER_28J60 and etherShield respectively and copied to the Arduino libraries folder:

Windows: C:\Program Files\Arduino\libraries
Linux: /usr/share/arduino/libraries

After installing libraries – restart the Arduino IDE, select menu item File >> Examples >> ETHER_28J60 >> HelloWorld

Arduino-select-example-ETHER_28J60-hello-world

The sketch is short and pretty clear. Only following lines might needed to be corrected:

static uint8_t mac[6] = {0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
static uint8_t ip[4] = {192, 168, 1, 15};

mac and ip – are MAC address and IP address of a network device – in this case addresses of an Arduino board connected to a network via an Ethernet card. Both addresses should be unique within the network (e.g. local network behind the home network router).

hint What is MAC address

An address which should be unique within the network. Usually such address may be printed on the label on network devices. E.g. at the bottom side of laptops some times there are MAC addresses of its LAN and WiFi adapters.

The address presented above may be written other ways (e.g. in router’s web-interfaces of GUI settings forms):

54:55:58:10:00:24 or 545558100024

MAC address can be left as is if there are no other devices with same address within the network.

hint MAC address vs IP address

OSI model (Open Systems Interconnection model) specifies seven layers of a communication system. While MAC address (Media Access Control address) operates on the law layer 2 (Data Link layer), IP address (Internet Protocol address) operates on more higher level 3 (Network layer). This discussion explains difference between these two addresses.

Upload the sketch on Arduino, connect the Ethernet card to a router by a network cable.

Connect-ethernet-bord-with-arduino-to-router

Check if Arduino works (Power LED is on), LED on Ethernet connector is on (second LED on the connector can blink), the router is on and its LED is on for the port to which the Ethernet card is connected.

LEDs-on-Arduino-and-ethernet-board

“etherShield” library from this repository (suggested to be used earlier) uses static IP address – when the address need to be configured on a router manually.

“EtherShield” library from this repository additionally has support of DHCP (Dynamic Host Configuration Protocol) when DHCP server (usually existing on the router) assigns an IP address to network devices automatically.

hint Configure static IP address on the router

Static network address can be configured on the local router (also known as network- or WiFi-router) over web-interface. Routers commonly use for its web-interfaces addresses 192.168.0.1 or 192.168.1.1 – this information can be fount on a label at a bottom of a router:

Router-label

Open a web-browser, type 192.168.0.1 (or 192.168.1.1) in the address bar, hit Enter and follow the instruction below (this example is for a D-Link router).

Setup-address-in-network-for-ethernet-board-with-arduino

  1. Login to a web-interface of the router
  2. Select the tab NET
  3. Select the tab LAN
  4. Hit the button “Add” in the “Static DHCP” section
  5. fill-in the IP address defined in the Arduino sketch
  6. fill-in the MAC address defined in the Arduino sketch – MAC address might need different separators than it is used in the sketch
  7. Type any desired Host name – “Arduino” in this example. This name can be used in web-browser address bar instead of the IP address
  8. Hit the “Save” button and wait while the router is restarted
  9. Open a web-browser and type the IP address in the address bar, hit Enter – a web page with “Hello World” is loaded from Arduino
  10. Type in the web-browser’s address bar chosen Host name, hit Enter – same web-page is loaded from Arduino

Open a web-browser and type in the address bar an IP address 192.168.1.15 (or the address specified in sketch and configured on the router as shown in instruction earlier), hit Enter – a web page with “Hello World” is loaded from Arduino.

Type in the web-browser’s address bar chosen Host name, hit Enter – same web-page is loaded from Arduino

Hello-world-web-page-from-arduino

In some cases it does not work – check if Arduino is connected to a power source (e.g. to USB), LEDs are indicating activity (restart the Arduino – they should start blinking for a while), check wires – some of them looks fine, but actually do not connect contacts.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmail