请帮助,我如何通过python程序从PC连接到ESP8266?
我在 ESP8266 中加载了这个程序:
代码:
全选#include "ESP8266WiFi.h"
const char* ssid = "home_wifi";
const char* password = "12345678";
WiFiServer wifiServer(5684);
byte message_buffer[10];
int data_index;
int client_count;
void setup() {
Serial.begin(115200);
delay(1000);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connec
ting..");
}
Serial.print("Connected to WiFi. IP:");
Serial.println(WiFi.localIP());
wifiServer.begin();
client_count = 0;
}
void loop()
{
WiFiClient client = wifiServer.available();
//Serial.println("čekám");
while(!(client = wifiServer.available())){}
if(client.connected())
{
Serial.println("Client Connected");
}
while(client.connected()){
while(client.available()>0){
// read data from the connected client
Serial.write(client.read());
}
//Send Data to connected client
while(Serial.available()>0)
{
client.write(Serial.read());
}
}
client.stop();
Serial.println("\nClient disconnected");
}
当我通过 Putty 连接到 ESP 时,串行监视器会正确打印“Client Connected”(然后是我发送的数据,并在关闭 Putty 后打印“Client disconnected”)。
但问题是,当我运行我的 Python 程序时:
代码:
全选import socket
HOST = '192.168.1.6' # The server's hostname or IP address
PORT = 5684
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
s.close()
ESP8266 有时只注册这个即将到来的套接字......我必须多次运行我的 python 程序才能得到与我通过腻子连接到 ESP8266 时相同的结果......
问题在哪里?