This project uses the ESP8266 board add-in for the Arduino 1.6.x development environment.

The best way to set this up is to follow Adafruit's guide on adding support for boards like the ESP8266 for the Arduino IDE.  This would be a the add-in for the Arduino 1.6.4+ IDE Boards menu. The ESP8266-arduino project online does have a prepackaged IDE with ESP8266 built-in.  Going forward, Adafruit recommends the 1.6.4+ add-in method of adding in support.

Required Libraries

When you install the ESP8266 support to the Arduino 1.6.4+ IDE, there should several ESP8266 examples installed.  One, ESP8266Webserver, has much of what is needed.  The libraries ESP8266WiFi, WiFiClient, and ESP8266WebServer libraries may be pre-installed (if you have the new 1.6.4+ IDE and the libraries are not visible, see this repository for the libraries with their corresponding examples).

The other library needed is the Adafruit DHT library.  If you need help knowing about Arduino software libraries, see Adafruit's great tutorial on libraries!

Begin by downloading the DHT library from the Adafruit github repository. To download, click the DOWNLOADS button in the top right corner. Rename the uncompressed folder DHT and make sure that it contains the dht.cpp file and others. Then drag the DHT folder into the arduinosketchfolder/libraries/ folder. You may have to create that libraries sub-folder if it doesnt exist.

You must restart the Arduino IDE programming program for the library to be available.


Copy the program below and save as DHTServer.ino

/* DHTServer - ESP8266 Webserver with a DHT sensor as an input

   Based on ESP8266Webserver, DHTexample, and BlinkWithoutDelay (thank you)

   Version 1.0  5/3/2014  Version 1.0   Anne Barela for Adafruit Industries
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN  2

const char* ssid     = "YourRouterID";
const char* password = "YourRouterPassword";

ESP8266WebServer server(80);
// Initialize DHT sensor 
// NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266,
// you need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold.  It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value.  The default for a 16mhz AVR is a value of 6.  For an
// Arduino Due that runs at 84mhz a value of 30 works.
// This is for the ESP8266 processor on ESP-01 
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temp_f;  // Values read from sensor
String webString="";     // String to display
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0;        // will store last temp was read
const long interval = 2000;              // interval at which to read sensor
void handle_root() {
  server.send(200, "text/plain", "Hello from the weather esp8266, read from /temp or /humidity");
void setup(void)
  // You can open the Arduino IDE Serial Monitor window to see what the code is doing
  Serial.begin(115200);  // Serial connection from ESP-01 via 3.3v console cable
  dht.begin();           // initialize temperature sensor

  // Connect to WiFi network
  WiFi.begin(ssid, password);
  Serial.print("\n\r \n\rWorking to connect");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("DHT Weather Reading Server");
  Serial.print("Connected to ");
  Serial.print("IP address: ");
  server.on("/", handle_root);
  server.on("/temp", [](){  // if you add this subdirectory to your webserver call, you get text below :)
    gettemperature();       // read sensor
    webString="Temperature: "+String((int)temp_f)+" F";   // Arduino has a hard time with float to string
    server.send(200, "text/plain", webString);            // send to someones browser when asked

  server.on("/humidity", [](){  // if you add this subdirectory to your webserver call, you get text below :)
    gettemperature();           // read sensor
    webString="Humidity: "+String((int)humidity)+"%";
    server.send(200, "text/plain", webString);               // send to someones browser when asked
  Serial.println("HTTP server started");
void loop(void)

void gettemperature() {
  // Wait at least 2 seconds seconds between measurements.
  // if the difference between the current time and last time you read
  // the sensor is bigger than the interval you set, read the sensor
  // Works better than delay for things happening elsewhere also
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis >= interval) {
    // save the last time you read the sensor 
    previousMillis = currentMillis;   

    // Reading temperature for humidity takes about 250 milliseconds!
    // Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
    humidity = dht.readHumidity();          // Read humidity (percent)
    temp_f = dht.readTemperature(true);     // Read temperature as Fahrenheit
    // Check if any reads failed and exit early (to try again).
    if (isnan(humidity) || isnan(temp_f)) {
      Serial.println("Failed to read from DHT sensor!");

This guide was first published on May 06, 2015. It was last updated on May 06, 2015.

This page (Code) was last updated on May 04, 2015.

Text editor powered by tinymce.