使用ESP8266在Arduino平台编写程序
试图制作Web配网并保存到EEPROM的程序,遭遇问题:配网成功后(连上提供的WiFi后)调用 write() 存入SSID和PSWD的输出都是正常的,并且调用 readEEPROM() 读取SSID输出结果也是正常的,但读取PSWD的结果就会多出来一个字节,可以给看一下咋回事?
附上不太完整的代码:
初始化部分:
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <EEPROM.h>
#define _DEBUG
#define ssidlenth 24
#define pswdlenth 24
#define ssidstart 0
#define pswdstart ssidstart + ssidlenth
//存储wifi账号密码
String sta_ssid = "";
String sta_pswd = "";
/*---------------------Web配网功能部分------------------------*/
bool wifiGot = false;
const char* AP_ssid = "WiFi配网"; //wifi名字
const char* AP_pswd = "sssssssssss"; //wifi密码
//配网页面代码略
EEPROM读写函数:
String readEEPROM(unsigned int start, unsigned int length) {
String _data = "";
unsigned int i = start;
unsigned int readmax = start + length;
char _char;
for (; i < readmax; i++) {
_char = char(EEPROM.read(i));
if (!_char) {
#ifdef _DEBUG
Serial.println("NonChar");
#endif
break;
}
_data += _char;
#ifdef _DEBUG
Serial.println("Read:");
Serial.println(_char);
#endif
}
#ifdef _DEBUG
Serial.println("Read String:");
Serial.println(_data);
#endif
return _data;
}
void writeEEPROM(String _data, unsigned int start) {
unsigned int length = _data.length();
if (!length) {
#ifdef _DEBUG
Serial.println("Wrint String Lengt h= 0");
#endif
return;
}
#ifdef _DEBUG
Serial.println(_data);
#endif
unsigned int i = start;
unsigned int j = 0;
unsigned int writemax = start + length;
for (; i < writemax; i++, j++) {
EEPROM.write(i, _data[j]);
#ifdef _DEBUG
Serial.println("Write:");
Serial.println(_data[j]);
#endif
}
i++;
EEPROM.write(i, '\0');
EEPROM.commit();
}
连接wifi的函数(包括调用EEPROM读写来存取WIFI信息的函数)
void readWiFi() {
#ifdef _DEBUG
Serial.println("readWiFi");
#endif
sta_ssid = readEEPROM(ssidstart, ssidlenth);
sta_pswd = readEEPROM(pswdstart, pswdlenth);
}
void saveWiFi() {
#ifdef _DEBUG
Serial.println("saveWiFi");
#endif
writeEEPROM(sta_ssid, ssidstart);
writeEEPROM(sta_pswd, pswdstart);
}
void connectWifi(void) { //采用两层循环嵌套,内层为每一次连接计算延时,判断是否成功,外层负责一次次获取wifi信息(通过eeprom或者web配网获得)启动连接,在连接成功后存密码或者失败后调用web配网相关方法
readWiFi();
#ifdef _DEBUG
Serial.println(sta_ssid);
Serial.println(sta_pswd);
#endif
while (true) {
WiFi.disconnect(true);
WiFi.mode(WIFI_STA); //切换为STA模式
WiFi.setAutoConnect(true); //设置自动连接
WiFi.begin(sta_ssid, sta_pswd); //连接上一次连接成功的wifi
#ifdef _DEBUG
Serial.println("");
Serial.print("Connect to wifi");
#endif
int count = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
count++;
if (count > 20) { //如果10秒内没有连上,就开启Web配网 可适当调整这个时间
#ifdef _DEBUG
Serial.println("Need Web!");
#endif
getWiFi();
break; //跳出 防止无限初始化
}
#ifdef _DEBUG
Serial.print(".");
#endif
}
#ifdef _DEBUG
Serial.println("");
#endif
if (WiFi.status() == WL_CONNECTED) { //如果连接上 就输出IP信息 防止未连接上break后会误输出
#ifdef _DEBUG
Serial.println("WIFI Connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //打印esp8266的IP地址
#endif
// server.stop();
// dnsServer.stop();
saveWiFi();
break;
}
}
}
基础结构代码
void setup() {
Serial.begin(115200);
delay(100);
#ifdef _DEBUG
Serial.println("");
#endif
EEPROM.begin(1024);
WiFi.hostname("CYQ's WeatherClock"); //设置ESP8266设备名
connectWifi();
timeClient.begin();
}
void loop() {
if (WiFi.status() != WL_CONNECTED) {
#ifdef _DEBUG
Serial.println("WiFi Break!");
#endif
connectWifi();
return;
}
}
串口打印的信息:
got ssid:testzuo
got password:zuosaiwen123
saveWiFi
//省略大量无效行,总之就是存一位打印一位,都是如下格式,正确输出
Write:
1
Write:
2
Write:
3
readWiFi
z
Read:
u
Read:
o
NonChar //代表读到空白字符,直接结束,不存这一位(因为用的String,不去手动存储结束符号)
Read String:
testzuo
//读密码时就会多出来一位(w)
Read:
3
Read:
w
NonChar