Basic Arduino Web Server Tutorial (Part 1 - Overview)

Overview

In this tutorial we are going to make a basic webserver using an Arduino and a DS18B20 temperature humidity sensor. We can use an Arduino with the Ethernet shield or Arduino with a built in Ethernet interface.

When you connect to the webserver using your favourite browser it will display the current temperture which we get from the DS18B20 temperature sensor. To connect to the Arduino webserver we use the IP address that we assign in the Arduino sketch.

Sample web output for basic webserver

Note: This Arduino web server is using a private IP address and will not be accessible to the Internet. However any other computer on your internal network will be able to access the web server. To access this from the Internet you will need to enable port forwarding on your router.

Hardware Requirements

  • Arduino Board with built in ethernet or ethernet shield that supports the Arduino Ethernet library
  • DS18B20 Sensor. It also available pre-wired. For example the Adafruit PN 381 Pre-wired Waterproof Sensor
  • Ethernet Cable and Switch

Software Requirements

Reference

Hooking Up the Hardware

To keep the tutorial simple we are using the DS18B20 temperature sensor as it requires only 3 wires. Power, ground and a data wire which is connected to digital pin 2 on the Arduino board. For more details we have a hookup guide called 'How to Hookup a Maxim DS18B20 Temperature Sensor to Arduino Board' which provides more details on this sensor plus a sample sketch.

Connect Arduino to DS18B20 Temperature Sensor Hookup

The DS18B20 sensor used here is a pre-wired sensor. There is a 4.7K resistor mounted inside the heat shrink. The resistor is soldered between the power (red) and data (yellow) wires. It was then covered with black heatshrink to protect it. See the picture below.

Wire up resistor to DS18B20 temperature sensor

Once we have connected up the Arduino to the sensor and prior to installing the web server sketch we should test that the sensor is working. We can use the example sketch to read the DS18B20 and output the temperature to the console.

Programming the Arduino Board

We cannot program the Arduino via the Ethernet port, so we need to use the USB port. While we are programming and testing the Arduino board it can be powered via the USB port.

Powering the Arduino Board

We have used the USB port to provide power to the Arduino for this simple tutorial.

Testing the Hardware

The basic test sketch is shown below. Before we start on the webserver software we are going to test that the board and temperature sensor are functioning. To do this we are going to read the sensor and display the temperature on the console.

We are going to use the cactus_io_DS18B20 library which will take care of communicating with the temperature sensor. We need to tell the Arduino what pin the sensor is connected to. In this tutorial we are using digital pin 2. We then create a DS18B20 object which we use to access the sensor data. We pass the DS18B20_Pin variable when creating the DS18B20 object.


Basic_Webserver_Hardware_Test_Sketch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "cactus_io_DS18B20.h"

int DS18B20_Pin = 2; //DS18B20 Signal pin on digital 2

// Create DS18B20 object
DS18B20 ds(DS18B20_Pin); // on digital pin 2

void setup() {
ds.readSensor();
Serial.begin(9600);
Serial.println("cactus.io | Basic Webserver Tutorial");
Serial.println("Temp (C)\tTemp (F)");
}

void loop() {
// put your main code here, to run repeatedly:
ds.readSensor();

Serial.print(ds.getTemperature_C()); Serial.print(" *C\t");
Serial.print(ds.getTemperature_F()); Serial.println(" *F");

// Add a 2 second delay.
delay(2000); //just here to slow down the output.
}

If the board and sensor is functioning we should see this displayed in the console.

Sample console output for DS18B20 test

If no data is displayed then check that the power and ground are connected to the sensor. Check that the data wire is connected to digital pin 2.

In the next section we implement the web code on the Arduino. We can then open a browser and connect to the Arduino, which should respond with the temperature.


License