Skip to content
Snippets Groups Projects
Commit 07ee29ab authored by aliceg3's avatar aliceg3
Browse files

test webpage sensors

parent a57c99c4
No related branches found
No related tags found
No related merge requests found
/*********
Rui Santos
Complete project details at https://randomnerdtutorials.com/esp8266-dht11dht22-temperature-and-humidity-web-server-with-arduino-ide/
*********/
// Import required libraries
#include <Arduino.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <AsyncTCP.h>
// Replace with your network credentials
const char* ssid = "Connectify-ESP32 Boot";
const char* password = "temp_pass1234";
#define PRESSUREPIN1 34
#define PRESSUREPIN2 35
#define motorIN1
#define motorIN2
#define motorIN3
#define motorIN4
#define PRESSUREGOAL1 357
#define PRESSUREGOAL2 2034
// current temperature & humidity, updated in loop()
int pressure1 = 0.0;
int pressure2 = 0.0;
float motorSpeed = 0.0;
// Create AsyncWebServer object on port 80
AsyncWebServer server(80);
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time DHT was updated
// Updates DHT readings every 10 seconds
const long interval = 1;
const char index_html[] PROGMEM = R"rawliteral(
<!DOCTYPE HTML><html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<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; }
.dht-labels{
font-size: 1.5rem;
vertical-align:middle;
padding-bottom: 15px;
}
</style>
</head>
<body>
<h2>Remotely Adjustable Boot</h2>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="dht-labels">Pressure LEFT</span>
<span id="pressure1">%PRESSURE1%</span>
<sup class="units">mmHg</sup>
</p>
<p>
<i class="fas fa-thermometer-half" style="color:#059e8a;"></i>
<span class="dht-labels">Pressure RIGHT</span>
<span id="pressure2">%PRESSURE2%</span>
<sup class="units">mmHg</sup>
</p>
<p>
<i class="fas fa-tint" style="color:#00add6;"></i>
<span class="dht-labels">Motor Speed</span>
<span id="motorspeed">%MOTORSPEED%</span>
<sup class="units">%</sup>
</p>
</body>
<script>
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pressure1").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pressure1", true);
xhttp.send();
}, 0.1 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("pressure2").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/pressure2", true);
xhttp.send();
}, 0.1 ) ;
setInterval(function ( ) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("motorspeed").innerHTML = this.responseText;
}
};
xhttp.open("GET", "/motorspeed", true);
xhttp.send();
}, 0.1 ) ;
</script>
</html>)rawliteral";
// Replaces placeholder with DHT values
String processor(const String& var){
//Serial.println(var);
if(var == "PRESSURE1"){
return String(pressure1);
}
else if(var == "PRESSURE2"){
return String(pressure2);
}
else if(var == "MOTORSPEED"){
return String(motorSpeed);
}
return String();
}
void setup(){
// Serial port for debugging purposes
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println(".");
}
// Print ESP8266 Local IP Address
Serial.println(WiFi.localIP());
// Route for root / web page
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/html", index_html, processor);
});
server.on("/pressure1", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(pressure1).c_str());
});
server.on("/pressure2", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(pressure2).c_str());
});
server.on("/motorspeed", HTTP_GET, [](AsyncWebServerRequest *request){
request->send_P(200, "text/plain", String(motorSpeed).c_str());
});
// Start server
server.begin();
}
void loop(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
int intervals1[7] = {0,0,0,0,0,0,0};
int intervalValues1[7] = {0,0,0,0,0,0,0};
int intervals2[7] = {0,0,0,0,0,0,0};
int intervalValues2[7] = {0,0,0,0,0,0,0};
for(int counter = 0; counter < 10; counter ++) {
int a1 = analogRead(PRESSUREPIN1);
int a2 = analogRead(PRESSUREPIN2);
int aModulus1 = int (a1 / (600));
int aModulus2 = int (a2 / (600));
intervals1[aModulus1] += 1;
intervalValues1[aModulus1] += int(a1 % 600);
intervals2[aModulus2] += 1;
intervalValues2[aModulus2] += int(a2 % 600);
Serial.println("Reading #" + String(counter) + ": " + String(a1) + ", " + String(aModulus1));
Serial.println("Reading #" + String(counter) + ": " + String(a2) + ", " + String(aModulus2));
delay(10);
}
Serial.println("Intervals array #1: [");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervals1[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("Interval Values array #1: ");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervalValues1[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("**********");
Serial.println("Intervals array #2: [");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervals2[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("Interval Values array #2: ");
for (int counter = 0; counter < 7; counter++) {
Serial.print(intervalValues2[counter]);
Serial.print(",");
}
Serial.print("]");
Serial.println("**********");
int maxIntervalSize1 = intervals1[0];
int maxIntervalIndex1 = 0;
for (int index = 1; index < 7; index ++) {
if (intervals1[index] > maxIntervalSize1) {
maxIntervalIndex1 = index;
maxIntervalSize1 = intervals1[index];
}
}
Serial.println("maxIntervalIndex #1: " + maxIntervalIndex1);
int maxIntervalSize2 = intervals2[0];
int maxIntervalIndex2 = 0;
for (int index = 1; index < 7; index ++) {
if (intervals2[index] > maxIntervalSize2) {
maxIntervalIndex2 = index;
maxIntervalSize2 = intervals2[index];
}
}
Serial.println("maxIntervalIndex #2: " + maxIntervalIndex2);
pressure1 = (intervalValues1[maxIntervalIndex1]/maxIntervalSize1)+ maxIntervalIndex1*600;
pressure2 = (intervalValues2[maxIntervalIndex2]/maxIntervalSize2)+ maxIntervalIndex2*600;
Serial.println("adcValue #1: " + pressure1);
Serial.println("adcValue #2: " + pressure2);
Serial.println("******************************");
float newMotorSpeed = 0;
if (isnan(newMotorSpeed)) {
Serial.println("Failed to read from DHT sensor!");
}
else {
motorSpeed = newMotorSpeed;
Serial.println(motorSpeed);
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment