Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <style>code{white-space:pre;}</style>
- <h1>MICROPYTHON ESP8266</h1>
- <h2>Install</h2>
- <p>Install esptool.<br>
- <code>$ pip install esptool</code>
- <p>Erase flash.<br>
- <code>$ esptool.py --port /dev/ttyUSB0 erase_flash</code>
- <p>Deploy firmware.<br>
- <code>$ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin</code><br>
- (add -fm dio if needed)
- <p>Connect USB-to-UART to board (GPIO1=Tx, GPIO3=Rx, baud=115200) and to computer.<br>
- Connect computer wifi to Micropython-xxxxxx (password: micropythoN)
- <p>Check firmware.<br>
- <code>$ picocom /dev/ttyUSB0 -b115200
- >>> import esp
- >>> esp.check_fw()</code>
- <h2>WebREPL</h2>
- <p>Download WebREPL from https://github.com/webrepl
- <p>Run this and follow the instructions.<br>
- <code>>>> import webrepl_setup</code>
- <p>Open WebREPL, connect to module (IP address 192.168.4.1), enter the WebREPL password.
- <h2>Example</h2>
- <code>>>> import machine
- >>> pin = machine.Pin( 2, machine.Pin.OUT )
- >>> pin.on()
- >>> pin.off()
- >>> def toggle(p):
- ... p.value( not p.value() )
- ...
- >>> import time
- >>> while True:
- ... toggle(pin)
- ... time.sleep_ms( 500 )
- ...</code><br>
- Ctrl+C to break
- <h2>Filesystem</h2>
- <p>Read/write file<br>
- <code>>>> f = open( 'data.txt', 'w' )
- >>> f.write( 'some data' )
- 9
- >>> f.close()
- >>> f = open( 'data.txt' )
- >>> f.read()
- 'some data'
- >>> f.close()</code>
- <p>Directories<br>
- <code>>>> import os
- >>> os.listdir()
- ['boot.py', 'port_config.py', 'data.txt']
- >>> os.mkdir( 'dir' )
- >>> os.remove( 'data.txt' )</code>
- <p>You can create boot.py and/or main.py files for start up script.
- <h2>Networking</h2>
- <p>Station and access point.<br>
- <code>>>> import network
- >>> sta_if = network.WLAN( network.STA_IF )
- >>> ap_if = network.WLAN( network.AP_IF )
- >>> sta_if.active()
- False
- >>> ap_if.active()
- True
- >>> ap_if.ifconfig()
- ('192.168.4.1', '255.255.255.0', '192.168.4.1', '8.8.8.8')</code><br>
- It is IP address, netmask, gateway, and DNS.
- <p>Enable station interface.<br>
- <code>>>> sta_if.active(True)
- >>> sta_if.connect( '<your ssid>', '<your password>' )
- >>> sta_if.isconnected()
- >>> sta_if.active( False )
- >>> def do_connect():
- ... import network
- ... sta_if = network.WLAN( network.STA_IF )
- ... if not sta_if.isconnected():
- ... print( 'Connecting...' )
- ... sta_if.active( True )
- ... sta_if.connect( 'test', 'wifiwifi' )
- ... while not sta_if.isconnected():
- ... pass
- ... print( 'ifconfig:', sta_if.ifconfig() )</code>
- <h2>TCP Socket</h2>
- <code>>>> import socket
- >>> addr_info = socket.getaddrinfo( 'towel.blinkenlights.nl', 23 )
- >>> addr = addr_info[0][-1]
- >>> s = socket.socket()
- >>> s.connect( addr )
- >>> while True:
- ... data = s.recv( 500 )
- ... print( str( data, 'utf8' ), end = '' )</code>
- <p>HTTP get.<br>
- <code>>>> 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' )</code>
- <p>HTTP server.<br>
- <code>>>> import machine
- >>> pins = [ machine.Pin( i, machine.Pin.IN ) for i in ( 0, 2, 4, 5, 12, 13, 14, 15 ) ]
- >>> html = "<!DOCTYPE HTML><html><body><h1>Pins</h1>%s</body></html>"
- >>> import socket
- >>> addr = socket.getaddrinfo( '0.0.0.0', 80 )[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 = [ '<p>pin %s is %d.' % ( str( p ), p.value() ) for p in pins ]
- ... response = html % '\n'.join( rows )
- ... cl.send( response )
- ... cl.close()</code>
- <h2>GPIO Pins</h2>
- <p>Pins are 0, 2, 4, 5, 12, 13, 14, 15, and 16.<br>
- <code>>>> pin = machine.Pin( 0, machine.Pin.IN, machine.Pin.PULL_UP )</code><br>
- There is also machine.Pin.OUT.<br>
- Use None to not use pull-up (default).<br>
- GPIO 16 doesn't have pull-up.<br>
- Use pin.on(), pin.off(), or pin.value(value) to set value.
- <p>Interrupts<br>
- <code>>>> def cb( p ):
- ... print( 'pin changed', p )
- >>> from machine import Pin
- >>> p0 = Pin( 0, Pin.IN )
- >>> p2 = Pin( 2, Pin.IN )
- >>> p0.irq( trigger = Pin.IRQ_FALLING, handler = cb )
- >>> p2.irq( trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = cb )</code><br>
- GPIO 16 cannot be configured to trigger a hard interrupt if its input changes.
- <h2>Pulse Width Modulation</h2>
- <p>Pins 0, 2, 4, 5, 12, 13, 14, 15 support PWM. They must be set at the same frequency.<br>
- Frequency is between 1Hz and 1kHz.<br>
- Duty is between 0 (fully off) and 1023 (fully on).<br>
- <code>>>> import machine
- >>> pwm = machine.PWM( machine.pin( 0 ) )
- >>> pwm.freq( 500 )
- >>> pwm.duty( 512 )
- >>> pwm.freq()
- 500
- >>> pwm.deinit()
- Fade LED
- >>> import time, math
- >>> def pulse( pwm, t ):
- ... for i in range( 20 ):
- ... pwm.duty( int( math.sin( i / 10 * math.pi ) * 500 + 500 ) )
- ... time.sleep_ms( t )
- >>> for i in range( 10 ):
- ... pulse( led, 20 )</code>
- <p>Hobby motor servo use PWM frequency 50Hz and duty cycle between 40 and 115 so 77 is the center.
- <h2>Analog to Digital</h2>
- <p>There is only one ADC pin, value between 0 and 1024 (1.0V).<br>
- <code>>>> import machine
- >>> adc = machine.ADC(0)
- >>> adc.read()
- 58</code>
- <h2>Power Control</h2>
- <p>You can double CPU speed.<br>
- <code>>>> import machine
- >>> machine.freq()
- 80000000
- >>> machine.freq(160000000)</code>
- <p>To deepsleep, connect GPIO16 to Reset pin.<br>
- <code>>>> rtc = machine.RTC()
- >>> rtc.irq( trigger = rtc.ALARM0, wake = machine.DEEPSLEEP )
- >>> rtc.alarm( rtc.ALARM0, 10000 )
- >>> machine.deepsleep()</code>
- <p>On (boot) script you can detect the reset cause.<br>
- <code>>>> if machine.reset_cause() == machine.DEEPSLEEP_RESET:
- >>> print( 'woke from deepsleep' )</code>
- <h2>DS18S20 or DS18B20</h2>
- <p>Temperature sensor, use 4k7 pull-up at data pin<br>
- <code>>>> import time, machine, onewire, ds18x20
- >>> ds = ds18x20.DS18X20( onewire.OneWire( machine.Pin( 12 ) ) )
- >>> roms = ds.scan()
- >>> ds.convert_temp()
- >>> time.sleep_ms( 750 )
- >>> for rom in roms:
- ... print( ds.read_temp( rom ) )</code>
- <h2>NeoPixel</h2>
- <code>>>> import machine, neopixel
- >>> np = neopixel.NeoPixel( machine.Pin( 4 ), 12 )
- >>> np[0] = ( 255, 0, 0 )
- >>> np[11] = ( 0, 0, 0 )
- >>> np.write()</code>
- <p>For RGBW or RGBY LEDs.<br>
- <code>>>> np = neopixel.NeoPixel( machine.Pin( 4 ), 12, bpp = 4 )
- >>> np[0] = ( 255, 0, 0, 128 )</code>
- <h2>DHT Sensor</h2>
- <p>Temperature in celcius, humidity in percent.<br>
- <code>>>> import machine, dht
- >>> dht11 = dht.DHT11( machine.Pin( 4 ) )
- >>> dht22 = dht.DHT22( machine.Pin( 5 ) )
- >>> dht22.measure()
- >>> dht22.temperature()
- >>> dht22.humidity()
- >>> time.sleep_ms( 2000 )</code><br>
- Pin configuration. Some support I2C, too.<br>
- DHT11, DHT22, AM2301, AM2302: 1-VDD 2-DATA 3-NC 4-GND.<br>
- DHT12, AM2320, AM2321, AM2322: 1-VDD 2-DATA 3-GND 4-GND also 1-VDD 2-SDA 3-GND 4-SCL.<br>
- Data, SDA, SCL use pull-up.
- <h2>NodeMCU Pins</h2>
- NodeMCU pin label to ESP8266 pin<br><code>Label D0 - GPIO 16 (User, Wake)
- Label D1 - GPIO_5
- Label D2 - GPIO_4
- Label D3 - GPIO_0 (Flash)
- Label D4 - GPIO_2
- Label D5 - GPIO_14 (, HSclk)
- Label D6 - GPIO_12 (, HMiso)
- Label D7 - GPIO_13 (Rxd2, HMosi)
- Label D8 - GPIO_15 (Txd2, HCS)
- Label RX - GPIO 3 (Rxd0)
- Label TX - GPIO 1 (Txd0)
- Label SK - SCLK (SDClk)
- Label S0 - MISO (SDD0)
- Label SC - CS (SDCmd)
- Label S1 - MOSI (SDD1)
- Label S2 - GPIO 9 (SDD2)
- Label S3 - GPIO 10 (SDD3)
- Label A0 - ADC0 (TOut)
- </code>
- </code>
- </code>
- <h2>Adafruit AMPY</h2>
- <code>>>> import esp
- >>> esp.osdebug(None)
- $ pip install adafruit-ampy
- $ ampy --help
- $ ampy --port /dev/ttyUSB0 run test.py
- $ ampy --port /dev/ttyUSB0 put src dest
- $ ampy --port /dev/ttyUSB0 get src dest
- $ ampy --port /dev/ttyUSB0 mkdir foobar
- $ ampy --port /dev/ttyUSB0 ls
- $ ampy --port /dev/ttyUSB0 rmdir foobar
- $ ampy --port /dev/ttyUSB0 rm foo</code>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement