/* Connect a DHT22 to pin 2 of the ESP8266 and the LED to Pin 5 of the ESP8266, this will allow you to control a LED and
* you can read out sensor data from a DHT sensor. This application if made for a DHT 22 but you can also change the type
* of DHT Sensor used by modifying line 21 "#define DHTTYPE DHT22"
* Pins: DHT Data: Pin 2
* LED Data: Pin 5
* VCC : 3.3V
*/
//-------------------------------------------------------------------------.-----------.
// | Libraries |
//-------------------------------------------------------------------------'-----------'
#include <ESP8266mDNS.h>
#include <ESP8266WebServer.h>
#include <DHT.h>
//-------------------------------------------------------------------------.---------------.
// | Sensor Anfang |
//-------------------------------------------------------------------------'---------------'
const int DHTPIN = 4; //Connect the sensor to pin 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int LED = 14;
//-------------------------------------------------------------------------.-------------------------.
// | Allgemeine HTML Befehle |
//-------------------------------------------------------------------------'-------------------------'
String AnfangA = "<!DOCTYPE html> <html>\n"
"<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"
"<style>" ".header1 { font-size: 40pt; text-align: center;}"
".text1 { font-size: 20pt; text-align: center;}"
".button1 { background-color: #00FF00; text-align: center; border: 2px solid black; color: black; padding: 16px 40px;}"
".button2 { background-color: #DF0101; text-align: center; border: 2px solid black; color: black; padding: 16px 40px;}"
".div1 { text-align:center;}"
"</style>";
String AnfangB = "<meta http-equiv=\"refresh\" content=\"2\" >" //refreshes the browser
"<body>\n";
String Ende = "</body>\n"
"</html>\n";
//-------------------------------------------------------------------------.--------------------.
// | Start Website HTML |
//-------------------------------------------------------------------------'--------------------'
String StartWebsite(int t,int h){
String ptr = AnfangA;
ptr +="<title>Wlan Temperatur Sensor</title>\n";
ptr += AnfangB;
ptr +="<h1 class=\"header1\">Wlan Temperatur Sensor</h1>\n";
ptr +="<p class=\"text1\">Temperatur: ";
ptr +=(int)t;
ptr +=" C</p>";
ptr +="<p class=\"text1\">Luftfeuchtigkeit: ";
ptr +=(int)h;
ptr +=" %</p>";
ptr += "<div class = div1>" ;
ptr +="<p><a href=\"LEDOn\"><button class=\"button button1\"> LED On </button>"; //If you press the button "LED On" you'll be redirected to the void LEDOn
ptr +="<p><a href=\"LEDOff\"><button class=\"button button2\"> LED Off </button>"; //If you press the button "LED On" you'll be redirected to the void LEDOff
ptr +="</p>";
ptr += "</div>";
ptr +=Ende;
return ptr;
}
//-------------------------------------------------------------------------.-----------------.
// | WiFi verbindung |
//-------------------------------------------------------------------------'-----------------'
ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80
#ifndef STASSID
#define STASSID "**********" //Change this to the name of your home wifi
#define STAPSK "********************" //Change this to the password of your home wifi
#endif
const char * SSID = STASSID;
const char * PSK = STAPSK;
//-------------------------------------------------------------------------.------------.
// | Funktionen |
//-------------------------------------------------------------------------'------------'
void handle_Sensor();
void LEDOn();
void LEDOff();
void handleNotFound();
void setup(void){
pinMode(LED, OUTPUT); //Declare LED as an output
//-------------------------------------------------------------------------.------------.
// | WiFi Start |
//-------------------------------------------------------------------------'------------'
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, PSK);
Serial.println("Connecting ...");
int i = 0;
while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect: scan for Wi-Fi networks, and connect to the strongest of the networks above
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local
Serial.println("mDNS responder started");
} else {
Serial.println("Error setting up MDNS responder!");
}
MDNS.addService("http", "tcp", 80);
//-------------------------------------------------------------------------.----------------.
// | Seiten starten |
//-------------------------------------------------------------------------'----------------'
server.on("/", handle_Sensor);
server.on("/LEDOn", LEDOn);
server.on("/LEDOff", LEDOff);
server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound"
server.begin(); // Actually start the server
Serial.println("HTTP server started");
//-------------------------------------------------------------------------.--------------.
// | Sensor Start |
//-------------------------------------------------------------------------'--------------'
pinMode(DHTPIN, INPUT); //Declare sensor as an input
dht.begin(); //Start the sensor
Serial.println("DHT11 Gestartet");
//-------------------------------------------------------------------------.--------------.
// | Setup vorbei |
//-------------------------------------------------------------------------'--------------'
}
void loop(void){
MDNS.update();
server.handleClient(); // Listen for HTTP requests from clients
}
//-------------------------------------------------------------------------.----------.
// | Websites |
//-------------------------------------------------------------------------'----------'
//-------------------------------------------------------------------------.--------------.
// | Main Website |
//-------------------------------------------------------------------------'--------------'
void handle_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);
// 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);
server.send(200, "text/html", StartWebsite(t,h));
}
//-------------------------------------------------------------------------.-------------.
// | LED Website |
//-------------------------------------------------------------------------'-------------'
void LEDOn(){
digitalWrite(LED,HIGH); //Turns the LED on and returns that value to the void "handle_Sensor"
handle_Sensor();
}
void LEDOff() {
digitalWrite(LED,LOW); //Turns the LED off and returns that value to the void "handle_Sensor"
handle_Sensor();
}
//-------------------------------------------------------------------------.----------------.
// | Fehler Meldung |
//-------------------------------------------------------------------------'----------------'
void handleNotFound(){
server.send(404, "text/plain", "404: Not found");
}
Comments