Last active
June 16, 2024 02:40
-
-
Save mthri/08a1f895bd0855a3ea2e0abb52c2fd81 to your computer and use it in GitHub Desktop.
esp8266 socket client and python socket server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Hi, thanks for the code. how can i include "WiFI.hostByName" command for "Server-IP". Regards
Hi, I don't know
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
Hi, thanks for the code. how can i include "WiFI.hostByName" command for "Server-IP".
Regards