EventsAndOrdinalTime.ino 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * This sketch prints a message at noon UTC, every second Tuesday of the month.
  3. * Not very useful, but demonstrates events and ordinal time.
  4. */
  5. #include <ezTime.h>
  6. #include <WiFi.h>
  7. void setup() {
  8. Serial.begin(115200);
  9. while (!Serial) { ; } // wait for Serial port to connect. Needed for native USB port only
  10. WiFi.begin("your-ssid", "your-password");
  11. waitForSync();
  12. // Set the event to trigger for the first time
  13. setEvent( itIsTheSecondTuesday, nextSecondTuesday() );
  14. }
  15. void loop() {
  16. events();
  17. }
  18. void itIsTheSecondTuesday() {
  19. Serial.print(F("It's the second Tuesday: "));
  20. Serial.println(UTC.dateTime());
  21. // The event then sets a new event for the next time
  22. setEvent( itIsTheSecondTuesday, nextSecondTuesday() );
  23. }
  24. time_t nextSecondTuesday() {
  25. int8_t m = UTC.month();
  26. int16_t y = UTC.year();
  27. time_t t = 0;
  28. while (t <= UTC.now()) {
  29. // Try in current month first, if that has passed, loop once more for next month
  30. t = makeOrdinalTime(12, 0, 0, SECOND, TUESDAY, m, y);
  31. m++;
  32. if (m == 13) {
  33. m = 1;
  34. y++;
  35. }
  36. }
  37. return t;
  38. }