Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update and improve example ConnectWithWPA #260

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 42 additions & 38 deletions examples/ConnectWithWEP/ConnectWithWEP.ino
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
/*
This example connects to a WEP-encrypted WiFi network.
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.

If you use 40-bit WEP, you need a key that is 10 characters long,
and the characters must be hexadecimal (0-9 or A-F).
e.g. for 40-bit, ABBADEAF01 will work, but ABBADEAF won't work
(too short) and ABBAISDEAF won't work (I and S are not
hexadecimal characters).

For 128-bit, you need a string that is 26 characters long.
D0D0DEADF00DABBADEAFBEADED will work because it's 26 characters,
all in the 0-9, A-F range.

created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 07 Nov 2023
by Frank Häfele
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

#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 keyIndex = 0; // your network key index number
int status = WL_IDLE_STATUS; // the WiFi radio's status
// ==> please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = MY_SSID; // your network SSID (name)
char pass[] = MY_PASSWORD; // your network password (use for WPA, or use as key 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
}
Serial.println("\n=== WIFININA - Wifi example ===\n\n");

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Expand All @@ -47,20 +36,36 @@ void setup() {
Serial.println("Please upgrade the firmware");
}

int status = WL_IDLE_STATUS;
int counter{0};
auto tic = millis();
// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WEP network, SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, keyIndex, pass);

// wait 10 seconds for connection:
delay(10000);
auto start = millis();

Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
while ((millis() - start) < 60000U) {
Serial.print(". ");
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
if (status == WL_CONNECTED) {
// you're connected now, so print out the data:
Serial.print("\nYou're connected to the network!\n");
printCurrentNet();
printWifiData();
break;
}
}

// once you are connected :
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();
if (status != WL_CONNECTED) {
while (true) {
Serial.print("\n\tError: Connection failed!");
delay(5000);
}
}
auto toc = millis();
Serial.print("=> Connection Time: ");
Serial.print(toc-tic);
Serial.print(" ms\n\n");
}

void loop() {
Expand All @@ -72,36 +77,35 @@ void loop() {
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.print("\tIP Address: ");
Serial.println(ip);

// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print("\tMAC address: ");
printMacAddress(mac);
}

void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.print("\tSSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print("\tBSSID: ");
printMacAddress(bssid);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print("\tRSSI: ");
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.print("\tEncryption Type: ");
Serial.println(encryption, HEX);
Serial.println();
}
Expand Down
64 changes: 37 additions & 27 deletions examples/ConnectWithWPA/ConnectWithWPA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,64 @@

created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
modified 07 Nov 2023
by Frank Häfele
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"

#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/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

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
}
Serial.println(F("\n=== WIFININA - Wifi example ===\n\n"));

// check for the WiFi module:
if (WiFi.status() == WL_NO_MODULE) {
Serial.println("Communication with WiFi module failed!");
Serial.println(F("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");
Serial.println(F("Please upgrade the firmware"));
}

// attempt to connect to WiFi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
int status = WL_IDLE_STATUS;
auto tic = millis();

Serial.print(F("Attempting to connect to WPA SSID: "));
Serial.println(ssid);
while ((millis() - tic) < 60000U) {
Serial.print(". ");
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);

// wait 10 seconds for connection:
delay(10000);
if (status == WL_CONNECTED) {
// you're connected now, so print out the data:
Serial.print(F("\nYou're connected to the network!\n"));
printCurrentNet();
printWifiData();
break;
}
}

// you're connected now, so print out the data:
Serial.print("You're connected to the network");
printCurrentNet();
printWifiData();

if (status != WL_CONNECTED) {
while (true) {
Serial.print(F("\n\tError: Connection failed!"));
delay(5000);
}
}
auto toc = millis();
Serial.print("=> Connection Time: ");
Serial.print(toc-tic);
Serial.print(" ms\n\n");
}

void loop() {
Expand All @@ -63,36 +74,35 @@ void loop() {
void printWifiData() {
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.print("\tIP Address: ");
Serial.println(ip);

// print your MAC address:
byte mac[6];
WiFi.macAddress(mac);
Serial.print("MAC address: ");
Serial.print("\tMAC address: ");
printMacAddress(mac);
}

void printCurrentNet() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.print("\tSSID: ");
Serial.println(WiFi.SSID());

// print the MAC address of the router you're attached to:
byte bssid[6];
WiFi.BSSID(bssid);
Serial.print("BSSID: ");
Serial.print("\tBSSID: ");
printMacAddress(bssid);

// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print("\tRSSI: ");
Serial.println(rssi);

// print the encryption type:
byte encryption = WiFi.encryptionType();
Serial.print("Encryption Type:");
Serial.print("\tEncryption Type: ");
Serial.println(encryption, HEX);
Serial.println();
}
Expand Down
Loading