使用ESP8266 / 8285将字符串从Web服务器转换为字节数组

问题描述 投票:0回答:1

我想在ESP8285的flash / EEPROM中存储WiFi凭证和其他一些配置。

我将从网络服务器输入字段中接收字符串。我写了一些测试代码来确定存储和转换是否有效。事实并非如此。当我要存储MAC地址时,我无法弄清楚如何将char数组转换为字节数组,并忽略某些字符,例如“:”。存储SSID,密码短语和通道效果很好,但MAC地址却不能。

这是写入EEPROM时的代码片段:

void writeCredentials() {
  String newSSID = "WRT";
  String newPass = "45568798789098794879784";
  String newBSSID = "3A:79:16:A8:B5:76";
  char buff[sizeof(settings.eeBSSID)];
  byte mac[6];
  char chBuff[3];
  char *ptr;
  newBSSID.toCharArray(buff, newBSSID.length() + 1);
  Serial.println(buff);
  ptr = strtok(buff, ":");
  int index = 0;
  while (ptr != NULL) {
    strcpy(chBuff, ptr);
    Serial.print(" Mac: ");
    Serial.print(chBuff);
    sscanf(&chBuff[2], "%02xd", &mac[index]); //no idea here maybe completely wrong
    index++;
    ptr = strtok(NULL, ":");
  }
  Serial.println("");
  newSSID.toCharArray(settings.eeSSID, newSSID.length() + 1);
  newPass.toCharArray(settings.eePass, newPass.length() + 1);
  for (int i = 0; i < sizeof(mac); i++) {
    Serial.println(char(mac[i]));
  }
  settings.eeChannel = 12345;
  memcpy(settings.eeBSSID, mac, sizeof(mac));
  EEPROM.put(0, settings);
  Serial.println("Settings saved!");
}

出于测试目的,我将字符串放入writeCredentials()函数中。稍后,它们将来自网络服务器文本输入表单。一切都在这样的配置结构中进行管理:

typedef struct WiFi_Settings {
  char eeSSID[32];
  char eePass[32];
  byte eeBSSID[6];
  int eeChannel;
};

WiFi_Settings settings {
  "",
  "",
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  1
};

我费了好几个小时才能将newBSSID转换为byte / uint8_t数组,该数组可以存储在我的结构中,以后将用于ESP的WiFi.begin函数...

使用strtok,sscanf测试了很多变体,并尝试将char数组(存储在buff中)拆分为2个char片段,然后我可以将其转换为字节并将其传递给byte数组,但是每次不起作用。也许有人可以帮助我?

c++ arduino esp8266 arduino-esp8266
1个回答
0
投票

如果我理解正确,您想从网络服务器存储新的SSID和密码。去年我做了一个大项目,自己创建了一个WiFi Manager。我刚刚找到了我创建的库,这就是将新的SSID和密码上传到EEPROM的方法。我希望这可以帮助您解决问题。

© www.soinside.com 2019 - 2024. All rights reserved.