/* Project: ESP8266 ESP-12E module, DHT module, LCD1602 I2C module Function: Webserver. Getting data from DHT module to the browser LCD1602 I2C module pin ESP8266 (Nodemcu) pin SDA D2 SCL D1 GND G(GND) VCC VU (5V) */ //************************************************************************ #include //library for ESP8266 #include //library for LCD1602 I2C #include //library for DHT sensor //************************************************************************ #define DHTPin 14 //DHT module attached to D5 pin of Nodemcu // Uncomment one of the lines below for whatever DHT sensor type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT21 // DHT 21 (AM2301) //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);//initialize the library with the numbers of the interface pins // Replace with your network credentials const char* ssid = "TR-2"; const char* password = "AQW112"; WiFiServer server(80);//set web server port number to 80 DHT dht(DHTPin, DHTTYPE);//initialize DHT sensor. // Temporary variables static char celsiusTemp[7]; static char fahrenheitTemp[7]; static char humidityTemp[7]; // Client variables char linebuf[80]; int charcount = 0; void setup() { dht.begin();// initialize the DHT sensor lcd.begin(16, 2);//initializing the 16X2 lcd display lcd.setBacklightPin(3, POSITIVE); //Switch on the backlight lcd.setBacklight(HIGH); //Initialize serial and wait for port to open: Serial.begin(9600); while (!Serial) { ; // wait for serial port to connect. Needed for native USB port only } // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); // attempt to connect to Wifi network: while (WiFi.status() != WL_CONNECTED) { // Connect to WPA/WPA2 network. Change this line if using open or WEP network: delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); server.begin(); } void loop() { // listen for incoming clients WiFiClient client = server.available(); if (client) { Serial.println("New client"); memset(linebuf, 0, sizeof(linebuf)); charcount = 0; // an http request ends with a blank line boolean currentLineIsBlank = true; while (client.connected()) { if (client.available()) { char c = client.read(); Serial.write(c); //read char by char HTTP request linebuf[charcount] = c; if (charcount < sizeof(linebuf) - 1) charcount++; // if you've gotten to the end of the line (received a newline // character) and the line is blank, the http request has ended, // so you can send a reply if (c == '\n' && currentLineIsBlank) { // 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). lcd.setCursor(0, 0); //setting display position lcd.print("Temp: "); lcd.print(celsiusTemp); lcd.print(" *C"); lcd.setCursor(0, 1); //setting display position //lcd.print("Temp: ");lcd.print(fahrenheitTemp);lcd.print(" *F\t"); lcd.print("Hum: "); lcd.print(humidityTemp); lcd.print(" %"); if (isnan(h) || isnan(t) || isnan(f)) { Serial.println("Failed to read from DHT sensor!"); lcd.setCursor(0, 0); //setting display position lcd.print("Failed to read"); lcd.setCursor(0, 1); //setting display position lcd.print("DHT sensor "); return; } else { // Computes temperature values in Celsius + Fahrenheit and Humidity float hic = dht.computeHeatIndex(t, h, false); dtostrf(hic, 6, 2, celsiusTemp); float hif = dht.computeHeatIndex(f, h); dtostrf(hif, 6, 2, fahrenheitTemp); dtostrf(h, 6, 2, humidityTemp); // You can delete the following Serial.print's, it's just for debugging purposes /*Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.print(" *F"); Serial.print("Humidity: "); Serial.print(h); Serial.print(" %\t Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.print(" *F\t Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F");*/ } // send a standard http response header client.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println("Connection: close"); // the connection will be closed after completion of the response client.println(); client.println(""); client.println(""); client.println("

ESP8266 ESP-12E - DHT 11 module

"); if (atoi(celsiusTemp) >= 25) { client.println("

"); } else if (atoi(celsiusTemp) < 25 && atoi(celsiusTemp) >= 5) { client.println("
"); } else if (atoi(celsiusTemp) < 5) { client.println("
"); } client.println(celsiusTemp); client.println("*C

"); client.println(fahrenheitTemp); client.println("*F

"); client.println(humidityTemp); client.println("%

"); client.println(""); break; } if (c == '\n') { // you're starting a new line currentLineIsBlank = true; memset(linebuf, 0, sizeof(linebuf)); charcount = 0; } else if (c != '\r') { // you've gotten a character on the current line currentLineIsBlank = false; } } } // give the web browser time to receive the data delay(1); // close the connection: client.stop(); Serial.println("client disconnected"); } }