Skip to content

Commit 2bc2381

Browse files
committed
Fix RTC WiFi example
1 parent 9a83a98 commit 2bc2381

File tree

1 file changed

+156
-86
lines changed
  • content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi

1 file changed

+156
-86
lines changed

content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md

Lines changed: 156 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -156,117 +156,187 @@ void printMacAddress(byte mac[]) {
156156

157157
```arduino
158158
/*
159-
This example connects to an unencrypted WiFi network.
160-
Then it prints the MAC address of the WiFi module,
161-
the IP address obtained, and other network details.
159+
Udp NTP Client
162160
163-
Circuit:
164-
* GIGA R1 WiFi
161+
Get the time from a Network Time Protocol (NTP) time server
162+
Demonstrates use of UDP sendPacket and ReceivePacket
163+
For more on NTP time servers and the messages needed to communicate with them,
164+
see http://en.wikipedia.org/wiki/Network_Time_Protocol
165165
166-
created 13 July 2010
167-
by dlf (Metodo2 srl)
168-
modified 31 May 2012
166+
created 4 Sep 2010
167+
by Michael Margolis
168+
modified 9 Apr 2012
169169
by Tom Igoe
170-
modified 22 March 2023
171-
by Karl Söderby
172-
*/
170+
modified 28 Dec 2022
171+
by Giampaolo Mancini
173172
173+
This code is in the public domain.
174+
*/
174175
175-
#include <SPI.h>
176176
#include <WiFi.h>
177+
#include <WiFiUdp.h>
178+
#include <mbed_mktime.h>
177179
178-
#include "arduino_secrets.h"
180+
int status = WL_IDLE_STATUS;
181+
#include "arduino_secrets.h"
179182
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
180-
char ssid[] = SECRET_SSID; // your network SSID (name)
181-
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
182-
int status = WL_IDLE_STATUS; // the WiFi radio's status
183+
char ssid[] = ""; // your network SSID (name)
184+
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
185+
int keyIndex = 0; // your network key index number (needed only for WEP)
183186
184-
void setup() {
185-
//Initialize serial and wait for port to open:
186-
Serial.begin(9600);
187-
while (!Serial) {
188-
; // wait for serial port to connect. Needed for native USB port only
189-
}
187+
unsigned int localPort = 2390; // local port to listen for UDP packets
190188
191-
// check for the WiFi module:
192-
if (WiFi.status() == WL_NO_MODULE) {
193-
Serial.println("Communication with WiFi module failed!");
194-
// don't continue
195-
while (true);
196-
}
189+
// IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server
197190
198-
// attempt to connect to WiFi network:
199-
while (status != WL_CONNECTED) {
200-
Serial.print("Attempting to connect to WPA SSID: ");
201-
Serial.println(ssid);
202-
// Connect to WPA/WPA2 network:
203-
status = WiFi.begin(ssid, pass);
191+
constexpr auto timeServer { "pool.ntp.org" };
204192
205-
// wait 10 seconds for connection:
206-
delay(10000);
207-
}
193+
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
208194
209-
// you're connected now, so print out the data:
210-
Serial.print("You're connected to the network");
211-
printCurrentNet();
212-
printWifiData();
195+
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
213196
214-
}
197+
// A UDP instance to let us send and receive packets over UDP
198+
WiFiUDP Udp;
215199
216-
void loop() {
217-
// check the network connection once every 10 seconds:
218-
delay(10000);
219-
printCurrentNet();
220-
}
200+
constexpr unsigned long printInterval { 1000 };
201+
unsigned long printNow {};
221202
222-
void printWifiData() {
223-
// print your board's IP address:
224-
IPAddress ip = WiFi.localIP();
225-
Serial.print("IP Address: ");
226-
Serial.println(ip);
227-
Serial.println(ip);
203+
void setup()
204+
{
205+
// Open serial communications and wait for port to open:
206+
Serial.begin(9600);
207+
while (!Serial) {
208+
; // wait for serial port to connect. Needed for native USB port only
209+
}
228210
229-
// print your MAC address:
230-
byte mac[6];
231-
WiFi.macAddress(mac);
232-
Serial.print("MAC address: ");
233-
printMacAddress(mac);
234-
}
211+
// check for the WiFi module:
212+
if (WiFi.status() == WL_NO_SHIELD) {
213+
Serial.println("Communication with WiFi module failed!");
214+
// don't continue
215+
while (true)
216+
;
217+
}
235218
236-
void printCurrentNet() {
237-
// print the SSID of the network you're attached to:
238-
Serial.print("SSID: ");
239-
Serial.println(WiFi.SSID());
219+
// attempt to connect to WiFi network:
220+
while (status != WL_CONNECTED) {
221+
Serial.print("Attempting to connect to SSID: ");
222+
Serial.println(ssid);
223+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
224+
status = WiFi.begin(ssid, pass);
240225
241-
// print the MAC address of the router you're attached to:
242-
byte bssid[6];
243-
WiFi.BSSID(bssid);
244-
Serial.print("BSSID: ");
245-
printMacAddress(bssid);
226+
// wait 10 seconds for connection:
227+
delay(10000);
228+
}
246229
247-
// print the received signal strength:
248-
long rssi = WiFi.RSSI();
249-
Serial.print("signal strength (RSSI):");
250-
Serial.println(rssi);
230+
Serial.println("Connected to WiFi");
231+
printWifiStatus();
232+
233+
setNtpTime();
251234
252-
// print the encryption type:
253-
byte encryption = WiFi.encryptionType();
254-
Serial.print("Encryption Type:");
255-
Serial.println(encryption, HEX);
256-
Serial.println();
257235
}
258236
259-
void printMacAddress(byte mac[]) {
260-
for (int i = 5; i >= 0; i--) {
261-
if (mac[i] < 16) {
262-
Serial.print("0");
237+
void loop()
238+
{
239+
if (millis() > printNow) {
240+
Serial.print("System Clock: ");
241+
Serial.println(getLocaltime());
242+
printNow = millis() + printInterval;
263243
}
264-
Serial.print(mac[i], HEX);
265-
if (i > 0) {
266-
Serial.print(":");
244+
}
245+
246+
void setNtpTime()
247+
{
248+
Udp.begin(localPort);
249+
sendNTPpacket(timeServer);
250+
delay(1000);
251+
parseNtpPacket();
252+
}
253+
254+
// send an NTP request to the time server at the given address
255+
unsigned long sendNTPpacket(const char * address)
256+
{
257+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
258+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
259+
packetBuffer[1] = 0; // Stratum, or type of clock
260+
packetBuffer[2] = 6; // Polling Interval
261+
packetBuffer[3] = 0xEC; // Peer Clock Precision
262+
// 8 bytes of zero for Root Delay & Root Dispersion
263+
packetBuffer[12] = 49;
264+
packetBuffer[13] = 0x4E;
265+
packetBuffer[14] = 49;
266+
packetBuffer[15] = 52;
267+
268+
Udp.beginPacket(address, 123); // NTP requests are to port 123
269+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
270+
Udp.endPacket();
271+
}
272+
273+
unsigned long parseNtpPacket()
274+
{
275+
if (!Udp.parsePacket())
276+
return 0;
277+
278+
Udp.read(packetBuffer, NTP_PACKET_SIZE);
279+
const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
280+
const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
281+
const unsigned long secsSince1900 = highWord << 16 | lowWord;
282+
constexpr unsigned long seventyYears = 2208988800UL;
283+
const unsigned long epoch = secsSince1900 - seventyYears;
284+
set_time(epoch);
285+
286+
#if defined(VERBOSE)
287+
Serial.print("Seconds since Jan 1 1900 = ");
288+
Serial.println(secsSince1900);
289+
290+
// now convert NTP time into everyday time:
291+
Serial.print("Unix time = ");
292+
// print Unix time:
293+
Serial.println(epoch);
294+
295+
// print the hour, minute and second:
296+
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
297+
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
298+
Serial.print(':');
299+
if (((epoch % 3600) / 60) < 10) {
300+
// In the first 10 minutes of each hour, we'll want a leading '0'
301+
Serial.print('0');
267302
}
268-
}
269-
Serial.println();
303+
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
304+
Serial.print(':');
305+
if ((epoch % 60) < 10) {
306+
// In the first 10 seconds of each minute, we'll want a leading '0'
307+
Serial.print('0');
308+
}
309+
Serial.println(epoch % 60); // print the second
310+
#endif
311+
312+
return epoch;
313+
}
314+
315+
String getLocaltime()
316+
{
317+
char buffer[32];
318+
tm t;
319+
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
320+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
321+
return String(buffer);
322+
}
323+
324+
void printWifiStatus()
325+
{
326+
// print the SSID of the network you're attached to:
327+
Serial.print("SSID: ");
328+
Serial.println(WiFi.SSID());
329+
330+
// print your board's IP address:
331+
IPAddress ip = WiFi.localIP();
332+
Serial.print("IP Address: ");
333+
Serial.println(ip);
334+
335+
// print the received signal strength:
336+
long rssi = WiFi.RSSI();
337+
Serial.print("signal strength (RSSI):");
338+
Serial.print(rssi);
339+
Serial.println(" dBm");
270340
}
271341
```
272342

0 commit comments

Comments
 (0)