利用教程中的提示,我编写了一个简单的wifi服务器和客户端,该服务器和客户端仅将通过wifi从串行线(使用USB连接到PC的连接)接收的字符中继到另一个对等方,并相应地使用inc / dec变量(请参见下面的代码)。我使用Arduino IDE(1.8.9)用纯C / C ++编写了此代码。我遇到的问题是该代码仅在重启后才能正常工作(我必须从模块上拔下USB电缆,然后再次插回)。如果仅按模块上的硬件复位按钮,或者在发送新版本的代码后(大多数情况下),一切正常,除了Serial.available()或Serial.read()函数。他们只是看不到通过USB /串行线从PC接收到任何东西。但是重新插入USB并重新连接Putty终端程序(即重新打开电源)可以解决该问题,然后一切又恢复了。重启电源后,它始终可以正常工作,但大多数情况下,在按下RST按钮(或上载新版本)后,该失败。当重置模块而没有完全断电时,ESP串行模块会以某种方式保持卡住或无法正确初始化。服务器和客户端上的行为相同。将模块替换为其他模块-结果相同。我使用昨天通过Arduino开发板管理器下载的最新NodeMCU / ESP开发板设置。 请问您有什么解决办法的提示吗?
代码:
服务器:
#include <ESP8266WiFi.h>
const char* ssid = "my ssid"; //replace this with your WiFi network name
const char* password = "my passwd"; //replace this with your WiFi network password
WiFiServer server(12345);
int a;
void setup()
{
a = 0;
pinMode(D5, OUTPUT); // piezo spkr
delay(1000);
Serial.begin(115200);
delay(1000);
tone(D5, 1720);
Serial.flush(); //irrelevant whether this is here or not
Serial.print("Wifi.begin()");
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
tone(D5, 220);
Serial.println("success!");
Serial.print("Server IP Address is: ");
Serial.println(WiFi.localIP());
delay(1000);
noTone(D5);
server.begin();
}
int read_and_process_packet(WiFiClient *client)
{
char packet_type = client->read();
switch (packet_type)
{
case 'a': a--; Serial.println(a); break;
case 'q': a++; Serial.println(a); break;
case '-': return 0;
}
return 1;
}
void loop() {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("Connected to client");
while (client.connected())
{
if (!read_and_process_packet(&client)) break;
if (Serial.available())
{
char c = Serial.read();
client.write(c);
}
delay(50);
}
client.stop();
Serial.println("Client left");
}
}
delay(1);
}
客户端:
#include <ESP8266WiFi.h>
// Replace these with your WiFi network settings
const char* ssid = "my ssid"; //replace this with your WiFi network name
const char* password = "my passwd"; //replace this with your WiFi network password
int a;
WiFiClient client;
char server_ip[20];
void setup()
{
a = 0;
pinMode(D5, OUTPUT);
delay(1000);
Serial.begin(115200);
delay(1000);
Serial.flush(); //irrelevant whether this is here or not
Serial.print("Enter server IP: ");
char *p = server_ip;
for (int i = 0; i < 4; i++)
{
int ip = read_number();
p = print_num(p, ip);
if (i < 3)
p = print_char(p, '.');
}
Serial.println();
tone(D5, 1720);
Serial.print("Wifi.begin()");
WiFi.begin(ssid, password);
Serial.println();
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
tone(D5, 220);
Serial.println("success!");
Serial.print("IP Address is: ");
Serial.println(WiFi.localIP());
delay(100);
noTone(D5);
Serial.print("connecting to ");
Serial.print(server_ip);
Serial.print(":");
Serial.println(12345);
if (!client.connect(server_ip, 12345))
{
Serial.println("Could not connect to the server.");
while (1);
}
tone(D5, 440, 50);
}
int read_number()
{
int x = 0;
while (1) {
if (Serial.available())
{
int c = Serial.read();
Serial.write(c);
if ((c >= '0') && (c <= '9'))
x = x * 10 + (c - '0');
else return x;
}
delay(100);
}
}
char *print_num(char *p, int num)
{
if (num == 0) *(p++) = '0';
else
{
if (num < 0) { *(p++) = '-'; num = -num; }
long q = 1;
while (q <= num) q *= 10;
while (q > 1)
{
q /= 10;
*(p++) = (num / q) + '0';
num %= q;
}
}
*p = 0;
return p;
}
char * print_char(char *p, int c)
{
*(p++) = c;
*p = 0;
return p;
}
int read_and_process_packet(WiFiClient *client)
{
char packet_type = client->read();
switch (packet_type)
{
case 'a': a--; Serial.println(a); break;
case 'q': a++; Serial.println(a); break;
case '-': return 0;
}
return 1;
}
void loop()
{
if (client.connected()) {
Serial.println("Connected to server");
while (client.connected())
{
if (Serial.available())
{
char c = Serial.read();
client.write(c);
}
if (!read_and_process_packet(&client)) break;
delay(1);
}
client.stop();
}
delay(1);
}