Skip to content

Instantly share code, notes, and snippets.

@neosarchizo
Created November 23, 2022 04:43
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/aebfc054f886b6536b2bcc185d98fae0 to your computer and use it in GitHub Desktop.
Save neosarchizo/aebfc054f886b6536b2bcc185d98fae0 to your computer and use it in GitHub Desktop.
MicroPython - RPi Pico W HTTP 통신하기
from network import WLAN, STA_IF
from time import sleep
import socket
SSID = 'devicemart'
PASSWORD = 'devicemart1'
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else: # wlan.status() == 3
print('connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
ai = socket.getaddrinfo('google.com', 80)
addr = ai[0][-1]
s = socket.socket()
s.connect(addr)
s.send(b'GET / HTTP/1.0\r\n\r\n')
print(s.recv(512))
from network import WLAN, STA_IF
from time import sleep
import urequests
SSID = 'devicemart'
PASSWORD = 'devicemart1'
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else: # wlan.status() == 3
print('connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
r = urequests.get('http://www.google.com')
print(r.content)
r.close()
from network import WLAN, STA_IF
from time import sleep
import urequests
SSID = 'devicemart'
PASSWORD = 'devicemart1'
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else: # wlan.status() == 3
print('connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
r = urequests.get('http://www.raspberrypi.com')
print(r.status_code) # 302 redirect code
r.close()
from network import WLAN, STA_IF
from time import sleep
import urequests
SSID = 'devicemart'
PASSWORD = 'devicemart1'
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
sleep(1)
if wlan.status() != 3:
raise RuntimeError('network connection failed')
else: # wlan.status() == 3
print('connected')
status = wlan.ifconfig()
print('ip = ' + status[0])
r = urequests.get('http://date.jsontest.com')
print(r.json())
r.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment