Quadrato

Untitled

Mar 15th, 2022
1,166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.64 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <SPI.h>
  3. #include <Adafruit_BMP280.h>
  4. #include <Adafruit_Sensor.h>
  5. #include <DHT.h>
  6. #include <DHT_U.h>
  7. #include <ESP8266WebServer.h>
  8.  
  9. #define BMP_SCK  (13)
  10. #define BMP_MISO (12)
  11. #define BMP_MOSI (11)
  12. #define BMP_CS   (10)
  13.  
  14. #define SEALEVELPRESSURE_HPA (1013.25)
  15.  
  16. // Sensor
  17. uint8_t DHTPIN = D3;
  18. #define DHTTYPE DHT22
  19. DHT_Unified dht(DHTPIN, DHTTYPE);
  20. uint32_t delayMS;
  21. Adafruit_BMP280 bmp;
  22.  
  23. float temperature, humidity, pressure, altitude;
  24.  
  25. // Wifi
  26. const char* ssid = "V9H2";  // Enter SSID here
  27. const char* password = "Vogel9.H2";  //Enter Password here
  28.  
  29. ESP8266WebServer server(80);
  30.  
  31. void setup() {
  32.   Serial.begin(9600);
  33.   while ( !Serial ) delay(100);   // wait for native usb
  34.   Serial.println(F("BMP280 test"));
  35.   unsigned status;
  36.   //status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  37.   status = bmp.begin(0x76);
  38.   if (!status) {
  39.     Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
  40.                       "try a different address!"));
  41.     Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
  42.     Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
  43.     Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
  44.     Serial.print("        ID of 0x60 represents a BME 280.\n");
  45.     Serial.print("        ID of 0x61 represents a BME 680.\n");
  46.     while (1) delay(10);
  47.   }
  48.  
  49.   /* Default settings from datasheet. */
  50.   bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
  51.                   Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
  52.                   Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
  53.                   Adafruit_BMP280::FILTER_X16,      /* Filtering. */
  54.                   Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
  55.  
  56.   dht.begin();
  57.   Serial.println(F("DHTxx Unified Sensor Example"));
  58.   // Print temperature sensor details.
  59.   sensor_t sensor;
  60.   dht.temperature().getSensor(&sensor);
  61.   Serial.println(F("------------------------------------"));
  62.   Serial.println(F("Temperature Sensor"));
  63.   Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  64.   Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  65.   Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  66.   Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
  67.   Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
  68.   Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
  69.   Serial.println(F("------------------------------------"));
  70.   // Print humidity sensor details.
  71.   dht.humidity().getSensor(&sensor);
  72.   Serial.println(F("Humidity Sensor"));
  73.   Serial.print  (F("Sensor Type: ")); Serial.println(sensor.name);
  74.   Serial.print  (F("Driver Ver:  ")); Serial.println(sensor.version);
  75.   Serial.print  (F("Unique ID:   ")); Serial.println(sensor.sensor_id);
  76.   Serial.print  (F("Max Value:   ")); Serial.print(sensor.max_value); Serial.println(F("%"));
  77.   Serial.print  (F("Min Value:   ")); Serial.print(sensor.min_value); Serial.println(F("%"));
  78.   Serial.print  (F("Resolution:  ")); Serial.print(sensor.resolution); Serial.println(F("%"));
  79.   Serial.println(F("------------------------------------"));
  80.   // Set delay between sensor readings based on sensor details.
  81.   delayMS = sensor.min_delay / 1000;
  82.  
  83.  
  84.   Serial.println("Connecting to ");
  85.   Serial.println(ssid);
  86.  
  87.   //connect to your local wi-fi network
  88.   WiFi.begin(ssid, password);
  89.  
  90.   //check wi-fi is connected to wi-fi network
  91.   while (WiFi.status() != WL_CONNECTED) {
  92.   delay(1000);
  93.   Serial.print(".");
  94.   }
  95.   Serial.println("");
  96.   Serial.println("WiFi connected..!");
  97.   Serial.print("Got IP: ");  Serial.println(WiFi.localIP());
  98.  
  99.   server.on("/", handle_OnConnect);
  100.   server.onNotFound(handle_NotFound);
  101.  
  102.   server.begin();
  103.   Serial.println("HTTP server started");
  104. }
  105.  
  106. void loop() {
  107.     // Delay between measurements.
  108.     delay(delayMS);
  109.    
  110.   server.handleClient();
  111. }
  112.  
  113. void handle_OnConnect() {
  114.   temperature = bmp.readTemperature();
  115.   // Get humidity event and print its value.
  116.   sensors_event_t event;
  117.   dht.humidity().getEvent(&event);
  118.   if (isnan(event.relative_humidity)) {
  119.     Serial.println(F("Error reading humidity!"));
  120.   } else {
  121.     humidity = event.relative_humidity;
  122.   }
  123.   pressure = bmp.readPressure() / 100.0F;
  124.   altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA);
  125.   server.send(200, "text/html", SendHTML(temperature,humidity,pressure,altitude));
  126. }
  127.  
  128. void handle_NotFound(){
  129.   server.send(404, "text/plain", "Not found");
  130. }
  131.  
  132. String SendHTML(float temperature,float humidity,float pressure,float altitude){
  133.   String ptr = "<!DOCTYPE html> <html>\n";
  134.   ptr +="<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  135.   ptr +="<title>ESP8266 Weather Station</title>\n";
  136.   ptr +="<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n";
  137.   ptr +="body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  138.   ptr +="p {font-size: 24px;color: #444444;margin-bottom: 10px;}\n";
  139.   ptr +="</style>\n";
  140.   ptr +="</head>\n";
  141.   ptr +="<body>\n";
  142.   ptr +="<div id=\"webpage\">\n";
  143.   ptr +="<h1>ESP8266 Weather Station</h1>\n";
  144.   ptr +="<p>Temperature: ";
  145.   ptr +=temperature;
  146.   ptr +="&deg;C</p>";
  147.   ptr +="<p>Humidity: ";
  148.   ptr +=humidity;
  149.   ptr +="%</p>";
  150.   ptr +="<p>Pressure: ";
  151.   ptr +=pressure;
  152.   ptr +="hPa</p>";
  153.   ptr +="<p>Altitude: ";
  154.   ptr +=altitude;
  155.   ptr +="m</p>";
  156.   ptr +="</div>\n";
  157.   ptr +="</body>\n";
  158.   ptr +="</html>\n";
  159.   return ptr;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment