Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. // Load Wi-Fi library
  2. #include <ESP8266WiFi.h>
  3. #include <Wire.h>
  4. #include <Adafruit_BME280.h>
  5. #include <Adafruit_Sensor.h>
  6.  
  7. //uncomment the following lines if you're using SPI
  8. /*#include <SPI.h>
  9. #define BME_SCK 14
  10. #define BME_MISO 12
  11. #define BME_MOSI 13
  12. #define BME_CS 15*/
  13.  
  14. #define SEALEVELPRESSURE_HPA (1013.25)
  15.  
  16. Adafruit_BME280 bme; // I2C
  17. //Adafruit_BME280 bme(BME_CS); // hardware SPI
  18. //Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
  19.  
  20. // Replace with your network credentials
  21. const char* ssid = "xxxxxxxxxxxxxxxxxxxx";
  22. const char* password = "xxxxxxxxxxxxxxx";
  23.  
  24. // Set web server port number to 80
  25. WiFiServer server(80);
  26.  
  27. // Variable to store the HTTP request
  28. String header;
  29.  
  30. void setup() {
  31. Serial.begin(115200);
  32. pinMode(LED_BUILTIN, OUTPUT);
  33. bool status;
  34.  
  35. // default settings
  36. // (you can also pass in a Wire library object like &Wire2)
  37. //status = bme.begin();
  38. if (!bme.begin(0x76)) {
  39. Serial.println("Could not find a valid BME280 sensor, check wiring!");
  40. while (1);
  41. }
  42.  
  43. // Connect to Wi-Fi network with SSID and password
  44. Serial.print("Connecting to ");
  45. Serial.println(ssid);
  46. WiFi.begin(ssid, password);
  47. while (WiFi.status() != WL_CONNECTED) {
  48. delay(500);
  49. Serial.print(".");
  50. }
  51. // Print local IP address and start web server
  52. Serial.println("");
  53. Serial.println("WiFi connected.");
  54. Serial.println("IP address: ");
  55. Serial.println(WiFi.localIP());
  56. server.begin();
  57. }
  58.  
  59. void loop(){
  60. WiFiClient client = server.available(); // Listen for incoming clients
  61.  
  62. if (client) { // If a new client connects,
  63. Serial.println("New Client."); // print a message out in the serial port
  64. String currentLine = ""; // make a String to hold incoming data from the client
  65. while (client.connected()) { // loop while the client's connected
  66. if (client.available()) { // if there's bytes to read from the client,
  67. char c = client.read(); // read a byte, then
  68. Serial.write(c); // print it out the serial monitor
  69. header += c;
  70. if (c == '\n') { // if the byte is a newline character
  71. // if the current line is blank, you got two newline characters in a row.
  72. // that's the end of the client HTTP request, so send a response:
  73. if (currentLine.length() == 0) {
  74. // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  75. // and a content-type so the client knows what's coming, then a blank line:
  76. client.println("HTTP/1.1 200 OK");
  77. client.println("Content-type:text/html");
  78. client.println("Connection: close");
  79. client.println();
  80.  
  81. // Display the HTML web page
  82. client.println("<!DOCTYPE html><html>");
  83. client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
  84. client.println("<link rel=\"icon\" href=\"data:,\">");
  85. // CSS to style the table
  86. client.println("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}");
  87. client.println("table { border-collapse: collapse; width:40%; margin-left:auto; margin-right:auto; }");
  88. client.println("th { padding: 12px; background-color: #0043af; color: white; }");
  89. client.println("tr { border: 1px solid #ddd; padding: 12px; }");
  90. client.println("tr:hover { background-color: #bcbcbc; }");
  91. client.println("td { border: none; padding: 12px; }");
  92. client.println(".sensor { color:white; font-weight: bold; background-color: #bcbcbc; padding: 1px; }");
  93.  
  94. // Web Page Heading
  95. digitalWrite(LED_BUILTIN, LOW);
  96. delay(1000);
  97. digitalWrite(LED_BUILTIN, HIGH);
  98. delay(1000);
  99. client.println("</style></head><body><h1>Temperatur 1</h1>");
  100. client.println("<table><tr><th>Messung</th><th>Wert</th></tr>");
  101. client.println("<tr><td>Temperatur C.</td><td><span class=\"sensor\">");
  102. client.println(bme.readTemperature());
  103. client.println(" *C</span></td></tr>");
  104. client.println("<tr><td>Temperatur F.</td><td><span class=\"sensor\">");
  105. client.println(1.8 * bme.readTemperature() + 32);
  106. client.println(" *F</span></td></tr>");
  107. client.println("<tr><td>Luftdruck</td><td><span class=\"sensor\">");
  108. client.println(bme.readPressure() / 100.0F);
  109. client.println(" hPa</span></td></tr>");
  110. client.println("<tr><td>Hoehe ca.</td><td><span class=\"sensor\">");
  111. client.println(bme.readAltitude(SEALEVELPRESSURE_HPA));
  112. client.println(" m</span></td></tr>");
  113. client.println("<tr><td>Luftfeuchte</td><td><span class=\"sensor\">");
  114. client.println(bme.readHumidity());
  115. client.println(" %</span></td></tr>");
  116. client.println("</body></html>");
  117.  
  118.  
  119. // The HTTP response ends with another blank line
  120. client.println();
  121. // Break out of the while loop
  122. break;
  123. } else { // if you got a newline, then clear currentLine
  124. currentLine = "";
  125. }
  126. } else if (c != '\r') { // if you got anything else but a carriage return character,
  127. currentLine += c; // add it to the end of the currentLine
  128. }
  129. }
  130. }
  131. // Clear the header variable
  132. header = "";
  133. // Close the connection
  134. client.stop();
  135. Serial.println("Client disconnected.");
  136. Serial.println("");
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement