Skip to content

secrets #8310

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

Closed
wants to merge 11 commits into from
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ _build/
debug.cfg
debug.svd
debug_custom.json

# Ignore changes in secrets.h
cores/esp32/secrets.h
21 changes: 21 additions & 0 deletions cores/esp32/secrets.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#define SECRETS_WIFI_SSID_1 "example-SSID1"
#define SECRETS_WIFI_PASSWORD_1 "example-password-1"

#define SECRETS_WIFI_SSID_2 "example-SSID2"
#define SECRETS_WIFI_PASSWORD_2 "example-password-2"

#define SECRETS_WIFI_ARRAY_MAX 3 // Number of entries in the array

char SECRET_WIFI_SSID_ARRAY[SECRETS_WIFI_ARRAY_MAX][32] = {
SECRETS_WIFI_SSID_1,
SECRETS_WIFI_SSID_2,
"example-SSID3"
};

char SECRET_WIFI_PASSWORD_ARRAY[SECRETS_WIFI_ARRAY_MAX][32] = {
SECRETS_WIFI_PASSWORD_1,
SECRETS_WIFI_PASSWORD_2,
"example-password-3"
};
58 changes: 58 additions & 0 deletions docs/source/guides/secrets.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#######
Secrets
#######

Why?
----
DRY (Don't Repeat Yourself) - having your passwords in one place is more manageable than changing them manually in each example, or new sketch.
Secure - safely share the code without worrying about accidentally leaving WiFi credentials.

How it works - your SSIDs and passwords are #defined as a plain text constants in a header file located in sketch folder (after you manually create it). This header file can be included in any sketch and the passwords used in there hidden with the constant name.


Setup:
------
1. Locate your sketch folder and create file secrets.h if it does not exist yet.
2. Edit the secrets.h file and input the WiFi credential you are often using.
3. For examples we are using constants `SECRETS_WIFI_SSID_1` and `SECRETS_WIFI_PASSWORD_1`. You can follow the numbering or create your own constant names, for example `WIFI_SSID_HOME` + `WIFI_PWD_HOME`.
4. For multi Access Point usage you can use an array of credential you can expand - either add existing constants, or create a plain text.


Example contents:
-----------------

.. code-block:: c++

#pragma once

#define SECRETS_WIFI_SSID_1 "example-SSID1"
#define SECRETS_WIFI_PASSWORD_1 "example-password-1"

#define SECRETS_WIFI_SSID_2 "example-SSID2"
#define SECRETS_WIFI_PASSWORD_2 "example-password-2"

#define SECRETS_WIFI_ARRAY_MAX 3 // Number of entries in the array

const char SECRET_WIFI_SSID_ARRAY[SECRETS_WIFI_ARRAY_MAX][32] = {
SECRETS_WIFI_SSID_1,
SECRETS_WIFI_SSID_2,
"example-SSID3"
};

const char SECRET_WIFI_PASSWORD_ARRAY[SECRETS_WIFI_ARRAY_MAX][32] = {
SECRETS_WIFI_PASSWORD_1,
SECRETS_WIFI_PASSWORD_2,
"example-password-3"
};


Example usage:
--------------

.. code-block:: c++

#include <WiFi.h>
#include "secrets.h"
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;
WiFi.begin(ssid, password);
5 changes: 3 additions & 2 deletions libraries/WiFi/examples/SimpleWiFiServer/SimpleWiFiServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ ported for sparkfun esp32
*/

#include <WiFi.h>
#include "secrets.h"

const char* ssid = "yourssid";
const char* password = "yourpasswd";
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

WiFiServer server(80);

Expand Down
5 changes: 3 additions & 2 deletions libraries/WiFi/examples/WiFiAccessPoint/WiFiAccessPoint.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include "secrets.h"

#define LED_BUILTIN 2 // Set the GPIO pin where you connected your test LED or comment this line out if your dev board has a built-in LED

// Set these to your desired credentials.
const char *ssid = "yourAP";
const char *password = "yourPassword";
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

WiFiServer server(80);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// Button is attached between GPIO 0 and GND and modes are switched with each press

#include "WiFi.h"
#define STA_SSID "your-ssid"
#define STA_PASS "your-pass"
#include "secrets.h"

const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

#define AP_SSID "esp32"

enum { STEP_BTON, STEP_BTOFF, STEP_STA, STEP_AP, STEP_AP_STA, STEP_OFF, STEP_BT_STA, STEP_END };
Expand All @@ -35,7 +38,7 @@ void onButton(){
break;
case STEP_STA://STA Only
Serial.println("** Starting STA");
WiFi.begin(STA_SSID, STA_PASS);
WiFi.begin(ssid, password);
break;
case STEP_AP://AP Only
Serial.println("** Stopping STA");
Expand All @@ -45,15 +48,15 @@ void onButton(){
break;
case STEP_AP_STA://AP+STA
Serial.println("** Starting STA");
WiFi.begin(STA_SSID, STA_PASS);
WiFi.begin(ssid, password);
break;
case STEP_OFF://All Off
Serial.println("** Stopping WiFi");
WiFi.mode(WIFI_OFF);
break;
case STEP_BT_STA://BT+STA
Serial.println("** Starting STA+BT");
WiFi.begin(STA_SSID, STA_PASS);
WiFi.begin(ssid, password);
btStart();
break;
case STEP_END://All Off
Expand Down
8 changes: 6 additions & 2 deletions libraries/WiFi/examples/WiFiClient/WiFiClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/

#include <WiFi.h>
#include "secrets.h"

const char* ssid = "your-ssid"; // Change this to your WiFi SSID
const char* password = "your-password"; // Change this to your WiFi password
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

//const char* ssid = "your-ssid"; // Change this to your WiFi SSID
//const char* password = "your-password"; // Change this to your WiFi password

const char* host = "api.thingspeak.com"; // This should not be changed
const int httpPort = 80; // This should not be changed
Expand Down
3 changes: 2 additions & 1 deletion libraries/WiFi/examples/WiFiClientBasic/WiFiClientBasic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <WiFi.h>
#include <WiFiMulti.h>
#include "secrets.h"

WiFiMulti WiFiMulti;

Expand All @@ -14,7 +15,7 @@ void setup()
delay(10);

// We start by connecting to a WiFi network
WiFiMulti.addAP("SSID", "passpasspass");
WiFiMulti.addAP(SECRETS_WIFI_SSID_1, SECRETS_WIFI_PASSWORD_1);

Serial.println();
Serial.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

*/
#include <WiFi.h>
#include "secrets.h"

const char* ssid = "your-ssid";
const char* password = "your-password";
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

int btnGPIO = 0;
int btnState = false;
Expand Down
6 changes: 3 additions & 3 deletions libraries/WiFi/examples/WiFiClientEvents/WiFiClientEvents.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
*/

#include <WiFi.h>
#include "secrets.h"

const char* ssid = "your-ssid";
const char* password = "your-password";

const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

void WiFiEvent(WiFiEvent_t event)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
*/

#include <WiFi.h>
#include "secrets.h"

const char* ssid = "your_network_name";
const char* password = "your_network_password";
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;
const char* host = "example.com";
const char* url = "/index.html";

Expand Down
9 changes: 5 additions & 4 deletions libraries/WiFi/examples/WiFiIPv6/WiFiIPv6.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "WiFi.h"
#include "secrets.h"

#define STA_SSID "**********"
#define STA_PASS "**********"
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;
#define AP_SSID "esp32-v6"

static volatile bool wifi_connected = false;
Expand All @@ -19,7 +20,7 @@ void wifiOnConnect(){
void wifiOnDisconnect(){
Serial.println("STA Disconnected");
delay(1000);
WiFi.begin(STA_SSID, STA_PASS);
WiFi.begin(ssid, password);
}

void wifiConnectedLoop(){
Expand Down Expand Up @@ -109,7 +110,7 @@ void setup(){
WiFi.onEvent(WiFiEvent);
WiFi.mode(WIFI_MODE_APSTA);
WiFi.softAP(AP_SSID);
WiFi.begin(STA_SSID, STA_PASS);
WiFi.begin(ssid, password);
}

void loop(){
Expand Down
10 changes: 5 additions & 5 deletions libraries/WiFi/examples/WiFiMulti/WiFiMulti.ino
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/*
* This sketch trys to Connect to the best AP based on a given list
* This sketch tries to Connect to the best AP based on a given list
*
*/

#include <WiFi.h>
#include <WiFiMulti.h>
#include "secrets.h"

WiFiMulti wifiMulti;

Expand All @@ -13,10 +14,9 @@ void setup()
Serial.begin(115200);
delay(10);

wifiMulti.addAP("ssid_from_AP_1", "your_password_for_AP_1");
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");

for(int i = 0; i < SECRETS_WIFI_ARRAY_MAX; ++i){
wifiMulti.addAP(SECRET_WIFI_SSID_ARRAY[i], SECRET_WIFI_PASSWORD_ARRAY[i]);
}
Serial.println("Connecting Wifi...");
if(wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@
*/
#include <WiFi.h>
#include <WiFiMulti.h>
#include "secrets.h"

WiFiMulti wifiMulti;

//how many clients should be able to telnet to this ESP32
#define MAX_SRV_CLIENTS 1
const char* ssid = "**********";
const char* password = "**********";
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
Expand Down
9 changes: 5 additions & 4 deletions libraries/WiFi/examples/WiFiUDPClient/WiFiUDPClient.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/
#include <WiFi.h>
#include <WiFiUdp.h>
#include "secrets.h"

// WiFi network name and password:
const char * networkName = "your-ssid";
const char * networkPswd = "your-password";
// WiFi network name (SSID) and password:
const char* ssid = SECRETS_WIFI_SSID_1;
const char* password = SECRETS_WIFI_PASSWORD_1;

//IP address to send UDP data to:
// either use the ip address of the server or
Expand All @@ -26,7 +27,7 @@ void setup(){
Serial.begin(115200);

//Connect to the WiFi network
connectToWiFi(networkName, networkPswd);
connectToWiFi(ssid, password);
}

void loop(){
Expand Down