Webserver with Arduino

Posted by Momen 09/08/2017 0 Comment(s)

Welcome to Zeroohm Arduino Training series. In this series of Arduino Training, Zeroohm will focus its efforts on helping the community learn more about Arduino fundamentals and basics starting with basic control of LED's, motors, sensors and utilizing of wirless communication. We hope that you find this useful, Please, leave a comment for any questions or comments or reviews!

We provide all our trainings for groups from schools, universities, public sector and private sector in Abu Dhabi, Dubai, Sharjah, Fujirah, RAK or anywhere in UAE. Please,click here to contact us directly for any training inquires.

We are also an authorized Arduino distributor for the UAE, Oman, Qatar, Saudi, Kuwait and Bahrain. We can provide you with any product of your choice. Please, contact us for inquires.

 

Our Arduino training series:

1. Start with Arduino - Zeroohm Arduino Training

2. Start with Arduino - Arduino with Motors and sensors

3. Start with Arduino - Wireless communication

4. How to setup Arduino with room temperature monitoring using LM36 sensor

5. Introduction to IoT using Arduino

5. Introduction to IoT using Arduino

6. IoT: Humidity and Temperature Sensor

7. ADXL345 Module Communication with Arduino UNO Board

 

  Workshop Objectives:

Level 5 workshop aims to lets you get familiar with creating and using webservers using Arduino Software (IDE) by:

 

 

What is a webserver?

  • Learning how to create a simple Arduino webserver.

  • Learning how to show images and videos from the internet through the webserver.

  • Learning how to make a pictureserver by uploading an image from a SD card to web browser.

  • Learning how to control a LED through a webserver.

A webserver is basically a program that uses HTTP (Hypertext Transfer Protocol) to serve the files that from Web pages to users, in response to their requests, which are forwarded to their computers’ HTTP clients. Dedicated computers and appliances can be referred to as Web servers as well.

It can be described as an example of a client/server model. All computers that host websites must have Web server programs.

client-server-model

  Experiment 1: Simple Arduino Webserver

A simple webserver can be created by using an Arduino UNO board and Ethernet Shield.

In this task you will show simple HTML text files from an Ethernet Shield. The ethernet chip will be used instead of SD card.

  1. Connect the components as shown in the below figure.

arduino-ethernet-uno-webserver

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

In the above figure, the Ethernet Shield can also be mounted on top of the UNO. Now connect the UNO to the PC and the Ethernet Shield with the router using an Ethernet cable.

  1. Open Arduino software and write the following code.

#include <SPI.h> // include the SPI library

#include <Ethernet.h> // include the Ethernet library

byte mac[] = { // the mac address from the Ethernet shield(default)

 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,1,131); // the IP address of the Ethernet shield that a local server should be created on. Be sure to adapt this to your internal IP spectrum and make sure it’s not jet used.  

EthernetServer server(80); // start a server at port 80, standard port for html browser

void setup()

{

 Ethernet.begin(mac, ip); // start the server

 server.begin();

}

void loop()

{

 EthernetClient client = server.available(); // if there is a client available try to connect to it

 {

  client.print("--enter here your text--"); // send these lines to the client. You can write anything here.

  client.println();

 }

 client.println();

 delay(100);

 client.stop(); // stop connection to the client ( at this point the webpage will be loaded on your browser and the 'circle' type of thing will stop)

 }

  1. Compile and upload the code on the UNO.

  2. Wait for the board to restart. In the meantime, open a web browser of your choice and type in the IP address in the search bar.

  3. You should be able to see ‘--enter here your text--‘, on your browser.

 

Discussion

 

  • We first declare the libraries for our code which are ‘SPI.h’ and ‘Ethernet.h’, for Ethernet Shield V2, declare the library ‘Ethernet2.h’ instead. SPI (Serial Peripheral Interface bus) being our protocol for communication between UNO and Ethernet Shield.

  • Next, we declare the variables, such as ‘byte mac []’ which is the Ethernet address of the shield. It can be found physically on the shield itself. The default port for HTML is port 80.

  • In the void setup(), we initialize the server.

  • In the void loop(), we connect to a client which in this case is our PC. The text we entered in client.print() should appear on the webpage. The client.stop() ends connection with the client.

Brainstorming:

In our code, we have used SPI communication protocol to communicate between the UNO and Ethernet Shield. So how does this SPI work? Pins 10,11,12 and 13 on both the UNO and Shield are used for this sort of communication. Pin 13 (SCK) is used for synchronization purposes, pin 12 (MISO) is Master-in-slave-out, pin 11 (MOSI) is Master-out-slave-in and pin 10 (SS) is slave select for Ethernet chip. So basically, our UNO is the Master and the Shield is the Slave! One more thing to be noted is pin 4 is slave select for SD card. Make sure SD card is not inserted or connected when using Ethernet chip.

Regarding configuring the IP address, open the ‘cmd’ window and type ‘ipconfig’. This will give your computers IP address and networks subnet mask. For example, my personal computer is connected to a class C network with an IP address of ‘192.168.1.130’ and subnet mask ‘255.255.255.0’. Just change the highlighted bit into any number up to 255 which is not used currently in the network. Same procedure can be applied to class B network, just modify the last two numbers instead of just the last number.

  Experiment 2: Showing images and videos through Arduino Webserver

In this experiment, the hardware connection will be similar to the previous experiment and we will show pictures and videos already on the internet through the webserver with a little modification to the code.

  1. Open any picture in your web browser and copy its address (URL).

  2. Modify the ‘client,print()’ command of your code by replacing it with the HTML code for the image.

  3. Give a name to your image and copy paste its address. The width can height can be defined by you. For example, the width can be 100px and height 200px.

  1. The modified code is shown below.

#include <SPI.h> // include the SPI library

#include <Ethernet.h> // include the ethernet library

byte mac[] = { // the mac adress from the ethernt shield(default)

 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

IPAddress ip(192,168,1,131); // the ip addr that a local server should be created on. Be sure to adpat this to your internal IP spectrum and also make sure that it's not jet used.  

EthernetServer server(80); // start a server at port 80 standard port for html browser

void setup()

{

 Ethernet.begin(mac, ip); // start the server

 server.begin();

}

void loop()

{

 EthernetClient client = server

 {
   client.print("<body><img style='width: xpx; height: xpx;' alt='Given a  name to your picture'src='Image address (URL)'> <br></body>"); // send these lines to the client.
   client.println();
  }
  client.println();
  delay(100);
  client.stop(); // stop connection to the client ( at this point the webpage will be loaded on your browser and the 'cricle' type of thing will stop)
}

5. Do steps 3 and 4 of previous experiment and see the results.

6. For the video, you do the same by modifying the ‘client.print()’ command with the following HTML code for video below. Define height and width and copy paste the video address.

iframe-code

7. Repeat step 5 for this experiment and see the results.

Discussion:

  • Nothing much to discuss, hardware connection and software code similar to previous experiment.

  • Only difference is to modify the ‘client.print()’ command to HTML format for pictures and videos in order to display them from your webserver.

 

Experiment 3: Pictureserver

In this task, we will upload an image to the web browser through a SD card in the Ethernet Shield.

  1. In the hardware connection, make sure you connect both the pin 4 of Uno and Ethernet Shield together to enable SD slave select.

  2. Download an image from the internet or create one, with the file size not being more than 500kbyte.

  3. Format the SD card using Windows Formatting Tool. Right click on your SD card and click ‘Format’. Remember you will lose all your files in the card once format is successful.

  4. Give your image a name for example, ‘picture ‘. Then copy and paste the selected image in the SD card.

  5. You can now insert the card in the SD card slot in the Ethernet Shield.

  6. Write the following code.

#include <SPI.h> // including the SPI library

#include <Ethernet.h> // including the Ethernet library

#include <SD.h> // include the SD library

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // mac address for the ethernt shield

IPAddress ip(192,168,1, 131);  //the internal IP address on which the server gets started. Be sure to match this to your internal IP spectrum and make sure that it is not already used by another device in your Network.

EthernetServer server(80);  // Server Port

File webFile; // defining a File

void setup()

{

if (!SD.begin(4)) {return;} // Start the SD card

Ethernet.begin(mac, ip);

server.begin();// Start the Webserver

}

void loop()

{

EthernetClient client = server.available();  // check for clients that are available

if ("GET /picture.jpj") {

webFile = SD.open("picture.jpg"); // if there is. Open the picture.jpg from the SD card and transmit it to the client.

if (webFile) {

client.println("HTTP/1.1 200 OK");

client.println();

}

}

if (webFile) {

while(webFile.available()) {

client.write(webFile.read()); // Send the file to the Client

}

webFile.close(); // then close the file again

}

delay(1);

client.stop(); // disconnect from the client at this point on the clients Browser the complete picture will be loaded and the ''circle'' type thing will stop turning.

}

Experiment 4: Controlling a LED through Webserver

In this task, we will control the blinking of a LED using UNO and Ethernet Shield.

  1. Remember to remove the SD card and its connection with the UNO from the Ethernet Shield.

  2. The hardware connection is a bit modified as shown below.

webserver-arduino-led

3. Write the following code.             

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //MAC address of server
EthernetServer server(80); //standard port for server
int led = 2; //output pin of LED

 
void setup() {
pinMode(led, OUTPUT); //initializing LED to be as ouput
Ethernet.begin(mac);
server.begin(); //server initialized
Serial.begin(9600);
Serial.println("Server address:");
Serial.println(Ethernet.localIP()); //obtaining IP address for Ethernet Shield automatically
}
void loop() {
EthernetClient client = server.available(); //Checking if client is trying to connect
if (client){ // if client is connected
boolean currentLineIsBlank = true;
String buffer = ""; //Storage for what the client reads
while (client.connected()){
if (client.available()) {
char c = client.read(); //Client will now begin to read
buffer+=c;
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<center><br><h1>Led Control</h1><br><br><br><FORM>");
client.print("<P> <INPUT type=\"submit\" name=\"status\" value=\"Turn ON\">");
client.print("<P> <INPUT type=\"submit\" name=\"status\" value=\"Turn OFF\">");
client.print("</FORM></center>"); //HTML format showing the client the options to turn the LED ON and OFF
break;
}
if (c == '\n') {
currentLineIsBlank = true;
buffer="";
}
else
if (c == '\r') {
if(buffer.indexOf("GET /?status=Turn+ON")>=0)
digitalWrite(led, HIGH); // LED values is now 1
if(buffer.indexOf("GET /?status=Turn+OFF")>=0)
digitalWrite(led, LOW);  // LED value is now 0
}
else {
currentLineIsBlank = false;
}
}
}
client.stop(); // ending transmission
}
}
4. After successful uploading of the code to UNO, open serial monitor to get the IP address. 
5. Paste the IP address on the search bar of the web browser.
6. You will be taken to page like the one below.

webserver-led-home-page

8. Turn On and Off the LED on the page to see if circuit is responding.

Discussion:

  • The libraries and variables declared here are similar to the first experiment except one more variable ‘led’ is declared here.

  • In the void setup(), we initialize the server and the LED as an output on pin 2. We find the IP address from the serial monitor.

  • In the void loop(), we write the HTML format of what the client will see on the webpage.

  • ‘c == '\r'’ is what is returned to the system when the client presses one of the buttons, whether on or off which will determine the status of the LED.

  • ‘client.stop()’ command ends the transmission.

Brainstorming:

In our code, we have used ‘Ethernet.localIP()’ command which is a built-in function in Arduino that finds the IP address without having to configure the IP address manually. Also in binary there are two values only 0 and 1 and we have used that logic regarding ‘digitalWrite(led, HIGH)’ and ‘digitalWrite(led, LOW)’ with HIGH indicating 1 and LOW indicating 0. These are already built in the Arduino Software.

 

Leave a Comment