This is the script:
#include <Arduino.h>
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com
*********/
// Import required libraries
#include "WiFi.h"
#include "ESPAsyncWebServer.h"
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Replace with your network credentials
const char *ssid = "ssid";
const char *password = "password";
#define DHTPIN 27 // 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()
{
}
These are the errors:
The terminal process "C:\Users\joema\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
> Executing task in folder Web_Server_DHT11_WeMos: C:\Users\joema\.platformio\penv\Scripts\platformio.exe run <
Processing wemos_d1_mini32 (platform: espressif32; board: wemos_d1_mini32; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/wemos_d1_mini32.html
PLATFORM: Espressif 32 1.12.4 > WeMos D1 MINI ESP32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-arduinoespressif32 3.10004.200129 (1.0.4)
- tool-esptoolpy 1.20600.0 (2.6.0)
- toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 48 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <ESP Async WebServer> 1.2.3
| |-- <AsyncTCP> 1.1.1
| |-- <FS> 1.0
| |-- <WiFi> 1.0
|-- <WiFi> 1.0
|-- <Adafruit Unified Sensor> 1.1.4
|-- <DHT> 1.2.1
| |-- <Ticker> 1.1
Building in release mode
Compiling .pio\build\wemos_d1_mini32\src\main.cpp.o
Compiling .pio\build\wemos_d1_mini32\lib282\ESP Async WebServer\SPIFFSEditor.cpp.o
Compiling .pio\build\wemos_d1_mini32\lib282\ESP Async WebServer\WebAuthentication.cpp.o
Compiling .pio\build\wemos_d1_mini32\lib282\ESP Async WebServer\WebHandlers.cpp.o
src\main.cpp:25:24: error: expected primary-expression before ')' token
DHT dht(DHTPIN, DHTTYPE);
^
src\main.cpp:25:5: error: cannot declare variable 'dht' to be of abstract type 'DHT'
DHT dht(DHTPIN, DHTTYPE);
^
In file included from C:/Users/joema/.platformio/lib/DHT/src/DHT.h:28:0,
from src\main.cpp:12:
C:/Users/joema/.platformio/lib/DHT/src/DHT.hpp:36:7: note: because the following virtual functions are pure within 'DHT':
class DHT {
^
C:/Users/joema/.platformio/lib/DHT/src/DHT.hpp:43:17: note: virtual float DHT::getTemperature()
virtual float getTemperature() = 0;
^
C:/Users/joema/.platformio/lib/DHT/src/DHT.hpp:44:17: note: virtual float DHT::getHumidity()
virtual float getHumidity() = 0;
^
src\main.cpp: In function 'String readDHTTemperature()':
src\main.cpp:34:33: error: expression cannot be used as a function
float t = dht.readTemperature();
^
src\main.cpp: In function 'String readDHTHumidity()':
src\main.cpp:53:30: error: expression cannot be used as a function
float h = dht.readHumidity();
^
src\main.cpp: In function 'void setup()':
src\main.cpp:148:7: error: 'class DHT' has no member named 'begin'
dht.begin();
^
Compiling .pio\build\wemos_d1_mini32\lib282\ESP Async WebServer\WebRequest.cpp.o
*** [.pio\build\wemos_d1_mini32\src\main.cpp.o] Error 1
================================================= [FAILED] Took 10.70 seconds =================================================
The terminal process "C:\Users\joema\.platformio\penv\Scripts\platformio.exe 'run'" terminated with exit code: 1.
Terminal will be reused by tasks, press any key to close it.
> Executing task in folder Web_Server_DHT11_WeMos: C:\Users\joema\.platformio\penv\Scripts\platformio.exe run <
Processing wemos_d1_mini32 (platform: espressif32; board: wemos_d1_mini32; framework: arduino)
--------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/espressif32/wemos_d1_mini32.html
PLATFORM: Espressif 32 1.12.4 > WeMos D1 MINI ESP32
HARDWARE: ESP32 240MHz, 320KB RAM, 4MB Flash
DEBUG: Current (esp-prog) External (esp-prog, iot-bus-jtag, jlink, minimodule, olimex-arm-usb-ocd, olimex-arm-usb-ocd-h, olimex-arm-usb-tiny-h, olimex-jtag-tiny, tumpa)
PACKAGES:
- framework-arduinoespressif32 3.10004.200129 (1.0.4)
- tool-esptoolpy 1.20600.0 (2.6.0)
- toolchain-xtensa32 2.50200.80 (5.2.0)
LDF: Library Dependency Finder -> http://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 48 compatible libraries
Scanning dependencies...
Dependency Graph
|-- <ESP Async WebServer> 1.2.3
| |-- <AsyncTCP> 1.1.1
| |-- <FS> 1.0
| |-- <WiFi> 1.0
|-- <WiFi> 1.0
|-- <Adafruit Unified Sensor> 1.1.4
|-- <DHT> 1.2.1
| |-- <Ticker> 1.1
Building in release mode
Compiling .pio\build\wemos_d1_mini32\src\main.cpp.o
Compiling .pio\build\wemos_d1_mini32\lib282\ESP Async WebServer\WebResponses.cpp.o
Compiling .pio\build\wemos_d1_mini32\lib282\ESP Async WebServer\WebServer.cpp.o
Compiling .pio\build\wemos_d1_mini32\libe0a\Adafruit Unified Sensor\Adafruit_Sensor.cpp.o
src\main.cpp:25:24: error: expected primary-expression before ')' token
DHT dht(DHTPIN, DHTTYPE);
^
src\main.cpp:25:5: error: cannot declare variable 'dht' to be of abstract type 'DHT'
DHT dht(DHTPIN, DHTTYPE);
^
In file included from C:/Users/joema/.platformio/lib/DHT/src/DHT.h:28:0,
from src\main.cpp:12:
C:/Users/joema/.platformio/lib/DHT/src/DHT.hpp:36:7: note: because the following virtual functions are pure within 'DHT':
class DHT {
^
C:/Users/joema/.platformio/lib/DHT/src/DHT.hpp:43:17: note: virtual float DHT::getTemperature()
virtual float getTemperature() = 0;
^
C:/Users/joema/.platformio/lib/DHT/src/DHT.hpp:44:17: note: virtual float DHT::getHumidity()
virtual float getHumidity() = 0;
^
src\main.cpp: In function 'String readDHTTemperature()':
src\main.cpp:34:33: error: expression cannot be used as a function
float t = dht.readTemperature();
^
src\main.cpp: In function 'String readDHTHumidity()':
src\main.cpp:53:30: error: expression cannot be used as a function
float h = dht.readHumidity();
^
src\main.cpp: In function 'void setup()':
src\main.cpp:148:7: error: 'class DHT' has no member named 'begin'
dht.begin();
Archiving .pio\build\wemos_d1_mini32\libe0a\libAdafruit Unified Sensor.a
^
Compiling .pio\build\wemos_d1_mini32\lib8c6\Ticker\Ticker.cpp.o
Archiving .pio\build\wemos_d1_mini32\lib8c6\libTicker.a
Compiling .pio\build\wemos_d1_mini32\lib068\DHT\DHT.cpp.o
*** [.pio\build\wemos_d1_mini32\src\main.cpp.o] Error 1
================================================= [FAILED]
This is the library I edited to add a few class members:
/*
Copyright 2018 Bert Melis
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <functional>
#include <Arduino.h>
#include <Ticker.h>
#include <FunctionalInterrupt.h>
typedef std::function<void(int8_t)> Callback;
class DHT {
public:
DHT();
void setPin(uint8_t pin);
void setCallback(Callback cb);
void read();
const int8_t ready() const;
virtual float getTemperature() = 0;
virtual float getHumidity() = 0;
const char* getError();
float readTemperature;
float readHumidity;
protected:
volatile int8_t _result;
volatile uint8_t _data[5];
private:
uint8_t _pin;
Callback _callback;
Ticker _timer;
char _errorStr[5];
volatile uint8_t _counter;
volatile uint32_t _previousMicros;
static void ICACHE_RAM_ATTR _handleRead(DHT* instance);
void ICACHE_RAM_ATTR _handleAck();
void ICACHE_RAM_ATTR _handleData();
static void ICACHE_RAM_ATTR _timeout(DHT* instance);
void ICACHE_RAM_ATTR _tryCallback();
};
class DHT11 : public DHT {
public:
DHT11();
float getTemperature();
float getHumidity();
};
class DHT22 : public DHT {
public:
DHT22();
float getTemperature();
float getHumidity();
};
(I added the two floats at the end to get these class members declared.)