Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <SPI.h>
- #include <Adafruit_BMP280.h>
- #include <Adafruit_Sensor.h>
- #include <DHT.h>
- #include <DHT_U.h>
- #include <ESP8266WebServer.h>
- #define BMP_SCK (13)
- #define BMP_MISO (12)
- #define BMP_MOSI (11)
- #define BMP_CS (10)
- #define SEALEVELPRESSURE_HPA (1013.25)
- // Sensor
- uint8_t DHTPIN = D3;
- #define DHTTYPE DHT22
- DHT_Unified dht(DHTPIN, DHTTYPE);
- uint32_t delayMS;
- Adafruit_BMP280 bmp;
- float temperature, humidity, pressure, altitude;
- // Wifi
- const char* ssid = "V9H2"; // Enter SSID here
- const char* password = "Vogel9.H2"; //Enter Password here
- ESP8266WebServer server(80);
- void setup() {
- Serial.begin(9600);
- while ( !Serial ) delay(100); // wait for native usb
- Serial.println(F("BMP280 test"));
- unsigned status;
- //status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
- status = bmp.begin(0x76);
- if (!status) {
- Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
- "try a different address!"));
- Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
- Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
- Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
- Serial.print(" ID of 0x60 represents a BME 280.\n");
- Serial.print(" ID of 0x61 represents a BME 680.\n");
- while (1) delay(10);
- }
- /* Default settings from datasheet. */
- bmp.setSampling(Adafruit_BMP280::MODE_NORMAL, /* Operating Mode. */
- Adafruit_BMP280::SAMPLING_X2, /* Temp. oversampling */
- Adafruit_BMP280::SAMPLING_X16, /* Pressure oversampling */
- Adafruit_BMP280::FILTER_X16, /* Filtering. */
- Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
- dht.begin();
- Serial.println(F("DHTxx Unified Sensor Example"));
- // Print temperature sensor details.
- sensor_t sensor;
- dht.temperature().getSensor(&sensor);
- Serial.println(F("------------------------------------"));
- Serial.println(F("Temperature Sensor"));
- Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
- Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
- Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
- Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
- Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
- Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
- Serial.println(F("------------------------------------"));
- // Print humidity sensor details.
- dht.humidity().getSensor(&sensor);
- Serial.println(F("Humidity Sensor"));
- Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
- Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
- Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
- Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
- Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
- Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
- Serial.println(F("------------------------------------"));
- // Set delay between sensor readings based on sensor details.
- delayMS = sensor.min_delay / 1000;
- Serial.println("Connecting to ");
- Serial.println(ssid);
- //connect to your local wi-fi network
- WiFi.begin(ssid, password);
- //check wi-fi is connected to wi-fi network
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected..!");
- Serial.print("Got IP: "); Serial.println(WiFi.localIP());
- server.on("/", handle_OnConnect);
- server.onNotFound(handle_NotFound);
- server.begin();
- Serial.println("HTTP server started");
- }
- void loop() {
- // Delay between measurements.
- delay(delayMS);
- server.handleClient();
- }
- void handle_OnConnect() {
- temperature = bmp.readTemperature();
- // Get humidity event and print its value.
- sensors_event_t event;
- dht.humidity().getEvent(&event);
- if (isnan(event.relative_humidity)) {
- Serial.println(F("Error reading humidity!"));
- } else {
- humidity = event.relative_humidity;
- }
- pressure = bmp.readPressure() / 100.0F;
- altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);
- server.send(200, "text/html", SendHTML(temperature,humidity,pressure,altitude));
- }
- void handle_NotFound(){
- server.send(404, "text/plain", "Not found");
- }
- String SendHTML(float temperature,float humidity,float pressure,float altitude){
- String ptr = "<!DOCTYPE html> <html>\n";
- ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
- ptr +="<title>ESP8266 Weather Station</title>\n";
- ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
- ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
- ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
- ptr +="</style>\n";
- ptr +="</head>\n";
- ptr +="<body>\n";
- ptr +="<div id=\"webpage\">\n";
- ptr +="<h1>ESP8266 Weather Station</h1>\n";
- ptr +="<p>Temperature: ";
- ptr +=temperature;
- ptr +="°C</p>";
- ptr +="<p>Humidity: ";
- ptr +=humidity;
- ptr +="%</p>";
- ptr +="<p>Pressure: ";
- ptr +=pressure;
- ptr +="hPa</p>";
- ptr +="<p>Altitude: ";
- ptr +=altitude;
- ptr +="m</p>";
- ptr +="</div>\n";
- ptr +="</body>\n";
- ptr +="</html>\n";
- return ptr;
- }
Advertisement
Add Comment
Please, Sign In to add comment