/* DHTServer - ESP8266 Webserver with a DHT sensor as an input
Based on Version 1.0 5/3/2014 Mike Barela for Adafruit Industries
and HTML code from elsewhere with my modifications
*/
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
#define DHTTYPE DHT22
#define DHTPIN 2
const char* ssid = "wireless";
const char* password = "5AEDDAD24D";
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
char tmpTemp[6]; // convert float Temperature to char array
char tmpHumidity[6]; // convert float Humidity to char array
boolean readsuccess = false; // sensor reading status
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
void handle_root() {
char temp[400];
gettemperature(); // read sensor
if(readsuccess==true)
{
dtostrf(temp_f,5, 2, tmpTemp);
dtostrf(humidity,5, 2, tmpHumidity);
Serial.print("Temperature : ");
Serial.print(tmpTemp);
Serial.println(" C'");
Serial.print("Humidity : ");
Serial.print(tmpHumidity);
Serial.println(" %'");
}
else
{
dtostrf(99.99,5, 2, tmpTemp);// 99.99 indicating error
dtostrf(99.99,5, 2, tmpHumidity);
Serial.println("ERROR READING SENSOR");
}
int sec = millis() / 1000;
int minutes = sec / 60;
int hr = minutes / 60;
snprintf ( temp, 400,
"<html>\
<head>\
<meta http-equiv='refresh' content='5'/>\
<title>ESP8266 Temp/humidity sensor</title>\
<style>\
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
</style>\
</head>\
<body>\
<h1>TEST TEMP / HUMIDITY</h1>\
<p>Uptime: %02d:%02d:%02d</p>\
<p>Temperature: %02d </p>\
<p>Humidity: %02d </p>\
</body>\
</html>",
hr, minutes % 60, sec % 60,tmpTemp,tmpHumidity
);
server.send ( 200, "text/html", temp);
yield();
}
void setup(void)
{
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\rConnecting to network");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("DHT HTML TEST");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", handle_root);
server.begin();
Serial.println("HTTP server started");
}
void loop(void)
{
server.handleClient();
}
void gettemperature() {
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(false); // Read temperature as Celcius (false)
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_f)) {
readsuccess = false;
return;
}
else
{
readsuccess = false;
}
}
}
Any help appreciated