Advertisement
sohotcall

micropython esp8266

Mar 16th, 2019
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 8.32 KB | None | 0 0
  1. <style>code{white-space:pre;}</style>
  2.  
  3. <h1>MICROPYTHON ESP8266</h1>
  4.  
  5. <h2>Install</h2>
  6.  
  7. <p>Install esptool.<br>
  8. <code>$ pip install esptool</code>
  9.  
  10. <p>Erase flash.<br>
  11. <code>$ esptool.py --port /dev/ttyUSB0 erase_flash</code>
  12.  
  13. <p>Deploy firmware.<br>
  14. <code>$ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash --flash_size=detect 0 esp8266-20170108-v1.8.7.bin</code><br>
  15. (add -fm dio if needed)
  16.  
  17. <p>Connect USB-to-UART to board (GPIO1=Tx, GPIO3=Rx, baud=115200) and to computer.<br>
  18. Connect computer wifi to Micropython-xxxxxx (password: micropythoN)
  19.  
  20. <p>Check firmware.<br>
  21. <code>$ picocom /dev/ttyUSB0 -b115200
  22. >>> import esp
  23. >>> esp.check_fw()</code>
  24.  
  25. <h2>WebREPL</h2>
  26.  
  27. <p>Download WebREPL from https://github.com/webrepl
  28.  
  29. <p>Run this and follow the instructions.<br>
  30. <code>>>> import webrepl_setup</code>
  31.  
  32. <p>Open WebREPL, connect to module (IP address 192.168.4.1), enter the WebREPL password.
  33.  
  34. <h2>Example</h2>
  35.  
  36. <code>>>> import machine
  37. >>> pin = machine.Pin( 2, machine.Pin.OUT )
  38. >>> pin.on()
  39. >>> pin.off()
  40. >>> def toggle(p):
  41. ...   p.value( not p.value() )
  42. ...
  43. >>> import time
  44. >>> while True:
  45. ...   toggle(pin)
  46. ...   time.sleep_ms( 500 )
  47. ...</code><br>
  48. Ctrl+C to break
  49.  
  50. <h2>Filesystem</h2>
  51.  
  52. <p>Read/write file<br>
  53. <code>>>> f = open( 'data.txt', 'w' )
  54. >>> f.write( 'some data' )
  55. 9
  56. >>> f.close()
  57. >>> f = open( 'data.txt' )
  58. >>> f.read()
  59. 'some data'
  60. >>> f.close()</code>
  61.  
  62. <p>Directories<br>
  63. <code>>>> import os
  64. >>> os.listdir()
  65. ['boot.py', 'port_config.py', 'data.txt']
  66. >>> os.mkdir( 'dir' )
  67. >>> os.remove( 'data.txt' )</code>
  68.  
  69. <p>You can create boot.py and/or main.py files for start up script.
  70.  
  71. <h2>Networking</h2>
  72.  
  73. <p>Station and access point.<br>
  74. <code>>>> import network
  75. >>> sta_if = network.WLAN( network.STA_IF )
  76. >>> ap_if = network.WLAN( network.AP_IF )
  77. >>> sta_if.active()
  78. False
  79. >>> ap_if.active()
  80. True
  81. >>> ap_if.ifconfig()
  82. ('192.168.4.1', '255.255.255.0', '192.168.4.1', '8.8.8.8')</code><br>
  83. It is IP address, netmask, gateway, and DNS.
  84.  
  85. <p>Enable station interface.<br>
  86. <code>>>> sta_if.active(True)
  87. >>> sta_if.connect( '<your ssid>', '<your password>' )
  88. >>> sta_if.isconnected()
  89. >>> sta_if.active( False )
  90. >>> def do_connect():
  91. ...   import network
  92. ...   sta_if = network.WLAN( network.STA_IF )
  93. ...   if not sta_if.isconnected():
  94. ...     print( 'Connecting...' )
  95. ...     sta_if.active( True )
  96. ...     sta_if.connect( 'test', 'wifiwifi' )
  97. ...     while not sta_if.isconnected():
  98. ...       pass
  99. ...   print( 'ifconfig:', sta_if.ifconfig() )</code>
  100.  
  101. <h2>TCP Socket</h2>
  102. <code>>>> import socket
  103. >>> addr_info = socket.getaddrinfo( 'towel.blinkenlights.nl', 23 )
  104. >>> addr = addr_info[0][-1]
  105. >>> s = socket.socket()
  106. >>> s.connect( addr )
  107. >>> while True:
  108. ...   data = s.recv( 500 )
  109. ...   print( str( data, 'utf8' ), end = '' )</code>
  110.  
  111. <p>HTTP get.<br>
  112. <code>>>> def http_get( url ):
  113. ...   _, _, host, path = url.split( '/', 3 )
  114. ...   addr = socket.getaddrinfo( host, 80 )[0][-1]
  115. ...   s = socket.socket()
  116. ...   s.connect( addr )
  117. ...   s.send( bytes( 'GET /%s HTTP/1.0\r\nHost: %s\r\n\r\n' % ( path, host ), 'utf8' ) )
  118. ...   while True:
  119. ...     data = s.recv( 100 )
  120. ...     if data:
  121. ...       print( str( data, 'utf8' ), end = '' )
  122. ...     else:
  123. ...       break
  124. ...   s.close()
  125. >>> http_get( 'http://micropython.org/ks/test.html' )</code>
  126.  
  127. <p>HTTP server.<br>
  128. <code>>>> import machine
  129. >>> pins = [ machine.Pin( i, machine.Pin.IN ) for i in ( 0, 2, 4, 5, 12, 13, 14, 15 ) ]
  130. >>> html = "&lt;!DOCTYPE HTML>&lt;html>&lt;body>&lt;h1>Pins&lt;/h1>%s&lt;/body>&lt;/html>"
  131. >>> import socket
  132. >>> addr = socket.getaddrinfo( '0.0.0.0', 80 )[0][-1]
  133. >>> s = socket.socket()
  134. >>> s.bind( addr )
  135. >>> s.listen( 1 )
  136. >>> print( 'listening on', addr )
  137. >>> while True:
  138. ...   cl, addr = s.accept()
  139. ...   print( 'client connected from', addr )
  140. ...   cl_file = cl.makefile( 'rwb', 0 )
  141. ...   while True:
  142. ...     line = cl_file.readline()
  143. ...     if not line or line == b'\r\n':
  144. ...       break
  145. ...   rows = [ '&lt;p>pin %s is %d.' % ( str( p ), p.value() ) for p in pins ]
  146. ...   response = html % '\n'.join( rows )
  147. ...   cl.send( response )
  148. ...   cl.close()</code>
  149.  
  150. <h2>GPIO Pins</h2>
  151.  
  152. <p>Pins are 0, 2, 4, 5, 12, 13, 14, 15, and 16.<br>
  153. <code>>>> pin = machine.Pin( 0, machine.Pin.IN, machine.Pin.PULL_UP )</code><br>
  154. There is also machine.Pin.OUT.<br>
  155. Use None to not use pull-up (default).<br>
  156. GPIO 16 doesn't have pull-up.<br>
  157. Use pin.on(), pin.off(), or pin.value(value) to set value.
  158.  
  159. <p>Interrupts<br>
  160. <code>>>> def cb( p ):
  161. ...   print( 'pin changed', p )
  162. >>> from machine import Pin
  163. >>> p0 = Pin( 0, Pin.IN )
  164. >>> p2 = Pin( 2, Pin.IN )
  165. >>> p0.irq( trigger = Pin.IRQ_FALLING, handler = cb )
  166. >>> p2.irq( trigger = Pin.IRQ_RISING | Pin.IRQ_FALLING, handler = cb )</code><br>
  167. GPIO 16 cannot be configured to trigger a hard interrupt if its input changes.
  168.  
  169. <h2>Pulse Width Modulation</h2>
  170. <p>Pins 0, 2, 4, 5, 12, 13, 14, 15 support PWM. They must be set at the same frequency.<br>
  171. Frequency is between 1Hz and 1kHz.<br>
  172. Duty is between 0 (fully off) and 1023 (fully on).<br>
  173. <code>>>> import machine
  174. >>> pwm = machine.PWM( machine.pin( 0 ) )
  175. >>> pwm.freq( 500 )
  176. >>> pwm.duty( 512 )
  177. >>> pwm.freq()
  178. 500
  179. >>> pwm.deinit()
  180. Fade LED
  181. >>> import time, math
  182. >>> def pulse( pwm, t ):
  183. ...   for i in range( 20 ):
  184. ...     pwm.duty( int( math.sin( i / 10 * math.pi ) * 500 + 500 ) )
  185. ...     time.sleep_ms( t )
  186. >>> for i in range( 10 ):
  187. ...   pulse( led, 20 )</code>
  188. <p>Hobby motor servo use PWM frequency 50Hz and duty cycle between 40 and 115 so 77 is the center.
  189.  
  190. <h2>Analog to Digital</h2>
  191.  
  192. <p>There is only one ADC pin, value between 0 and 1024 (1.0V).<br>
  193. <code>>>> import machine
  194. >>> adc = machine.ADC(0)
  195. >>> adc.read()
  196. 58</code>
  197.  
  198. <h2>Power Control</h2>
  199.  
  200. <p>You can double CPU speed.<br>
  201. <code>>>> import machine
  202. >>> machine.freq()
  203. 80000000
  204. >>> machine.freq(160000000)</code>
  205.  
  206. <p>To deepsleep, connect GPIO16 to Reset pin.<br>
  207. <code>>>> rtc = machine.RTC()
  208. >>> rtc.irq( trigger = rtc.ALARM0, wake = machine.DEEPSLEEP )
  209. >>> rtc.alarm( rtc.ALARM0, 10000 )
  210. >>> machine.deepsleep()</code>
  211.  
  212. <p>On (boot) script you can detect the reset cause.<br>
  213. <code>>>> if machine.reset_cause() == machine.DEEPSLEEP_RESET:
  214. >>>   print( 'woke from deepsleep' )</code>
  215.  
  216. <h2>DS18S20 or DS18B20</h2>
  217. <p>Temperature sensor, use 4k7 pull-up at data pin<br>
  218. <code>>>> import time, machine, onewire, ds18x20
  219. >>> ds = ds18x20.DS18X20( onewire.OneWire( machine.Pin( 12 ) ) )
  220. >>> roms = ds.scan()
  221. >>> ds.convert_temp()
  222. >>> time.sleep_ms( 750 )
  223. >>> for rom in roms:
  224. ...   print( ds.read_temp( rom ) )</code>
  225.  
  226. <h2>NeoPixel</h2>
  227.  
  228. <code>>>> import machine, neopixel
  229. >>> np = neopixel.NeoPixel( machine.Pin( 4 ), 12 )
  230. >>> np[0] = ( 255, 0, 0 )
  231. >>> np[11] = ( 0, 0, 0 )
  232. >>> np.write()</code>
  233.  
  234. <p>For RGBW or RGBY LEDs.<br>
  235. <code>>>> np = neopixel.NeoPixel( machine.Pin( 4 ), 12, bpp = 4 )
  236. >>> np[0] = ( 255, 0, 0, 128 )</code>
  237.  
  238. <h2>DHT Sensor</h2>
  239. <p>Temperature in celcius, humidity in percent.<br>
  240. <code>>>> import machine, dht
  241. >>> dht11 = dht.DHT11( machine.Pin( 4 ) )
  242. >>> dht22 = dht.DHT22( machine.Pin( 5 ) )
  243. >>> dht22.measure()
  244. >>> dht22.temperature()
  245. >>> dht22.humidity()
  246. >>> time.sleep_ms( 2000 )</code><br>
  247. Pin configuration. Some support I2C, too.<br>
  248. DHT11, DHT22, AM2301, AM2302: 1-VDD 2-DATA 3-NC 4-GND.<br>
  249. DHT12, AM2320, AM2321, AM2322: 1-VDD 2-DATA 3-GND 4-GND also 1-VDD 2-SDA 3-GND 4-SCL.<br>
  250. Data, SDA, SCL use pull-up.
  251.  
  252. <h2>NodeMCU Pins</h2>
  253. NodeMCU pin label to ESP8266 pin<br><code>Label D0 - GPIO 16 (User, Wake)
  254. Label D1 - GPIO_5
  255. Label D2 - GPIO_4
  256. Label D3 - GPIO_0  (Flash)
  257. Label D4 - GPIO_2
  258. Label D5 - GPIO_14 (,     HSclk)
  259. Label D6 - GPIO_12 (,     HMiso)
  260. Label D7 - GPIO_13 (Rxd2, HMosi)
  261. Label D8 - GPIO_15 (Txd2, HCS)
  262. Label RX - GPIO 3  (Rxd0)
  263. Label TX - GPIO 1  (Txd0)
  264. Label SK - SCLK    (SDClk)
  265. Label S0 - MISO    (SDD0)
  266. Label SC - CS      (SDCmd)
  267. Label S1 - MOSI    (SDD1)
  268. Label S2 - GPIO 9  (SDD2)
  269. Label S3 - GPIO 10 (SDD3)
  270. Label A0 - ADC0    (TOut)
  271. </code>
  272. </code>
  273. </code>
  274.  
  275. <h2>Adafruit AMPY</h2>
  276. <code>>>> import esp
  277. >>> esp.osdebug(None)
  278. $ pip install adafruit-ampy
  279. $ ampy --help
  280. $ ampy --port /dev/ttyUSB0 run test.py
  281. $ ampy --port /dev/ttyUSB0 put src dest
  282. $ ampy --port /dev/ttyUSB0 get src dest
  283. $ ampy --port /dev/ttyUSB0 mkdir foobar
  284. $ ampy --port /dev/ttyUSB0 ls
  285. $ ampy --port /dev/ttyUSB0 rmdir foobar
  286. $ ampy --port /dev/ttyUSB0 rm foo</code>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement