From 2bc238123dc8d0076d197df8fe2e0ae65a7dbe5a Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Tue, 8 Aug 2023 15:17:16 +0200 Subject: [PATCH 1/2] Fix RTC WiFi example --- .../tutorials/giga-wifi/giga-wifi.md | 242 +++++++++++------- 1 file changed, 156 insertions(+), 86 deletions(-) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md index dcb9e99da6..f30417425b 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md @@ -156,117 +156,187 @@ void printMacAddress(byte mac[]) { ```arduino /* - This example connects to an unencrypted WiFi network. - Then it prints the MAC address of the WiFi module, - the IP address obtained, and other network details. + Udp NTP Client - Circuit: - * GIGA R1 WiFi + Get the time from a Network Time Protocol (NTP) time server + Demonstrates use of UDP sendPacket and ReceivePacket + For more on NTP time servers and the messages needed to communicate with them, + see http://en.wikipedia.org/wiki/Network_Time_Protocol - created 13 July 2010 - by dlf (Metodo2 srl) - modified 31 May 2012 + created 4 Sep 2010 + by Michael Margolis + modified 9 Apr 2012 by Tom Igoe - modified 22 March 2023 - by Karl Söderby - */ + modified 28 Dec 2022 + by Giampaolo Mancini +This code is in the public domain. + */ -#include #include +#include +#include -#include "arduino_secrets.h" +int status = WL_IDLE_STATUS; +#include "arduino_secrets.h" ///////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 status = WL_IDLE_STATUS; // the WiFi radio's status +char ssid[] = ""; // your network SSID (name) +char pass[] = ""; // your network password (use for WPA, or use as key for WEP) +int keyIndex = 0; // your network key index number (needed only for WEP) -void setup() { - //Initialize serial and wait for port to open: - Serial.begin(9600); - while (!Serial) { - ; // wait for serial port to connect. Needed for native USB port only - } +unsigned int localPort = 2390; // local port to listen for UDP packets - // check for the WiFi module: - if (WiFi.status() == WL_NO_MODULE) { - Serial.println("Communication with WiFi module failed!"); - // don't continue - while (true); - } +// IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server - // attempt to connect to WiFi network: - while (status != WL_CONNECTED) { - Serial.print("Attempting to connect to WPA SSID: "); - Serial.println(ssid); - // Connect to WPA/WPA2 network: - status = WiFi.begin(ssid, pass); +constexpr auto timeServer { "pool.ntp.org" }; - // wait 10 seconds for connection: - delay(10000); - } +const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message - // you're connected now, so print out the data: - Serial.print("You're connected to the network"); - printCurrentNet(); - printWifiData(); +byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets -} +// A UDP instance to let us send and receive packets over UDP +WiFiUDP Udp; -void loop() { - // check the network connection once every 10 seconds: - delay(10000); - printCurrentNet(); -} +constexpr unsigned long printInterval { 1000 }; +unsigned long printNow {}; -void printWifiData() { - // print your board's IP address: - IPAddress ip = WiFi.localIP(); - Serial.print("IP Address: "); - Serial.println(ip); - Serial.println(ip); +void setup() +{ + // Open serial communications and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } - // print your MAC address: - byte mac[6]; - WiFi.macAddress(mac); - Serial.print("MAC address: "); - printMacAddress(mac); -} + // check for the WiFi module: + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("Communication with WiFi module failed!"); + // don't continue + while (true) + ; + } -void printCurrentNet() { - // print the SSID of the network you're attached to: - Serial.print("SSID: "); - Serial.println(WiFi.SSID()); + // attempt to connect to WiFi network: + while (status != 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: + status = WiFi.begin(ssid, pass); - // print the MAC address of the router you're attached to: - byte bssid[6]; - WiFi.BSSID(bssid); - Serial.print("BSSID: "); - printMacAddress(bssid); + // wait 10 seconds for connection: + delay(10000); + } - // print the received signal strength: - long rssi = WiFi.RSSI(); - Serial.print("signal strength (RSSI):"); - Serial.println(rssi); + Serial.println("Connected to WiFi"); + printWifiStatus(); + + setNtpTime(); - // print the encryption type: - byte encryption = WiFi.encryptionType(); - Serial.print("Encryption Type:"); - Serial.println(encryption, HEX); - Serial.println(); } -void printMacAddress(byte mac[]) { - for (int i = 5; i >= 0; i--) { - if (mac[i] < 16) { - Serial.print("0"); +void loop() +{ + if (millis() > printNow) { + Serial.print("System Clock: "); + Serial.println(getLocaltime()); + printNow = millis() + printInterval; } - Serial.print(mac[i], HEX); - if (i > 0) { - Serial.print(":"); +} + +void setNtpTime() +{ + Udp.begin(localPort); + sendNTPpacket(timeServer); + delay(1000); + parseNtpPacket(); +} + +// send an NTP request to the time server at the given address +unsigned long sendNTPpacket(const char * address) +{ + memset(packetBuffer, 0, NTP_PACKET_SIZE); + packetBuffer[0] = 0b11100011; // LI, Version, Mode + packetBuffer[1] = 0; // Stratum, or type of clock + packetBuffer[2] = 6; // Polling Interval + packetBuffer[3] = 0xEC; // Peer Clock Precision + // 8 bytes of zero for Root Delay & Root Dispersion + packetBuffer[12] = 49; + packetBuffer[13] = 0x4E; + packetBuffer[14] = 49; + packetBuffer[15] = 52; + + Udp.beginPacket(address, 123); // NTP requests are to port 123 + Udp.write(packetBuffer, NTP_PACKET_SIZE); + Udp.endPacket(); +} + +unsigned long parseNtpPacket() +{ + if (!Udp.parsePacket()) + return 0; + + Udp.read(packetBuffer, NTP_PACKET_SIZE); + const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); + const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); + const unsigned long secsSince1900 = highWord << 16 | lowWord; + constexpr unsigned long seventyYears = 2208988800UL; + const unsigned long epoch = secsSince1900 - seventyYears; + set_time(epoch); + +#if defined(VERBOSE) + Serial.print("Seconds since Jan 1 1900 = "); + Serial.println(secsSince1900); + + // now convert NTP time into everyday time: + Serial.print("Unix time = "); + // print Unix time: + Serial.println(epoch); + + // print the hour, minute and second: + Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) + Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) + Serial.print(':'); + if (((epoch % 3600) / 60) < 10) { + // In the first 10 minutes of each hour, we'll want a leading '0' + Serial.print('0'); } - } - Serial.println(); + Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) + Serial.print(':'); + if ((epoch % 60) < 10) { + // In the first 10 seconds of each minute, we'll want a leading '0' + Serial.print('0'); + } + Serial.println(epoch % 60); // print the second +#endif + + return epoch; +} + +String getLocaltime() +{ + char buffer[32]; + tm t; + _rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT); + strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t); + return String(buffer); +} + +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"); } ``` From c4400eb09a94efae854535638d6a7a2bd95682c3 Mon Sep 17 00:00:00 2001 From: Hannes Siebeneicher Date: Tue, 8 Aug 2023 15:22:27 +0200 Subject: [PATCH 2/2] Add missing frontmatter --- .../giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md | 2 +- .../boards/giga-r1-wifi/tutorials/giga-audio/content.md | 6 ++++++ .../giga-r1-wifi/tutorials/giga-camera/giga-camera.md | 6 ++++++ .../giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md | 6 ++++++ .../tutorials/giga-getting-started/giga-getting-started.md | 6 ++++++ .../tutorials/giga-micropython/giga-micropython.md | 2 ++ .../boards/giga-r1-wifi/tutorials/giga-usb/giga-usb.md | 6 ++++++ .../boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md | 6 ++++++ 8 files changed, 39 insertions(+), 1 deletion(-) diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md index 5bb66c6df1..6b8a36fa64 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md @@ -15,7 +15,7 @@ tags: - Audio Jack author: 'Jacob Hylén' hardware: - - hardware/08.mega/boards/giga-r1-wifi + - hardware/10.mega/boards/giga-r1-wifi software: - ide-v1 - ide-v2 diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-audio/content.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-audio/content.md index 06a6ccd188..6797175109 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-audio/content.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-audio/content.md @@ -2,6 +2,12 @@ title: Guide to GIGA R1 Advanced ADC/DAC and Audio Features description: 'Learn how to use the ADC/DAC features, along with useful examples on how to generate waveforms and play audio from a file.' author: José Bagur, Taddy Chung & Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi +software: + - ide-v1 + - ide-v2 + - web-editor tags: [ADC, DAC, Audio, USB] --- diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-camera/giga-camera.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-camera/giga-camera.md index cb06295e8c..1aaa4f1abc 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-camera/giga-camera.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-camera/giga-camera.md @@ -3,6 +3,12 @@ title: GIGA R1 Camera Guide description: Learn about the GIGA R1 WiFi's camera connector, and how to use existing examples. tags: [ArduCAM, Camera, Processing] author: Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi +software: + - ide-v1 + - ide-v2 + - web-editor --- The GIGA R1 has a dedicated camera connector that allows certain camera modules to mount directly on the board. This makes it possible to add machine vision to your GIGA R1 board without much effort at all. diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md index 884ec84c7c..b80457611a 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-dual-core/giga-dual-core.md @@ -2,6 +2,12 @@ title: Guide to GIGA R1 Dual Cores description: Learn how to access and control the M4 and M7 cores on the GIGA R1, and how to communicate between them using RPC. author: Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi +software: + - ide-v1 + - ide-v2 + - web-editor tags: [Dual Core, M4, M7] --- diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-getting-started/giga-getting-started.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-getting-started/giga-getting-started.md index 65aa374053..bc540d8d3e 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-getting-started/giga-getting-started.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-getting-started/giga-getting-started.md @@ -2,6 +2,12 @@ title: Getting Started with GIGA R1 WiFi description: A step-by-step guide to install the board package needed for the GIGA R1 WiFi board. author: Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi +software: + - ide-v1 + - ide-v2 + - web-editor tags: [GIGA R1 WiFi, Installation, IDE] --- diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-micropython/giga-micropython.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-micropython/giga-micropython.md index 0eafc7d904..1133f2b997 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-micropython/giga-micropython.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-micropython/giga-micropython.md @@ -2,6 +2,8 @@ title: MicroPython on the GIGA R1 description: Get started with MicroPython on the GIGA R1. author: Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi tags: [MicroPython, dfu-util] --- diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-usb/giga-usb.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-usb/giga-usb.md index e533b031e9..54fc6679bb 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-usb/giga-usb.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-usb/giga-usb.md @@ -2,6 +2,12 @@ title: Guide to Arduino GIGA USB Features description: 'Learn how you can turn your USB device into a mouse or keyboard, how to read & write to a USB mass storage, and connecting a keyboard via the USB-A connector.' author: Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi +software: + - ide-v1 + - ide-v2 + - web-editor tags: [USB, USB HID, USBHost, Mass Storage, Keyboard, Mouse] --- diff --git a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md index f30417425b..4e13c9aa03 100644 --- a/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md +++ b/content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md @@ -2,6 +2,12 @@ title: GIGA R1 WiFi Network Examples description: Discover examples compatible with the WiFi library included in the GIGA core. author: Karl Söderby +hardware: + - hardware/10.mega/boards/giga-r1-wifi +software: + - ide-v1 + - ide-v2 + - web-editor tags: [Wi-Fi, Web Server, AP Mode, SSL, UDP] ---