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