Last active
January 22, 2023 03:40
-
-
Save Tech500/2e2eb6d7710612dfc59d7bb8ec7e621a 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
//After debugging; 1st ChatGPT Webserver and refinements: | |
//by William Lucid | |
#include <WiFi.h> | |
#include <ESPAsyncWebServer.h> | |
#include "EnvironmentCalculations.h" | |
#include "BME280I2C.h" | |
#include <NTPClient.h> | |
#include <Wire.h> | |
// Replace with your network credentials | |
const char* ssid = "yourSSID"; | |
const char* password = "yourPASSWORD7"; | |
AsyncWebServer server(8030); | |
//BME280 | |
// Assumed environmental values: | |
float referencePressure = 1023.7; // hPa local QFF (official meteor-station reading) -> KEYE, Indianapolis, IND | |
float outdoorTemp = 41; // °F measured local outdoor temp. | |
float barometerAltitude = 250.698; // meters ... map readings + barometer position -> 824 Feet Garmin, GPS measured Altitude. | |
float baroOffset = 0.08; | |
BME280I2C::Settings settings( | |
BME280::OSR_X1, | |
BME280::OSR_X1, | |
BME280::OSR_X1, | |
BME280::Mode_Forced, | |
BME280::StandbyTime_1000ms, | |
BME280::Filter_16, | |
BME280::SpiEnable_False, | |
BME280I2C::I2CAddr_0x76 | |
); | |
BME280I2C bme(settings); | |
float temp(NAN), hum(NAN), pres(NAN), heatIndex, dewPoint, absHum, altitude, seaLevel; | |
float heat, dew, altFeet, temperature, currentPressure; | |
void getData() | |
{ | |
BME280::TempUnit tempUnit(BME280::TempUnit_Celsius); | |
BME280::PresUnit presUnit(BME280::PresUnit_hPa); | |
bme.read(pres, temp, hum, tempUnit, presUnit); | |
EnvironmentCalculations::AltitudeUnit envAltUnit = EnvironmentCalculations::AltitudeUnit_Meters; | |
EnvironmentCalculations::TempUnit envTempUnit = EnvironmentCalculations::TempUnit_Celsius; | |
delay(300); | |
/// To get correct local altitude/height (QNE) the reference Pressure | |
/// should be taken from meteorologic messages (QNH or QFF) | |
/// To get correct seaLevel pressure (QNH, QFF) | |
/// the altitude value should be independent on measured pressure. | |
/// It is necessary to use fixed altitude point e.g. the altitude of barometer read in a map | |
absHum = EnvironmentCalculations::AbsoluteHumidity(temp, hum, envTempUnit); | |
altitude = EnvironmentCalculations::Altitude(pres, envAltUnit, referencePressure, outdoorTemp, envTempUnit); | |
dewPoint = EnvironmentCalculations::DewPoint(temp, hum, envTempUnit); | |
heatIndex = EnvironmentCalculations::HeatIndex(temp, hum, envTempUnit); | |
seaLevel = EnvironmentCalculations::EquivalentSeaLevelPressure(barometerAltitude, temp, pres, envAltUnit, envTempUnit); | |
temperature = ((temp * 1.8) + 32); //Convert to Fahrenheit | |
heat = (heatIndex * 1.8) + 32; | |
dew = (dewPoint * 1.8) + 32; | |
altFeet = 843; | |
temperature = (temp * 1.8) + 32; //Convert to Fahrenheit | |
currentPressure = (seaLevel * 0.02953) + baroOffset; //Convert from hPa to in Hg. | |
} | |
WiFiUDP ntpUDP; | |
// Create an instance of the NTP client | |
NTPClient timeClient(ntpUDP, "pool.ntp.org", 7200, 60000); | |
void setup() { | |
// Start the serial communication | |
Serial.begin(115200); | |
// Connect to Wi-Fi | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting to WiFi..."); | |
} | |
Serial.println("Connected to WiFi"); | |
Serial.println("local ip"); | |
Serial.println(WiFi.localIP()); | |
Wire.begin(); | |
while(!bme.begin()) | |
{ | |
Serial.println("Could not find BME280 sensor!"); | |
delay(1000); | |
} | |
switch(bme.chipModel()) | |
{ | |
case BME280::ChipModel_BME280: | |
Serial.println("Found BME280 sensor! Success."); | |
break; | |
case BME280::ChipModel_BMP280: | |
Serial.println("Found BMP280 sensor! No Humidity available."); | |
break; | |
default: | |
Serial.println("Found UNKNOWN sensor! Error!"); | |
} | |
getData(); | |
printBME280Data(); | |
// Start the NTP client | |
timeClient.begin(); | |
// Serve a web page that displays the sensor readings and calculations | |
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ | |
String html = "<html><body>"; | |
html += "<h1>BME280 Sensor Readings and Calculations</h1>"; | |
html += "<p>Temperature: " + (String)temperature + " F</p>"; | |
html += "<p>Humidity: " + (String)hum + " %</p>"; | |
html += "<p>Absolute Humidity: " + (String)absHum + " g/m cubed</p>"; | |
html += "<p>Pressure: " + (String)currentPressure + " inHg</p>"; | |
html += "<p>Dew Point: " + (String)dew + " F</p>"; | |
html += "<p>Heat Index: " + (String)heat + " F</p>"; | |
html += "<p>Altitude: " + (String)altFeet + " Ft</p>"; | |
html += "</body></html>"; | |
request->send(200, "text/html", html); | |
}); | |
// Start the server | |
server.begin(); | |
} | |
void loop() { | |
// Update the time | |
timeClient.update(); | |
// Every 15 minutes | |
if (timeClient.getMinutes() % 15 == 0 && timeClient.getSeconds() == 0) { | |
getData(); | |
printBME280Data(); | |
} | |
} | |
void printBME280Data() | |
{ | |
getData(); | |
delay(300); | |
Serial.print("Temp: "); | |
Serial.print(temperature); | |
Serial.println(" F"); | |
Serial.print("Humidity: "); | |
Serial.print(hum); | |
Serial.println("% RH"); | |
Serial.print("Pressure: "); | |
Serial.print(currentPressure); | |
Serial.println(" inHg"); | |
Serial.print("Altitude: "); | |
Serial.print(altFeet); | |
Serial.println(" ft"); | |
Serial.print("Dew point: "); | |
Serial.print(dew); | |
Serial.println(" F"); | |
Serial.print("Equivalent Sea Level Pressure: "); | |
Serial.print(seaLevel); | |
Serial.println(" hPa"); | |
Serial.print("Heat Index: "); | |
Serial.print(heat); | |
Serial.println(" F"); | |
Serial.print("Absolute Humidity: "); | |
Serial.print(absHum); | |
Serial.println(" g/m cubed"); | |
Serial.print("Altitude: "); | |
Serial.print(altFeet); | |
Serial.println(" Ft"); | |
} | |
/* | |
Serial Monitor Output: | |
Connecting to WiFi... | |
Connecting to WiFi... | |
Connected to WiFi | |
local ip | |
10.0.0.115 | |
Found BME280 sensor! Success. | |
Temp: 67.03 F | |
Humidity: 30.54% RH | |
Pressure: 30.04 inHg | |
Altitude: 843.00 ft | |
Dew point: 35.01 F | |
Equivalent Sea Level Pressure: 1014.68 hPa | |
Heat Index: 64.87 F | |
Absolute Humidity: 5.11 g/m cubed | |
Altitude: 843.00 Ft | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment