我試圖讓我的ESP8266設置AP名稱爲Stand
+ MAC地址減去分號,如Stand5CCF7F238734
。如何將一個變量傳遞給wifiManager.autoConnect()來命名AP
我寫的GetMyMacAddress()
函數顯然工作,串口輸出顯示。
每當我嘗試將字符串或字符變量傳遞給wifiManager.autoConnect()
時,我都會收到編譯器錯誤。即使頭文件標識了字符串類型。
如果我通過macStr
或*macStr
從 '字符' 到 '爲const char *'
無效的轉換[-fpermissive]
如果我通過ap2
(字符串型)獲得:
呼叫'WiFiManager :: autoConnect(String &)'
沒有匹配的功能
我的代碼:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>
String ap = "Stand";
String ap2;
uint8_t mac[6];
char const macStr[19] = {0};
void setup() {
Serial.begin(115200);
WiFiManager wifiManager; //WiFiManager -- Local intialization.
ap2 = ap + GetMyMacAddress();
//std::string ap2;
char *macStr = new char[ap2.length()+ 1 ];
strcpy(macStr, ap2.c_str());
//fetches ssid and pass from eeprom and tries to connect
//if connect fails it starts an access point with the specified name
//here "AutoConnectAP" and goes into a loop awaiting configuration
wifiManager.autoConnect("Stand");
//or use this for auto generated name ESP + ChipID
//wifiManager.autoConnect();
//if you get here you have connected to the WiFi
Serial.println("connected...yeey :)");
Serial.print("ap2"); Serial.print(" "); Serial.print(ap2); Serial.println(" String");
Serial.print("macStr"); Serial.print(" "); Serial.print(macStr); Serial.println(" Char");
}
void loop() {
}
String GetMyMacAddress()
{
uint8_t mac[6];
char macStr[18] = {0};
WiFi.macAddress(mac);
sprintf(macStr, "%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); // no :'s
// sprintf(macStr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); // with :'s
return String(macStr);
}
連接後,串行輸出:
connected...yeey :)
ap2 Stand5CCF7F238734 String
macStr Stand5CCF7F238734 Char
怎麼樣'wifiManager.autoConnect(ap2.c_str(),NULL);'? –