Seelenkind

Wetterstation NodeMCU Esp8266 + BME280 + ML8511

Jun 12th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 14.93 KB | None | 0 0
  1. //*******************************************************************************
  2. // Bibliotheken integrieren
  3. #include <NTPClient.h>
  4. #include <ESP8266WiFi.h>
  5. #include <WiFiUdp.h>
  6. #include <Adafruit_BME280.h>
  7. #include <Adafruit_Sensor.h>
  8. #include <Adafruit_GFX.h>
  9. #include <Adafruit_SSD1306.h>
  10. #include <SPI.h>
  11. #include <TFT_eSPI.h>
  12. #include <TaskScheduler.h>
  13. //*******************************************************************************
  14.  
  15. //*******************************************************************************
  16. // Constanten definieren
  17. #define UVsensorIn A0
  18. #define SCREEN_WIDTH 128
  19. #define SCREEN_HEIGHT 64
  20. #define cs 10
  21. #define dc 9
  22. #define rst 8
  23. //*******************************************************************************
  24.  
  25. //*******************************************************************************
  26. // Objekte generieren
  27. Adafruit_BME280 bme;
  28. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
  29. TFT_eSPI tft = TFT_eSPI();
  30. WiFiUDP ntpUDP;
  31. WiFiServer server(80);
  32. WiFiClient client = server.available();
  33. NTPClient timeClient(ntpUDP, "ptbtime1.ptb.de", 3600); // 3600
  34. Scheduler ablauf;
  35. //*******************************************************************************
  36.  
  37. //*******************************************************************************
  38. // Globale Variablen
  39. const char *ssid = "SSID";
  40. const char *password = "WiFi Sifresi";
  41. float temperatur, old_temperatur, druck, old_druck, feuchtigkeit, old_feuchtigkeit, outputVoltage, uvIntensity, old_uvIntensity;
  42. int uvLevel, uhr_zeit;
  43. const float inMin = 1.10, inMax = 3.17, outMin = 0.0, outMax = 15.0;
  44. byte sayac = 1;
  45. String old_time, uhrzeit, header;
  46. //*******************************************************************************
  47.  
  48. //*******************************************************************************
  49. // Text an Position x,y mit der Schriftgröße Sze ausgeben
  50. void printOled(byte x, byte y, byte Sze, String txt)
  51. {
  52.   display.setTextSize(Sze);
  53.   display.setTextColor(SSD1306_WHITE);
  54.   display.setCursor(x, y);
  55.   display.println(txt);
  56. }
  57. //*******************************************************************************
  58.  
  59. //*******************************************************************************
  60. // mapf funktion als float um Analog Input werte als Fließkommazahl auszuwerten
  61. float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  62. {
  63.   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
  64. }
  65. //*******************************************************************************
  66.  
  67. //*******************************************************************************
  68. // UV Wert wird vom Sensor x mal ausgelesenlesen
  69. int averageAnalogRead(int pinToRead, byte numberOfReadings)
  70. {
  71.   unsigned int runningValue = 0;
  72.   for (int x = 0; x < numberOfReadings; x++)
  73.   {
  74.     runningValue += analogRead(pinToRead);
  75.   }
  76.   return (runningValue / numberOfReadings);
  77. }
  78. //*******************************************************************************
  79.  
  80. //*******************************************************************************
  81. // Werte von den Sensoren einlesen
  82. void getValues()
  83. {
  84.   old_temperatur = temperatur;
  85.   old_druck = druck;
  86.   old_feuchtigkeit = feuchtigkeit;
  87.   old_uvIntensity = uvIntensity;
  88.   temperatur = bme.readTemperature() - 1;
  89.   druck = int(bme.readPressure() / 100.0F) - 1;
  90.   feuchtigkeit = bme.readHumidity() + 4.43;
  91.   uvLevel = averageAnalogRead(UVsensorIn, 8); // UV Sensor 8 mal einlesen und Durchschnitt ermitteln
  92.   outputVoltage = 3.27 * uvLevel / 1023;      // eingelesenen Wert in Spannung umwandeln
  93.   // eingelesene Spannung mit Werten aus dem Datenblatt in UV Wert umwandeln
  94.   uvIntensity = constrain(mapfloat(outputVoltage, inMin, inMax, outMin, outMax), 0, 15);
  95.   Serial.println();
  96.   Serial.println("Werte aktualisiert");
  97. }
  98. //*******************************************************************************
  99.  
  100. //*******************************************************************************
  101. // Zeit über WLAN abrufen und 1 Addieren wegen Sommerzeit
  102. void get_time()
  103. {
  104.   uhr_zeit = (timeClient.getFormattedTime().substring(0, 2).toInt() + 1);
  105.   if (uhr_zeit > 23)
  106.     uhr_zeit = 0;
  107.   // führende null hinzufügen wenn Uhrzeit < 10
  108.   uhrzeit = String(uhr_zeit);
  109.   if (uhr_zeit < 10)
  110.     uhrzeit = "0" + uhrzeit;
  111.   // Uhrzeit mit den Minuten & Sekunden zusammensetzen
  112.   uhrzeit = uhrzeit + timeClient.getFormattedTime().substring(2);
  113. }
  114. //*******************************************************************************
  115.  
  116. //*******************************************************************************
  117. // Werte auf dem OLED Display abwechselnd ausgeben
  118. void printOLED()
  119. {
  120.   Serial.println();
  121.   display.clearDisplay();
  122.   if (sayac == 1)
  123.   {
  124.     Serial.println("Anzeige auf OLED Display >> Temperatur = " + String(temperatur) + " °C");
  125.     printOled(24, 0, 3, String(temperatur, 1));
  126.     printOled(58, 32, 3, "C");
  127.   }
  128.   if (sayac == 2)
  129.   {
  130.     Serial.println("Anzeige auf OLED Display >> Druck = " + String(druck) + " hPa");
  131.     printOled(40, 0, 3, String(druck, 0));
  132.     printOled(40, 32, 3, "hPa");
  133.   }
  134.   if (sayac == 3)
  135.   {
  136.     Serial.println("Anzeige auf OLED Display >> Luftfeuchtigkeit = " + String(feuchtigkeit) + " %");
  137.     printOled(50, 0, 3, String(feuchtigkeit, 0));
  138.     printOled(58, 32, 3, "%");
  139.   }
  140.   if (sayac == 4)
  141.   {
  142.     //Serial.println("Analaog Read A0:  " + String(uvLevel));
  143.     Serial.println("Anzeige auf OLED Display >> UV Intensität: " + String(uvIntensity) + " mW/cm^2");
  144.     printOled(35, 0, 3, String(uvIntensity, 1));
  145.     printOled(12, 32, 2, " mW/cm^2");
  146.   }
  147.   if (sayac == 5)
  148.   {
  149.     Serial.println("Anzeige auf OLED Display >> Uhrzeit: " + uhrzeit);
  150.     printOled(40, 0, 2, "ZEIT");
  151.     printOled(15, 40, 2, uhrzeit);
  152.   }
  153.   display.display();
  154. }
  155. //*******************************************************************************
  156.  
  157. //*******************************************************************************
  158. // Werte auf dem TFT Display ausgeben
  159. void printTFT()
  160. {
  161.   tft.setCursor(85, 5);
  162.   tft.setTextColor(TFT_LIGHTGREY);
  163.   tft.print(String(old_temperatur, 1));
  164.   tft.setCursor(85, 5);
  165.   tft.setTextColor(TFT_RED);
  166.   tft.print(String(temperatur, 1) + " C");
  167.  
  168.   tft.setCursor(97, 25);
  169.   tft.setTextColor(TFT_LIGHTGREY);
  170.   tft.print(String(old_druck, 0));
  171.   tft.setCursor(97, 25);
  172.   tft.setTextColor(TFT_BLACK);
  173.   tft.print(String(druck, 0));
  174.   tft.setTextSize(1);
  175.   tft.setCursor(135, 30);
  176.   tft.print(" hPa");
  177.   tft.setTextSize(2);
  178.  
  179.   tft.setCursor(107, 45);
  180.   tft.setTextColor(TFT_LIGHTGREY);
  181.   tft.print(String(old_feuchtigkeit, 0));
  182.   tft.setCursor(107, 45);
  183.   tft.setTextColor(TFT_BLUE);
  184.   tft.print(String(feuchtigkeit, 0) + " %");
  185.  
  186.   tft.setCursor(110, 65);
  187.   tft.setTextColor(TFT_LIGHTGREY);
  188.   tft.print(String(old_uvIntensity, 1));
  189.   tft.setCursor(110, 65);
  190.   tft.setTextColor(TFT_YELLOW);
  191.   tft.print(String(uvIntensity, 1));
  192.   Serial.println("Aktuelle Werte auf dem TFT Display ausgegeben");
  193. }
  194. //*******************************************************************************
  195.  
  196. //*******************************************************************************
  197. // jede sekunde über WLAN Zeit abrufen und auf TFT Display anzeigen
  198. void t1Callback()
  199. {
  200.   timeClient.update();
  201.   get_time();
  202.   tft.setCursor(32, 105);
  203.   tft.setTextColor(TFT_LIGHTGREY);
  204.   tft.print(old_time);
  205.   tft.setCursor(32, 105);
  206.   tft.setTextColor(TFT_DARKGREEN);
  207.   tft.print(uhrzeit);
  208.   old_time = uhrzeit;
  209.   client.stopAll();
  210. }
  211. Task t1(1000, TASK_FOREVER, &t1Callback);
  212. //*******************************************************************************
  213.  
  214. //*******************************************************************************
  215. // alle 3 sekunden Werte abwechselnd auf OLED Display anzeigen
  216. void t2Callback()
  217. {
  218.   sayac++;
  219.   if (sayac >= 6)
  220.     sayac = 1;
  221.   printOLED();
  222. }
  223. Task t2(3000, TASK_FOREVER, &t2Callback);
  224. //*******************************************************************************
  225.  
  226. //*******************************************************************************
  227. // alle 10 Sekunden Werte einlesen und auf TFT Display anzeigen
  228. void t3Callback()
  229. {
  230.   getValues();
  231.   printTFT();
  232. }
  233. Task t3(10000, TASK_FOREVER, &t3Callback);
  234. //*******************************************************************************
  235.  
  236. //*******************************************************************************
  237. void wifiConnect()
  238. {
  239.   Serial.print("Connecting to ");
  240.   Serial.println(ssid);
  241.   WiFi.begin(ssid, password);
  242.   while (WiFi.status() != WL_CONNECTED)
  243.   {
  244.     delay(500);
  245.     Serial.print(".");
  246.   }
  247.   // Print local IP address and start web server
  248.   Serial.println("");
  249.   Serial.println("WiFi connected.");
  250.   Serial.println("IP address: ");
  251.   Serial.println(WiFi.localIP());
  252.   server.begin();
  253. }
  254. //*******************************************************************************
  255.  
  256. void setup()
  257. {
  258.   //*******************************************************************************
  259.   Serial.begin(115200);
  260.   //*******************************************************************************
  261.   timeClient.begin();
  262.   //*******************************************************************************
  263.   bme.begin();
  264.   //***************************************************
  265.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  266.   //*******************************************************************************
  267.   // TFT Display initialisieren
  268.   tft.init();
  269.   tft.setRotation(1);
  270.   tft.fillScreen(TFT_LIGHTGREY);
  271.   tft.setTextSize(2);
  272.   //*******************************************************************************
  273.   // Tasks starten
  274.   ablauf.init();
  275.   ablauf.addTask(t1);
  276.   t1.enable();
  277.   ablauf.addTask(t2);
  278.   t2.enable();
  279.   ablauf.addTask(t3);
  280.   t3.enable();
  281.   //*******************************************************************************
  282.   // Connect to Wi-Fi network with SSID and password
  283.   wifiConnect();
  284.   //*******************************************************************************
  285.  
  286.   //*******************************************************************************
  287.   // Statischen Text auf TFT Display anzeigen
  288.   tft.setCursor(5, 5);
  289.   tft.setTextColor(TFT_RED);
  290.   tft.print("TEMP:");
  291.  
  292.   tft.setCursor(5, 25);
  293.   tft.setTextColor(TFT_BLACK);
  294.   tft.print("DRUCK:");
  295.  
  296.   tft.setCursor(5, 45);
  297.   tft.setTextColor(TFT_BLUE);
  298.   tft.print("FEUCHTE:");
  299.  
  300.   tft.setCursor(5, 65);
  301.   tft.setTextColor(TFT_YELLOW);
  302.   tft.print("UV Index:");
  303.   //*******************************************************************************
  304.  
  305.   //*******************************************************************************
  306.   // nach Systemstart Werte erstmalig holen und auf TFT & OLED Display anzeigen
  307.   getValues();
  308.   printTFT();
  309.   printOLED();
  310.   //*******************************************************************************
  311. }
  312.  
  313. //*******************************************************************************
  314. // WIFI Funktion um die Werte ins Netz zu stellen
  315. void loopwifi()
  316. {
  317.   {
  318.     WiFiClient client = server.available(); // Listen for incoming clients
  319.     if (client)
  320.     {                                // If a new client connects,
  321.       Serial.println("New Client."); // print a message out in the serial port
  322.       String currentLine = "";
  323.       // make a String to hold incoming data from the client
  324.       while (client.connected())
  325.       { // loop while the client's connected
  326.         if (client.available())
  327.         {                         // if there's bytes to read from the client,
  328.           char c = client.read(); // read a byte, then
  329.           Serial.write(c);        // print it out the serial monitor
  330.           header += c;
  331.           if (c == '\n')
  332.           { // if the byte is a newline character
  333.             // if the current line is blank, you got two newline characters in a row.
  334.             // that's the end of the client HTTP request, so send a response:
  335.             if (currentLine.length() == 0)
  336.             {
  337.               // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  338.               // and a content-type so the client knows what's coming, then a blank line:
  339.               client.println(F("HTTP/1.1 200 OK"));
  340.               client.println(F("Content-type:text/html"));
  341.               client.print("Connection: close\r\n\r\n");
  342.               //client.println(F("Connection: close"));
  343.               client.println();
  344.  
  345.               // Display the HTML web page
  346.               client.println(F("<!DOCTYPE html><html>"));
  347.               client.println(F("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"));
  348.               client.println(F("<link rel=\"icon\" href=\"data:,\">"));
  349.               // CSS to style the table
  350.               client.println(F("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}"));
  351.               client.println(F("table { border-collapse: collapse; width:auto; margin-left:auto; margin-right:auto; }"));
  352.               client.println(F("th { padding: 12px; background-color: blue; color: white; }"));
  353.               client.println(F("tr { border: 1px solid #ddd; padding: 12px; }"));
  354.               client.println(F("tr:hover { background-color: #bcbcbc; }"));
  355.               client.println(F("td { border: none; padding: 12px; }"));
  356.               client.println(F(".sensor { color:black; font-weight: bold; background-color: white; padding: 1px; }"));
  357.  
  358.               // Web Page Heading
  359.               client.println(F("</style></head><body><h1>WETTERSTATION</h1>"));
  360.  
  361.               client.println(F("<table><tr><th>MESSUNG</th><th>WERT</th></tr>"));
  362.  
  363.               client.println(F("<tr><td>Grad</td><td><span class=\"sensor\">"));
  364.               client.println(temperatur, 1);
  365.               client.println(F(" *C</span></td></tr>"));
  366.  
  367.               client.println(F("<tr><td>Luftruck</td><td><span class=\"sensor\">"));
  368.               client.println(druck, 0);
  369.               client.println(F(" hPa</span></td></tr>"));
  370.  
  371.               client.println(F("<tr><td>Luftfeuchte</td><td><span class=\"sensor\">"));
  372.               client.println(feuchtigkeit, 0);
  373.               client.println(F(" %</span></td></tr>"));
  374.  
  375.               client.println(F("<tr><td>UV Staerke</td><td><span class=\"sensor\">"));
  376.               client.println(uvIntensity, 1);
  377.               client.println(F(" mW/cm^2</span></td></tr>"));
  378.  
  379.               client.println(F("<tr><td>Uhrzeit</td><td><span class=\"sensor\">"));
  380.               client.println(uhrzeit);
  381.  
  382.               client.println(F("</body></html>"));
  383.  
  384.               // The HTTP response ends with another blank line
  385.               client.println();
  386.               Serial.println(F("Daten ausgelesen"));
  387.               header = "";
  388.               WiFi.disconnect();
  389.               Serial.println("Client disconnected.");
  390.               Serial.println("");
  391.               wifiConnect();
  392.               // Break out of the while loop
  393.               break;
  394.             }
  395.             else
  396.             { // if you got a newline, then clear currentLine
  397.               currentLine = "";
  398.             }
  399.           }
  400.           else if (c != '\r')
  401.           {                   // if you got anything else but a carriage return character,
  402.             currentLine += c; // add it to the end of the currentLine
  403.           }
  404.         }
  405.       }
  406.       delay(100);
  407.       // Clear the header variable
  408.       header = "";
  409.       // Close the connection
  410.       client.stop();
  411.       WiFi.disconnect();
  412.       Serial.println("Client disconnected.");
  413.       Serial.println("");
  414.       wifiConnect();
  415.     }
  416.   }
  417. }
  418. //*******************************************************************************
  419.  
  420. void loop()
  421. {
  422.   ablauf.execute(); // Tasksheduler ausführen
  423.   loopwifi();       // Wifi funktion ist ausgelagert kann bei bedarf auskommentiert werden
  424. }
Add Comment
Please, Sign In to add comment