Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
Humidity Sensor Project

Humidity Sensor Project



Once set it is setup - upload the code, then you can access real-time temperature and humidity data through any browser connected to the same network. This project is not only a fun weekend activity but also an excellent educational tool for anyone interested in electronics and environmental monitoring.

We sell the kit HERE, or you can use your own parts...

Parts:

  1. D1 Mini
  2. DHT11 Sensor
  3. Wires
  4. 3D Printed Enclosure (Optional)
  5. USB Power Supply
  6. USB Cable
Humiditiy-Thumbnail.png
Screenshot_2024-05-02_205834_2048x2048.jpg
DHT11.00_01_28_07.Still001_af8086d5-6a93-4e54-9d04-57abaab734ce_2048x2048.jpg
After you upload the code, open the serial monitor and check the IP address. This is what you will put in the address bar of your browser to see the monitoring page.
Screenshot-2024-05-02-213116_2048x2048.jpg

Screenshot_2024-05-02_210031_2048x2048.jpg

DHT11.00_00_49_24.Still002_2048x2048.jpg

If you have an 128x64 OLED display the same code is already setup to display information.
Screenshot_2024-05-02_205900_2048x2048.jpg

3D Model for the enclosure can be downloaded HERE.
Screenshot_2024-05-02_204405_2048x2048.jpg



Arduino Code:
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // No reset pin
#define SCREEN_ADDRESS 0x3C // I2C address for the display

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define DHTPIN D4 // Pin where the DHT11 is connected
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

// Network credentials. Add the EXACT name for your network SSID and the EXACT password...
const char* ssid = "********";
const char* password = "********";

ESP8266WebServer server(80); // Create a webserver object that listens for HTTP requests on port 80

const float tempOffset = 0.0; // Offset in degrees Celsius for calibration

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password); // Connect to your Wi-Fi network
  while (WiFi.status() != WL_CONNECTED) { // Wait until connected
    delay(1000);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  dht.begin();

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println("SSD1306 allocation failed. Continuing without display.");
  } else {
    display.clearDisplay();
  }

  server.on("/", handleRoot); // Call handleRoot when we access the web server's root URL
  server.begin(); // Actually start the server
  Serial.println("HTTP server started.");
}

void loop() {
  server.handleClient(); // Handle client requests
  displaySensorData();
  delay(2000); // Wait a bit before reading the sensors again
}

int calculateSignalQuality() {
  int rssi = WiFi.RSSI();
  return rssi == 31 ? 0 : map(rssi, -100, -50, 0, 100);
}

void displaySensorData() {
  int h = dht.readHumidity();
  float tC = dht.readTemperature() + tempOffset; // Apply offset to Celsius reading
  float tF = tC * 9.0 / 5.0 + 32.0; // Convert adjusted Celsius to Fahrenheit

  if (isnan(h) || isnan(tC) || isnan(tF)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  String quality = getSignalQuality(WiFi.RSSI());

  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Temp: " + String(tF) + " F/" + String(tC) + " C");
  display.println("");
  display.println("Humidity: " + String(h) + "%");
  display.println("");
  display.println("Connection: " + quality);
  display.println("");
  display.println("IP address: ");
  display.println(WiFi.localIP());

  display.display();

  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("%  Temperature: ");
  Serial.print(tF);
  Serial.print(" F / ");
  Serial.print(tC);
  Serial.println(" C");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");
}

String getSignalQuality(long rssi) {
  if (rssi > -50) return "Excellent";
  else if (rssi > -60) return "Good";
  else if (rssi > -70) return "Poor";
  else return "Disconnected";
}

void handleRoot() {
  int h = dht.readHumidity();
  float tC = dht.readTemperature() + tempOffset; // Apply offset to Celsius reading
  float tF = tC * 9.0 / 5.0 + 32.0; // Convert adjusted Celsius to Fahrenheit
  String quality = getSignalQuality(WiFi.RSSI());

  String webpage = "<!DOCTYPE html><html><head><title>ESP8266 Weather Station</title><meta http-equiv='refresh' content='2'><meta charset='UTF-8'><style>body { display: flex; justify-content: center; align-items: flex-start; height: 100vh; padding-top: 20px; margin: 0; font-family: Arial, sans-serif; background-color: #f0f0f0; } .info { width: 80%; min-width: 300px; background-color: #2196F3; color: white; border-radius: 10px; padding: 20px; box-shadow: 0 4px 8px rgba(0,0,0,0.1); text-align: center; font-size: 36px; }</style><script>var lastUpdate = new Date().getTime();function updateContent(temp, humidity, quality) { document.getElementById('data-content').innerHTML = '<p>Temperature: ' + temp + ' °F / ' + ((temp-32)*5/9).toFixed(2) + ' °C</p><p>Humidity: ' + humidity + '%</p><p>Connection: ' + quality + '</p>'; lastUpdate = new Date().getTime(); } function checkSensor() { var currentTime = new Date().getTime(); if (currentTime - lastUpdate > 6000) { document.getElementById('data-content').innerHTML = '<p>Connection: Disconnected</p>'; } else { document.getElementById('data-content').parentNode.className = 'info'; } } setInterval(checkSensor, 2000); window.onload = checkSensor;</script></head><body><div class='info'><h1>Weather Station</h1><div id='data-content'><p>Temperature: " + String(tF) + " °F / " + String(tC) + " °C</p><p>Humidity: " + String(h) + "%</p><p>Connection: " + quality + "</p></div></div></body></html>";
  server.send(200, "text/html", webpage);
}
  • Like
Reactions: CryptoRulz
Author
HackMakeMod
Views
220
First release
Last update

Ratings

More projects from HackMakeMod

Back
Top