String Arduino-无法连接到wifi

String Arduino-无法连接到wifi,string,arduino,char,wifi,String,Arduino,Char,Wifi,我有这个(部分)代码,用于从SD卡上的文本文件中读取ssid和密码。 然后,应使用该数据连接到本地wifi。但它无法连接,一直说“建立WiFi连接…” 我似乎也无法打印我从SD卡上读到的内容。以前,我的readline函数返回了一个字符串。我能够正确打印并验证文件是否按预期读取。然后,由于wifi需要Char*我将代码更改为Char*并且从此不再工作: #include <SD.h> #include <AccelStepper.h> #include <Multi

我有这个(部分)代码,用于从SD卡上的文本文件中读取ssid和密码。 然后,应使用该数据连接到本地wifi。但它无法连接,一直说“建立WiFi连接…”

我似乎也无法打印我从SD卡上读到的内容。以前,我的readline函数返回了一个字符串。我能够正确打印并验证文件是否按预期读取。然后,由于wifi需要Char*我将代码更改为Char*并且从此不再工作:

#include <SD.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <WiFi.h>
File myFile;
char* ssid;
char* password;


void setup() {
  // put your setup code here, to run once:

  pinMode(thetaPos, INPUT);
  pinMode(rhoPos, INPUT);

  Serial.begin(115200);
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);

  }
  Serial.println("initialization done.");
  myFile = SD.open("/config.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    ssid = readLine();
    password = readLine();
    
      Serial.println(ssid);
      Serial.println(password);
  
  connectToNetwork();

    
    // close the file:
    myFile.close();
  } else {
    // file didn't open       
  }
 }
 
void loop() {
}


char* readLine() {
  char* received = "";
  char ch;
  while (myFile.available()) {
    ch = myFile.read();
    if (ch == '\n') {
      return received;
    } else {
      received += ch;
    }
  }
  return received;
}

void connectToNetwork() {
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Establishing connection to WiFi..");
  }
 
  Serial.println("Connected to network");
 
}

那么,错误在哪里呢?这不会像您希望的那样起作用:

char* readLine() {
  char* received = "";         // poiner to \0 somewhere... 
  char ch;
  while (myFile.available()) {
    ch = myFile.read();
    if (ch == '\n') {
      return received;
    } else {
      received += ch;          // move pointer to location ch bytes after...
    }
  }
  return received;
}
您正在使用指针算法—您正在内存中移动指针,并且没有在任何地方存储任何内容。重载
+=char
并允许您将字符连接到其末尾的不是String类

基本上,您可以使用:

String readLine() {
  String received;
  while (myFile.available()) {
    ch = myFile.read();
    if (ch == '\n') {
      return received;
    } else {
      received += ch;
    }
  }
  return received;
}
而且它也可以工作(尽管将字符逐个追加到字符串中不是一个好主意,但通过这种方法,堆内存的碎片化速度非常快)

为了避免其他错误,用法如下:

String ssid = readLine();
String pass = readLine();

Wifi.begin(ssid.c_str(), pass.c_str());

//// definitely do not do something like:
// Wifi.begin(readLine().c_str(), readLine().c_str()); // !!! WRONG !!!
//// as the order of executing parameters isn't specified (and in the GCC it's from the last to the first)

嗯,说得太早了。。很抱歉实际上是Wifi.begin(ssid.c_str(),pass.c_str())不起作用。。如果我用char*替换它们,并将值写入其中,那么我就不能毫无问题地连接..Serial.print(“|”);串行打印(ssid);连续打印(“|”);连续打印(pass);Serial.println(“|”);如果它打印的内容超过| myssid | mypass |,则您忘记了\r\n(windows中的行分隔符)所有内容似乎都是正确的<代码>| Bill Clinternet | testpass |建立WiFi连接。。正在建立与WiFi的连接仍然没有连接..哦,通行证错了。。对不起,它现在起作用了。。。非常感谢。我刚注意到你在最后一节写的关于执行顺序的说明。。我不确定我是否理解这一点。。该计划现在运行良好,我只想尽可能多地学习并理解这一点。你能解释一下你的意思吗?非常感谢。
String ssid = readLine();
String pass = readLine();

Wifi.begin(ssid.c_str(), pass.c_str());

//// definitely do not do something like:
// Wifi.begin(readLine().c_str(), readLine().c_str()); // !!! WRONG !!!
//// as the order of executing parameters isn't specified (and in the GCC it's from the last to the first)