ESP8266 DHT11 on ESP8266 display values on a browser PC or smart phone.
Автор: Electricum
Загружено: 2024-08-12
Просмотров: 38
Описание:
This code sets up a basic web server on an ESP8266 that serves temperature and humidity data from a DHT11 sensor when accessed. Here’s a breakdown of how it works:
1. Wiring the DHT11 Sensor to the ESP8266 Node MCU:
DHT11 Pinout:
VCC: Connect to 3.3V on the ESP8266
GND: Connect to GND on the ESP8266
Data: Connect to a digital pin on the ESP8266 (e.g., D4)
2. Install the Required Libraries:
Ensure you have the DHT sensor library and Adafruit Unified Sensor library installed. You can install these via the Arduino IDE's Library Manager.
Libraries and Includes
ESP8266WiFi.h - Manages Wi-Fi connectivity for the ESP8266.
ESP8266WebServer.h - Sets up an HTTP web server to handle client requests.
DHT.h - Interfaces with the DHT11 sensor to read temperature and humidity data.
secrets.h - Stores sensitive Wi-Fi credentials, which are defined in a separate file for security.
Pin and Type Definition
DHTPIN D4 and DHTTYPE DHT11 define the DHT sensor’s pin (D4) and its model (DHT11).
DHT dht(DHTPIN, DHTTYPE); creates an instance of the DHT class to interface with the sensor.
Server and Wi-Fi Setup in setup()
Wi-Fi Connection:
WiFi.begin(ssid, password); initiates a connection to the Wi-Fi network with credentials from secrets.h.
A while loop with WiFi.status() != WL_CONNECTED checks the connection status, delaying 1 second between each attempt and printing "Connecting to WiFi...".
Once connected, it outputs "Connected to WiFi" and the device’s IP address to the serial monitor.
Web Server Setup:
server.on("/", handleRoot); defines the root route ("/") and sets handleRoot as the function that handles requests to this route.
server.begin(); starts the server, and "HTTP server started" is printed to the serial monitor.
Root Route Handler - handleRoot()
This function reads temperature and humidity data from the DHT sensor.
float temp = dht.readTemperature(); and float humidity = dht.readHumidity(); store the temperature and humidity values.
An HTML response is created to display the data:
The HTML is styled with CSS to make it visually appealing.
The temperature and humidity readings are embedded as dynamic values in the HTML.
server.send(200, "text/html", html); sends this HTML as an HTTP response when a client accesses the root URL.
Main Loop - loop()
server.handleClient(); continuously listens for incoming HTTP requests and calls the appropriate handler (like handleRoot).
secrets.h File
The secrets.h file is included to store Wi-Fi credentials securely. It defines ssid and password as constants that can be referenced in the main code without exposing them directly in the source file.
Summary of Functionality
When a client connects to the ESP8266's IP address, they are served an HTML page displaying the temperature and humidity readings from the DHT11 sensor, presented in a styled, easy-to-read format.
Code:
https://github.com/ukkokalevala/DHT11...
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: