Components and supplies
1
DHT22 Temperature Sensor
1
LED (generic)
1
NodeMCU Lua Amica Modul V2 ESP8266 ESP-12F WIFI Wifi Development Board mit CP2102
Apps and platforms
1
Arduino IDE
Project description
Code
Code
c_cpp
1/* 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 2 * 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 3 * of DHT Sensor used by modifying line 21 "#define DHTTYPE DHT22" 4 * Pins: DHT Data: Pin 2 5 * LED Data: Pin 5 6 * VCC : 3.3V 7 */ 8 9//-------------------------------------------------------------------------.-----------. 10// | Libraries | 11//-------------------------------------------------------------------------'-----------' 12 13#include <ESP8266mDNS.h> 14#include <ESP8266WebServer.h> 15#include <DHT.h> 16 17//-------------------------------------------------------------------------.---------------. 18// | Sensor Anfang | 19//-------------------------------------------------------------------------'---------------' 20const int DHTPIN = 4; //Connect the sensor to pin 2 21#define DHTTYPE DHT22 22DHT dht(DHTPIN, DHTTYPE); 23 24const int LED = 14; 25//-------------------------------------------------------------------------.-------------------------. 26// | Allgemeine HTML Befehle | 27//-------------------------------------------------------------------------'-------------------------' 28 String AnfangA = "<!DOCTYPE html> <html>\ 29" 30 "<head><meta name=\\"viewport\\" content=\\"width=device-width, initial-scale=1.0, user-scalable=no\\">\ 31" 32 "<style>" ".header1 { font-size: 40pt; text-align: center;}" 33 ".text1 { font-size: 20pt; text-align: center;}" 34 ".button1 { background-color: #00FF00; text-align: center; border: 2px solid black; color: black; padding: 16px 40px;}" 35 ".button2 { background-color: #DF0101; text-align: center; border: 2px solid black; color: black; padding: 16px 40px;}" 36 ".div1 { text-align:center;}" 37 38 39 40 41 "</style>"; 42 String AnfangB = "<meta http-equiv=\\"refresh\\" content=\\"2\\" >" //refreshes the browser 43 44 "<body>\ 45"; 46 String Ende = "</body>\ 47" 48 "</html>\ 49"; 50//-------------------------------------------------------------------------.--------------------. 51// | Start Website HTML | 52//-------------------------------------------------------------------------'--------------------' 53 54 55String StartWebsite(int t,int h){ 56 57 String ptr = AnfangA; 58 59 ptr +="<title>Wlan Temperatur Sensor</title>\ 60"; 61 62 ptr += AnfangB; 63 64 ptr +="<h1 class=\\"header1\\">Wlan Temperatur Sensor</h1>\ 65"; 66 67 ptr +="<p class=\\"text1\\">Temperatur: "; 68 ptr +=(int)t; 69 ptr +=" C</p>"; 70 ptr +="<p class=\\"text1\\">Luftfeuchtigkeit: "; 71 ptr +=(int)h; 72 ptr +=" %</p>"; 73 74 75 ptr += "<div class = div1>" ; 76 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 77 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 78 ptr +="</p>"; 79 ptr += "</div>"; 80 81 82 ptr +=Ende; 83 return ptr; 84} 85 86//-------------------------------------------------------------------------.-----------------. 87// | WiFi verbindung | 88//-------------------------------------------------------------------------'-----------------' 89ESP8266WebServer server(80); // Create a webserver object that listens for HTTP request on port 80 90 91#ifndef STASSID 92#define STASSID "**********" //Change this to the name of your home wifi 93#define STAPSK "********************" //Change this to the password of your home wifi 94#endif 95 96const char * SSID = STASSID; 97const char * PSK = STAPSK; 98 99//-------------------------------------------------------------------------.------------. 100// | Funktionen | 101//-------------------------------------------------------------------------'------------' 102void handle_Sensor(); 103void LEDOn(); 104void LEDOff(); 105void handleNotFound(); 106 107 108 109void setup(void){ 110 pinMode(LED, OUTPUT); //Declare LED as an output 111//-------------------------------------------------------------------------.------------. 112// | WiFi Start | 113//-------------------------------------------------------------------------'------------' 114 Serial.begin(115200); // Start the Serial communication to send messages to the computer 115 delay(10); 116 Serial.println('\n'); 117 118 119 WiFi.mode(WIFI_STA); 120 WiFi.begin(SSID, PSK); 121 122 123 Serial.println("Connecting ..."); 124 int i = 0; 125 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 126 delay(250); 127 Serial.print('.'); 128 } 129 130 Serial.println('\n'); 131 Serial.print("Connected to "); 132 Serial.println(WiFi.SSID()); // Tell us what network we're connected to 133 Serial.print("IP address:\ "); 134 Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer 135 136 if (MDNS.begin("esp8266")) { // Start the mDNS responder for esp8266.local 137 Serial.println("mDNS responder started"); 138 } else { 139 Serial.println("Error setting up MDNS responder!"); 140 } 141 142 MDNS.addService("http", "tcp", 80); 143 144//-------------------------------------------------------------------------.----------------. 145// | Seiten starten | 146//-------------------------------------------------------------------------'----------------' 147 server.on("/", handle_Sensor); 148 server.on("/LEDOn", LEDOn); 149 server.on("/LEDOff", LEDOff); 150 server.onNotFound(handleNotFound); // When a client requests an unknown URI (i.e. something other than "/"), call function "handleNotFound" 151 152 server.begin(); // Actually start the server 153 Serial.println("HTTP server started"); 154 155//-------------------------------------------------------------------------.--------------. 156// | Sensor Start | 157//-------------------------------------------------------------------------'--------------' 158 159 pinMode(DHTPIN, INPUT); //Declare sensor as an input 160 dht.begin(); //Start the sensor 161 Serial.println("DHT11 Gestartet"); 162 163//-------------------------------------------------------------------------.--------------. 164// | Setup vorbei | 165//-------------------------------------------------------------------------'--------------' 166} 167 168 169 170 171 172 173void loop(void){ 174 MDNS.update(); 175 server.handleClient(); // Listen for HTTP requests from clients 176} 177 178//-------------------------------------------------------------------------.----------. 179// | Websites | 180//-------------------------------------------------------------------------'----------' 181//-------------------------------------------------------------------------.--------------. 182// | Main Website | 183//-------------------------------------------------------------------------'--------------' 184void handle_Sensor() { 185 186 float h = dht.readHumidity(); 187 // Read temperature as Celsius (the default) 188 float t = dht.readTemperature(); 189 // Read temperature as Fahrenheit (isFahrenheit = true) 190 float f = dht.readTemperature(true); 191 192 // Compute heat index in Fahrenheit (the default) 193 float hif = dht.computeHeatIndex(f, h); 194 // Compute heat index in Celsius (isFahreheit = false) 195 float hic = dht.computeHeatIndex(t, h, false); 196 197 server.send(200, "text/html", StartWebsite(t,h)); 198} 199//-------------------------------------------------------------------------.-------------. 200// | LED Website | 201//-------------------------------------------------------------------------'-------------' 202 203void LEDOn(){ 204 205 digitalWrite(LED,HIGH); //Turns the LED on and returns that value to the void "handle_Sensor" 206 207 208 handle_Sensor(); 209} 210 211void LEDOff() { 212 213 digitalWrite(LED,LOW); //Turns the LED off and returns that value to the void "handle_Sensor" 214 215 handle_Sensor(); 216} 217 218//-------------------------------------------------------------------------.----------------. 219// | Fehler Meldung | 220//-------------------------------------------------------------------------'----------------' 221void handleNotFound(){ 222 server.send(404, "text/plain", "404: Not found"); 223}
Comments
Only logged in users can leave comments