ESP8266/Py

From Noisebridge Wiki
Jump to navigation Jump to search

microPython on ESP8266

update firmware[edit]

Download latest "dirty" build, made at noisebridge, File:Esp8266-dirty-20170119-green.dat

https://github.com/nubcore/micropython/tree/%EB%88%95

esptool --port /dev/ttyUSB0 erase_flash
esptool --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20161110-v1.8.6.bin 


random blinks[edit]

import time
from machine import Pin
from neopixel import NeoPixel

pin = Pin(5, Pin.OUT)
np = NeoPixel(pin, 9)

def randomByte():
    return int.from_bytes(uos.urandom(1), 'little')

def randomPixel():
    return [randomByte(), randomByte(), randomByte()]

def loop():
    while True:
        for j in range(9):
            np[j] = randomPixel()
            np.write()
            time.sleep_ms(round(randomByte()/10))

loop()

blink/wink[edit]

>>> import machine
>>> led = machine.Pin(2, machine.Pin.OUT)
>>> led.high()
>>> led.low()
>>> from time import sleep
>>> def blink():
...     led.low()
...     sleep(0.5)
...     led.high()
... 
>>> blink()
>>> def wink():
...     led.low()
...     sleep(0.1)
...     led.high()
...     sleep(0.2)
...     led.low()
...     sleep(0.1)
...     led.high()
... 
>>> wink()

#505[edit]

#050 green strip
#005 blue

import time
from machine import Pin, PWM, Timer
from neopixel import NeoPixel

NEO_PIN = const(3)
NEO_LENGTH = const(60)

TIMER_INTERVAL = const(100)

blue = PWM(Pin(2))
blue.duty(512)

color = [14, 0, 32]

blue.duty(1023)
time.sleep(0.2)
blue.duty(0)
time.sleep(0.1)
blue.duty(999)

neo = NeoPixel(Pin(NEO_PIN), NEO_LENGTH)

t = Timer(-1)

def stripColor():
	global neo, color
	for x in range(0, NEO_LENGTH):
		if uos.urandom(1)[0] < 10:
			color[0] = uos.urandom(1)[0] // 2
			neo[x] = color
	neo.write()

def update(_t):
	stripColor()

def reboot():
	import machine
	time.sleep(2)
	machine.reset()

t.init(period=TIMER_INTERVAL, mode=Timer.PERIODIC, callback=update)
stripColor()

in/out w/weather[edit]

import dht
import machine
from machine import Pin
from time import sleep

d3 = machine.Pin(3, machine.Pin.OUT)
d0 = machine.Pin(0, machine.Pin.IN)
d2 = machine.Pin(2, machine.Pin.IN)
d4 = machine.Pin(4, machine.Pin.OUT)
d5 = machine.Pin(5, machine.Pin.OUT)

d = dht.DHT11(machine.Pin(1))

def callback(p):
 if p == d2:
  if d4.value() == 0:
   d4.high()
  else:
   d4.low()
  sleep(1)
  beep()
 elif p == d0:
  if d5.value() == 0:
   d5.high()
  else:
   d5.low()
  sleep(1)
  beep()
  beep()

d2.irq(trigger=Pin.IRQ_FALLING, handler=callback)
d0.irq(trigger=Pin.IRQ_FALLING, handler=callback)

def weather():
 d.measure()
 t = d.temperature()
 h = d.humidity()
 print(t, h)

def beep():
 d3.high()
 sleep(.1)
 d3.low()
 sleep(.2)

beep()
beep()
beep()

PS2'ish[edit]

>>> d0 = machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP)
>>> d1 = machine.Pin(5, machine.Pin.IN, machine.Pin.PULL_UP)
>>> d0.value()
>>> 
>>> def callback(p):
...     print(p, p.value())
... 
>>> from machine import Pin
>>> d0.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=callback)
>>> d1.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=callback)
>>> import machine
>>> from machine import Pin
>>> import utime
>>> def callback(p):
...     print(utime.ticks_us())
... 
>>> d0 = machine.Pin(4, machine.Pin.IN)
>>> d0.irq(trigger=Pin.IRQ_FALLING, handler=callback)
<IRQ>
>>> 

'SPACE'
>>> 161328207
161328513
161328708
161328900
161405882
161406119
161406266
161406458
161406648
161406845
161408507
161408654
161408849
161409041
161409239
161409433

'SPACE'
165145125
165145405
165145596
165145797
165145992
165211956
165212194
165212385
165212586
165212782
165212928
165214540
165214737
165214934
165215129
165215276
165215469

'SPACE'
167842588
167842838
167843033
167843180
167843373
167920437
167920681
167920877
167921023
167921216
167921407
167923024
167923219
167923370
167923581
167923772
167923969
>>> 



>>> import machine
>>> from machine import Pin
>>> import utime
>>> def callback(p):
...     print(utime.ticks_us(), p.value(), p)
... 
>>> d0 = machine.Pin(4, machine.Pin.IN)
>>> d1 = machine.Pin(5, machine.Pin.IN)
>>> d0.irq(trigger=Pin.IRQ_FALLING, handler=callback)
<IRQ>
>>> d1.irq(trigger=Pin.IRQ_FALLING | Pin.IRQ_RISING, handler=callback)
<IRQ>
[SPACE]
>>> 210770016 1 Pin(5)
210770569 0 Pin(4)
210770865 1 Pin(5)
210771213 1 Pin(4)
210771514 1 Pin(5)
210847670 0 Pin(5)
210848033 1 Pin(4)
210848336 1 Pin(4)
210848682 1 Pin(5)
210848986 1 Pin(4)
210850323 1 Pin(5)
210850627 1 Pin(4)
210850921 0 Pin(5)
210851268 0 Pin(4)
210852497 1 Pin(5)

[SPACE]
214292169 0 Pin(5)
214292636 1 Pin(4)
214292932 1 Pin(5)
214293280 0 Pin(4)
214293580 1 Pin(5)
214402831 0 Pin(5)
214403247 0 Pin(4)
214403540 1 Pin(5)
214403890 0 Pin(4)
214405434 1 Pin(5)
214405740 0 Pin(4)
214406038 0 Pin(5)
214406336 0 Pin(4)
214406678 1 Pin(5)

[Caps]
>>> 223902923 1 Pin(5)
223903519 0 Pin(4)`
223903810 1 Pin(5)
223904113 1 Pin(4)
223904456 1 Pin(5)
223964179 0 Pin(5)
223964508 0 Pin(4)
223964806 0 Pin(4)
223965101 1 Pin(5)
223965451 1 Pin(4)
223966912 0 Pin(5)
223967413 0 Pin(4)
223967746 1 Pin(5)
223968047 0 Pin(4)
223968997 1 Pin(5)

[U]
>>> 231044948 1 Pin(5)
231045553 0 Pin(4)
231045848 1 Pin(5)
231046203 1 Pin(4)
231133566 0 Pin(5)
231133897 1 Pin(4)
231134194 0 Pin(4)
231134492 1 Pin(5)
231134842 1 Pin(4)
231136321 1 Pin(5)
231136711 0 Pin(4)
231137006 1 Pin(5)
231137305 0 Pin(4)
231138390 1 Pin(5)

[U]
>>> 232928590 1 Pin(5)
232929128 1 Pin(4)
232929436 1 Pin(5)
232929735 1 Pin(4)
232930084 1 Pin(5)
233011829 0 Pin(5)
233012233 1 Pin(4)
233012527 1 Pin(5)
233012829 0 Pin(4)
233014394 0 Pin(5)
233014699 0 Pin(4)
233014994 0 Pin(5)
233015294 0 Pin(4)
233015587 1 Pin(5)

[U]
>>> 234233385 1 Pin(5)
234233821 0 Pin(4)
234234119 1 Pin(5)
234234418 0 Pin(4)
234234713 1 Pin(5)
234311186 0 Pin(5)
234311583 0 Pin(4)
234311878 1 Pin(5)
234312179 0 Pin(4)
234313706 0 Pin(5)
234314064 0 Pin(4)
234314362 1 Pin(5)
234314665 0 Pin(4)
234314959 1 Pin(5)