ESP8266/Py: Difference between revisions

From Noisebridge
Jump to navigation Jump to search
ESP8266 (talk | contribs)
#B0B 槿
ESP8266 (talk | contribs)
install #00A Ⅹ
Line 1: Line 1:
microPython on ESP8266
microPython on ESP8266
== update firmware ==
<pre>
esptool --port /dev/ttyUSB0 erase_flash
esptool --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20161110-v1.8.6.bin
</pre>


== blink/wink ==
== blink/wink ==

Revision as of 16:46, 9 December 2016

microPython on ESP8266

update firmware

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

blink/wink

>>> 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()

in/out w/weather

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

>>> 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)