Store the WiFi SSID & password (not at config.h)

For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Post Reply
User avatar
damomad
 
Posts: 48
Joined: Wed Jul 05, 2023 3:38 am

Store the WiFi SSID & password (not at config.h)

Post by damomad »

Hi, I'm using Matrix Portal S3 and need to use them, from time to time, at different places with different WiFi. So the SSID & pw will be different each time. So now way to put that info in the config.h.
I can introduce the SSID & pw every time the board is brought to a new WiFi connection, that's ok. But each time power is lost, it will loose it's WiFi credentials.
Is there anyway to keep (store) those credentials and reconnect when power returns?
Thank you in advance.
Dany

User avatar
mikeysklar
 
Posts: 14226
Joined: Mon Aug 01, 2016 8:10 pm

Re: Store the WiFi SSID & password (not at config.h)

Post by mikeysklar »

Dany,

Do you know the different locations SSID and WiFi you will be using in advance?

You could put them all into config.h as arrays and and a 10s timeout in between trying for different SSIDs.

pseudo generated code using WiFi.h:

Code: Select all

// Define the number of WiFi networks you want to connect to
#define NUM_NETWORKS 3

// Define arrays to store SSID and passwords
const char* ssids[NUM_NETWORKS] = {"SSID1", "SSID2", "SSID3"};
const char* passwords[NUM_NETWORKS] = {"Password1", "Password2", "Password3"};

Code: Select all

#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  // Initialize serial communication at 115200 bits per second:

  WiFi.mode(WIFI_STA); // Set WiFi to station mode

  for (int i = 0; i < NUM_NETWORKS; i++) {
    Serial.print("Connecting to ");
    Serial.println(ssids[i]);

    // Attempt to connect to WiFi network:
    WiFi.begin(ssids[i], passwords[i]);

    int attempts = 0;
    // Wait for connection, timeout after 10 attempts
    while (WiFi.status() != WL_CONNECTED && attempts < 10) {
      delay(1000);
      Serial.print(".");
      attempts++;
    }

    if (WiFi.status() == WL_CONNECTED) {
      Serial.println("");
      Serial.println("WiFi connected successfully");
      Serial.print("IP address: ");
      Serial.println(WiFi.localIP());
      break; // Exit the loop if connected
    } else {
      Serial.println("Connection Failed! Moving to next network...");
    }
  }
}

void loop() {
  // Your loop code goes here
}

User avatar
damomad
 
Posts: 48
Joined: Wed Jul 05, 2023 3:38 am

Re: Store the WiFi SSID & password (not at config.h)

Post by damomad »

Hi. Thank you for your answer.

No, I won't know them as they will be asigned by the people receiving the device.
I've been thinking in adding an external memory, like: Adafruit 24LC32 I2C EEPROM Breakout - 32Kbit / 4 KB - Stemma QT.
It's not too expensive and in case of loosing power, the credentials could be read from here.
Nevertheless great idea from you.
Thank you again.

User avatar
mikeysklar
 
Posts: 14226
Joined: Mon Aug 01, 2016 8:10 pm

Re: Store the WiFi SSID & password (not at config.h)

Post by mikeysklar »

Is CircuitPython viable or would that be too big a change from your Arduino code base?

CircuitPython uses a settings.toml file for the code on the flash which can be mounted from any computer as a USB device. It is easy to manually edit the plain text file with SSID and password with no other changes needed.

User avatar
damomad
 
Posts: 48
Joined: Wed Jul 05, 2023 3:38 am

Re: Store the WiFi SSID & password (not at config.h)

Post by damomad »

My program file has more than 3000 lines. Quite a bit to rewrite again.

Thank you!

User avatar
jm1704
 
Posts: 2
Joined: Sat Apr 13, 2024 5:42 pm

Re: Store the WiFi SSID & password (not at config.h)

Post by jm1704 »

create a file called secrets.py alongside this one and add the
following two lines to it:

WIFI_SSID = "<ssid>"
WIFI_PASSWORD = "<password>"

with your wifi details instead of <ssid> and <password>.

In main programming

import secrets

connect_to_wifi(secrets.WIFI_SSID, secrets.WIFI_PASSWORD)

User avatar
mikeysklar
 
Posts: 14226
Joined: Mon Aug 01, 2016 8:10 pm

Re: Store the WiFi SSID & password (not at config.h)

Post by mikeysklar »

In this case @damomad is using Arduino code.

User avatar
jm1704
 
Posts: 2
Joined: Sat Apr 13, 2024 5:42 pm

Re: Store the WiFi SSID & password (not at config.h)

Post by jm1704 »

In a file like arduino_secret.h
just the 2 line below
#define SECRET_SSID "ssid"
#define SECRET_PASS "pass"

and include the library in your arduino application
# include <arduino_secret.h>

and variable
const char* ssid = SECRET_SSID;
const char* pass = SECRET_PASS;

User avatar
tonynunan
 
Posts: 10
Joined: Wed Apr 26, 2017 6:58 pm

Re: Store the WiFi SSID & password (not at config.h)

Post by tonynunan »

Have you thought about an SD card?

Most people would be able to create the correct credentials on the card before starting the device by editing the file with a computer.

Post Reply
Please be positive and constructive with your questions and comments.

Return to “Wireless: WiFi and Bluetooth”