Create a gist now

Instantly share code, notes, and snippets.

Embed
"""
COPYRIGHT
This code is licensed under the MIT license.
<https://opensource.org/licenses/MIT>
Copyright (c) 2017 Switch Science, inc.
Permission is hereby granted, free of charge,
to any person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
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