Nano RP2040 Connect (in Arduino Framework) connect to WiFi network

This Arduino exercise run on Nano RP2040 Connect with WiFiNINA library, to connect to WiFi network. If connection lost, it will try to re-connect again. The onboard LED indicate the connection status.


Library need:

Install WiFiNINA in Arduino IDE Library Manager.


Code:

 NRP_toConnect.ino
//"WiFiNINA by Arduino" needed to be installed in Arduino IDE Library Manager
#include <WiFiNINA.h>

int status = WL_IDLE_STATUS;
char ssid[] = "ssid";        // your network SSID (name)
char pass[] = "password";    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key index number (needed only for WEP)

unsigned long previousMillis;
const long interval = 1000;       //interval for 1 second

void setup() {
  delay(500);
  Serial.begin(9600);
  delay(500);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("\n--- Start ---");

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  Serial.println("WiFi firmware ver: " + (String)WiFi.firmwareVersion());

  previousMillis = millis();;
  
  pinMode(LED_BUILTIN, OUTPUT);
  //Flash 3 times to indicate program start
  digitalWrite(LED_BUILTIN, LOW);  //LED OFF
  delay(500);
  digitalWrite(LED_BUILTIN, HIGH);  //LED ON
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);  //LED OFF
  delay(200);
  digitalWrite(LED_BUILTIN, HIGH);  //LED ON
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);  //LED OFF
  delay(200);
  digitalWrite(LED_BUILTIN, HIGH);  //LED ON
  delay(200);
  digitalWrite(LED_BUILTIN, LOW);  //LED OFF
  delay(200);
}

void loop() {

  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;

    if(WiFi.status() != WL_CONNECTED){
      Serial.println("WiFi.status(): " + deCode_WiFiStatus(WiFi.status()));
      digitalWrite(LED_BUILTIN, LOW);  //LED OFF

      //re-connect if dis-connected
      if(WiFi.begin(ssid, pass) == WL_CONNECTED){
        digitalWrite(LED_BUILTIN, HIGH);  //LED ON
        Serial.println("\n--- WiFi connected ---");
        printWifiStatus();
      }else{
        Serial.println("WiFi.status(): " + deCode_WiFiStatus(WiFi.status()));
      }
    }else{
      digitalWrite(LED_BUILTIN, HIGH);  //LED ON
    }
  }
  
}

void printWifiStatus() {
  Serial.println("WiFi.status(): " + deCode_WiFiStatus(WiFi.status()));
  // 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");
}

String deCode_WiFiStatus(uint8_t status){
  switch (status){
    case WL_CONNECTED: return "WL_CONNECTED";
    case WL_AP_CONNECTED: return "WL_AP_CONNECTED";
    case WL_AP_LISTENING: return "WL_AP_LISTENING";
    case WL_NO_SHIELD: return "WL_NO_SHIELD";
    //case WL_NO_MODULE: return "WL_NO_MODULE";
    case WL_IDLE_STATUS: return "WL_IDLE_STATUS";
    case WL_NO_SSID_AVAIL: return "WL_NO_SSID_AVAIL";
    case WL_SCAN_COMPLETED: return "WL_SCAN_COMPLETED";
    case WL_CONNECT_FAILED: return "WL_CONNECT_FAILED";
    case WL_CONNECTION_LOST: return "WL_CONNECTION_LOST";
    case WL_DISCONNECTED: return "WL_DISCONNECTED";
    default: return("unknown!!!");
    
  }
}

next:
Nano RP2040 Connect get time from Network Time Protocol (NTP) time server display on ST7789 SPI IPS

Comments

Popular posts from this blog

480x320 TFT/ILI9488 SPI wih EP32C3 (arduino-esp32) using Arduino_GFX Library

Drive 320x240 ILI9341 SPI TFT using ESP32-S3 (NodeMCU ESP-S3-12K-Kit) using TFT_eSPI library, in Arduino Framework.