今までスケッチにWiFiのSSIDやパスワードをベタ書きしていましたが、スケッチを書き換えなくてもWiFiの接続先を変えたい場合があるかもしれません。そこで、SDカードにあるファイルを読み込んでWiFiに接続するスケッチを書いてみました。
1:材料
・ESP32 DevKitC
・マイクロSDカードスロットDIP化キット
・壊れても良いようなmicroSDカード
2:配線
こちらの真ん中あたりにある画像の通りにESP32とSDカードスロットのピンを接続します。
github.com
2023/01/31 追記:以下が動いた回路図
3:SDカードにSSID、パスワードを記述したファイルを配置
ファイル名を「pass.txt」として以下のようなファイルを作成し、SDカードの直下に配置します。
・pass.txt
ssidhogehoge passwordhoge ※1行目がssid、2行目がpass
記述形式としては、1行目にSSID、2行目にパスワード、3行目は特に何も記述しなくて良いですが3行目は必ず作成しておいてください。
4:スケッチ
以下のスケッチをESP32に書き込んで実行すると、pass.txtに書かれたWiFiに接続できます。
#include <WiFi.h> #include "FS.h" #include "SD.h" #include "SPI.h" // SSIDとPASSを渡すための構造体 typedef struct { String ssidInfo; String passInfo; } WifiInfo; const char* path = "/pass.txt"; void setup() { Serial.begin(115200); // SDカードがマウントされているかの確認 if(!SD.begin()){ Serial.println("Card Mount Failed"); while (1) {} } // カードタイプの取得 uint8_t cardType = SD.cardType(); if(cardType == CARD_NONE){ Serial.println("No SD card attached"); while (1) {} } // テキストファイルの読み込み WifiInfo result = readFileText(SD, path); // 設定された値が正しいかどうかを判定 if (result.ssidInfo == NULL || result.ssidInfo == "") { Serial.println("read file error!"); while (1) {} } else { connectWifi(stringTochar(result.ssidInfo), stringTochar(result.passInfo)); } } void loop() { } /** * SDカードの中にあるファイルを読み込んで中のテキストを抽出する関数 */ WifiInfo readFileText(fs::FS &fs, const char * path){ String result[] = {"", ""}; WifiInfo resultInfo = {result[0], result[1]}; // ファイルを開く File file = fs.open(path); if (!file){ Serial.printf("file: %s ", path); Serial.println("Failed to open file for reading"); return resultInfo; } // ファイルの中身をテキストとして読み込む int cnt = 0; while(file.available()){ result[cnt] = result[cnt] + char(file.read()); if (result[cnt].indexOf('\n') != -1) { result[cnt] = result[cnt].substring(0, result[cnt].length()-1); cnt++; if (cnt == 3) { break; } } } file.close(); resultInfo.ssidInfo = result[0]; resultInfo.passInfo = result[1]; return resultInfo; } /* Wifi接続関数 */ void connectWifi(char* ssid, char* password) { Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } /** * 文字列をchar*に変換する関数 */ char* stringTochar(String command){ if(command.length()!=0){ char *p = const_cast<char*>(command.c_str()); return p; } }
pass.txtを書き換えるだけで別のWiFiにも接続できるので、スケッチを書き換えるよりかは面倒が少なくなったと思います。ただセキュリティ面を考えると、一度ESP32の不揮発性領域に書き込んでおくのが一番安全な気がします。
・参考資料
github.com
coderwall.com