Last active
March 18, 2023 16:25
-
-
Save seansplayin/85d3b7fa0ae201483a737d042e2e03f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#include <WiFi.h> | |
#include <WiFiClient.h> | |
#include <WiFiAP.h> | |
#include <WebServer.h> | |
#include "htmlreporting.h" | |
String index_html; | |
String htmlreporting = html; | |
String htmlPage; | |
#define ROLLING_ARRAY_SIZE 5 | |
#define DS_SAMPLES 5 | |
// WiFi credentials | |
const char *ssid = ""; | |
const char *password = ""; | |
// Create an instance of the web server | |
WebServer server(80); | |
// Data wire is plugged into port 2 on the ESP32 | |
#define ONE_WIRE_BUS 2 | |
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) | |
OneWire oneWire(ONE_WIRE_BUS); | |
// Pass oneWire reference to DallasTemperature library | |
DallasTemperature sensors(&oneWire); | |
// Temperature sensor addresses | |
#define SENSOR_COUNT 11 | |
float dsensors[SENSOR_COUNT]; | |
DeviceAddress DSensor1 = {0x28, 0xFF, 0x56, 0xC6, 0x83, 0x16, 0x2, 0x6C}; | |
DeviceAddress DSensor2 = {0x28, 0xFF, 0x18, 0xE1, 0x8C, 0x16, 0x3, 0x56}; | |
DeviceAddress DSensor3 = {0x28, 0xFF, 0x87, 0x18, 0x8E, 0x16, 0x4, 0x6E}; | |
DeviceAddress DSensor4 = {0x28, 0xFF, 0x77, 0xA1, 0x8E, 0x16, 0x3, 0x8F}; | |
DeviceAddress DSensor5 = {0x28, 0xFF, 0x19, 0xA9, 0x8D, 0x16, 0x4, 0x8B}; | |
DeviceAddress DSensor6 = {0x28, 0xFF, 0x89, 0x7C, 0x8E, 0x16, 0x4, 0x85}; | |
DeviceAddress DSensor7 = {0x28, 0xFF, 0xF5, 0x22, 0x8C, 0x16, 0x3, 0x9A}; | |
DeviceAddress DSensor8 = {0x28, 0xFF, 0xE7, 0x26, 0x8E, 0x16, 0x4, 0x62}; | |
DeviceAddress DSensor9 = {0x28, 0xFF, 0x8, 0x45, 0x8D, 0x16, 0x4, 0x2F}; | |
DeviceAddress DSensor10 = {0x28, 0xFF, 0x10, 0x62, 0x8C, 0x16, 0x3, 0x2B}; | |
DeviceAddress DSensor11 = {0x28, 0x7C, 0xC9, 0x81, 0xE3, 0x6F, 0x3C, 0x00}; | |
//DeviceAddress DSensor11 = {0x28, 0xFF, 0x9E, 0x24, 0x8C, 0x16, 0x3, 0x20}; | |
//Pump Relay Pins | |
int lagPumpPin = 25; | |
int leadPumpPin = 32; | |
int domesticHeatingPumpPin = 33; | |
int domesticHotWaterPumpPin = 34; | |
int circulationLoopPumpPin = 35; | |
int leadPumpMode = 0; | |
int lagPumpMode = 0; | |
int domesticHotWaterPumpMode = 0; | |
int domesticHeatingPumpMode = 0; | |
int circulationLoopPumpMode = 0; | |
float PT1000 = 0.0; | |
#define PT1000_AVG_BUFFER_SIZE 10 | |
float pt1000AvgBuffer[PT1000_AVG_BUFFER_SIZE]; | |
int pt1000AvgIndex = 0; | |
float pt1000AvgSum = 0.0; | |
const int PT1000Pin = 4; | |
const float PT1000Resistance = 1000.0; | |
float resistanceToTemperature(float resistance) { | |
// implementation of the resistanceToTemperature function | |
} | |
float readPT1000() { | |
float reading = analogRead(PT1000Pin); | |
float voltage = reading * 3.3 / 4095.0; | |
float resistance = voltage * PT1000Resistance / (3.3 - voltage); | |
float temperature = resistanceToTemperature(resistance); | |
return temperature; | |
} | |
float temperature = ((analogRead(PT1000Pin) * 0.00322265625) - 1.25) / 0.005; | |
float pt1000_temps[ROLLING_ARRAY_SIZE]; | |
int pt1000_index = 0; | |
//float pt1000_temps[ROLLING_ARRAY_SIZE]; | |
float ds1_temps[DS_SAMPLES]; | |
float ds2_temps[DS_SAMPLES]; | |
float ds3_temps[DS_SAMPLES]; | |
float ds4_temps[DS_SAMPLES]; | |
float ds5_temps[DS_SAMPLES]; | |
float ds6_temps[DS_SAMPLES]; | |
float ds7_temps[DS_SAMPLES]; | |
float ds8_temps[DS_SAMPLES]; | |
float ds9_temps[DS_SAMPLES]; | |
float ds10_temps[DS_SAMPLES]; | |
float ds11_temps[DS_SAMPLES]; | |
float pt1000_f; | |
float pt1000avg_f; | |
float dsensor1_f; | |
float dsensor1avg_f; | |
float dsensor2_f; | |
float dsensor2avg_f; | |
float dsensor3_f; | |
float dsensor3avg_f; | |
float dsensor4_f; | |
float dsensor4avg_f; | |
float dsensor5_f; | |
float dsensor5avg_f; | |
float dsensor6_f; | |
float dsensor6avg_f; | |
float dsensor7_f; | |
float dsensor7avg_f; | |
float dsensor8_f; | |
float dsensor8avg_f; | |
float dsensor9_f; | |
float dsensor9avg_f; | |
float dsensor10_f; | |
float dsensor10avg_f; | |
float dsensor11_f; | |
float dsensor11avg_f; | |
// Rolling average arrays | |
float PT1000Array[5]; | |
float DSensor1Array[5]; | |
float DSensor2Array[5]; | |
float DSensor3Array[5]; | |
float DSensor4Array[5]; | |
float DSensor5Array[5]; | |
float DSensor6Array[5]; | |
float DSensor7Array[5]; | |
float DSensor8Array[5]; | |
float DSensor9Array[5]; | |
float DSensor10Array[5]; | |
float DSensor11Array[5]; | |
float PT1000Average = 0.0; | |
float DSensor1Average = 0.0; | |
float DSensor2Average = 0.0; | |
float DSensor3Average = 0.0; | |
float DSensor4Average = 0.0; | |
float DSensor5Average = 0.0; | |
float DSensor6Average = 0.0; | |
float DSensor7Average = 0.0; | |
float DSensor8Average = 0.0; | |
float DSensor9Average = 0.0; | |
float DSensor10Average = 0.0; | |
float DSensor11Average = 0.0; | |
// Pump control variables | |
bool leadPumpStatus = false; | |
bool lagPumpStatus = false; | |
bool domesticHotWaterPumpStatus = false; | |
bool domesticHeatingPumpStatus = false; | |
bool circulationLoopPumpStatus = false; | |
bool circulation = false; // declare the variable outside of any function | |
// Declare the functions readDSensor1() through readDSensor11() before calling them in loop() | |
// Function declaration for readDSensor1() | |
float readDSensor1(); | |
// Function declaration for readDSensor2() | |
float readDSensor2(); | |
// Function declaration for readDSensor3() | |
float readDSensor3(); | |
// Function declaration for readDSensor4() | |
float readDSensor4(); | |
// Function declaration for readDSensor5() | |
float readDSensor5(); | |
// Function declaration for readDSensor6() | |
float readDSensor6(); | |
// Function declaration for readDSensor7() | |
float readDSensor7(); | |
// Function declaration for readDSensor8() | |
float readDSensor8(); | |
// Function declaration for readDSensor9() | |
float readDSensor9(); | |
// Function declaration for readDSensor10() | |
float readDSensor10(); | |
// Function declaration for readDSensor11() | |
float readDSensor11(); | |
// Define the functions to read temperature from each DS18B20 sensor | |
float readDSensor1() { | |
// Code to read temperature from DS18B20 sensor 1 | |
sensors.requestTemperaturesByAddress(DSensor1); | |
float temperature = sensors.getTempC(DSensor1); | |
} | |
float readDSensor2() { | |
// Code to read temperature from DS18B20 sensor 2 | |
sensors.requestTemperaturesByAddress(DSensor2); | |
float temperature = sensors.getTempC(DSensor2); | |
} | |
float readDSensor3() { | |
// Code to read temperature from DS18B20 sensor 3 | |
sensors.requestTemperaturesByAddress(DSensor3); | |
float temperature = sensors.getTempC(DSensor3); | |
} | |
float readDSensor4() { | |
// Code to read temperature from DS18B20 sensor 4 | |
sensors.requestTemperaturesByAddress(DSensor4); | |
float temperature = sensors.getTempC(DSensor4); | |
} | |
float readDSensor5() { | |
// Code to read temperature from DS18B20 sensor 5 | |
sensors.requestTemperaturesByAddress(DSensor5); | |
float temperature = sensors.getTempC(DSensor5); | |
} | |
float readDSensor6() { | |
// Code to read temperature from DS18B20 sensor 6 | |
sensors.requestTemperaturesByAddress(DSensor6); | |
float temperature = sensors.getTempC(DSensor6); | |
} | |
float readDSensor7() { | |
// Code to read temperature from DS18B20 sensor 7 | |
sensors.requestTemperaturesByAddress(DSensor7); | |
float temperature = sensors.getTempC(DSensor7); | |
} | |
float readDSensor8() { | |
// Code to read temperature from DS18B20 sensor 8 | |
sensors.requestTemperaturesByAddress(DSensor8); | |
float temperature = sensors.getTempC(DSensor8); | |
} | |
float readDSensor9() { | |
// Code to read temperature from DS18B20 sensor 9 | |
sensors.requestTemperaturesByAddress(DSensor9); | |
float temperature = sensors.getTempC(DSensor9); | |
} | |
float readDSensor10() { | |
// Code to read temperature from DS18B20 sensor 10 | |
sensors.requestTemperaturesByAddress(DSensor10); | |
float temperature = sensors.getTempC(DSensor10); | |
} | |
float readDSensor11() { | |
// Code to read temperature from DS18B20 sensor 11 | |
sensors.requestTemperaturesByAddress(DSensor11); | |
float temperature = sensors.getTempC(DSensor11); | |
} | |
/* | |
// Functions to set pump control variables | |
void setLeadPumpStatus(bool newStatus) { | |
leadPumpStatus = newStatus; | |
} | |
void setLagPumpStatus(bool newStatus) { | |
lagPumpStatus = newStatus; | |
} | |
void setDomesticHotWaterPumpStatus(bool newStatus) { | |
domesticHotWaterPumpStatus = newStatus; | |
} | |
void setDomesticHeatingPumpStatus(bool newStatus) { | |
domesticHeatingPumpStatus = newStatus; | |
} | |
void setCirculationLoopPumpStatus(bool newStatus) { | |
circulationLoopPumpStatus = newStatus; | |
} | |
// Functions to get pump control variables as strings for display | |
String getLeadPumpStatus() { | |
return leadPumpStatus ? "On" : "Off"; | |
} | |
String getLagPumpStatus() { | |
return lagPumpStatus ? "On" : "Off"; | |
} | |
String getDomesticHotWaterPumpStatus() { | |
return domesticHotWaterPumpStatus ? "On" : "Off"; | |
} | |
String getDomesticHeatingPumpStatus() { | |
return domesticHeatingPumpStatus ? "On" : "Off"; | |
} | |
String getCirculationLoopPumpStatus() { | |
return circulationLoopPumpStatus ? "On" : "Off"; | |
} | |
// Functions to get pump control variables as strings for radio buttons | |
String getLeadPumpOn() { | |
return leadPumpStatus ? "checked" : ""; | |
} | |
String getLeadPumpOff() { | |
return !leadPumpStatus ? "checked" : ""; | |
} | |
String getLeadPumpAuto() { | |
circulation = digitalRead(circulationLoopPumpPin); // assign a value to the global variable | |
return !leadPumpStatus && !circulation ? "checked" : ""; | |
} | |
String getLagPumpOn() { | |
return lagPumpStatus ? "checked" : ""; | |
} | |
String getLagPumpOff() { | |
return !lagPumpStatus ? "checked" : ""; | |
} | |
String getLagPumpAuto() { | |
circulation = digitalRead(circulationLoopPumpPin); | |
return !lagPumpStatus && !circulation ? "checked" : ""; | |
} | |
String getDomesticHotWaterPumpOn() { | |
return domesticHotWaterPumpStatus ? "checked" : ""; | |
} | |
String getDomesticHotWaterPumpOff() { | |
return !domesticHotWaterPumpStatus ? "checked" : ""; | |
} | |
String getDomesticHotWaterPumpAuto() { | |
circulation = digitalRead(circulationLoopPumpPin); | |
return !domesticHotWaterPumpStatus && !circulation ? "checked" : ""; | |
} | |
String getDomesticHeatingPumpOn() { | |
return domesticHeatingPumpStatus ? "checked" : ""; | |
} | |
String getDomesticHeatingPumpOff() { | |
return !domesticHeatingPumpStatus ? "checked" : ""; | |
} | |
String getDomesticHeatingPumpAuto() { | |
circulation = digitalRead(circulationLoopPumpPin); | |
return !domesticHeatingPumpStatus && !circulation ? "checked" : ""; | |
} | |
String getCirculationLoopPumpOn() { | |
return circulationLoopPumpStatus ? "checked" : ""; | |
} | |
String getCirculationLoopPumpOff() { | |
return !circulationLoopPumpStatus ? "checked" : ""; | |
} | |
String getCirculationLoopPumpAuto() { | |
circulation = digitalRead(circulationLoopPumpPin); | |
return circulation ? "checked" : ""; | |
} | |
// Functions to handle pump control requests | |
void handleLeadPumpOn() { | |
leadPumpStatus = true; | |
server.send(200, "text/plain", "Lead pump turned on"); | |
} | |
void handleLeadPumpOff() { | |
leadPumpStatus = false; | |
server.send(200, "text/plain", "Lead pump turned off"); | |
} | |
void handleLeadPumpAuto() { | |
leadPumpStatus = !circulation; | |
server.send(200, "text/plain", "Lead pump set to auto"); | |
} | |
void handleLagPumpOn() { | |
lagPumpStatus = true; | |
server.send(200, "text/plain", "Lag pump turned on"); | |
} | |
void handleLagPumpOff() { | |
lagPumpStatus = false; | |
server.send(200, "text/plain", "Lag pump turned off"); | |
} | |
void handleLagPumpAuto() { | |
lagPumpStatus = !circulation; | |
server.send(200, "text/plain", "Lag pump set to auto"); | |
} | |
void handleDomesticHotWaterPumpOn() { | |
domesticHotWaterPumpStatus = true; | |
server.send(200, "text/plain", "Domestic hot water pump turned on"); | |
} | |
void handleDomesticHotWaterPumpOff() { | |
domesticHotWaterPumpStatus = false; | |
server.send(200, "text/plain", "Domestic hot water pump turned off"); | |
} | |
void handleDomesticHotWaterPumpAuto() { | |
domesticHotWaterPumpStatus = !circulation; | |
server.send(200, "text/plain", "Domestic hot water pump set to auto"); | |
} | |
void handleDomesticHeatingPumpOn() { | |
domesticHeatingPumpStatus = true; | |
server.send(200, "text/plain", "Domestic heating pump turned on"); | |
} | |
void handleDomesticHeatingPumpOff() { | |
domesticHeatingPumpStatus = false; | |
server.send(200, "text/plain", "Domestic heating pump turned off"); | |
} | |
void handleDomesticHeatingPumpAuto() { | |
domesticHeatingPumpStatus = !circulation; | |
server.send(200, "text/plain", "Domestic heating pump set to auto"); | |
} | |
void handleCirculationLoopPumpOn() { | |
circulationLoopPumpStatus = true; | |
server.send(200, "text/plain", "Circulation loop pump turned on"); | |
} | |
void handleCirculationLoopPumpOff() { | |
circulationLoopPumpStatus = false; | |
server.send(200, "text/plain", "Circulation loop pump turned off"); | |
} | |
void handleCirculationLoopPumpAuto() { | |
circulationLoopPumpStatus = circulation; | |
server.send(200, "text/plain", "Circulation loop pump set to auto"); | |
} | |
void handleRoot() { | |
String html = "<html><body><h1>Sean's ESP32 Web Server</h1>"; | |
/* | |
html += "<p>Lead Pump: " + String(leadPumpMode) + "</p>"; | |
html += "<p>Lag Pump: " + String(lagPumpMode) + "</p>"; | |
html += "<p>Domestic Hot Water Pump: " + String(domesticHotWaterPumpMode) + "</p>"; | |
html += "<p>Domestic Heating Pump: " + String(domesticHeatingPumpMode) + "</p>"; | |
html += "<p>Circulation Loop Pump: " + String(circulationLoopPumpMode) + "</p>"; | |
html += "<p>PT1000: " + String(PT1000) + "</p>"; | |
html += "<form action='/form'>"; | |
html += "<p>Lead Pump Mode: <input type='text' name='leadPumpMode' value='" + String(leadPumpMode) + "'></p>"; | |
html += "<p>Lag Pump Mode: <input type='text' name='lagPumpMode' value='" + String(lagPumpMode) + "'></p>"; | |
html += "<p>Domestic Hot Water Pump Mode: <input type='text' name='domesticHotWaterPumpMode' value='" + String(domesticHotWaterPumpMode) + "'></p>"; | |
html += "<p>Domestic Heating Pump Mode: <input type='text' name='domesticHeatingPumpMode' value='" + String(domesticHeatingPumpMode) + "'></p>"; | |
html += "<p>Circulation Loop Pump Mode: <input type='text' name='circulationLoopPumpMode' value='" + String(circulationLoopPumpMode) + "'></p>"; | |
html += "<p>PT1000 Value: <input type='text' name='PT1000' value='" + String(PT1000) + "'></p>"; | |
html += "<p>DSensor1 Value: <input type='text' name='DSensor1' value='" + String((int)DSensor1[0]) + "'></p>"; | |
html += "<p><input type='submit' value='Submit'></p>"; | |
html += "</form>"; | |
html += "</body></html>"; | |
server.send(200, "text/html", html); | |
} | |
*/ | |
void setup() { | |
// Initialize serial communication | |
Serial.begin(115200); | |
pinMode(PT1000Pin, INPUT); | |
pinMode(leadPumpPin, OUTPUT); | |
pinMode(lagPumpPin, OUTPUT); | |
pinMode(domesticHotWaterPumpPin, OUTPUT); | |
pinMode(domesticHeatingPumpPin, OUTPUT); | |
pinMode(circulationLoopPumpPin, OUTPUT); | |
digitalWrite(leadPumpPin, LOW); | |
digitalWrite(lagPumpPin, LOW); | |
digitalWrite(domesticHotWaterPumpPin, LOW); | |
digitalWrite(domesticHeatingPumpPin, LOW); | |
digitalWrite(circulationLoopPumpPin, LOW); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.print("Connecting to "); | |
Serial.print(ssid); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.print("."); | |
} | |
Serial.println(); | |
Serial.print("Connected to WiFi network with IP Address: "); | |
Serial.println(WiFi.localIP()); | |
// Initialize the web server | |
server.begin(); | |
server.on("/", handleRoot); | |
server.on("/", [](){ | |
server.send_P(200, "text/html", htmlreporting.c_str()); | |
}); | |
Serial.println("Web server started"); | |
// Initialize PT1000 analog input | |
pinMode(PT1000Pin, INPUT); | |
// Initialize DS18b20 digital temperature sensors | |
sensors.begin(); | |
// Locate sensors on the bus | |
if (!sensors.getAddress(DSensor1, 0)) { | |
Serial.println("Error: Unable to locate DSensor1."); | |
} | |
if (!sensors.getAddress(DSensor2, 1)) { | |
Serial.println("Error: Unable to locate DSensor2."); | |
} | |
if (!sensors.getAddress(DSensor3, 2)) { | |
Serial.println("Error: Unable to locate DSensor3."); | |
} | |
if (!sensors.getAddress(DSensor4, 3)) { | |
Serial.println("Error: Unable to locate DSensor4."); | |
} | |
if (!sensors.getAddress(DSensor5, 4)) { | |
Serial.println("Error: Unable to locate DSensor5."); | |
} | |
if (!sensors.getAddress(DSensor6, 5)) { | |
Serial.println("Error: Unable to locate DSensor6."); | |
} | |
if (!sensors.getAddress(DSensor7, 6)) { | |
Serial.println("Error: Unable to locate DSensor7."); | |
} | |
if (!sensors.getAddress(DSensor8, 7)) { | |
Serial.println("Error: Unable to locate DSensor8."); | |
} | |
if (!sensors.getAddress(DSensor9, 8)) { | |
Serial.println("Error: Unable to locate DSensor9."); | |
} | |
if (!sensors.getAddress(DSensor10, 9)) { | |
Serial.println("Error: Unable to locate DSensor10."); | |
} | |
if (!sensors.getAddress(DSensor11, 10)) { | |
Serial.println("Error: Unable to locate DSensor11."); | |
} | |
} | |
// Loop function | |
void loop() { | |
// Print ESP32 Local IP Address | |
Serial.println(WiFi.localIP()); | |
int ds1_index = 0; | |
int ds2_index = 0; | |
int ds3_index = 0; | |
int ds4_index = 0; | |
int ds5_index = 0; | |
int ds6_index = 0; | |
int ds7_index = 0; | |
int ds8_index = 0; | |
int ds9_index = 0; | |
int ds10_index = 0; | |
int ds11_index = 0; | |
// Read PT1000 temperature | |
// Read PT1000 temperature | |
float pt1000_temp = readPT1000(); | |
PT1000 = readPT1000(); | |
// Add PT1000 temperature to rolling average array | |
pt1000_temps[pt1000_index] = pt1000_temp; | |
pt1000_index = (pt1000_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate PT1000 rolling average | |
float pt1000_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
pt1000_sum += pt1000_temps[i]; | |
} | |
pt1000avg_f = pt1000_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor1 temperature | |
float ds1_temp = readDSensor1(); | |
// Add DSensor1 temperature to rolling average array | |
ds1_temps[ds1_index] = ds1_temp; | |
ds1_index = (ds1_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor1 rolling average | |
float ds1_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds1_sum += ds1_temps[i]; | |
} | |
DSensor1Average = ds1_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor2 temperature | |
float ds2_temp = readDSensor2(); | |
// Add DSensor2 temperature to rolling average array | |
ds2_temps[ds2_index] = ds2_temp; | |
ds2_index = (ds2_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor2 rolling average | |
float ds2_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds2_sum += ds2_temps[i]; | |
} | |
DSensor2Average = ds2_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor3 temperature | |
float ds3_temp = readDSensor3(); | |
// Add DSensor3 temperature to rolling average array | |
ds3_temps[ds3_index] = ds3_temp; | |
ds3_index = (ds3_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor3 rolling average | |
float ds3_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds3_sum += ds3_temps[i]; | |
} | |
DSensor3Average = ds3_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor4 temperature | |
float ds4_temp = readDSensor4(); | |
// Add DSensor4 temperature to rolling average array | |
ds4_temps[ds4_index] = ds4_temp; | |
ds4_index = (ds4_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor4 rolling average | |
float ds4_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds4_sum += ds4_temps[i]; | |
} | |
DSensor4Average = ds4_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor5 temperature | |
float ds5_temp = readDSensor5(); | |
// Add DSensor5 temperature to rolling average array | |
ds5_temps[ds5_index] = ds5_temp; | |
ds5_index = (ds5_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor5 rolling average | |
float ds5_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds5_sum += ds5_temps[i]; | |
} | |
DSensor5Average = ds5_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor6 temperature | |
float ds6_temp = readDSensor6(); | |
// Add DSensor6 temperature to rolling average array | |
ds6_temps[ds6_index] = ds6_temp; | |
ds6_index = (ds6_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor6 rolling average | |
float ds6_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds6_sum += ds6_temps[i]; | |
} | |
DSensor6Average = ds6_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor7 temperature | |
float ds7_temp = readDSensor7(); | |
// Add DSensor7 temperature to rolling average array | |
ds7_temps[ds7_index] = ds7_temp; | |
ds7_index = (ds7_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor7 rolling average | |
float ds7_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds7_sum += ds7_temps[i]; | |
} | |
DSensor7Average = ds7_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor8 temperature | |
float ds8_temp = readDSensor8(); | |
// Add DSensor8 temperature to rolling average array | |
ds8_temps[ds8_index] = ds8_temp; | |
ds8_index = (ds8_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor8 rolling average | |
float ds8_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds8_sum += ds8_temps[i]; | |
} | |
DSensor8Average = ds8_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor9 temperature | |
float ds9_temp = readDSensor9(); | |
// Add DSensor9 temperature to rolling average array | |
ds9_temps[ds9_index] = ds9_temp; | |
ds9_index = (ds9_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor9 rolling average | |
float ds9_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds9_sum += ds9_temps[i]; | |
} | |
DSensor9Average = ds9_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor10 temperature | |
float ds10_temp = readDSensor10(); | |
// Add DSensor10 temperature to rolling average array | |
ds10_temps[ds10_index] = ds10_temp; | |
ds10_index = (ds10_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor10 rolling average | |
float ds10_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds10_sum += ds10_temps[i]; | |
} | |
DSensor10Average = ds10_sum / ROLLING_ARRAY_SIZE; | |
// Read DSensor11 temperature | |
float ds11_temp = readDSensor11(); | |
// Add DSensor11 temperature to rolling average array | |
ds11_temps[ds11_index] = ds11_temp; | |
ds11_index = (ds11_index + 1) % ROLLING_ARRAY_SIZE; | |
// Calculate DSensor11 rolling average | |
float ds11_sum = 0; | |
for (int i = 0; i < ROLLING_ARRAY_SIZE; i++) { | |
ds11_sum += ds11_temps[i]; | |
} | |
DSensor11Average = ds11_sum / ROLLING_ARRAY_SIZE; | |
// Add the new temperature reading to the rolling average array for each sensor | |
PT1000Array[4] = temperature; | |
DSensor1Array[4] = sensors.getTempC(DSensor1); | |
DSensor2Array[4] = sensors.getTempC(DSensor2); | |
DSensor3Array[4] = sensors.getTempC(DSensor3); | |
DSensor4Array[4] = sensors.getTempC(DSensor4); | |
DSensor5Array[4] = sensors.getTempC(DSensor5); | |
DSensor6Array[4] = sensors.getTempC(DSensor6); | |
DSensor7Array[4] = sensors.getTempC(DSensor7); | |
DSensor8Array[4] = sensors.getTempC(DSensor8); | |
DSensor9Array[4] = sensors.getTempC(DSensor9); | |
DSensor10Array[4] = sensors.getTempC(DSensor10); | |
DSensor11Array[4] = sensors.getTempC(DSensor11); | |
// Shift the rolling average array for each sensor | |
for (int i = 0; i < 4; i++) { | |
PT1000Array[i] = PT1000Array[i + 1]; | |
DSensor1Array[i] = DSensor1Array[i + 1]; | |
DSensor2Array[i] = DSensor2Array[i + 1]; | |
DSensor3Array[i] = DSensor3Array[i + 1]; | |
DSensor4Array[i] = DSensor4Array[i + 1]; | |
DSensor5Array[i] = DSensor5Array[i + 1]; | |
DSensor6Array[i] = DSensor6Array[i + 1]; | |
DSensor7Array[i] = DSensor7Array[i + 1]; | |
DSensor8Array[i] = DSensor8Array[i + 1]; | |
DSensor9Array[i] = DSensor9Array[i + 1]; | |
DSensor10Array[i] = DSensor10Array[i + 1]; | |
DSensor11Array[i] = DSensor11Array[i + 1]; | |
} | |
// Calculate the rolling average for each sensor | |
PT1000Average = (PT1000Array[0] + PT1000Array[1] + PT1000Array[2] + PT1000Array[3] + PT1000Array[4]) / 5.0; | |
DSensor1Average = (DSensor1Array[0] + DSensor1Array[1] + DSensor1Array[2] + DSensor1Array[3] + DSensor1Array[4]) / 5.0; | |
DSensor2Average = (DSensor2Array[0] + DSensor2Array[1] + DSensor2Array[2] + DSensor2Array[3] + DSensor2Array[4]) / 5.0; | |
DSensor3Average = (DSensor3Array[0] + DSensor3Array[1] + DSensor3Array[2] + DSensor3Array[3] + DSensor3Array[4]) / 5.0; | |
DSensor4Average = (DSensor4Array[0] + DSensor4Array[1] + DSensor4Array[2] + DSensor4Array[3] + DSensor4Array[4]) / 5.0; | |
DSensor5Average = (DSensor5Array[0] + DSensor5Array[1] + DSensor5Array[2] + DSensor5Array[3] + DSensor5Array[4]) / 5.0; | |
DSensor6Average = (DSensor6Array[0] + DSensor6Array[1] + DSensor6Array[2] + DSensor6Array[3] + DSensor6Array[4]) / 5.0; | |
DSensor7Average = (DSensor7Array[0] + DSensor7Array[1] + DSensor7Array[2] + DSensor7Array[3] + DSensor7Array[4]) / 5.0; | |
DSensor8Average = (DSensor8Array[0] + DSensor8Array[1] + DSensor8Array[2] + DSensor8Array[3] + DSensor8Array[4]) / 5.0; | |
DSensor9Average = (DSensor9Array[0] + DSensor9Array[1] + DSensor9Array[2] + DSensor9Array[3] + DSensor9Array[4]) / 5.0; | |
DSensor10Average = (DSensor10Array[0] + DSensor10Array[1] + DSensor10Array[2] + DSensor10Array[3] + DSensor10Array[4]) / 5.0; | |
DSensor11Average = (DSensor11Array[0] + DSensor11Array[1] + DSensor11Array[2] + DSensor11Array[3] + DSensor11Array[4]) / 5.0; | |
// Print the temperatures to the serial monitor | |
Serial.print("PT1000: "); | |
Serial.println(PT1000); | |
Serial.print("DSensor1: "); | |
Serial.println(readDSensor1()); | |
Serial.print("DSensor2: "); | |
Serial.println(readDSensor2()); | |
Serial.print("DSensor3: "); | |
Serial.println(readDSensor3()); | |
Serial.print("DSensor4: "); | |
Serial.println(readDSensor4()); | |
Serial.print("DSensor5: "); | |
Serial.println(readDSensor5()); | |
Serial.print("DSensor6: "); | |
Serial.println(readDSensor6()); | |
Serial.print("DSensor7: "); | |
Serial.println(readDSensor7()); | |
Serial.print("DSensor8: "); | |
Serial.println(readDSensor8()); | |
Serial.print("DSensor9: "); | |
Serial.println(readDSensor9()); | |
Serial.print("DSensor10: "); | |
Serial.println(readDSensor10()); | |
Serial.print("DSensor11: "); | |
Serial.println(readDSensor11()); | |
// Print the rolling averages to the serial monitor | |
Serial.print("PT1000 Average: "); | |
Serial.println(PT1000Average); | |
Serial.print("DSensor1 Average: "); | |
Serial.println(DSensor1Average); | |
Serial.print("DSensor2 Average: "); | |
Serial.println(DSensor2Average); | |
Serial.print("DSensor3 Average: "); | |
Serial.println(DSensor3Average); | |
Serial.print("DSensor4 Average: "); | |
Serial.println(DSensor4Average); | |
Serial.print("DSensor5 Average: "); | |
Serial.println(DSensor5Average); | |
Serial.print("DSensor6 Average: "); | |
Serial.println(DSensor6Average); | |
Serial.print("DSensor7 Average: "); | |
Serial.println(DSensor7Average); | |
Serial.print("DSensor8 Average: "); | |
Serial.println(DSensor8Average); | |
Serial.print("DSensor9 Average: "); | |
Serial.println(DSensor9Average); | |
Serial.print("DSensor10 Average: "); | |
Serial.println(DSensor10Average); | |
Serial.print("DSensor11 Average: "); | |
Serial.println(DSensor11Average); | |
server.handleClient(); | |
// Wait for 1 second before taking the next reading | |
delay(1000); | |
} | |
void handleRoot() { | |
String html = "<!DOCTYPE html><html><head><meta http-equiv=\"refresh\" content=\"5\"></head><body>"; | |
char buf[7]; | |
for (int i = 0; i < SENSOR_COUNT; i++) { | |
String temperature_str = dtostrf(dsensors[i], 6, 2, buf); | |
String sensor_name = "DSensor" + String(i + 1); | |
html += "<p>" + sensor_name + ": " + temperature_str + " °C</p>"; | |
} | |
/* | |
for (int i = 0; i < SENSOR_COUNT; i++) { | |
sensors.requestTemperatures(); // Send the command to get temperatures | |
float temperature = sensors.getTempCByIndex(i); | |
dsensors[i] = temperature; | |
} | |
*/ | |
// Replace PT1000 temperature placeholder with actual value | |
String pt1000_f_str = dtostrf(pt1000_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{pt1000_f}}", pt1000_f_str); | |
// Replace PT1000 average temperature placeholder with actual value | |
String pt1000avg_f_str = dtostrf(pt1000avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{pt1000avg_f}}", pt1000avg_f_str); | |
// Replace Dsensor1 temperature placeholder with actual value | |
String dsensor1_f_str = dtostrf(dsensor1_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor1_f}}", dsensor1_f_str); | |
// Replace Dsensor1 average temperature placeholder with actual value | |
String dsensor1avg_f_str = dtostrf(dsensor1avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor1avg_f}}", dsensor1avg_f_str); | |
// Replace Dsensor2 temperature placeholder with actual value | |
String dsensor2_f_str = dtostrf(dsensor2_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor2_f}}", dsensor2_f_str); | |
// Replace Dsensor2 average temperature placeholder with actual value | |
String dsensor2avg_f_str = dtostrf(dsensor2avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor2avg_f}}", dsensor2avg_f_str); | |
// Replace Dsensor3 temperature placeholder with actual value | |
String dsensor3_f_str = dtostrf(dsensor3_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor3_f}}", dsensor3_f_str); | |
// Replace Dsensor3 average temperature placeholder with actual value | |
String dsensor3avg_f_str = dtostrf(dsensor3avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor3avg_f}}", dsensor3avg_f_str); | |
// Replace Dsensor4 temperature placeholder with actual value | |
String dsensor4_f_str = dtostrf(dsensor4_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor4_f}}", dsensor4_f_str); | |
// Replace Dsensor4 average temperature placeholder with actual value | |
String dsensor4avg_f_str = dtostrf(dsensor4avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor4avg_f}}", dsensor4avg_f_str); | |
// Replace Dsensor5 temperature placeholder with actual value | |
String dsensor5_f_str = dtostrf(dsensor5_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor5_f}}", dsensor5_f_str); | |
// Replace Dsensor5 average temperature placeholder with actual value | |
String dsensor5avg_f_str = dtostrf(dsensor5avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor5avg_f}}", dsensor5avg_f_str); | |
// Replace Dsensor6 temperature placeholder with actual value | |
String dsensor6_f_str = dtostrf(dsensor6_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor6_f}}", dsensor6_f_str); | |
// Replace Dsensor6 average temperature placeholder with actual value | |
String dsensor6avg_f_str = dtostrf(dsensor6avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor6avg_f}}", dsensor6avg_f_str); | |
// Replace Dsensor7 temperature placeholder with actual value | |
String dsensor7_f_str = dtostrf(dsensor7_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor7_f}}", dsensor7_f_str); | |
// Replace Dsensor7 average temperature placeholder with actual value | |
String dsensor7avg_f_str = dtostrf(dsensor7avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor7avg_f}}", dsensor7avg_f_str); | |
// Replace Dsensor8 temperature placeholder with actual value | |
String dsensor8_f_str = dtostrf(dsensor8_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor8_f}}", dsensor8_f_str); | |
// Replace Dsensor8 average temperature placeholder with actual value | |
String dsensor8avg_f_str = dtostrf(dsensor8avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor8avg_f}}", dsensor8avg_f_str); | |
// Replace Dsensor9 temperature placeholder with actual value | |
String dsensor9_f_str = dtostrf(dsensor9_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor9_f}}", dsensor9_f_str); | |
// Replace Dsensor9 average temperature placeholder with actual value | |
String dsensor9avg_f_str = dtostrf(dsensor9avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor9avg_f}}", dsensor9avg_f_str); | |
// Replace Dsensor10 temperature placeholder with actual value | |
String dsensor10_f_str = dtostrf(dsensor10_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor10_f}}", dsensor10_f_str); | |
// Replace Dsensor10 average temperature placeholder with actual value | |
String dsensor10avg_f_str = dtostrf(dsensor10avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor10avg_f}}", dsensor10avg_f_str); | |
// Replace Dsensor11 temperature placeholder with actual value | |
String dsensor11_f_str = dtostrf(dsensor11_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor11_f}}", dsensor11_f_str); | |
// Replace Dsensor11 average temperature placeholder with actual value | |
String dsensor11avg_f_str = dtostrf(dsensor11avg_f, 6, 2, buf); // Convert float to string | |
htmlPage.replace("{{dsensor11avg_f}}", dsensor11avg_f_str); | |
// Send the updated HTML page to the client | |
server.send(200, "text/html", htmlPage); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const char html[] PROGMEM = R"====( | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> | |
<meta http-equiv="refresh" content="5" /> | |
<title>Sean's ESP32 Web Server</title> | |
</head> | |
<body> | |
<h1>Sean's ESP32 Web Server</h1> | |
<h2>Sensor Readings:</h2> | |
<table border="1"> | |
<tr> | |
<th>Sensor</th> | |
<th>Reading</th> | |
</tr> | |
<tr> | |
<td>PT1000</td> | |
<td>{{pt1000_f}} °F</td> | |
</tr> | |
<tr> | |
<td>PT1000 Average</td> | |
<td>{{pt1000avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor1</td> | |
<td>{{dsensor1_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor1 Average</td> | |
<td>{{dsensor1avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor2</td> | |
<td>{{dsensor2_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor2 Average</td> | |
<td>{{dsensor2avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor3</td> | |
<td>{{dsensor3_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor3 Average</td> | |
<td>{{dsensor3avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor4</td> | |
<td>{{dsensor4_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor4 Average</td> | |
<td>{{dsensor4avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor5</td> | |
<td>{{dsensor5_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor5 Average</td> | |
<td>{{dsensor5avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor6</td> | |
<td>{{dsensor6_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor6 Average</td> | |
<td>{{dsensor6avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor7</td> | |
<td>{{dsensor7_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor7 Average</td> | |
<td>{{dsensor7avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor8</td> | |
<td>{{dsensor8_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor8 Average</td> | |
<td>{{dsensor8avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor9</td> | |
<td>{{dsensor9_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor9 Average</td> | |
<td>{{dsensor9avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor10</td> | |
<td>{{dsensor10_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor10 Average</td> | |
<td>{{dsensor10avg_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor11</td> | |
<td>{{dsensor11_f}} °F</td> | |
</tr> | |
<tr> | |
<td>DSensor11 Average</td> | |
<td>{{dsensor11avg_f}} °F</td> | |
</tr> | |
</tbody> | |
</table> | |
</body> | |
</html> | |
)===="; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment