Hello guys, I have set up an ESP32 to work as an access point with an Async webserver serving 3 sensors --> 2 digital (Temp - ds18b20) and 1 analog for pressure (will add 1 more as soon as this works
).
Everything works as expected, the only issue is that after the addition of the second digital sensor it stopped auto refreshing the values on the website.
Code:
/*********
Nicolas Constantinou
*********/
// Import required libraries
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to GPIO 4
#define ONE_WIRE_BUS 4
// 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);
DeviceAddress sensor1 = { 0x28, 0x12, 0x9D, 0xD1, 0xD, 0x0, 0x0, 0xEE };
DeviceAddress sensor2 = { 0x28, 0xB5, 0xD5, 0x7, 0xB6, 0x1, 0x3C, 0x4C };
// Variables to store temperature values
String temperatureC1 = "";
String temperatureC2 = "";
String pressureValuePSI = "";
// Float Vars to store pressure values - NEW
const int pressureInput = 32; //select the analog input pin for the pressure OIL
//const int pressureInput2 = A2; //select the analog input pin for the pressure of FUEL
const int pressureZero = 285.3; //analog reading of pressure transducer at 0psi
const int pressureMax = 4095; //analog reading of pressure transducer at 100psi
const int pressuretransducermaxPSI = 100; //psi value of transducer being used
//const int baudRate = 115200; //constant integer to set the baud rate for serial monitor
//const int sensorreadDelay = 250; //constant integer to set the sensor read delay in milliseconds
String pressureValue = ""; //variable to store the value coming from the pressure transducer
//Call Sensors and store actual value after equation to string
String readPressureValues () {
float pressureValueRead = analogRead(pressureInput); //reads value from input pin and assigns to variable
pressureValuePSI = (((pressureValueRead-pressureZero)*pressuretransducermaxPSI)/(pressureMax-pressureZero))/14.504; //conversion equation to convert analog reading to psi
Serial.print("OIL Press:"); //prints label
Serial.print(pressureValuePSI); //prints value from previous line to serial
Serial.println("bar"); //prints label to serial
Serial.println(pressureValueRead);
return String(pressureValuePSI);
}
// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 300;
// Replace with your network credentials - CHANGED
const char *ssid = "ESP32CarGauges";
const char *password = "123456789";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
String readDSTemperatureC1() {
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float tempC = sensors.getTempC(sensor1);
if (tempC == -127.00) {
Serial.println("Failed to read from DS18B20 sensor 1");
return "--";
} else {
Serial.print("Temperature 1 Celsius: ");
Serial.println(tempC);
}
return String(tempC);
}
String readDSTemperatureC2() {
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float tempC = sensors.getTempC(sensor2);
if (tempC == -127.00) {
Serial.println("Failed to read from DS18B20 sensor 2");
return "--";
} else {
Serial.print("Temperature 2 Celsius: ");
Serial.println(tempC);
}
return String(tempC);
}
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: 3.0rem; }
.units { font-size: 1.2rem; }
.ds-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>ESP32 Car Gauges</h2>
<p>
<span class="ds-labels">Temperature</span><br>
<span id="temperaturec">%TEMPERATUREC1%</span>
<sup class="units">°C</sup>
</p>
<p>
<p>
<span class="ds-labels">Temperature</span><br>
<span id="temperaturec">%TEMPERATUREC2%</span>
<sup class="units">°C</sup>
</p>
<p>
<span class="ds-labels">Oil Pressure</span><br>
<span id="pressureValue">%pressureValue%</span>
<sup class="units">BAR</sup>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperaturec1").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperaturec1", true);
xhttp.send();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("temperaturec2").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/temperaturec2", true);
xhttp.send();
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pressureValue").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pressureValue", true);
xhttp.send();
}, 50) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DS18B20 values - NEW - pressure values too
String processor(const String& var) {
//Serial.println(var);
if (var == "TEMPERATUREC1") {
return temperatureC1;
}
else if (var == "TEMPERATUREC2") {
return temperatureC2;
}
else if (var == "pressureValue") {
return pressureValuePSI;
}
return String();
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
Serial.println();
// Start up the DS18B20 library
sensors.begin();
//Serial.println(xPortGetCoreID());
//Declare strings to be used by webserver
temperatureC1 = readDSTemperatureC1();
temperatureC2 = readDSTemperatureC2();
pressureValue = readPressureValues() ;
// Wi-Fi Soft AP start
WiFi.softAP(ssid, password);
// Print ESP Local IP Address
Serial.println(WiFi.localIP());
Serial.println();
Serial.print("IP address: ");
Serial.println(WiFi.softAPIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/html", index_html, processor);
});
server.on("/temperaturec1", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", temperatureC1.c_str());
});
server.on("/temperaturec2", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", temperatureC2.c_str());
});
server.on("/pressureValue", HTTP_GET, [](AsyncWebServerRequest * request) {
request->send_P(200, "text/plain", pressureValue.c_str());
});
// Start server
server.begin();
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
temperatureC1 = readDSTemperatureC1();
temperatureC2 = readDSTemperatureC2();
pressureValue = readPressureValues () ;
lastTime = millis();
}
}