Advertisement
NittyGritty

JahresSchaltUhr.ino

Mar 25th, 2018
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.33 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <Wire.h>
  3. #include <Time.h>
  4. #include <Timezone.h>  
  5. #include <string.h>
  6. #include <DS3232RTC.h>  // A  DS3231/DS3232 library
  7. #include <TimeLord.h>
  8.  
  9. // west longitude and north lattitude - approximate -
  10. //      I'm not giving you my address in an online code example anyway... :-)
  11. // 48.8143634,8.9808786
  12. double Lw = 8.9808786;
  13. double Ln = 48.8143634;
  14.  
  15. // 51.5023276,13.2808041
  16. // double Ln = 51.5023276;
  17. // double Lw = 13.2808041;
  18.  
  19. // Adjust this block of code if you have another LCD (e.g. direct connected)
  20. #include <LiquidCrystal_I2C.h>
  21.  
  22. #define I2C_ADDR    0x27
  23. #define BACKLIGHT_PIN     3
  24. #define En_pin  2
  25. #define Rw_pin  1
  26. #define Rs_pin  0
  27. #define D4_pin  4
  28. #define D5_pin  5
  29. #define D6_pin  6
  30. #define D7_pin  7
  31.  
  32. LiquidCrystal_I2C    lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin, BACKLIGHT_PIN, POSITIVE);
  33. // =============================================
  34.  
  35.  
  36. // it's a 16x2 LCD so...
  37. int screenWidth = 24;
  38. int screenHeight = 2;
  39.  
  40.  
  41. int countMovement = 0;
  42. int stringStart = 0;
  43. int stringStop = 0;
  44. int scrollCursor = 0;
  45.  
  46. char cb[40];
  47. char* weekdays[] = { "So", "Mo", "Di", "Mi", "Do", "Fr", "Sa" };
  48.  
  49. //Central European Time (Frankfurt, Paris)
  50. TimeChangeRule CEST = {"CEST", Last, Sun, Mar, 2, 120};     //Central European Summer Time
  51. TimeChangeRule  CET = {"CET ", Last, Sun, Oct, 3, 60};      //Central European Standard Time
  52. Timezone CE(CEST, CET);
  53.  
  54. TimeChangeRule *tcr;        //pointer to the time change rule, use to get TZ abbrev
  55. time_t utc, local;
  56.  
  57. #define BUFF_MAX 128
  58.  
  59. // uint8_t time[8];
  60. // char recv[BUFF_MAX];
  61. unsigned int recv_size = 0;
  62. unsigned long prev, interval = 1000;
  63.  
  64. int osec = -1;
  65. int I = 50;
  66.  
  67. // Tor stuff
  68. // Tor = 0 - auf
  69. // Tor = 1 - zu
  70. // Stunden           0    1    2    3    4    5    6    7    8    9
  71. char* Torzeit [] = {"1", "1", "1", "1", "1", "1", "0", "0", "0", "0",
  72.                     "0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
  73.                     "1", "1", "1", "1"
  74.                     };
  75. //Wochentage        So,  Mo,  Di,  Mi,  Do,  Fr,  Sa
  76. char* TorTage [] = {"1", "0", "0", "0", "0", "0", "1" };
  77.  
  78. // Tor = Hour(t) > Tor TorZuZeit
  79. boolean Tor = 0;
  80. int Relay = 12;
  81.  
  82. const char compile_date[] =  __DATE__ "-" __TIME__;
  83. const char compile_file[] =  __FILE__ ;
  84.  
  85.  
  86. // ------------------------------------------------------------------------------
  87. void setup()
  88. {
  89.  
  90.   pinMode(Relay, OUTPUT);
  91.  
  92.   Serial.begin(9600);
  93.  
  94.   lcd.begin(screenWidth, screenHeight);             // LCD Hintergrundbeleuchtung aktivieren
  95.   lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  96.   lcd.clear();
  97.   lcd.setCursor(0, 0);
  98.  
  99.   // ------- Quick 3 blinks of backlight  -------------
  100.   for(int i = 0; i< 3; i++)
  101.   {
  102.     lcd.backlight();
  103.     delay(250);
  104.     digitalWrite(Relay, LOW);
  105.     lcd.noBacklight();
  106.     delay(250);
  107.     digitalWrite(Relay, HIGH);
  108.   }
  109.   lcd.backlight(); // finish with backlight on  
  110.  
  111.   Serial.println(compile_date);
  112.   roop(0, compile_date, 100);
  113.  
  114.   Serial.println(compile_file);
  115.   roop(1, compile_file, 100);
  116.  
  117.   delay(1000);
  118.  
  119.  
  120.   setSyncProvider(RTC.get);   // the function to get the time from the RTC
  121.  
  122.   if(timeStatus()!= timeSet)
  123.   {
  124.     lcd.println(F("RTC sync Error"));
  125.   }  
  126.   else
  127.   {
  128.     lcd.print(F("RTC sync OK"));
  129.   }
  130.  
  131.   // memset(recv, 0, BUFF_MAX);
  132.  
  133.  
  134.   delay(2000);
  135.   lcd.clear();
  136.   lcd.setCursor(0, 0);
  137.  
  138.   setTime(hour(),minute(),second(),day(),month(),(year()-2000) );
  139.  
  140.   digitalClockDisplay();
  141.   CheckTor();
  142. }
  143.  
  144. void loop()
  145. {
  146.  
  147. if (second() != osec)
  148.  {
  149.  
  150.     switch (second())
  151.     {
  152.     case 0:
  153.  
  154.       break;
  155.  
  156.     case 1:
  157.       CheckTor();
  158.       break;
  159.  
  160.     case 15:
  161.  
  162.       break;
  163.  
  164.     case 30:
  165.  
  166.       break;
  167.  
  168.     case 45:
  169.  
  170.       break;
  171.     }
  172.     osec = second();
  173.  
  174.     digitalClockDisplay();
  175.     CheckClockSet();
  176.   }
  177. }
  178. // ---------------------------------------------------
  179. // Tor = 0 - auf
  180. // Tor = 1 - zu
  181. // Tor = Hour(t) > Tor TorZuZeit
  182. void CheckTor()
  183. {
  184.   local = CE.toLocal(utc, &tcr);
  185.   boolean TestTor1 = CheckTorZeit(local, tcr -> abbrev);
  186.   sprintf(cb, "Lt. TorZeit soll Tor %s sein", (TestTor1)?"zu":"auf" ) ;
  187.   Serial.println(cb);
  188.  
  189.   boolean TestTor2 = CheckTorTage(local, tcr -> abbrev);
  190.   sprintf(cb, "Lt. TorTag soll Tor %s sein", (TestTor2)?"zu":"auf" ) ;
  191.   Serial.println(cb);
  192.  
  193.   local = CE.toLocal(utc, &tcr);
  194.   boolean TestFeiertag =feiertag(local, tcr -> abbrev);   // Ermittelt ob heute ein Feiertag ist
  195.   sprintf(cb, "Heute ist %s Feiertag", (TestFeiertag)?"ein":"kein" ) ;
  196.   Serial.println(cb);
  197.  
  198.   Tor = TestTor1 || TestTor2 || TestFeiertag;
  199.   sprintf(cb, "Tor ist %s", (Tor)?"zu":"auf" ) ;
  200.   Serial.println(cb);
  201.  
  202.   digitalWrite(Relay, (Tor)?HIGH:LOW);
  203.  
  204.   PrintSunRise();
  205. }
  206.  
  207. boolean CheckTorZeit(time_t t, char *tz)
  208. {
  209.   sprintf(cb, "Stunde %02d, Tor %s",hour(t), Torzeit[hour(t)] ) ;
  210.   Serial.println(cb);
  211.   if ( Torzeit[hour(t)] == "1" )
  212.   {
  213.     return true;
  214.   } else {
  215.     return false;
  216.   }  
  217. }  
  218.  
  219. boolean CheckTorTage(time_t t, char *tz)
  220. {
  221.   sprintf(cb, "Wochentag %01d, Tor %s",weekday(t), TorTage[weekday(t) - 1] ) ;
  222.   Serial.println(cb);
  223.   if ( TorTage[weekday(t) - 1] == "1" )
  224.   {
  225.     return true;
  226.   } else {
  227.     return false;
  228.   }  
  229. }  
  230. /*
  231.  lcd.clear();
  232.  lcd.setCursor(0, 1);
  233.  lcd.print("Scroll-row0");
  234.  scroll(0, "The quick brown fox jumps over the lazy dog",200);
  235.  */
  236.  
  237.  
  238. // ==========================================
  239. void scroll(int row, String line, int speed) {
  240.   countMovement = 0;
  241.   stringStart = 0;
  242.   stringStop = 0;
  243.   scrollCursor = 0;
  244.  
  245.   // Serial.print(countMovement); Serial.print("-"); Serial.println(line.length() + 1);
  246.   while ( ( countMovement ) < (line.length() + 2 )  )
  247.   {
  248.     lcd.setCursor(scrollCursor, row);
  249.     // Serial.print("Cursor: "); Serial.print(scrollCursor); Serial.print(" "); Serial.println(row);
  250.     lcd.print(line.substring(stringStart, stringStop));
  251.     // Serial.println( line.substring(stringStart, stringStop) );
  252.     delay(speed);
  253.  
  254.     if (stringStart == 0 && scrollCursor > 0) {
  255.       // Serial.println(scrollCursor);
  256.       scrollCursor--;
  257.       stringStop++;
  258.     }
  259.     else if (stringStart == stringStop) {
  260.       stringStart = stringStop = 0;
  261.       scrollCursor = screenWidth;
  262.     }
  263.     else if (stringStop == line.length() && scrollCursor == 0) {
  264.       stringStart++;
  265.     }
  266.     else {
  267.       stringStart++;
  268.       stringStop++;
  269.     }
  270.     countMovement++;
  271.   }
  272. }
  273.  
  274. // =================================================================
  275. void digitalClockDisplay() {
  276.   utc = now();
  277.   local = CE.toLocal(utc, &tcr);
  278.   printTimeLCD(local, tcr -> abbrev);
  279. }
  280.  
  281. void printTimeLCD(time_t t, char *tz) {
  282.   // lcd.clear();
  283.   lcd.setCursor(0, 0);  
  284.   sprintf(cb, "%s %02d.%02d.%02d %s",  (weekdays[weekday(t) - 1]), day(t), month(t), year(t) - 2000, tz);
  285.   lcd.print(cb);
  286.  
  287.   lcd.setCursor(0, 1);  
  288.   sprintf(cb, "%02d:%02d:%02d Tor %s", hour(t), minute(t), second(t), (Tor)?"zu ":"auf" ) ;
  289.   lcd.print(cb);
  290.  
  291.   utc = now();
  292. //  printTime(utc, "UTC");
  293.   local = CE.toLocal(utc, &tcr);
  294.   printTime(local, tcr -> abbrev);
  295.  
  296. }
  297. // ----------------------------------------------
  298. // Function to print time with time zone
  299. // ----------------------------------------------
  300. void printTime(time_t t, char *tz)
  301. {
  302.   sprintf(cb, "%s, %02d.%02d.%04d %02d:%02d:%02d %s", (weekdays[weekday(t) - 1]), day(t), month(t),year(t), hour(t), minute(t), second(t), tz ) ;
  303.   Serial.println(cb);
  304. }
  305.  
  306. // ------------------------------------------------------------------------------------------------------
  307. boolean feiertag(time_t t, char *tz)
  308. {
  309.   String testDate;
  310.   int tag;
  311.   int monat=3;
  312.   int ostersonntag;
  313.  
  314.   testDate = String(day(t)) +"."+ String(month(t));
  315.  
  316.   // Zuerst die festen Feiertage
  317.   // Hinweis: 24.12 und 31.12 sind eigentlich keine Feiertage, werden aber hier als solche behandelt
  318.   if (testDate==("1.1"))   { return true; }     // Neujahr
  319.   if (testDate==("1.5"))   { return true; }     // Maifeiertag
  320.   if (testDate==("3.10"))  { return true; }     // Tag d. dt. Einheit
  321.   if (testDate==("24.12")) { return true; }     // Heiligabend (eigentlich kein Feiertag)
  322.   if (testDate==("25.12")) { return true; }     // Erster Weihnachtsfeirtag
  323.   if (testDate==("26.12")) { return true; }     // Zweiter Weihnachsfeiertag
  324.   if (testDate==("31.12")) { return true; }     // Silvester (eigentlich kein Feiertag)
  325.  
  326.   // Nachfolgend einige Feiertage die nicht bundeseinheitlich sind: (ggf. auskommentieren)
  327.   if (testDate==("6.1"))   { return true; }     // Heilige Drei Könige (nur in best. Bundesländern)
  328. //if (testDate==("15.8"))  { return true; }     // Mariae Himmelfahrt (nur im Saarland)
  329. //if (testDate==("31.10")) { return true; }     // Reformationstag (nur in best. Bundesländern)
  330.   if (testDate==("1.11"))  { return true; }     // Allerheiligen (nur in best. Bundesländern)
  331.  
  332.   if (month(t)==3) { tag=day(t); }            // Wenn März, aktuellen Tag ermitteln
  333.   if (month(t)==4) { tag=day(t)+31; }
  334.   if (month(t)==5) { tag=day(t)+31+30; }
  335.   if (month(t)==6) { tag=day(t)+31+30+31; }
  336.  
  337.   ostersonntag = berechne_Ostern();
  338.   if (ostersonntag-2 ==tag) { return true; }    // Karfreitag
  339.   if (ostersonntag   ==tag) { return true; }    // Ostersonntag
  340.   if (ostersonntag+1 ==tag) { return true; }    // Ostermontag
  341.   if (ostersonntag+39==tag) { return true; }    // Christi Himmelfahrt
  342.   if (ostersonntag+49==tag) { return true; }    // Pfingstsonntag
  343.   if (ostersonntag+50==tag) { return true; }    // Pfingstmontag
  344.   if (ostersonntag+60==tag) { return true; }    // Fronleichnam (nicht bundeseinheitlich)
  345.  
  346.   // Buss- und Bettag gibt es nur in einigen Bundeländern, also ggf. auskommentieren
  347.   // if (BussUndBettag()) {return true;}
  348.  
  349.   return false;
  350. }
  351.  
  352. int berechne_Ostern()
  353. {
  354.   // nach der Gauß-Formel
  355.   // Rückgabewert: Datum von Ostern ab 1. März (max=56 für 25. April)
  356.  
  357.   int a; int b; int c; int d; int e; int f; int k; int M; int N; int p; int q;
  358.   int jahr=year();
  359.  
  360.   // Die "magische" Gauss-Formel anwenden:
  361.   a = jahr % 19;
  362.   b = jahr % 4;
  363.   c = jahr % 7;
  364.   k = jahr / 100;
  365.   p = (8*k+13) / 25;
  366.   q = k / 4;
  367.   M =(15 + k - p - q) % 30;
  368.   N = (4 + k-q) % 7;
  369.   d = (19*a + M) % 30;
  370.   e = (2*b + 4*c + 6*d + N) % 7;
  371.   f = 22+d+e;   // Tag auf Maerz bezogen: 32 bedeutet Erster April usw.
  372.  
  373.  
  374.   if (f==57) {
  375.     f=50;
  376.   }   // Wenn der 26. April ermittelt wird (31+26=57), dann muss Ostern am 19.4. sein (19+31=50)
  377.   /// Falls der 25. April ermittelt wird, gilt dies nur wenn d=28 und a>10
  378.   if ((f==56) && (d==28) && (a>10))  {
  379.     f=49;
  380.   }
  381.   return f;
  382.  
  383. }
  384. // -------------------- kein Problem mit dem Datum und der Zeitzone, daher ohne (t) -----------------
  385. boolean BussUndBettag()
  386. {
  387.   // Buss- und Bettag fällt auf den Mittwoch zwischen demm 16. und 22. November
  388.   if ( (month()==11) && (day()>15) && (day ()< 23) )
  389.   {
  390.     if (weekday() == 4) {
  391.       return true;
  392.     }  // Wenn heute Mittwoch ist
  393.   }
  394.  
  395.   return false;
  396.  
  397. }
  398.  
  399. // ---------------------------------------------------
  400. void CheckFeiertag ()
  401. {
  402.   local = CE.toLocal(utc, &tcr);
  403.   boolean TestFeiertag = feiertag(local, tcr -> abbrev);   // Ermittelt ob heute ein Feiertag ist
  404.   sprintf(cb, "Heute ist %s Feiertag\r\n", (TestFeiertag)?"ein":"kein" ) ;
  405.   Serial.print(cb);
  406. }
  407.  
  408. // ------------------------------------------------------------------------
  409. void CheckClockSet() {
  410.   if (Serial.available()) {
  411.     time_t t = processSyncMessage();
  412.     if (t > 0)
  413.     {
  414.       RTC.set(t);   // set the RTC and the system time to the received value
  415.       setTime(t);
  416.  
  417.       Serial.println("New Tiime has been set");
  418.       utc = now();
  419.       printTime(utc, "UTC");
  420.       local = CE.toLocal(utc, &tcr);
  421.       printTime(local, tcr -> abbrev);
  422.     }
  423.   } // if no input, just return
  424. }
  425.  
  426. /*  code to process time sync messages from the serial port   */
  427. #define TIME_MSG_LEN  11   // time sync to PC is HEADER followed by unix time_t as ten ascii digits
  428. #define TIME_HEADER  'T'   // Header tag for serial time sync message
  429.  
  430. time_t processSyncMessage() {
  431.   // return the time if a valid sync message is received on the serial port.
  432.   while (Serial.available() >=  TIME_MSG_LEN ) { // time message consists of a header and ten ascii digits
  433.     char c = Serial.read() ;
  434.     Serial.print(c);
  435.     if ( c == TIME_HEADER ) {
  436.       time_t pctime = 0;
  437.       for (int i = 0; i < TIME_MSG_LEN - 1; i++) {
  438.         c = Serial.read();
  439.         if ( c >= '0' && c <= '9') {
  440.           pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
  441.         }
  442.       }
  443.       return pctime;
  444.     }
  445.   }
  446.   return 0;
  447. }
  448.  
  449. // ==========================================
  450. void roop(int Reihe, String line2, int speed) {
  451.   countMovement = 0;
  452.   stringStart = 0;
  453.   stringStop = 0;
  454.   scrollCursor = 0;
  455.  
  456.   // Serial.print(countMovement); Serial.print("-"); Serial.println(line2.length() + 1);
  457.   while ( ( countMovement ) < (line2.length() + 2 )  )
  458.   {
  459.     lcd.setCursor(scrollCursor, Reihe);
  460.     // Serial.print("Cursor: "); Serial.print(scrollCursor); Serial.print(" "); Serial.println(Reihe);
  461.     lcd.print(line2.substring(stringStart, stringStop));
  462.     // Serial.println( line2.substring(stringStart, stringStop) );
  463.     delay(speed);
  464.  
  465.     if (stringStart == 0 && scrollCursor > 0) {
  466.       // Serial.println(scrollCursor);
  467.       scrollCursor--;
  468.       stringStop++;
  469.     } else if (stringStart == stringStop) {
  470.       stringStart = stringStop = 0;
  471.       scrollCursor = screenWidth;
  472.     } else if (stringStop == line2.length() && scrollCursor == 0) {
  473.       stringStart++;
  474.     } else {
  475.       stringStart++;
  476.       stringStop++;
  477.     }
  478.     countMovement++;
  479.   }
  480. }
  481. // ==============================================================================
  482. void SunRise(time_t t, char *tz)
  483. {
  484.   byte day1[] = { 0, 0, 12, day(t), month(t), year(t) }; // noon
  485.  
  486.   TimeLord tardis;
  487.   tardis.TimeZone(2 * 60);
  488.   tardis.Position(Ln, Lw);
  489.  
  490.   if (tardis.SunRise(day1))
  491.   {
  492.     sprintf(cb, "Sunrise: %02d:", (day1[tl_hour]) );
  493.     Serial.print(cb);
  494.     sprintf(cb, "%02d", (day1[tl_minute] ));
  495.     Serial.println(cb);
  496.  
  497.   }
  498.  
  499.   if (tardis.SunSet(day1))
  500.   {
  501. //    sprintf(cb, "Sunset:  %02d:%02d", (day1[tl_hour], day1[tl_minute] ) ) ;
  502. //     Serial.println(cb);
  503.     sprintf(cb, "Sunset:  %02d:", (day1[tl_hour]) );
  504.     Serial.print(cb);
  505.     sprintf(cb, "%02d", (day1[tl_minute] ));
  506.     Serial.println(cb);
  507.  
  508.   }
  509. }
  510.  
  511. void PrintSunRise()
  512. {
  513.       local = CE.toLocal(utc, &tcr);
  514.       SunRise(local, tcr -> abbrev);
  515. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement