Hi, I wrote a code for a project using multiple different websites. Now i am facing an error idk how to solve. Can someone please help me and make my code better.
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <SPIFFS.h>
int x = 0; //Reset to 0
int y = 0;
int z = 0;
//*FSR sensors*/
#define noFSRs 3 // Number of FSRs connected
#define FSR1 12 //Analogue ports
#define FSR2 13
#define FSR3 14
float fsrVoltageArray[3]; // The analog reading converted and //scaled to voltage as a floating point //number
float fsrForceArray[3]; // The force in Newton
float fsrWeightInGramsArray[3]; // Weight converted to grams
int pinArray[3] = {FSR1, FSR2, FSR3}; // The pin ID for the //three devices
float forceMaxArray[3] = {100.0, 100.0, 100.0}; // Maximum forces //supported
float million = 1000000.0; // Unit for "1/micro
float conversionToKgrams = 1.0/9.80665;
long K = 1000;
long R = 10*K; // R in K Ohm
long Vcc = 5000; // 5V=5000mV, 3.3V = 3300 mV
float voltageMax = 0.98 * Vcc; // Maximum voltage set to 95% of Vcc. Set //the force to the maximum beyond this //value.
// Replace with your network credentials
const char* ssid = "Loading...";
const char* password = "verybigpp";
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
bool status;
// Initialize SPIFFS
if(!SPIFFS.begin()){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Connect to Wi-Fi
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());
for (int FSR = 0; FSR < noFSRs; FSR++) {
fsrVoltageArray[FSR] = 0.0; //Reset values upon entry
fsrForceArray[FSR] = 0.0;
int fsrPin = pinArray[FSR];
int fsrReading = analogRead(fsrPin);
fsrVoltageArray[FSR] = (float) map(fsrReading, 0, 1023, 0, 5000);
for (int FSR = 0; FSR < noFSRs; FSR++) {
// The value of the force F as a function of the voltage V is ///computed as: F(V) = (Fmax/Vmax) * V
float force_value = (forceMaxArray[FSR]/voltageMax) * fsrVoltageArray[FSR];
// Three situations are distinguished:
//
// 1. If V is too close to the maximum (as defined by voltageMax // ), the force can
// go to infinity. This is avoided by setting it the maximum //value as soon as it is higher than our threshold voltageMax.
//
// 2. If the computed force F is too small, we set it to zero to // avoid noise effects.
//
// 3. In all other cases, we take the logarithmic value to
//reduce the sloop and better distinguish small changes.
if ( fsrVoltageArray[FSR] < voltageMax ) {
// V is not too high in this branch
if ( force_value <= 1.00 ) {
fsrForceArray[FSR] = 0.0; // Force is too small, set it to // zero
} else {
fsrForceArray[FSR] = log10(force_value); // Value is okay, //take the log of //this
}
} else {
// Cap the force if the voltage is too close to Vcc (for Vcc //it would be infinity)
fsrForceArray[FSR] = log10(forceMaxArray[FSR]);
Serial.print("Cut off activated for FSR = "); Serial.println(FSR);
}
for (int FSR = 0; FSR < noFSRs; FSR++) {
fsrWeightInGramsArray[FSR] = fsrForceArray[FSR] * conversionToKgrams * 1000.0;
}
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(SPIFFS, "/index.html");
});
server.on("/1", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", fsrWeightInGramsArray[0]);
});
server.on("/2", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", fsrWeightInGramsArray[1]);
});
server.on("/3", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", fsrWeightInGramsArray[2]);
});
// Start server
server.begin();
}
void loop(){
}
this is the code for the ESP32
<!DOCTYPE HTML><html>
<!-- Rui Santos - Complete project details at https://RandomNerdTutorials.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software. -->
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://code.highcharts.com/highcharts.js"></script>
<style>
body {
min-width: 310px;
max-width: 800px;
height: 400px;
margin: 0 auto;
}
h2 {
font-family: Arial;
font-size: 2.5rem;
text-align: center;
}
</style>
</head>
<body>
<h2>Pressure distribution</h2>
<div id="chart-1" class="container"></div>
<div id="chart-2" class="container"></div>
<div id="chart-3" class="container"></div>
</body>
<script>
var chartT = new Highcharts.Chart({
chart:{ renderTo : 'chart-1' },
title: { text: '1' },
series: [{
showInLegend: false,
data: []
}],
plotOptions: {
line: { animation: false,
dataLabels: { enabled: true }
},
series: { color: '#059e8a' }
},
xAxis: { type: 'datetime',
dateTimeLabelFormats: { second: '%H:%M:%S' }
},
yAxis: {
title: { text: 'gram' }
//title: { text: 'gram' }
},
credits: { enabled: false }
});
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var x = (new Date()).getTime(),
y = parseFloat(this.responseText);
//console.log(this.responseText);
if(chartT.series[0].data.length > 40) {
chartT.series[0].addPoint([x, y], true, true, true);
} else {
chartT.series[0].addPoint([x, y], true, false, true);
}
}
};
xhttp.open("GET", "/1", true);
xhttp.send();
}, 30000 ) ;
var chartH = new Highcharts.Chart({
chart:{ renderTo:'chart-2' },
title: { text: '2' },
series: [{
showInLegend: false,
data: []
}],
plotOptions: {
line: { animation: false,
dataLabels: { enabled: true }
}
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { second: '%H:%M:%S' }
},
yAxis: {
title: { text: 'Gram' }
},
credits: { enabled: false }
});
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var x = (new Date()).getTime(),
y = parseFloat(this.responseText);
//console.log(this.responseText);
if(chartH.series[0].data.length > 40) {
chartH.series[0].addPoint([x, y], true, true, true);
} else {
chartH.series[0].addPoint([x, y], true, false, true);
}
}
};
xhttp.open("GET", "/2", true);
xhttp.send();
}, 30000 ) ;
var chartP = new Highcharts.Chart({
chart:{ renderTo:'3' },
title: { text: '3' },
series: [{
showInLegend: false,
data: []
}],
plotOptions: {
line: { animation: false,
dataLabels: { enabled: true }
},
series: { color: '#18009c' }
},
xAxis: {
type: 'datetime',
dateTimeLabelFormats: { second: '%H:%M:%S' }
},
yAxis: {
title: { text: 'Gram' }
},
credits: { enabled: false }
});
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var x = (new Date()).getTime(),
y = parseFloat(this.responseText);
//console.log(this.responseText);
if(chartP.series[0].data.length > 40) {
chartP.series[0].addPoint([x, y], true, true, true);
} else {
chartP.series[0].addPoint([x, y], true, false, true);
}
}
};
xhttp.open("GET", "/3", true);
xhttp.send();
}, 30000 ) ;
</script>
</html>
this is the code for the website