Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Last active September 6, 2020 12:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggjaskiewicz/0b9e751993282fdc32e9590d1a097b14 to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/0b9e751993282fdc32e9590d1a097b14 to your computer and use it in GitHub Desktop.
nodeMCU DHT11 display and wifi server
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <limits>
#include <DHT.h>
#include <DHT_U.h>
#ifndef STASSID
#define STASSID "foo"
#define STAPSK "bar"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
int D0 = 16;
int D1 = 5;
int D2 = 4;
int D3 = 0;
int D4 = 2;
int D5 = 14;
int D6 = 12;
int D7 = 13;
int D8 = 15;
int RX = 3;
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define DHTPIN D0 // Pin which is connected to the DHT sensor.
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT_Unified dht(DHTPIN, DHTTYPE);
static float lastTemperature = std::numeric_limits<float>::infinity();
void setup() {
Serial.begin(115200);
Wire.begin(D2, D1);
dht.begin();
lcd.begin(16, 2);
lcd.home();
// Print a message to the LCD.
lcd.print("Measuring temp!");
lcd.setCursor(0, 1);
lcd.backlight();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
lcd.setCursor(0, 1);
lcd.print("connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
lcd.print(".");
delay(400);
}
lcd.setCursor(0, 0);
String status1 = String("IP:" + WiFi.localIP().toString() + " ");
lcd.print(status1);
if (MDNS.begin("esp8266")) {
lcd.setCursor(0, 1);
lcd.print("connected ");
} else {
lcd.setCursor(0, 1);
lcd.print("MDNS failed ");
delay(2000);
}
server.on("/", handleRoot);
server.begin();
}
void handleRoot() {
String tempString = String(lastTemperature, 3);
String d = String("It's " + tempString + " C here!");
if (lastTemperature == std::numeric_limits<float>::infinity()) {
d = String("no measurements done yet");
}
// digitalWrite(led, 1);
server.send(200, "text/plain", d);
// digitalWrite(led, 0);
}
// the loop function runs over and over again forever
void loop() {
// digitalWrite(D4, HIGH);
delay(50);
// digitalWrite(D4, LOW);
delay(50);
lcd.setCursor(0, 1);
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature) == false) {
lastTemperature = event.temperature;
String tempString = String(event.temperature, 3);
String d = String(tempString + " 'C ");
lcd.print(d);
} else {
lcd.print("temp fail! ");
}
server.handleClient();
MDNS.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment