Added webserver
This commit is contained in:
parent
3d1e6f67a2
commit
5af6ffafca
|
@ -8,6 +8,9 @@
|
||||||
; Please visit documentation for the other options and examples
|
; Please visit documentation for the other options and examples
|
||||||
; https://docs.platformio.org/page/projectconf.html
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
|
[env]
|
||||||
|
monitor_speed = 115200
|
||||||
|
|
||||||
[env:denky32]
|
[env:denky32]
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
board = denky32
|
board = denky32
|
||||||
|
@ -16,3 +19,7 @@ lib_deps =
|
||||||
adafruit/Adafruit Unified Sensor@^1.1.14
|
adafruit/Adafruit Unified Sensor@^1.1.14
|
||||||
adafruit/DHT sensor library@^1.4.6
|
adafruit/DHT sensor library@^1.4.6
|
||||||
marcoschwartz/LiquidCrystal_I2C@^1.1.4
|
marcoschwartz/LiquidCrystal_I2C@^1.1.4
|
||||||
|
esphome/ESPAsyncWebServer-esphome@^3.1.0
|
||||||
|
|
||||||
|
[env:env1]
|
||||||
|
lib_deps = esphome/ESPAsyncWebServer-esphome@^3.1.0
|
||||||
|
|
179
src/main.cpp
179
src/main.cpp
|
@ -1,6 +1,8 @@
|
||||||
|
#include "ESPAsyncWebServer.h"
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
#include <DHT.h>
|
#include <DHT.h>
|
||||||
#include <LiquidCrystal_I2C.h>
|
#include <LiquidCrystal_I2C.h>
|
||||||
|
#include "WiFi.h"
|
||||||
|
|
||||||
const int DHT_PIN = 4;
|
const int DHT_PIN = 4;
|
||||||
const int PIEZO_PIN = 17;
|
const int PIEZO_PIN = 17;
|
||||||
|
@ -11,12 +13,126 @@ const int LEDRED_PIN = 26;
|
||||||
|
|
||||||
const int LEDWH_PIN = 13;
|
const int LEDWH_PIN = 13;
|
||||||
|
|
||||||
|
const char* ssid ="";
|
||||||
|
const char* password="";
|
||||||
|
|
||||||
|
AsyncWebServer server(80);
|
||||||
|
|
||||||
|
|
||||||
int prevRating = -1;
|
int prevRating = -1;
|
||||||
|
|
||||||
DHT dht(DHT_PIN, DHT11);
|
DHT dht(DHT_PIN, DHT11);
|
||||||
|
|
||||||
|
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</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">Feuchtigkeit</span>
|
||||||
|
<span id="humidity">%HUMIDITY%</span>
|
||||||
|
<sup class="uHumiditynits">%</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();
|
||||||
|
}, 2000 ) ;
|
||||||
|
|
||||||
|
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();
|
||||||
|
}, 2000 ) ;
|
||||||
|
</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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
LiquidCrystal_I2C lcd(0x27, 16, 5);
|
LiquidCrystal_I2C lcd(0x27, 16, 5);
|
||||||
|
|
||||||
|
float temp;
|
||||||
|
float humid;
|
||||||
|
|
||||||
void playWarnTone()
|
void playWarnTone()
|
||||||
{
|
{
|
||||||
tone(PIEZO_PIN, 2000);
|
tone(PIEZO_PIN, 2000);
|
||||||
|
@ -35,15 +151,31 @@ void OFFLEDS()
|
||||||
digitalWrite(LEDRED_PIN, HIGH);
|
digitalWrite(LEDRED_PIN, HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UpdateLCD()
|
||||||
|
{
|
||||||
|
lcd.clear();
|
||||||
|
lcd.setContrast(16);
|
||||||
|
Serial.println("Updating LCD");
|
||||||
|
|
||||||
|
lcd.setCursor(0, 0);
|
||||||
|
lcd.print("Temp: " + (String)temp);
|
||||||
|
lcd.setCursor(0, 1);
|
||||||
|
lcd.print("Humid: " + (String)humid);
|
||||||
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(9600);
|
Serial.begin(115200);
|
||||||
dht.begin();
|
dht.begin();
|
||||||
|
|
||||||
lcd.init();
|
lcd.init();
|
||||||
lcd.backlight();
|
lcd.backlight();
|
||||||
|
|
||||||
lcd.print("Hello");
|
lcd.noAutoscroll();
|
||||||
|
|
||||||
|
lcd.print("Welcome!");
|
||||||
|
lcd.setCursor(0, 1);
|
||||||
|
lcd.print("www.ipmake.dev");
|
||||||
|
|
||||||
pinMode(PIEZO_PIN, OUTPUT);
|
pinMode(PIEZO_PIN, OUTPUT);
|
||||||
|
|
||||||
|
@ -60,13 +192,45 @@ void setup()
|
||||||
tone(PIEZO_PIN, 2000);
|
tone(PIEZO_PIN, 2000);
|
||||||
delay(250);
|
delay(250);
|
||||||
noTone(PIEZO_PIN);
|
noTone(PIEZO_PIN);
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
// // Initialize the timer
|
||||||
|
// hw_timer_t *timer = timerBegin(1, 80, true); // Timer ID 1, prescaler 80 (1MHz clock)
|
||||||
|
// // Set the timer interval
|
||||||
|
// timerAlarmWrite(timer, 10000, true);
|
||||||
|
// // Attach the function to the timer
|
||||||
|
// timerAttachInterrupt(timer, &UpdateLCD, true);
|
||||||
|
// // Start the timer
|
||||||
|
// timerAlarmEnable(timer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
float temp = dht.readTemperature();
|
temp = dht.readTemperature();
|
||||||
float humid = dht.readHumidity();
|
humid = dht.readHumidity();
|
||||||
delay(100);
|
delay(1000);
|
||||||
|
|
||||||
int rating = 0;
|
int rating = 0;
|
||||||
|
|
||||||
|
@ -103,6 +267,7 @@ void loop()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serial.println("Temp: " + (String)temp + " Humid: " + (String)humid + " Rating: " + (String)rating);
|
// Serial.println("Temp: " + (String)temp + " Humid: " + (String)humid + " Rating: " + (String)rating);
|
||||||
|
|
||||||
prevRating = rating;
|
prevRating = rating;
|
||||||
}
|
|
||||||
|
UpdateLCD();
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue