我试图让我的 ESP8266 将 AP 名称设置为Stand
+ MAC 地址减去分号,例如Stand5CCF7F238734
.
我写的GetMyMacAddress()
函数显然是有效的,串行输出表明了这一点。
每次我尝试将 String 或 char 变量传递给wifiManager.autoConnect()
我都会遇到编译器错误。即使头文件标识了字符串类型。
如果我通过macStr
或*macStr
从 'char' 到 '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