/********* Rui Santos & Sara Santos - Random Nerd Tutorials Complete project details at https://RandomNerdTutorials.com/esp8266-nodemcu-bmp388-arduino/ 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. *********/ #include #include #include #include #include #include // Replace with your network credentials const char* ssid = "REPLACE_WITH_YOUR_SSID"; const char* password = "REPLACE_WITH_YOUR_PASSWORD"; //Uncomment if using SPI /*#define BMP_SCK 14 #define BMP_MISO 12 #define BMP_MOSI 13 #define BMP_CS 15*/ #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BMP3XX bmp; float temp; float pres; float alt; AsyncWebServer server(80); AsyncEventSource events("/events"); unsigned long lastTime = 0; unsigned long timerDelay = 30000; // send readings timer void getBMPReadings(){ if (! bmp.performReading()) { Serial.println("Failed to perform reading :("); return; } temp = bmp.temperature; pres = bmp.pressure / 100.0; alt = bmp.readAltitude(SEALEVELPRESSURE_HPA); } String processor(const String& var){ getBMPReadings(); //Serial.println(var); if(var == "TEMPERATURE"){ return String(temp); } else if(var == "PRESSURE"){ return String(pres); } else if(var == "ALTITUDE"){ return String(alt); } } const char index_html[] PROGMEM = R"rawliteral( BMP388 Web Server

BMP388 WEB SERVER

PRESSURE

%PRESSURE% hPa

ALTITUDE

%ALTITUDE% m

TEMPERATURE

%TEMPERATURE% °C

)rawliteral"; void setup() { Serial.begin(115200); WiFi.mode(WIFI_STA); // Set device as a Wi-Fi Station WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Setting as a Wi-Fi Station.."); } Serial.print("Station IP Address: "); Serial.println(WiFi.localIP()); Serial.println(); // Init BMEP388 sensor if (!bmp.begin_I2C()) { // hardware I2C mode, can pass in address & alt Wire //if (! bmp.begin_SPI(BMP_CS)) { // hardware SPI mode //if (! bmp.begin_SPI(BMP_CS, BMP_SCK, BMP_MISO, BMP_MOSI)) { // software SPI mode Serial.println("Could not find a valid BMP3 sensor, check wiring!"); while (1); } // Set up oversampling and filter initialization bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X); bmp.setPressureOversampling(BMP3_OVERSAMPLING_4X); bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3); bmp.setOutputDataRate(BMP3_ODR_50_HZ); //Get readings when initializing getBMPReadings(); // Handle Web Server server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/html", index_html, processor); }); // Handle Web Server Events events.onConnect([](AsyncEventSourceClient *client){ if(client->lastId()){ Serial.printf("Client reconnected! Last message ID that it got is: %u\n", client->lastId()); } // send event with message "hello!", id current millis // and set reconnect delay to 1 second client->send("hello!", NULL, millis(), 10000); }); server.addHandler(&events); server.begin(); } void loop() { if ((millis() - lastTime) > timerDelay) { getBMPReadings(); Serial.printf("Pressure = %.2f hPa \n", pres); Serial.printf("Altitude = %.2f m \n", alt); Serial.printf("Temperature = %.2f ÂșC \n", temp); Serial.println(); // Send Events to the Web Server with the Sensor Readings events.send("ping",NULL,millis()); events.send(String(pres).c_str(),"pressure",millis()); events.send(String(alt).c_str(),"altitude",millis()); events.send(String(temp).c_str(),"temperature",millis()); lastTime = millis(); } }