Last active
October 28, 2022 07:26
-
-
Save neosarchizo/3aee545ecda45352959c35cb14edea2a to your computer and use it in GitHub Desktop.
MicroPython - ESP32 WiFi 소켓 통신
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
from network import WLAN, STA_IF | |
from time import time, sleep | |
from usocket import socket | |
wlan = WLAN(STA_IF) | |
wlan.active(True) | |
start_time = time() | |
if not wlan.isconnected(): | |
print('Connecting to network...') | |
wlan.connect('devicemart', 'devicemart1') | |
while not wlan.isconnected(): | |
if time() - start_time > 15: | |
print('Connecting WiFi Timeout!') | |
quit() | |
config = wlan.ifconfig() | |
print(config) | |
print('IP : ' + config[0]) | |
print('SUBNET : ' + config[1]) | |
print('GATEWAY : ' + config[2]) | |
print('DNS : ' + config[3]) | |
s = socket() | |
addr = ('192.168.86.21', 65432) | |
s.connect(addr) | |
while True: | |
s.send('Hello!') | |
sleep(1) |
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 | |
HOST = '192.168.86.21' | |
PORT = 65432 | |
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: | |
s.bind((HOST, PORT)) | |
s.listen() | |
conn, addr = s.accept() | |
with conn: | |
print(f"Connected by {addr}") | |
while True: | |
data = conn.recv(1024) | |
print(f'{addr} : {data}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment