我正在使用ESP8266。我被抓到了一个地方。如果你能帮助我,我会很高兴的。
我想做的项目是,ESP8266将连接到调制解调器,在连接后,它将开始作为接入点进行广播。从我的电脑我也想连接到ESP8266并发送修改数据。
当我用WiFi.mode(WIFI_AP_STA); /*1*/
代码插入ESP8266时,ESP调制解调器没有连接。所以我无法将数据从计算机发送到修改器。
当我在//WiFi.mode(WIFI_STA); /*2*/
代码中运行ESP8266时,它会连接到我的机器。我可以从我的计算机连接到ESP但它不发送数据。过了一会儿,我与调制解调器的连接断开了。
这是我想要问的问题。如何在AP和STA模式下定期运行ESP8266?
#include <ESP8266WiFi.h>
const char* ssid = "Stationssid"; //agin ssid
const char* password = "1234567890";//agin password
const char* host="192.168.4.1"; // hosted host
const char* apssid = "APssid"; // I will broadcast the ssid name of the network
const char* appassword = "1234567890"; // I will broadcast the encryption of the network
String cgv; // my variable to read the data from the client
int k;
const int port=5001; // port i am connected to
WiFiServer server(80); // AP server description
WiFiClient clientSTA; // client mod for station mod
WiFiClient clientAP; // Client name for AP mode
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_AP_STA); /*1*/ //ESP8266 works in both AP mode and station mode
//WiFi.mode(WIFI_STA); /*2*/ // ESP8266 works in station mode
WiFi.begin(ssid, password); // given the network
Serial.print(ssid);
Serial.print("connecting to ");
while (WiFi.status() != WL_CONNECTED) {
// not connected to the network
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print(ssid);
Serial.println(" Connected to WiFi address.");
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // Printing IP address
Serial.print("Signal Power: ");
Serial.println(WiFi.RSSI()); // printing signal strength
clientSTA.connect(host,port);
Serial.println("setup settings made.");
}
void loop() {
if(!clientSTA.connect(host,port)) {
// If there is no connect from host and port given
Serial.println("No Station connection");
k=0;
return;
}
while(clientSTA.connected()) // while in station mode
{
if(k==0)
{
Serial.println("Station connection available");
// WiFi.mode(WIFI_AP_STA); /*setup for wifi mode 2 * / / If WiFi is in modem station mode, then it will work in both modes after connect
WiFi.softAP(apssid, appassword); // Start AP mode
IPAddress myIP = WiFi.softAPIP(); // IP address for AP mode
Serial.print("AP IP address: ");
Serial.println(myIP);
clientAP=server.available(); // Replace client mode of AP mode with server
server.begin(); // server start
k=1;
}
while(clientSTA.available()) {
// If data is coming from the station
Serial.println("Data is coming from the station");
cgv=clientSTA.readStringUntil('\r'); // read incoming data
Serial.print("Data: ");
Serial.println(cgv);
clientSTA.flush(); // clear clientSTA
cgv="";
if(clientAP) {
// If clientAP is active
Serial.println("There is an AP link");
clientAP.print(cgv); // send incoming data to clientAP
}
}
if(clientAP.connected()) {
// If clientAP is connected
while(clientAP.available()) {
// If the clientAP da data is coming
Serial.println("Data comes from access point");
cgv=clientAP.readStringUntil('\r'); // read incoming data
Serial.print("AP data: ");
Serial.println(cgv);
clientAP.flush(); // clear clientAP
clientSTA.print(cgv); // send incoming data to station
cgv="";
}
}
}
}
这是因为当你的softAP通道在1时,当你的AP +站通道移动到9.所以tcp连接被重置。你的问题已经在这里讨论了LINK
设置WiFi.mode(WIFI_AP_STA);
,离开和if(WiFi.status() !=WL_CONNECTED)
与虚空循环。 STA通常不会出现创建工作站的问题,但在WiFi.h lib的某些版本上,没有重新连接工作功能,请尝试一下,看看它是否有帮助!