Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.13 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <WiFiNINA.h>
  3. #include "DHT.h"
  4. #include "arduino_secrets.h"
  5.  
  6. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  7. //Webserver
  8.  
  9. ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
  10. char ssid[] = SECRET_SSID;        // your network SSID (name)
  11. char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
  12. int keyIndex = 0;                 // your network key Index number (needed only for WEP)
  13. WiFiClient client;  // Initialize the Wifi client library
  14. int status = WL_IDLE_STATUS;
  15. WiFiServer server(80);
  16. char meinserver[] = "lenk-haibach.de";  // server address:
  17.  
  18. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. //DHT22
  20.  
  21. int led =  6;
  22. #define DHTPIN 8
  23. #define DHTTYPE DHT22
  24. DHT dht(DHTPIN, DHTTYPE);
  25.  
  26. float hkf = 0;
  27. float hkt = 0;
  28.  
  29. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  30. //DCF77
  31. int signalPin = 5;
  32.  
  33. char dcf77Signal;
  34. char buffer;
  35.  
  36. unsigned long nowTime, lastTime;
  37. int diffTime;
  38.  
  39. byte impulsLevel; // Impulsdauer 100ms => logisch 0, 200 ms => logisch 1
  40. byte Impuls[59]; // eingehende Impulse
  41. byte impulsWert[8] = {1, 2, 4, 8, 10, 20, 40, 80}; //Impulswertigkeit für Level logisch 1
  42. byte impulsZaehler = 0; //Anzahl eingehende Impulse
  43.  
  44. byte dcf77Stunde = 0;
  45. byte dcf77Minute = 0;
  46.  
  47. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  48.  
  49. unsigned long lastConnectionTime = 0;            // last time you connected to the server, in milliseconds
  50. const unsigned long postingInterval = 600L * 1000L; // delay between updates, in milliseconds
  51.  
  52. //////////////////////////////////////////////////////////////////////////////////////////////////////////
  53.  
  54. void setup() {
  55.   Serial.begin(9600);      // initialize serial communication
  56.   pinMode(led, OUTPUT);      // set the LED pin mode
  57.   pinMode(signalPin, INPUT);
  58.   dht.begin();
  59.  
  60.   // check for the WiFi module:
  61.   if (WiFi.status() == WL_NO_MODULE) {
  62.     Serial.println("Kommunikation mit WiFi-Modul fehlgeschlagen!");
  63.     // don't continue
  64.     while (true);
  65.   }
  66.  
  67.   // attempt to connect to Wifi network:
  68.   while (status != WL_CONNECTED) {
  69.     Serial.print("Versuche, eine Verbindung zum Netzwerk mit diesem Namen herzustellen:");
  70.     Serial.println(ssid);                   // print the network name (SSID);
  71.  
  72.     // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  73.     status = WiFi.begin(ssid, pass);
  74.     // wait 10 seconds for connection:
  75.     delay(10000);
  76.   }
  77.   server.begin();                           // start the web server on port 80
  78.   Serial.println("Server gestartet");
  79. }
  80.  
  81.  
  82. void loop() {
  83.   dcf77Signal = digitalRead(signalPin);
  84.   if (dcf77Signal != buffer)
  85.   {
  86.     nowTime = millis();
  87.     delay(5); //Wartezeit bis eingeschwungener Zustand
  88.     diffTime = nowTime - lastTime;
  89.     lastTime = nowTime;
  90.     if (diffTime < 150) impulsLevel = 0;
  91.     else if (diffTime < 250) impulsLevel = 1;
  92.     else if (diffTime > 1000)
  93.     {
  94.       if (impulsZaehler == 59 || impulsZaehler == 60) dekodiereZeit();
  95.       impulsZaehler = 0;
  96.     }
  97.     if (dcf77Signal == 0) // Abfrage auf "1", wenn das invertierte Signal verwendet wird
  98.     {
  99.       Impuls[impulsZaehler] = impulsLevel;
  100.       impulsZaehler++;
  101.     }
  102.    buffer = dcf77Signal;
  103.   neuerserver();
  104.   if (millis() - lastConnectionTime > postingInterval) {
  105.    DHTSensor();
  106.    neuerclient();
  107.   }
  108. }
  109. }
  110.  
  111.  
  112. void neuerserver() {
  113.   WiFiClient client = server.available();   // listen for incoming clients
  114.  
  115.   if (client) {                             // if you get a client,
  116.     Serial.println("Neuer client");           // print a message out the serial port
  117.     String currentLine = "";                // make a String to hold incoming data from the client
  118.     while (client.connected()) {            // loop while the client's connected
  119.       if (client.available()) {             // if there's bytes to read from the client,
  120.         char c = client.read();             // read a byte, then
  121.         Serial.write(c);                    // print it out the serial monitor
  122.         if (c == '\n') {                    // if the byte is a newline character
  123.  
  124.           // if the current line is blank, you got two newline characters in a row.
  125.           // that's the end of the client HTTP request, so send a response:
  126.           if (currentLine.length() == 0) {
  127.             // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  128.             // and a content-type so the client knows what's coming, then a blank line:
  129.             client.println("HTTP/1.1 200 OK");
  130.             client.println("Content-type:text/html");
  131.             client.println();
  132.  
  133.             // the content of the HTTP response follows the header:
  134.             client.print("Click <a href=\"/H\">here</a> turn the LED on pin 9 on<br>");
  135.             client.print("Click <a href=\"/L\">here</a> turn the LED on pin 9 off<br>");
  136.  
  137.             // The HTTP response ends with another blank line:
  138.             client.println();
  139.             // break out of the while loop:
  140.             break;
  141.           } else {    // if you got a newline, then clear currentLine:
  142.             currentLine = "";
  143.           }
  144.         } else if (c != '\r') {  // if you got anything else but a carriage return character,
  145.           currentLine += c;      // add it to the end of the currentLine
  146.         }
  147.  
  148.         // Check to see if the client request was "GET /H" or "GET /L":
  149.         if (currentLine.endsWith("GET /H")) {
  150.           digitalWrite(led, HIGH);               // GET /H turns the LED on
  151.         }
  152.         if (currentLine.endsWith("GET /L")) {
  153.           digitalWrite(led, LOW);                // GET /L turns the LED off
  154.         }
  155.       }
  156.     }
  157.     // close the connection:
  158.     client.stop();
  159.     Serial.println("client disonnected");
  160.   }
  161. }
  162.  
  163. void neuerclient() {
  164.   while (client.available()) {
  165.     char c = client.read();
  166.     Serial.write(c);
  167.   }
  168.     // close any connection before send a new request.
  169.   // This will free the socket on the Nina module
  170.   client.stop();
  171.  
  172.   // if there's a successful connection:
  173.   if (client.connect(meinserver, 80)) {
  174.     Serial.println("verbinde...");
  175.     // send the HTTP PUT request:
  176.     client.print("GET /getandsave.php?hkt=");
  177.     client.print(hkt);
  178.     client.print("&hkf=");
  179.     client.print(hkf);
  180.     client.println(" HTTP/1.1");
  181.     client.println("Host:www.lenk-haibach.de");
  182.     client.println("User-Agent: ArduinoWiFi/1.1");
  183.     client.println("Connection: close");
  184.     client.println();
  185.  
  186.     // note the time that the connection was made:
  187.     lastConnectionTime = millis();
  188.   } else {
  189.     // if you couldn't make a connection:
  190.     Serial.println("Verbindung fehlgeschlagen");
  191.   }
  192. }
  193.  
  194. void DHTSensor() {
  195.   hkf = dht.readHumidity();
  196.   hkt = dht.readTemperature();
  197.   delay(2000);
  198.   Serial.println(F("DHT Test!"));
  199.   if (isnan(hkf) || isnan(hkt)) {
  200.     Serial.println(F("Fehler beim Lesen des DHT-Sensors!"));
  201.     return;
  202.   }
  203.   Serial.print(F("Luftfeuchte: "));
  204.   Serial.print(hkf);
  205.   Serial.print(F("%  Temperatur: "));
  206.   Serial.print(hkt);
  207.   Serial.println(F("°C "));
  208. }
  209.  
  210. void dekodiereZeit(){
  211.   bool dateTimeOk = true;
  212.   byte paritaetStunde = 0;
  213.   byte paritaetMinute = 0;
  214.  
  215.   dcf77Stunde = 0;
  216.   dcf77Minute = 0;
  217.  
  218.   //Ueberpruefen der Stundenparitaet
  219.   for (byte i = 29; i < 35; i++) paritaetStunde ^= Impuls[i];
  220.  
  221.   if (Impuls[35] != paritaetStunde) dateTimeOk = false;
  222.  
  223.   //Ueberpruefen der Minutenparitaet
  224.   for (byte i = 21; i < 28; i++) paritaetMinute ^= Impuls[i];
  225.   if (Impuls[28] != paritaetMinute) dateTimeOk = false;
  226.  
  227.   //Zuweisen der Impulswertigkeit
  228.   for (byte i = 29; i < 35; i++) (Impuls[i] == 1 ? dcf77Stunde += impulsWert[i - 29] : 0);
  229.   for (byte i = 21; i < 28; i++) (Impuls[i] == 1 ? dcf77Minute += impulsWert[i - 21] : 0);
  230.  
  231.   //Ueberpruefen des Wertebereiches
  232.   if (dcf77Stunde > 23 || dcf77Minute > 59) dateTimeOk = false;
  233.  
  234.   if (dateTimeOk)
  235.   {
  236.     Serial.print(dcf77Stunde);
  237.     Serial.print(" : ");
  238.     Serial.println(dcf77Minute);
  239.   }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement