#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
// IP address
IPAddress ip (192, 168, 1, 211);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
const char* ssid = "YOURSSID";
const char* password = "YOURPASSKEY";
#define DHTPIN 2
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// built in LED to show activity
#define led 13
ESP8266WebServer server(80);
String doSensorReading(String sType){
String returnString = "";
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
} else {
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
char cHumidity[7];
char cTemperatureF[8];
char cTemperatureC[8];
char cHeatIndexF[8];
char cHeatIndexC[8];
String Humidity, TemperatureF, TemperatureC, HeatIndexF, HeatIndexC;
dtostrf(h,6,1, cHumidity);
dtostrf(f,6,1, cTemperatureF);
dtostrf(t,6,1, cTemperatureC);
dtostrf(hif,6,1, cHeatIndexF);
dtostrf(hic,6,1, cHeatIndexC);
Humidity = String(cHumidity);
TemperatureF = String(cTemperatureF);
TemperatureC = String(cTemperatureC);
HeatIndexF = String(cHeatIndexF);
HeatIndexC = String(cHeatIndexC);
if (sType == "")
returnString = "Humidity: " + Humidity + " %\t" +"Temperature: " + TemperatureC + " *C " + TemperatureF + " *F\t" + "Heat index: " + HeatIndexC + " *C " + HeatIndexF + " *F";
if (sType == "TemperatureF")
returnString = TemperatureF;
if (sType == "TemperatureC")
returnString = TemperatureC;
if (sType == "Humidity")
returnString = Humidity;
if (sType == "HeatIndexF")
returnString = HeatIndexF;
if (sType == "HeatIndexC")
returnString = HeatIndexC;
// test stuff Serial.print("Humidity: ");// Serial.print(h);// Serial.print(" %\t");// Serial.print("Temperature: ");// Serial.print(t);// Serial.print(" *C ");// Serial.print(f);// Serial.print(" *F\t");// Serial.print("Heat index: ");// Serial.print(hic);// Serial.print(" *C ");// Serial.print(hif);// Serial.println(" *F");
}
return returnString;
}
void handleRoot() {
digitalWrite(led, 1);
String output = doSensorReading("");
server.send(200, "text/plain", output );
Serial.println(output);
digitalWrite(led, 0);
}
void handleSensor(String sType) {
digitalWrite(led, 1);
String output = doSensorReading(sType);
server.send(200, "text/plain", output );
Serial.println(output);
digitalWrite(led, 0);
}
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void){
Serial.begin(9600);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
Serial.println("");
dht.begin();
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
Serial.println("MDNS responder started");
}
server.on("/", handleRoot);
server.on("/TemperatureF", [](){
handleSensor("TemperatureF");
});
server.on("/TemperatureC", [](){
handleSensor("TemperatureC");
});
server.on("/Humidity", [](){
handleSensor("Humidity");
});
server.on("/HeatIndexF", [](){
handleSensor("HeatIndexF");
});
server.on("/HeatIndexC", [](){
handleSensor("HeatIndexC");
});
server.onNotFound(handleNotFound);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
String retVal;
server.handleClient();
}
Comments
Please log in or sign up to comment.