Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Da war ein Fehler in der Monatsanzeige. Hier die korrekte Version:
- /**
- * RTC_NTPSync
- *
- * This example shows how to set the RTC (Real Time Clock) on the Portenta C33 / UNO R4 WiFi
- * to the current date and time retrieved from an NTP server on the Internet (pool.ntp.org).
- * Then the current time from the RTC is printed to the Serial port.
- *
- * Instructions:
- * 1. Download the NTPClient library (https://github.com/arduino-libraries/NTPClient) through the Library Manager
- * 2. Change the WiFi credentials in the arduino_secrets.h file to match your WiFi network.
- * 3. Upload this sketch to Portenta C33 / UNO R4 WiFi.
- * 4. Open the Serial Monitor.
- *
- * Initial author: Sebastian Romero @sebromero
- *
- * Find the full UNO R4 WiFi RTC documentation here:
- * https://docs.arduino.cc/tutorials/uno-r4-wifi/rtc
- *
- *
- * Scrolling clock
- *
- * Shows the date and time as scrolling text on the LED matrix of the Arduino UNO R4 WiFi.
- * by zeugsman
- *
- */
- // Include the RTC library
- #include "RTC.h"
- //Include the NTP library
- #include <NTPClient.h>
- #if defined(ARDUINO_PORTENTA_C33)
- #include <WiFiC3.h>
- #elif defined(ARDUINO_UNOWIFIR4)
- #include <WiFiS3.h>
- #endif
- #include <WiFiUdp.h>
- #include "arduino_secrets.h"
- #include "ArduinoGraphics.h"
- #include "Arduino_LED_Matrix.h"
- #include <Wire.h>
- ArduinoLEDMatrix matrix;
- ///////please enter your sensitive data in the Secret tab/arduino_secrets.h
- char ssid[] = SECRET_SSID; // your network SSID (name)
- char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
- int wifiStatus = WL_IDLE_STATUS;
- WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
- NTPClient timeClient(Udp);
- const char dateFormat[] PROGMEM = " %02d.%02d.%d %02d:%02d:%02d ";
- void printWifiStatus() {
- // print the SSID of the network you're attached to:
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your board's IP address:
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength:
- long rssi = WiFi.RSSI();
- Serial.print("signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- }
- void connectToWiFi() {
- // check for the WiFi module:
- if (WiFi.status() == WL_NO_MODULE) {
- Serial.println("Communication with WiFi module failed!");
- // don't continue
- while (true)
- ;
- }
- String fv = WiFi.firmwareVersion();
- if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
- Serial.println("Please upgrade the firmware");
- }
- // attempt to connect to WiFi network:
- while (wifiStatus != WL_CONNECTED) {
- Serial.print("Attempting to connect to SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
- wifiStatus = WiFi.begin(ssid, pass);
- // wait 10 seconds for connection:
- delay(10000);
- }
- Serial.println("Connected to WiFi");
- printWifiStatus();
- }
- void setup() {
- Serial.begin(9600);
- while (!Serial)
- ;
- connectToWiFi();
- matrix.begin();
- RTC.begin();
- Serial.println("\nStarting connection to server...");
- timeClient.begin();
- timeClient.update();
- // Get the current date and time from an NTP server and convert
- // it to UTC +2 by passing the time zone offset in hours.
- // You may change the time zone offset to your local one.
- auto timeZoneOffsetHours = 1;
- auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
- Serial.print("Unix time = ");
- Serial.println(unixTime);
- RTCTime timeToSet = RTCTime(unixTime);
- RTC.setTime(timeToSet);
- // Retrieve the date and time from the RTC and print them
- RTCTime currentTime;
- RTC.getTime(currentTime);
- Serial.println("The RTC was just set to: " + String(currentTime));
- }
- void loop() {
- RTCTime currentTime;
- RTC.getTime(currentTime);
- int bufferSize = snprintf(NULL, 0, dateFormat,
- currentTime.getDayOfMonth(), Month2int(currentTime.getMonth()), currentTime.getYear(),
- currentTime.getHour(), currentTime.getMinutes(), currentTime.getSeconds());
- char buffer[bufferSize];
- snprintf(buffer, bufferSize, dateFormat,
- currentTime.getDayOfMonth(), Month2int(currentTime.getMonth()), currentTime.getYear(),
- currentTime.getHour(), currentTime.getMinutes(), currentTime.getSeconds());
- matrix.beginDraw();
- matrix.stroke(0xFFFFFFFF);
- matrix.textScrollSpeed(75);
- matrix.textFont(Font_5x7);
- matrix.beginText(0, 1, 0xFFFFFFFF);
- matrix.println(buffer);
- matrix.endText(SCROLL_LEFT);
- matrix.endDraw();
- /*
- Serial.println(currentTime);
- Serial.print(" - ");
- Serial.print(buffer);
- //delay(1000);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement