Advertisement
zeugsman

Arduino Uno Wifi Date and Time as scrolling text on intern LED matrix

Feb 8th, 2024 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | Software | 0 0
  1. Da war ein Fehler in der Monatsanzeige. Hier die korrekte Version:
  2.  
  3. /**
  4. * RTC_NTPSync
  5. *
  6. * This example shows how to set the RTC (Real Time Clock) on the Portenta C33 / UNO R4 WiFi
  7. * to the current date and time retrieved from an NTP server on the Internet (pool.ntp.org).
  8. * Then the current time from the RTC is printed to the Serial port.
  9. *
  10. * Instructions:
  11. * 1. Download the NTPClient library (https://github.com/arduino-libraries/NTPClient) through the Library Manager
  12. * 2. Change the WiFi credentials in the arduino_secrets.h file to match your WiFi network.
  13. * 3. Upload this sketch to Portenta C33 / UNO R4 WiFi.
  14. * 4. Open the Serial Monitor.
  15. *
  16. * Initial author: Sebastian Romero @sebromero
  17. *
  18. * Find the full UNO R4 WiFi RTC documentation here:
  19. * https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
  20. *
  21. *
  22. * Scrolling clock
  23. *
  24. * Shows the date and time as scrolling text on the LED matrix of the Arduino UNO R4 WiFi.
  25. * by zeugsman
  26. *
  27. */
  28.  
  29. // Include the RTC library
  30. #include "RTC.h"
  31.  
  32. //Include the NTP library
  33. #include <NTPClient.h>
  34.  
  35. #if defined(ARDUINO_PORTENTA_C33)
  36. #include <WiFiC3.h>
  37. #elif defined(ARDUINO_UNOWIFIR4)
  38. #include <WiFiS3.h>
  39. #endif
  40.  
  41. #include <WiFiUdp.h>
  42. #include "arduino_secrets.h"
  43.  
  44. #include "ArduinoGraphics.h"
  45. #include "Arduino_LED_Matrix.h"
  46. #include <Wire.h>
  47.  
  48. ArduinoLEDMatrix matrix;
  49.  
  50. ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
  51. char ssid[] = SECRET_SSID; // your network SSID (name)
  52. char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
  53.  
  54. int wifiStatus = WL_IDLE_STATUS;
  55. WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
  56. NTPClient timeClient(Udp);
  57.  
  58. const char dateFormat[] PROGMEM = " %02d.%02d.%d %02d:%02d:%02d ";
  59.  
  60. void printWifiStatus() {
  61. // print the SSID of the network you're attached to:
  62. Serial.print("SSID: ");
  63. Serial.println(WiFi.SSID());
  64.  
  65. // print your board's IP address:
  66. IPAddress ip = WiFi.localIP();
  67. Serial.print("IP Address: ");
  68. Serial.println(ip);
  69.  
  70. // print the received signal strength:
  71. long rssi = WiFi.RSSI();
  72. Serial.print("signal strength (RSSI):");
  73. Serial.print(rssi);
  74. Serial.println(" dBm");
  75. }
  76.  
  77. void connectToWiFi() {
  78. // check for the WiFi module:
  79. if (WiFi.status() == WL_NO_MODULE) {
  80. Serial.println("Communication with WiFi module failed!");
  81. // don't continue
  82. while (true)
  83. ;
  84. }
  85.  
  86. String fv = WiFi.firmwareVersion();
  87. if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
  88. Serial.println("Please upgrade the firmware");
  89. }
  90.  
  91. // attempt to connect to WiFi network:
  92. while (wifiStatus != WL_CONNECTED) {
  93. Serial.print("Attempting to connect to SSID: ");
  94. Serial.println(ssid);
  95. // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
  96. wifiStatus = WiFi.begin(ssid, pass);
  97.  
  98. // wait 10 seconds for connection:
  99. delay(10000);
  100. }
  101.  
  102. Serial.println("Connected to WiFi");
  103. printWifiStatus();
  104. }
  105.  
  106. void setup() {
  107. Serial.begin(9600);
  108. while (!Serial)
  109. ;
  110.  
  111. connectToWiFi();
  112. matrix.begin();
  113. RTC.begin();
  114. Serial.println("\nStarting connection to server...");
  115. timeClient.begin();
  116. timeClient.update();
  117.  
  118. // Get the current date and time from an NTP server and convert
  119. // it to UTC +2 by passing the time zone offset in hours.
  120. // You may change the time zone offset to your local one.
  121. auto timeZoneOffsetHours = 1;
  122. auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
  123. Serial.print("Unix time = ");
  124. Serial.println(unixTime);
  125. RTCTime timeToSet = RTCTime(unixTime);
  126.  
  127. RTC.setTime(timeToSet);
  128.  
  129. // Retrieve the date and time from the RTC and print them
  130.  
  131. RTCTime currentTime;
  132. RTC.getTime(currentTime);
  133. Serial.println("The RTC was just set to: " + String(currentTime));
  134. }
  135.  
  136. void loop() {
  137.  
  138. RTCTime currentTime;
  139. RTC.getTime(currentTime);
  140.  
  141. int bufferSize = snprintf(NULL, 0, dateFormat,
  142. currentTime.getDayOfMonth(), Month2int(currentTime.getMonth()), currentTime.getYear(),
  143. currentTime.getHour(), currentTime.getMinutes(), currentTime.getSeconds());
  144.  
  145. char buffer[bufferSize];
  146. snprintf(buffer, bufferSize, dateFormat,
  147. currentTime.getDayOfMonth(), Month2int(currentTime.getMonth()), currentTime.getYear(),
  148. currentTime.getHour(), currentTime.getMinutes(), currentTime.getSeconds());
  149.  
  150. matrix.beginDraw();
  151. matrix.stroke(0xFFFFFFFF);
  152. matrix.textScrollSpeed(75);
  153. matrix.textFont(Font_5x7);
  154. matrix.beginText(0, 1, 0xFFFFFFFF);
  155. matrix.println(buffer);
  156. matrix.endText(SCROLL_LEFT);
  157. matrix.endDraw();
  158. /*
  159. Serial.println(currentTime);
  160. Serial.print(" - ");
  161. Serial.print(buffer);
  162. //delay(1000);
  163. */
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement