Hello,
I would like to ask if there is something like ESPAsyncWebServer.h for Arduino Wifi Rev2.
I would like to replicate this example ESP32 DHT11/DHT22 Web Server using Arduino IDE | Random Nerd Tutorials
I already managed to connect sensor-wifi-arduino and display data (see image) but I'd like to add ajax logic avoiding the client.println("Refresh: 10");.
Here my starting sketch.
#include <WiFiNINA.h>
#include <SPI.h>
#include <DHT.h>
// sensor
int dhtPin = 2; // signal in pin 2 (digital)
#define DHT_TYPE DHT22
DHT dht(dhtPin, DHT_TYPE);
// wifi
char ssid[] = "mywifi";
char pass[] = "mypwd";
int status = WL_IDLE_STATUS;
WiFiServer server(80); // server instance
void setup() {
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Arduino attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
server.begin();
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
delay(250);
Serial.println("Reading temperature and humidity data...go to browser!");
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
float hic = dht.computeHeatIndex(temperature, humidity, false); // heat index Celsius
// Check to see if the values are empty. Display error if they are.
if( isnan(humidity) || isnan(temperature) ) {
Serial.println("DHT Sensor read Failed!");
return;
}
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = true; // an http request ends with a blank line
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if end of the line (received a newline character) and the line is blank
// http request has ended so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 10");
client.println();
// html page
client.println("");
client.println("");
client.println("");
client.println("");
client.print("
DHT22 sensor data
");// sensor data
client.print(("Humidity: "));
client.print(humidity);
client.print(("% "));
client.print(("Temperature: "));
client.print(temperature);
client.print(char(176));
client.print("C ");
client.print(("Heat index: "));
client.print(hic);
client.print(char(176));
client.print("C");
client.println("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
Serial.println("client disonnected");
}
}
