mc_workshop

/Weather Station whit ESP8266 nodeMCU and BME280 sensor

Apr 27th, 2019
131
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Weather Station whit ESP8266 nodeMCU and BME280 sensor
  2. //Maggio 2019 by M.C.
  3. //This example code is in the public domain.
  4. #include <ESP8266WiFi.h>
  5. #include <ESP8266WebServer.h>
  6. #include <Wire.h>
  7. #include <Adafruit_Sensor.h>
  8. #include <Adafruit_BME280.h>
  9. #define BME280_ADDRESS (0x76)
  10. #define SEALEVELPRESSURE_HPA (1013.25)
  11. Adafruit_BME280 bme;
  12. float temperature, humidity, pressure, altitude;
  13. const char* ssid = "xxxxxxxxxxxx";
  14. const char* password = "xxxxxxxxxxx";
  15. IPAddress local_ip(192,168,1,1);
  16. IPAddress gateway(192,168,1,1);
  17. IPAddress subnet(255,255,255,0);
  18. ESP8266WebServer server(80);
  19. void setup() {
  20. Serial.begin(115200);
  21. bme.begin(0x76);
  22. Serial.println("Connecting to ");
  23. Serial.println(ssid);
  24. WiFi.softAP(ssid, password);
  25. WiFi.softAPConfig(local_ip, gateway, subnet);
  26. delay(100);
  27. server.begin();
  28. Serial.println("HTTP server started");
  29. }
  30. void loop() {
  31. server.handleClient();
  32. temperature = bme.readTemperature();
  33. humidity = bme.readHumidity();
  34. pressure = bme.readPressure() / 100.0F;
  35. altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);
  36. server.send(200, "text/html", SendHTML(temperature, humidity, pressure, altitude));
  37. }
  38. void handle_NotFound() {
  39. server.send(404, "text/plain", "Not found");
  40. }
  41. String SendHTML(float temperature, float humidity, float pressure, float altitude) {
  42. String ptr = "<!DOCTYPE html> <html>\n";
  43. ptr += "<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n";
  44. ptr += "<title>xxxxxxxxxxxxxxx</title>\n";
  45. ptr += "<style>html { font-family: Arial; display: inline-block; margin: 0px auto; text-align: center;}\n";
  46. ptr += "body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;}\n";
  47. ptr += "p {font-size: 18px;color: #444444;margin-bottom: 10px;}\n";
  48. ptr += "</style>\n";
  49. ptr += "<script>\n";
  50. ptr += "setInterval(loadDoc,1000);\n";
  51. ptr += "function loadDoc() {\n";
  52. ptr += "var xhttp = new XMLHttpRequest();\n";
  53. ptr += "xhttp.onreadystatechange = function() {\n";
  54. ptr += "if (this.readyState == 4 && this.status == 200) {\n";
  55. ptr += "document.body.innerHTML =this.responseText}\n";
  56. ptr += "};\n";
  57. ptr += "xhttp.open(\"GET\", \"/\", true);\n";
  58. ptr += "xhttp.send();\n";
  59. ptr += "}\n";
  60. ptr += "</script>\n";
  61. ptr += "</head>\n";
  62. ptr += "<body>\n";
  63. ptr += "<div id=\"webpage\">\n";
  64. ptr += "<h1>xxxxxxxxxxxxx</h1>\n";
  65. ptr += "<p>Temperature: ";
  66. ptr += temperature;
  67. ptr += "&deg;C</p>";
  68. ptr += "<p>Humidity: ";
  69. ptr += humidity;
  70. ptr += "UR %</p>";
  71. ptr += "<p>Pressure: ";
  72. ptr += pressure;
  73. ptr += "hPa</p>";
  74. ptr += "<p>Altitude: ";
  75. ptr += altitude;
  76. ptr += "m s.l.m.</p>";
  77. ptr += "</div>\n";
  78. ptr += "</body>\n";
  79. ptr += "</html>\n";
  80. return ptr;
  81. }
RAW Paste Data