Skip to content

Instantly share code, notes, and snippets.

@mthri
Last active June 16, 2024 02:40
Show Gist options
  • Save mthri/08a1f895bd0855a3ea2e0abb52c2fd81 to your computer and use it in GitHub Desktop.
Save mthri/08a1f895bd0855a3ea2e0abb52c2fd81 to your computer and use it in GitHub Desktop.
esp8266 socket client and python socket server
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const uint16_t port = 8585;
const char *host = "SERVER-IP";
WiFiClient client;
void setup()
{
Serial.begin(115200);
Serial.println("Connecting...\n");
WiFi.mode(WIFI_STA);
WiFi.begin("USSID", "PASSWORD"); // change it to your ussid and password
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
}
void loop()
{
if (!client.connect(host, port))
{
Serial.println("Connection to host failed");
delay(1000);
return;
}
Serial.println("Connected to server successful!");
client.println("Hello From ESP8266");
delay(250);
while (client.available() > 0)
{
char c = client.read();
Serial.write(c);
}
Serial.print('\n');
client.stop();
delay(5000);
}
import socket
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 8585 ))
s.listen(0)
while True:
client, addr = s.accept()
client.settimeout(5)
while True:
content = client.recv(1024)
if len(content) ==0:
break
if str(content,'utf-8') == '\r\n':
continue
else:
print(str(content,'utf-8'))
client.send(b'Hello From Python')
client.close()
@Martini002
Copy link

thank you, this is what I was looking for

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment