[esp] Add temperature and humidity example.
This commit is contained in:
parent
8bf174d256
commit
e6ba60da89
161
esp/cpp/03_temp-humidity-web/03_temp-humidity-web.ino
Normal file
161
esp/cpp/03_temp-humidity-web/03_temp-humidity-web.ino
Normal file
@ -0,0 +1,161 @@
|
||||
// Import required libraries
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "ESPAsyncWebServer.h"
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <DHT.h>
|
||||
|
||||
// Replace with your network credentials
|
||||
const char* ssid = "network-name";
|
||||
const char* password = "password";
|
||||
|
||||
#define DHTPIN 4 // Digital pin connected to the DHT sensor
|
||||
|
||||
// Uncomment the type of sensor in use:
|
||||
#define DHTTYPE DHT11 // DHT 11
|
||||
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
|
||||
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
|
||||
|
||||
DHT dht(DHTPIN, DHTTYPE);
|
||||
|
||||
// Create AsyncWebServer object on port 80
|
||||
AsyncWebServer server(80);
|
||||
|
||||
String readDHTTemperature() {
|
||||
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
// Read temperature as Celsius (the default)
|
||||
float t = dht.readTemperature();
|
||||
// Read temperature as Fahrenheit (isFahrenheit = true)
|
||||
//float t = dht.readTemperature(true);
|
||||
// Check if any reads failed and exit early (to try again).
|
||||
if (isnan(t)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return "--";
|
||||
}
|
||||
else {
|
||||
Serial.println(t);
|
||||
return String(t);
|
||||
}
|
||||
}
|
||||
|
||||
String readDHTHumidity() {
|
||||
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
|
||||
float h = dht.readHumidity();
|
||||
if (isnan(h)) {
|
||||
Serial.println("Failed to read from DHT sensor!");
|
||||
return "--";
|
||||
}
|
||||
else {
|
||||
Serial.println(h);
|
||||
return String(h);
|
||||
}
|
||||
}
|
||||
|
||||
const char index_html[] PROGMEM = R"rawliteral(
|
||||
<!DOCTYPE HTML><html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
|
||||
<style>
|
||||
html {
|
||||
font-family: Arial;
|
||||
display: inline-block;
|
||||
margin: 0px auto;
|
||||
text-align: center;
|
||||
}
|
||||
h2 { font-size: 3.0rem; }
|
||||
p { font-size: 3.0rem; }
|
||||
.units { font-size: 1.2rem; }
|
||||
.dht-labels{
|
||||
font-size: 1.5rem;
|
||||
vertical-align:middle;
|
||||
padding-bottom: 15px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h2>ESP32 DHT Server</h2>
|
||||
<p>
|
||||
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
|
||||
<span class="dht-labels">Temperature</span>
|
||||
<span id="temperature">%TEMPERATURE%</span>
|
||||
<sup class="units">°C</sup>
|
||||
</p>
|
||||
<p>
|
||||
<i class="fas fa-tint" style="color:#00add6;"></i>
|
||||
<span class="dht-labels">Humidity</span>
|
||||
<span id="humidity">%HUMIDITY%</span>
|
||||
<sup class="units">%</sup>
|
||||
</p>
|
||||
</body>
|
||||
<script>
|
||||
setInterval(function ( ) {
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
document.getElementById("temperature").innerHTML = this.responseText;
|
||||
}
|
||||
};
|
||||
xhttp.open("GET", "/temperature", true);
|
||||
xhttp.send();
|
||||
}, 10000 ) ;
|
||||
|
||||
setInterval(function ( ) {
|
||||
var xhttp = new XMLHttpRequest();
|
||||
xhttp.onreadystatechange = function() {
|
||||
if (this.readyState == 4 && this.status == 200) {
|
||||
document.getElementById("humidity").innerHTML = this.responseText;
|
||||
}
|
||||
};
|
||||
xhttp.open("GET", "/humidity", true);
|
||||
xhttp.send();
|
||||
}, 10000 ) ;
|
||||
</script>
|
||||
</html>)rawliteral";
|
||||
|
||||
// Replaces placeholder with DHT values
|
||||
String processor(const String& var){
|
||||
//Serial.println(var);
|
||||
if(var == "TEMPERATURE"){
|
||||
return readDHTTemperature();
|
||||
}
|
||||
else if(var == "HUMIDITY"){
|
||||
return readDHTHumidity();
|
||||
}
|
||||
return String();
|
||||
}
|
||||
|
||||
void setup(){
|
||||
// Serial port for debugging purposes
|
||||
Serial.begin(115200);
|
||||
|
||||
dht.begin();
|
||||
|
||||
// Connect to Wi-Fi
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
Serial.println("Connecting to WiFi..");
|
||||
}
|
||||
|
||||
// Print ESP32 Local IP Address
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
// Route for root / web page
|
||||
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
request->send_P(200, "text/html", index_html, processor);
|
||||
});
|
||||
server.on("/temperature", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
request->send_P(200, "text/plain", readDHTTemperature().c_str());
|
||||
});
|
||||
server.on("/humidity", HTTP_GET, [](AsyncWebServerRequest *request){
|
||||
request->send_P(200, "text/plain", readDHTHumidity().c_str());
|
||||
});
|
||||
|
||||
// Start server
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void loop(){
|
||||
|
||||
}
|
56
esp/cpp/03_temp-humidity-web/README.md
Normal file
56
esp/cpp/03_temp-humidity-web/README.md
Normal file
@ -0,0 +1,56 @@
|
||||
# 03_temp-humidity-web
|
||||
|
||||

|
||||
|
||||
Temperature and humidity sensor served on a web page within the local network.
|
||||
|
||||

|
||||
|
||||
## Dependencies
|
||||
|
||||
You need to install a couple of libraries for this project:
|
||||
|
||||
* The DHT and the Adafruit Unified Sensor Driver libraries to read from the DHT sensor.
|
||||
* ESPAsyncWebServer and Async TCP libraries to build the asynchronous web server.
|
||||
|
||||
The default Arduino IDE installation will store libraries at `~/Arduino/ibraries` - if this directory doesn't exist, create it.
|
||||
|
||||
### Installing DHT Sensor Library
|
||||
|
||||
To read from the DHT sensor using Arduino IDE, you need to install the [DHT sensor library](https://github.com/adafruit/DHT-sensor-library).
|
||||
|
||||
```bash
|
||||
wget -O DHT_sensor.zip https://github.com/adafruit/DHT-sensor-library/archive/master.zip
|
||||
unzip DHT_sensor.zip
|
||||
mv DHT-sensor-library-master/ ~/Arduino/libraries/DHT_sensor
|
||||
```
|
||||
|
||||
## Install Adafruit Unified Sensor Driver Library
|
||||
|
||||
You also need to install the [Adafruit Unified Sensor Driver library](https://github.com/adafruit/Adafruit_Sensor) to work with the DHT sensor.
|
||||
|
||||
```bash
|
||||
wget -O Adafruit_sensor.zip https://github.com/adafruit/Adafruit_Sensor/archive/master.zip
|
||||
unzip Adafruit_sensor.zip
|
||||
mv Adafruit_Sensor-master/ ~/Arduino/libraries/Adafruit_sensor
|
||||
```
|
||||
|
||||
## Installing ESPAsyncWebServer Library
|
||||
|
||||
Follow the next steps to install the [ESPAsyncWebServer library](https://github.com/me-no-dev/ESPAsyncWebServer):
|
||||
|
||||
```bash
|
||||
wget -O ESPAsyncWebServer.zip https://github.com/me-no-dev/ESPAsyncWebServer/archive/master.zip
|
||||
unzip ESPAsyncWebServer.zip
|
||||
mv ESPAsyncWebServer-master/ ~/Arduino/libraries/ESPAsyncWebServer
|
||||
```
|
||||
|
||||
## Installing Async TCP Library
|
||||
|
||||
The ESPAsyncWebServer library requires the [AsyncTCP library](https://github.com/me-no-dev/AsyncTCP) to work. Follow the next steps to install that library:
|
||||
|
||||
```bash
|
||||
wget -O AsyncTCP.zip https://github.com/me-no-dev/AsyncTCP/archive/master.zip
|
||||
unzip AsyncTCP.zip
|
||||
mv AsyncTCP-master/ ~/Arduino/libraries/AsyncTCP
|
||||
```
|
BIN
esp/cpp/03_temp-humidity-web/schematic.png
Normal file
BIN
esp/cpp/03_temp-humidity-web/schematic.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 238 KiB |
BIN
esp/cpp/03_temp-humidity-web/screenshot.png
Normal file
BIN
esp/cpp/03_temp-humidity-web/screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 33 KiB |
@ -4,6 +4,7 @@
|
||||
shaunrd0/klips/esp/
|
||||
├── 01_led-button # Simple LED circuit controlled by an on board button.
|
||||
├── 02_led-button-web # LED controlled by a button or within a web browser.
|
||||
├── 03_temp-humidity-web # Temperature and humidity sensor within a web browser.
|
||||
├── ESP32-basic-starter-kit.pdf # PDF for tutorials in ESP32 starter kit.
|
||||
├── ESP32-dev-module.png
|
||||
└── README.md
|
||||
|
Loading…
x
Reference in New Issue
Block a user