Please help, how I can connect to ESP8266 from PC via python program?
I have this program loaded in 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("Connecting..");
}
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");
}
When I connect to ESP via Putty, Serial monitor prints correctly "Client Connected" (and then data, which I send and after closing Putty "Client disconnected").
But problem is, when I run my Python program:
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 only sometimes register this coming socket...I must run my python program several times to have same result as when I connect to ESP8266 via putty...
Where is problem?
Please help!