WidgetRTC.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * @file WidgetRTC.h
  3. * @author Volodymyr Shymanskyy
  4. * @license This project is released under the MIT License (MIT)
  5. * @copyright Copyright (c) 2015 Volodymyr Shymanskyy
  6. * @date Mar 2016
  7. * @brief
  8. *
  9. */
  10. #ifndef WidgetRTC_h
  11. #define WidgetRTC_h
  12. #include <Blynk/BlynkApi.h>
  13. class WidgetRTC
  14. {
  15. public:
  16. WidgetRTC() {}
  17. void setVPin(int vPin);
  18. void onWrite(BlynkReq& request, const BlynkParam& param);
  19. private:
  20. static unsigned long requestTimeSync();
  21. static uint8_t mPin;
  22. };
  23. #ifndef WidgetRTC_h_NO_IMPL
  24. #include <Time.h>
  25. inline
  26. void WidgetRTC::setVPin(int vPin)
  27. {
  28. mPin = vPin;
  29. setSyncProvider(requestTimeSync);
  30. }
  31. // This is called by Time library when it needs time sync
  32. unsigned long WidgetRTC::requestTimeSync()
  33. {
  34. Blynk.syncVirtual(mPin); // Request RTC widget update from the server
  35. return 0; // Tell the Time library that we'll set it later
  36. }
  37. inline
  38. void WidgetRTC::onWrite(BlynkReq& request, const BlynkParam& param)
  39. {
  40. const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  41. unsigned long blynkTime = param.asLong();
  42. if ( blynkTime >= DEFAULT_TIME) { // Check the integer is a valid time (greater than Jan 1 2013)
  43. setTime(blynkTime); // Sync Time library clock to the value received from Blynk
  44. BLYNK_LOG("Time sync: OK");
  45. }
  46. }
  47. #endif
  48. #endif