My esp32 weather station web server is not working properly #9938
Unanswered
AESIRAEROSPAZIALE
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi!
I've been trying for days to build a weather station with an anemometer and dht22. My esp32 can connect to the wifi, thw web page is shown correctly but wind speed data and terrain humidity doesn't work... can somebdy help me please? Sorry for my english
`#include <Arduino.h>
#include <SimpleDHT.h>
#include <WiFi.h>
#include "ESPAsyncWebServer.h"
#define DHTPIN 16
const char* SSID = "TIM-45618837";
const char* Password = "ucTpESqcNcZD5Nyhy5k9XDQy";
float analog_read_wind, analog_read_terr, velocita, temp, hum, hum_terr;
const char index_html[] PROGMEM = R"rawliteral(
)rawliteral";
AsyncWebServer server(80);
SimpleDHT22 dht22(DHTPIN);
String readDHTHumidity() {
dht22.read2(&temp, &hum, NULL);
return String(hum);
}
String readDHTTemperature() {
dht22.read2(&temp, &hum, NULL);
return String(temp);
}
String readHumidityTerr(){
analog_read_terr = ((612-analogRead(4))*100)/22; //a:22=x:100
if(analog_read_terr<0)
{
analog_read_terr = 0;
} else if (analog_read_terr > 100){
analog_read_terr = 100;
}
return String(analog_read_terr);
}
String readWind(){
analog_read_wind = (analogRead(12)*3.3)/4095;
velocita = analog_read_wind * 50;
return String(velocita);
}
String processor(const String& var){
if(var == "temperature"){
return readDHTTemperature();
}
else if(var == "humidity"){
return readDHTHumidity();
}
else if(var == "humidity_terr")
{
return readHumidityTerr();
}
else if(var == "wind")
{
return readWind();
}
return String();
}
void setup() {
Serial.begin(115200);
pinMode(12, INPUT);
pinMode(4, INPUT);
WiFi.begin(SSID, Password);
int counter = 0;
while (WiFi.status() != WL_CONNECTED){
delay(200);
if(++counter > 100) ESP.restart();
Serial.print(".");
}
Serial.println(WiFi.localIP());
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());
});
server.on("/humidity_terr", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readHumidityTerr().c_str());
});
server.on("/wind", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readWind().c_str());
});
server.begin();
}
void loop() {
}`
here's the html:
<style> html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center; } h1.meriti{text-align: right; font-size: 1rem;} 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; } .location{ font-size: 1rem; vertical-align: right; } .lable_data{ font-size: 1rem; vertical-align: right; } </style>METEO MONTORIO AL VOMANO
Via Poliseo De Angelis:
Temperatura attuale: %TEMPERATURE% °C
Umidità: %HUMIDITY% %
Umidità del terreno: %HUMIDITY_TERR% %
Velocità del vento: %VEL_WIND% Km/h
Creato da Riccardo Di Lorenzo
<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 ) ; setInterval(function ( ) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("humidity_terr").innerHTML = this.responseText; } }; xhttp.open("GET", "/humidity_terr", true); xhttp.send(); }, 10000 ); setInterval(function ( ) { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("wind").innerHTML = this.responseText; } }; xhttp.open("GET", "/wind", true); xhttp.send(); }, 10000 ); </script>Beta Was this translation helpful? Give feedback.
All reactions