Skip to content

Instantly share code, notes, and snippets.

@neosarchizo
Last active October 28, 2022 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neosarchizo/3aee545ecda45352959c35cb14edea2a to your computer and use it in GitHub Desktop.
Save neosarchizo/3aee545ecda45352959c35cb14edea2a to your computer and use it in GitHub Desktop.
MicroPython - ESP32 WiFi 소켓 통신
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)
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