WiFi RTC

With this tutorial you will learn to use the RTC (Real Time Clock) and the WiFi capabilities.

With this tutorial you will learn to use the RTC (Real Time Clock) and the WiFi capabilities of the boards Arduino MKR1000, Arduino MKR WiFi 1010 and Arduino MKR VIDOR 4000. The network connection is used to access one Time Server on the Internet and to get from it the correct Time, using the Network Time Protocol builtin in the used WiFi module. The time is then used to configure the internal RTC (UTC format) of the board using the linux epoch format.

Hardware Required

  • Arduino MKR1000, Arduino MKR WiFi 1010 and Arduino MKR VIDOR 4000

  • WiFi access to the Internet

The Circuit

No circuit is required for this tutorial.

Software Essentials

Includes:

<SPI.h>
This library allows you to communicate with SPI devices, with the Arduino as the master device. In this sketch it is used to communicate with the WiFi Radio. Included for compatibillity with IDE versions before 1.6.5.

<WiFi101.h>
This is the library shared across the old WiFi enabled boards to manage the connections to the Internet through WiFi.

or

<WiFiNINA.h>
This is the library shared across the new WiFi enabled boards to manage the connections to the Internet through WiFi. Please change
<WiFi101.h>
into
<WiFiNINA.h>
if you are using an Arduino MKR WiFi 1010 or Arduino MKR VIDOR 4000.

<RTCZero.h>
This library allows an Arduino Zero or MKR1000 board to control and use the internal RTC (Real Time Clock).

Functions defined in the sketch:

printTime() Reads the time from the RTC object and prints hour, minutes and seconds to the Serial console.

printDate() Reads the date from the RTC object and prints day, month and year to the Serial console with zero padding.

printWiFiStatus() Prints to Serial console a full set of information including the SSID of the network you're connected to, the local IP address and the signal strength.

Code

The code is based on Epoch example in RTCZero.

1/*
2
3 MKR1000 - MKR WiFi 1010 - MKR VIDOR 4000 WiFi RTC
4
5 This sketch asks NTP for the Linux epoch and sets the internal Arduino MKR1000's RTC accordingly.
6
7 created 08 Jan 2016
8
9 by Arturo Guadalupi <a.guadalupi@arduino.cc>
10
11 modified 26 Sept 2018
12
13 http://arduino.cchttps://www.arduino.cc/en/Tutorial/WiFiRTC
14
15 This code is in the public domain.
16
17*/
18
19#include <SPI.h>
20#include <WiFi101.h>
21//#include <WiFiNINA.h> //Include this instead of WiFi101.h as needed
22#include <WiFiUdp.h>
23#include <RTCZero.h>
24
25RTCZero rtc;
26
27#include "arduino_secrets.h"
28///////please enter your sensitive data in the Secret tab/arduino_secrets.h
29char ssid[] = SECRET_SSID; // your network SSID (name)
30char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
31int keyIndex = 0; // your network key Index number (needed only for WEP)
32
33int status = WL_IDLE_STATUS;
34
35const int GMT = 2; //change this to adapt it to your time zone
36
37void setup() {
38
39 Serial.begin(115200);
40
41 // check if the WiFi module works
42
43 if (WiFi.status() == WL_NO_SHIELD) {
44
45 Serial.println("WiFi shield not present");
46
47 // don't continue:
48
49 while (true);
50
51 }
52
53 // attempt to connect to WiFi network:
54
55 while ( status != WL_CONNECTED) {
56
57 Serial.print("Attempting to connect to SSID: ");
58
59 Serial.println(ssid);
60
61 // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
62
63 status = WiFi.begin(ssid, pass);
64
65 // wait 10 seconds for connection:
66
67 delay(10000);
68
69 }
70
71 // you're connected now, so print out the status:
72
73 printWiFiStatus();
74
75 rtc.begin();
76
77 unsigned long epoch;
78
79 int numberOfTries = 0, maxTries = 6;
80
81 do {
82
83 epoch = WiFi.getTime();
84
85 numberOfTries++;
86
87 }
88
89 while ((epoch == 0) && (numberOfTries < maxTries));
90
91 if (numberOfTries == maxTries) {
92
93 Serial.print("NTP unreachable!!");
94
95 while (1);
96
97 }
98
99 else {
100
101 Serial.print("Epoch received: ");
102
103 Serial.println(epoch);
104
105 rtc.setEpoch(epoch);
106
107 Serial.println();
108
109 }
110}
111
112void loop() {
113
114 printDate();
115
116 printTime();
117
118 Serial.println();
119
120 delay(1000);
121}
122
123void printTime()
124{
125
126 print2digits(rtc.getHours() + GMT);
127
128 Serial.print(":");
129
130 print2digits(rtc.getMinutes());
131
132 Serial.print(":");
133
134 print2digits(rtc.getSeconds());
135
136 Serial.println();
137}
138
139void printDate()
140{
141
142 Serial.print(rtc.getDay());
143
144 Serial.print("/");
145
146 Serial.print(rtc.getMonth());
147
148 Serial.print("/");
149
150 Serial.print(rtc.getYear());
151
152 Serial.print(" ");
153}
154
155void printWiFiStatus() {
156
157 // print the SSID of the network you're attached to:
158
159 Serial.print("SSID: ");
160
161 Serial.println(WiFi.SSID());
162
163 // print your WiFi shield's IP address:
164
165 IPAddress ip = WiFi.localIP();
166
167 Serial.print("IP Address: ");
168
169 Serial.println(ip);
170
171 // print the received signal strength:
172
173 long rssi = WiFi.RSSI();
174
175 Serial.print("signal strength (RSSI):");
176
177 Serial.print(rssi);
178
179 Serial.println(" dBm");
180}
181
182void print2digits(int number) {
183
184 if (number < 10) {
185
186 Serial.print("0");
187
188 }
189
190 Serial.print(number);
191}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.

Suggest Changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.