What are you trying to access from the browser?
If you are hosting a web page in the ESP device can you share the source code?
Thank you.
/*********
Built on the base of Rui Santos 18b20 Async webserver sketch
Complete project details at https://RandomNerdTutorials.com
*********/
// Import required libraries
#ifdef ESP32
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#else
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Hash.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#endif
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to GPIO 4
#define ONE_WIRE_BUS 4
int ledPin =5;
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
/* Put your SSID & Password */
const char* ssid = "tempprobe"; // Enter SSID here
const char* password = "********"; //Enter Password here
/* Put IP Address details */
IPAddress local_ip(192,168,1,1);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
String readDSTemperatureF() {
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float tempF = sensors.getTempFByIndex(0);
if(int(tempF) == -196){
Serial.println("Failed to read from DS18B20 sensor");
return "--";
} else {
Serial.print("Temperature Fahrenheit: ");
Serial.println(tempF);
}
return String(tempF);
}
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
html {
font-family: Arial;
display: inline-block;
margin: 0px auto;
text-align: center;
}
h2 { font-size: 3.0rem; }
p { font-size: 8.0rem; }
.units { font-size: 2.2rem; }
</style>
</head>
<body>
<h2>Temperature</h2>
<p>
<span id="temperaturef">%TEMPERATUREF%</span>
<sup class="units">°F</sup>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperaturef").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperaturef", true);
xhttp.send();
}, 5000) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DHT values
String processor(const String& var){
//Serial.println(var);
if(var == "TEMPERATUREF"){
return readDSTemperatureF();
}
return String();
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
Serial.println();
Serial.println("");
Serial.println("Starting AP mode");
WiFi.softAP(ssid, password);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);
pinMode(ledPin,OUTPUT);
// Start up the DS18B20 library
sensors.begin();
Serial.println("sensor started");
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperaturef", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", readDSTemperatureF().c_str());
});
// Start server
server.begin();
}
void loop(){
sensors.requestTemperatures();
float cTemp = sensors.getTempFByIndex(0);
if(int(cTemp) == -196){
Serial.println("Failed to read from DS18B20 sensor");
} else {
Serial.print("cTemp : ");
Serial.println(cTemp);
}
if(int(cTemp >85)){
digitalWrite(ledPin,HIGH);
}
else{
digitalWrite(ledPin,LOW);
}
delay (2000);
}
I think the issue is happening because you are using xhttp.onreadystatechange function in the web page.
Try to use the socket instead, it will allow you to update certain fields in the web page without refreshing it and data from the sensor will be sent in real time.
Also I've experienced that storing the webpade inside the Programm partition is causing it to load slower later.
so better to define it as a local variable then use .send function not .sendP.