// DHT11 hygrometer with GUI ver.2.0.0 // Copyright 2017 Pavel Janko, www.fancon.cz // MIT license, http://opensource.org/licenses/MIT //********************************************************************* // Add libraries DHT sensor library 1.3.0 by Adafruit and // Adafruit Unified Sensor 1.0.2 from // Arduino IDE - Sketch/Include Library/Manage Libraries ! const char* ssid = "YourSSID"; // Set your router SSID const char* password = "YourPassword"; // Set your router password bool grade = false; // Set scale - false = Celsius , true = Fahrenheit #define DHTTYPE DHT11 // Set sensor type - DHT11 or DHT22 #define DHTPIN 2 // Set pin for sensor connecting //********************************************************************* #include #include // For periodic events #include #include String GradeUnit; String buff; float temperature; float humidity; int FloatRounding = 1; float PointerAngle; bool ReadSensorFlag = true; int SensorPeriod = 5; // Period of sensor reading in seconds int PageRefreshPeriod = 30; // Period of web page refresh in seconds ESP8266WebServer server(80); DHT dht(DHTPIN, DHTTYPE); Ticker ReadSensorTick; // Preparing for periodic sensor reading void ReadSensorFlagSet(void) { // Periodic flag setting ReadSensorFlag = true; } void setup(void) { if (grade) { GradeUnit = "F"; // Setting the temperature scale to fahrenheit } else { GradeUnit = "C"; // or celsius } Serial.begin(115200); WiFi.begin(ssid, password); // Connect to router Serial.println(""); 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()); ReadSensorTick.attach(SensorPeriod, ReadSensorFlagSet); // Periodic flag setting //http main page server.on("/", HTTP_GET, []() { buff = "\n"; buff += "\n"; buff += "Hygrometer\n"; buff += "\"Hygrometer\"\n"; buff += "
\n"; server.send(200, "text/html", buff); }); // SVG picture generator server.on("/hygro.svg", HTTP_GET, []() { buff = "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "\n"; buff += "0\n"; buff += "10\n"; buff += "20\n"; buff += "30\n"; buff += "40\n"; buff += "50\n"; buff += "60\n"; buff += "70\n"; buff += "80\n"; buff += "90\n"; buff += "100\n"; buff += "% RH\n"; buff += "" + String(temperature, FloatRounding) + "\n"; buff += "" + GradeUnit + "\n"; buff += "" + String(humidity, FloatRounding) + "\n"; buff += "\n"; buff += "\n"; buff += "\n"; server.send(200, "image/svg+xml", buff); }); server.onNotFound (FileNotFound); // Fast serves answer 404 File not found as a favicon.ico etc... server.begin(); Serial.println("Web server started!"); } void loop(void) { server.handleClient(); if (ReadSensorFlag == true) { // Test for periodic reading sensor GetTemperature(); } } void GetTemperature() { humidity = dht.readHumidity(); // Read humidity (percent) temperature = dht.readTemperature(grade); // Read temperature as Celsius or Fahrenheit PointerAngle = 40 + humidity * 2.8; // Calculating angle of pointer from humidity Serial.print("Humidity % :"); Serial.println(humidity); Serial.print("Temperature " + GradeUnit + " :"); Serial.println(temperature); Serial.println(" "); ReadSensorFlag = false; } void FileNotFound(void) { server.send(404, "text/plain", "Page not found !"); }