/**
* 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
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSerif24pt7b.h>
// 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"
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
// The pins for I2C are defined by the Wire-library.
// On an arduino UNO: A4(SDA), A5(SCL)
// On an arduino MEGA 2560: 20(SDA), 21(SCL)
// On an arduino LEONARDO: 2(SDA), 3(SCL), ...
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3c ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
///////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);
volatile bool irqFlag = false;
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);
// }
// attempt to connect to WiFi network:
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
wifiStatus = WiFi.begin(ssid, pass);
while (wifiStatus != WL_CONNECTED) {
Serial.print("Connecting to SSID: "); Serial.println(ssid);
display.clearDisplay();
display.setCursor(0, 0);
display.print("Connecting to SSID: "); display.println(ssid);
display.display();
wifiStatus = WiFi.status();
// wait 0.5 seconds for connection:
delay(500);
}
Serial.println("Connected to WiFi");
printWifiStatus();
delay(2000);
}
RTCTime GetCurrentTime() {
RTCTime currentTime;
RTC.getTime(currentTime);
return currentTime;
}
void periodicCallback()
{
irqFlag = true;
}
void initDisplay() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
}
void showTime() {
auto time = GetCurrentTime();
display.setFont();
display.setTextSize(1);
display.setCursor(0, 0);
char strDate[11], strTime[9];
sprintf(strDate, "%02d/%02d/%4d", time.getDayOfMonth(), (int)(time.getMonth()) + 1, time.getYear());
display.print(strDate);
//display.setTextSize(4);
display.setFont(&FreeSerif24pt7b);
display.setCursor(10, SCREEN_HEIGHT - 10);
sprintf(strTime, "%2d%s%02d", time.getHour(), time.getSeconds() % 2 == 0 ? ":" : " ", time.getMinutes());
display.print(strTime);
Serial.println("The RTC was just set to: " + String(time));
}
void showRSSI() {
display.setFont();
display.setTextSize(1);
display.setCursor(80, 0);
char strRSSI[10];
sprintf(strRSSI, "%4d dbm", WiFi.RSSI());
display.print(strRSSI);
}
void setup(){
Serial.begin(9600);
while (!Serial);
initDisplay();
connectToWiFi();
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 = 2;
auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600);
Serial.print("Unix time = ");
Serial.println(unixTime);
RTCTime timeToSet = RTCTime(unixTime);
RTC.setTime(timeToSet);
if (!RTC.setPeriodicCallback(periodicCallback, Period::ONCE_EVERY_1_SEC)) {
Serial.println("ERROR: periodic callback not set");
}
// 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));
Serial.println("The RTC was just set to: " + String(GetCurrentTime()));
}
void loop(){
if(irqFlag) {
// Clear the buffer
display.clearDisplay();
showTime();
showRSSI();
display.display();
irqFlag = false;
}
}