Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import pyb
import network
import machine
import usocket
import utime
spi = pyb.SPI(1)
cs = machine.Pin(pyb.Pin.board.X5, machine.Pin.OUT)
# USR button
btn = machine.Pin(pyb.Pin.board.X17, machine.Pin.IN, machine.Pin.PULL_UP)
def setMacAddress(address):
cs.low()
# address of the register of the mac address
spi.send(0x00)
spi.send(0x09)
# write action
spi.send(0x80)
# 6 byte of datasize
spi.send(0x06)
# mac address
for add in address:
spi.send(add)
# not sure if this needed
utime.sleep(1)
cs.high()
# init network driver
nic = network.WIZNET5K(spi, pyb.Pin.board.X5, pyb.Pin.board.X4)
nic.ifconfig(('192.168.0.10', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
print(nic.ifconfig())
# set mac address
setMacAddress([0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
print(nic.regs())
# http get
def btnpushed(p):
host = 'xxxxxxxxxxxx.com'
path = '/xxxxxxxxxxx.txt'
addr = usocket.getaddrinfo(host, 80)[0][-1]
s = usocket.socket()
s.connect(addr)
s.send(b'GET %s HTTP/1.1\r\nHost: %s\r\n\r\n' % (path, host))
data = s.recv(1000)
s.close()
print(data)
# set interrupt
btn.irq(handler=btnpushed, trigger=machine.Pin.IRQ_FALLING)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment