diff --git a/esp/cpp/03_temp-humidity-web/03_temp-humidity-web.ino b/esp/cpp/03_temp-humidity-web/03_temp-humidity-web.ino new file mode 100644 index 0000000..a4836ec --- /dev/null +++ b/esp/cpp/03_temp-humidity-web/03_temp-humidity-web.ino @@ -0,0 +1,161 @@ +// Import required libraries + +#include "WiFi.h" +#include "ESPAsyncWebServer.h" +#include +#include + +// 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( + + + + + + + +

ESP32 DHT Server

+

+ + Temperature + %TEMPERATURE% + °C +

+

+ + Humidity + %HUMIDITY% + % +

+ + +)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(){ + +} diff --git a/esp/cpp/03_temp-humidity-web/README.md b/esp/cpp/03_temp-humidity-web/README.md new file mode 100644 index 0000000..0e58257 --- /dev/null +++ b/esp/cpp/03_temp-humidity-web/README.md @@ -0,0 +1,56 @@ +# 03_temp-humidity-web + +![schematic](./schematic.png) + +Temperature and humidity sensor served on a web page within the local network. + +![screenshot](./screenshot.png) + +## 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 +``` diff --git a/esp/cpp/03_temp-humidity-web/schematic.png b/esp/cpp/03_temp-humidity-web/schematic.png new file mode 100644 index 0000000..7545de7 Binary files /dev/null and b/esp/cpp/03_temp-humidity-web/schematic.png differ diff --git a/esp/cpp/03_temp-humidity-web/screenshot.png b/esp/cpp/03_temp-humidity-web/screenshot.png new file mode 100644 index 0000000..7e46bcd Binary files /dev/null and b/esp/cpp/03_temp-humidity-web/screenshot.png differ diff --git a/esp/cpp/README.md b/esp/cpp/README.md index 0812a3b..ea13be7 100644 --- a/esp/cpp/README.md +++ b/esp/cpp/README.md @@ -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