Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*******************************************************************************
- // Bibliotheken integrieren
- #include <NTPClient.h>
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <Adafruit_BME280.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <SPI.h>
- #include <TFT_eSPI.h>
- #include <TaskScheduler.h>
- //*******************************************************************************
- //*******************************************************************************
- // Constanten definieren
- #define UVsensorIn A0
- #define SCREEN_WIDTH 128
- #define SCREEN_HEIGHT 64
- #define cs 10
- #define dc 9
- #define rst 8
- //*******************************************************************************
- //*******************************************************************************
- // Objekte generieren
- Adafruit_BME280 bme;
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire);
- TFT_eSPI tft = TFT_eSPI();
- WiFiUDP ntpUDP;
- WiFiServer server(80);
- WiFiClient client = server.available();
- NTPClient timeClient(ntpUDP, "ptbtime1.ptb.de", 3600); // 3600
- Scheduler ablauf;
- //*******************************************************************************
- //*******************************************************************************
- // Globale Variablen
- const char *ssid = "SSID";
- const char *password = "WiFi Sifresi";
- float temperatur, old_temperatur, druck, old_druck, feuchtigkeit, old_feuchtigkeit, outputVoltage, uvIntensity, old_uvIntensity;
- int uvLevel, uhr_zeit;
- const float inMin = 1.10, inMax = 3.17, outMin = 0.0, outMax = 15.0;
- byte sayac = 1;
- String old_time, uhrzeit, header;
- //*******************************************************************************
- //*******************************************************************************
- // Text an Position x,y mit der Schriftgröße Sze ausgeben
- void printOled(byte x, byte y, byte Sze, String txt)
- {
- display.setTextSize(Sze);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(x, y);
- display.println(txt);
- }
- //*******************************************************************************
- //*******************************************************************************
- // mapf funktion als float um Analog Input werte als Fließkommazahl auszuwerten
- float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
- {
- return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
- }
- //*******************************************************************************
- //*******************************************************************************
- // UV Wert wird vom Sensor x mal ausgelesenlesen
- int averageAnalogRead(int pinToRead, byte numberOfReadings)
- {
- unsigned int runningValue = 0;
- for (int x = 0; x < numberOfReadings; x++)
- {
- runningValue += analogRead(pinToRead);
- }
- return (runningValue / numberOfReadings);
- }
- //*******************************************************************************
- //*******************************************************************************
- // Werte von den Sensoren einlesen
- void getValues()
- {
- old_temperatur = temperatur;
- old_druck = druck;
- old_feuchtigkeit = feuchtigkeit;
- old_uvIntensity = uvIntensity;
- temperatur = bme.readTemperature() - 1;
- druck = int(bme.readPressure() / 100.0F) - 1;
- feuchtigkeit = bme.readHumidity() + 4.43;
- uvLevel = averageAnalogRead(UVsensorIn, 8); // UV Sensor 8 mal einlesen und Durchschnitt ermitteln
- outputVoltage = 3.27 * uvLevel / 1023; // eingelesenen Wert in Spannung umwandeln
- // eingelesene Spannung mit Werten aus dem Datenblatt in UV Wert umwandeln
- uvIntensity = constrain(mapfloat(outputVoltage, inMin, inMax, outMin, outMax), 0, 15);
- Serial.println();
- Serial.println("Werte aktualisiert");
- }
- //*******************************************************************************
- //*******************************************************************************
- // Zeit über WLAN abrufen und 1 Addieren wegen Sommerzeit
- void get_time()
- {
- uhr_zeit = (timeClient.getFormattedTime().substring(0, 2).toInt() + 1);
- if (uhr_zeit > 23)
- uhr_zeit = 0;
- // führende null hinzufügen wenn Uhrzeit < 10
- uhrzeit = String(uhr_zeit);
- if (uhr_zeit < 10)
- uhrzeit = "0" + uhrzeit;
- // Uhrzeit mit den Minuten & Sekunden zusammensetzen
- uhrzeit = uhrzeit + timeClient.getFormattedTime().substring(2);
- }
- //*******************************************************************************
- //*******************************************************************************
- // Werte auf dem OLED Display abwechselnd ausgeben
- void printOLED()
- {
- Serial.println();
- display.clearDisplay();
- if (sayac == 1)
- {
- Serial.println("Anzeige auf OLED Display >> Temperatur = " + String(temperatur) + " °C");
- printOled(24, 0, 3, String(temperatur, 1));
- printOled(58, 32, 3, "C");
- }
- if (sayac == 2)
- {
- Serial.println("Anzeige auf OLED Display >> Druck = " + String(druck) + " hPa");
- printOled(40, 0, 3, String(druck, 0));
- printOled(40, 32, 3, "hPa");
- }
- if (sayac == 3)
- {
- Serial.println("Anzeige auf OLED Display >> Luftfeuchtigkeit = " + String(feuchtigkeit) + " %");
- printOled(50, 0, 3, String(feuchtigkeit, 0));
- printOled(58, 32, 3, "%");
- }
- if (sayac == 4)
- {
- //Serial.println("Analaog Read A0: " + String(uvLevel));
- Serial.println("Anzeige auf OLED Display >> UV Intensität: " + String(uvIntensity) + " mW/cm^2");
- printOled(35, 0, 3, String(uvIntensity, 1));
- printOled(12, 32, 2, " mW/cm^2");
- }
- if (sayac == 5)
- {
- Serial.println("Anzeige auf OLED Display >> Uhrzeit: " + uhrzeit);
- printOled(40, 0, 2, "ZEIT");
- printOled(15, 40, 2, uhrzeit);
- }
- display.display();
- }
- //*******************************************************************************
- //*******************************************************************************
- // Werte auf dem TFT Display ausgeben
- void printTFT()
- {
- tft.setCursor(85, 5);
- tft.setTextColor(TFT_LIGHTGREY);
- tft.print(String(old_temperatur, 1));
- tft.setCursor(85, 5);
- tft.setTextColor(TFT_RED);
- tft.print(String(temperatur, 1) + " C");
- tft.setCursor(97, 25);
- tft.setTextColor(TFT_LIGHTGREY);
- tft.print(String(old_druck, 0));
- tft.setCursor(97, 25);
- tft.setTextColor(TFT_BLACK);
- tft.print(String(druck, 0));
- tft.setTextSize(1);
- tft.setCursor(135, 30);
- tft.print(" hPa");
- tft.setTextSize(2);
- tft.setCursor(107, 45);
- tft.setTextColor(TFT_LIGHTGREY);
- tft.print(String(old_feuchtigkeit, 0));
- tft.setCursor(107, 45);
- tft.setTextColor(TFT_BLUE);
- tft.print(String(feuchtigkeit, 0) + " %");
- tft.setCursor(110, 65);
- tft.setTextColor(TFT_LIGHTGREY);
- tft.print(String(old_uvIntensity, 1));
- tft.setCursor(110, 65);
- tft.setTextColor(TFT_YELLOW);
- tft.print(String(uvIntensity, 1));
- Serial.println("Aktuelle Werte auf dem TFT Display ausgegeben");
- }
- //*******************************************************************************
- //*******************************************************************************
- // jede sekunde über WLAN Zeit abrufen und auf TFT Display anzeigen
- void t1Callback()
- {
- timeClient.update();
- get_time();
- tft.setCursor(32, 105);
- tft.setTextColor(TFT_LIGHTGREY);
- tft.print(old_time);
- tft.setCursor(32, 105);
- tft.setTextColor(TFT_DARKGREEN);
- tft.print(uhrzeit);
- old_time = uhrzeit;
- client.stopAll();
- }
- Task t1(1000, TASK_FOREVER, &t1Callback);
- //*******************************************************************************
- //*******************************************************************************
- // alle 3 sekunden Werte abwechselnd auf OLED Display anzeigen
- void t2Callback()
- {
- sayac++;
- if (sayac >= 6)
- sayac = 1;
- printOLED();
- }
- Task t2(3000, TASK_FOREVER, &t2Callback);
- //*******************************************************************************
- //*******************************************************************************
- // alle 10 Sekunden Werte einlesen und auf TFT Display anzeigen
- void t3Callback()
- {
- getValues();
- printTFT();
- }
- Task t3(10000, TASK_FOREVER, &t3Callback);
- //*******************************************************************************
- //*******************************************************************************
- void wifiConnect()
- {
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- // Print local IP address and start web server
- Serial.println("");
- Serial.println("WiFi connected.");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- server.begin();
- }
- //*******************************************************************************
- void setup()
- {
- //*******************************************************************************
- Serial.begin(115200);
- //*******************************************************************************
- timeClient.begin();
- //*******************************************************************************
- bme.begin();
- //***************************************************
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- //*******************************************************************************
- // TFT Display initialisieren
- tft.init();
- tft.setRotation(1);
- tft.fillScreen(TFT_LIGHTGREY);
- tft.setTextSize(2);
- //*******************************************************************************
- // Tasks starten
- ablauf.init();
- ablauf.addTask(t1);
- t1.enable();
- ablauf.addTask(t2);
- t2.enable();
- ablauf.addTask(t3);
- t3.enable();
- //*******************************************************************************
- // Connect to Wi-Fi network with SSID and password
- wifiConnect();
- //*******************************************************************************
- //*******************************************************************************
- // Statischen Text auf TFT Display anzeigen
- tft.setCursor(5, 5);
- tft.setTextColor(TFT_RED);
- tft.print("TEMP:");
- tft.setCursor(5, 25);
- tft.setTextColor(TFT_BLACK);
- tft.print("DRUCK:");
- tft.setCursor(5, 45);
- tft.setTextColor(TFT_BLUE);
- tft.print("FEUCHTE:");
- tft.setCursor(5, 65);
- tft.setTextColor(TFT_YELLOW);
- tft.print("UV Index:");
- //*******************************************************************************
- //*******************************************************************************
- // nach Systemstart Werte erstmalig holen und auf TFT & OLED Display anzeigen
- getValues();
- printTFT();
- printOLED();
- //*******************************************************************************
- }
- //*******************************************************************************
- // WIFI Funktion um die Werte ins Netz zu stellen
- void loopwifi()
- {
- {
- WiFiClient client = server.available(); // Listen for incoming clients
- if (client)
- { // If a new client connects,
- Serial.println("New Client."); // print a message out in the serial port
- String currentLine = "";
- // make a String to hold incoming data from the client
- while (client.connected())
- { // loop while the client's connected
- if (client.available())
- { // if there's bytes to read from the client,
- char c = client.read(); // read a byte, then
- Serial.write(c); // print it out the serial monitor
- header += c;
- if (c == '\n')
- { // if the byte is a newline character
- // if the current line is blank, you got two newline characters in a row.
- // that's the end of the client HTTP request, so send a response:
- if (currentLine.length() == 0)
- {
- // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
- // and a content-type so the client knows what's coming, then a blank line:
- client.println(F("HTTP/1.1 200 OK"));
- client.println(F("Content-type:text/html"));
- client.print("Connection: close\r\n\r\n");
- //client.println(F("Connection: close"));
- client.println();
- // Display the HTML web page
- client.println(F("<!DOCTYPE html><html>"));
- client.println(F("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"));
- client.println(F("<link rel=\"icon\" href=\"data:,\">"));
- // CSS to style the table
- client.println(F("<style>body { text-align: center; font-family: \"Trebuchet MS\", Arial;}"));
- client.println(F("table { border-collapse: collapse; width:auto; margin-left:auto; margin-right:auto; }"));
- client.println(F("th { padding: 12px; background-color: blue; color: white; }"));
- client.println(F("tr { border: 1px solid #ddd; padding: 12px; }"));
- client.println(F("tr:hover { background-color: #bcbcbc; }"));
- client.println(F("td { border: none; padding: 12px; }"));
- client.println(F(".sensor { color:black; font-weight: bold; background-color: white; padding: 1px; }"));
- // Web Page Heading
- client.println(F("</style></head><body><h1>WETTERSTATION</h1>"));
- client.println(F("<table><tr><th>MESSUNG</th><th>WERT</th></tr>"));
- client.println(F("<tr><td>Grad</td><td><span class=\"sensor\">"));
- client.println(temperatur, 1);
- client.println(F(" *C</span></td></tr>"));
- client.println(F("<tr><td>Luftruck</td><td><span class=\"sensor\">"));
- client.println(druck, 0);
- client.println(F(" hPa</span></td></tr>"));
- client.println(F("<tr><td>Luftfeuchte</td><td><span class=\"sensor\">"));
- client.println(feuchtigkeit, 0);
- client.println(F(" %</span></td></tr>"));
- client.println(F("<tr><td>UV Staerke</td><td><span class=\"sensor\">"));
- client.println(uvIntensity, 1);
- client.println(F(" mW/cm^2</span></td></tr>"));
- client.println(F("<tr><td>Uhrzeit</td><td><span class=\"sensor\">"));
- client.println(uhrzeit);
- client.println(F("</body></html>"));
- // The HTTP response ends with another blank line
- client.println();
- Serial.println(F("Daten ausgelesen"));
- header = "";
- WiFi.disconnect();
- Serial.println("Client disconnected.");
- Serial.println("");
- wifiConnect();
- // Break out of the while loop
- break;
- }
- else
- { // if you got a newline, then clear currentLine
- currentLine = "";
- }
- }
- else if (c != '\r')
- { // if you got anything else but a carriage return character,
- currentLine += c; // add it to the end of the currentLine
- }
- }
- }
- delay(100);
- // Clear the header variable
- header = "";
- // Close the connection
- client.stop();
- WiFi.disconnect();
- Serial.println("Client disconnected.");
- Serial.println("");
- wifiConnect();
- }
- }
- }
- //*******************************************************************************
- void loop()
- {
- ablauf.execute(); // Tasksheduler ausführen
- loopwifi(); // Wifi funktion ist ausgelagert kann bei bedarf auskommentiert werden
- }
Add Comment
Please, Sign In to add comment