btree - simple BTree databaseframebuf - Frame buffer manipulationmachine - functions related to the hardwaremicropython - access and control MicroPython internalsnetwork - network configurationucryptolib - cryptographic ciphersuctypes - access binary data in a structured wayesp
In [35]:
%serialconnect
In [36]:
import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True) # activate station interface
#sta_if.connect('<your ESSID>', '<your password>')
if sta_if.isconnected():
ip_address, netmask, gateway, dns = sta_if.ifconfig()
print('IP: {}, netmask: {}, gateway: {}, DNS: {}'.format(*sta_if.ifconfig()))
else:
'Not Connected'
In [4]:
ap_if = network.WLAN(network.AP_IF)
print(ap_if.active()) # Activate access point.
ap_if.active(False) # Disable if not using.
In [5]:
import socket
def http_get(url):
_, _, host, path = url.split('/', 3)
addr = socket.getaddrinfo(host, 80)[0][-1]
s = socket.socket()
s.connect(addr)
s.send(bytes('GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % (path, host), 'utf8'))
while True:
data = s.recv(100)
if data:
print(str(data, 'utf8'), end='')
else:
break
s.close()
http_get('http://micropython.org/ks/test.html')
In [6]:
import machine
pins = [machine.Pin(i, machine.Pin.IN) for i in (0, 2, 4, 5, 12, 13, 14, 15)]
html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 Pins</title> </head>
<body> <h1>ESP8266 Pins</h1>
<table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
</body>
</html>
"""
import socket
addr = socket.getaddrinfo('0.0.0.0', 8081)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)
print('listening on', addr)
while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
rows = ['<tr><td>%s</td><td>%d</td></tr>' % (str(p), p.value()) for p in pins]
response = html % '\n'.join(rows)
cl.send(response)
cl.close()
ModuleNotFoundError: No module named 'prompt_toolkit.formatted_text'
In [60]:
%serialconnect
# May need to allow permission:
# sudo chmod 777 /dev/ttyUSB0
# Could also add your user to the dialout group
# sudo usermod -a -G dialout your_username
In [61]:
%lsmagic
# Common commands:
# %rebootdevice
# %sendtofile
# %disconnect
# %sendtofile yourfilename.py
In [46]:
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('Student', 'Improving') # connect to an AP
wlan.config('mac') # get the interface's MAC adddress
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point
In [63]:
# Simple demo.
import machine
from machine import Pin
import dht
from time import sleep
motion = Pin(14, Pin.IN, Pin.PULL_UP)
light = Pin(12, Pin.IN, Pin.PULL_UP)
hum = dht.DHT22(Pin(13))
blue = Pin(0, Pin.OUT)
green = Pin(4, Pin.OUT)
red = Pin(5, Pin.OUT)
In [64]:
def blink(led, loops=1, delay=0.25):
for _ in range(loops):
led.on()
sleep(delay)
led.off()
sleep(delay)
def main():
while True:
if light.value():
blue.on()
else:
blue.off()
if motion.value():
green.on()
else:
green.off()
hum.measure()
if hum.temperature() > 26 or hum.humidity() > 50:
red.on()
else:
red.off()
sleep(2)
main() # To end script, interrupt kernel (or press 'Esc' twice and 'i' twice).
In [65]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
start = time.ticks_us()
for i in range(loops):
blue.on()
blue.off()
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / loops, loops / diff * 1e3))
In [66]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
def blink_me(num):
for i in range(num):
blue.on()
blue.off()
def time_me(func, num):
start = time.ticks_us()
func(num)
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / num, num / diff * 1e3))
time_me(blink_me, loops)
In [67]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
def blink_me(num):
for i in range(num):
blue.on()
blue.off()
def time_me(func, num):
start = time.ticks_us()
func(num)
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / num, num / diff * 1e3))
time_me(blink_me, loops)
In [69]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
def blink_me(num):
num //= 8
on = blue.on
off = blue.off
r = range(num)
for i in r:
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
In [70]:
# continued from above...
def time_me(func, num):
start = time.ticks_us()
func(num)
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / num, num / diff * 1e3))
time_me(blink_me, loops)
In [71]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
@micropython.native
def blink_me(num):
num //= 8
on = blue.on
off = blue.off
r = range(num)
for i in r:
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
on()
off()
In [72]:
# continued from above...
def time_me(func, num):
start = time.ticks_us()
func(num)
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / num, num / diff * 1e3))
time_me(blink_me, loops)
In [ ]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
@micropython.viper
def blink_me(num:int):
num //= 8
p = ptr32(0x60000328)
for i in range(num)
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
p[0] = 1 << 4 # High
p[1] = 1 << 4 # Low
In [ ]:
# continued from above...
def time_me(func, num):
start = time.ticks_us()
func(num)
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / num, num / diff * 1e3))
time_me(blink_me, loops)
In [ ]:
import time, machine
blue = machine.Pin(0, machine.Pin.OUT)
loops = 200_000
@micropython.asm_thumb
def blink_me(r0):
lsr(r0, r0, 3)
movwt(r1, 0x60000328)
mov(r2, 1 << 4)
lable(loop)
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
strh(r2, [r1, 0]) # High
strh(r2, [r1, 2]) # Low
In [ ]:
# continued from above...
def time_me(func, num):
start = time.ticks_us()
func(num)
end = time.ticks_us()
diff = time.ticks_diff(end, start)
temp = '{:5.3f} sec, {:6.3f} usec/blink, {:8.2f} kblinks/sec'
print(temp.format(diff * 1e-6, diff / num, num / diff * 1e3))
time_me(blink_me, loops)
In [32]:
help("modules")
In [31]:
import machine
help(machine)
In [ ]:
def do_connect():
import network
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('<essid>', '<password>')
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
In [89]:
help(sta_if)
In [90]:
[print(i) for i in sta_if.scan()]
In [47]:
help(network)
In [56]:
%rebootdevice
In [ ]: