logo资料库

Micropython官方使用手册.pdf

第1页 / 共285页
第2页 / 共285页
第3页 / 共285页
第4页 / 共285页
第5页 / 共285页
第6页 / 共285页
第7页 / 共285页
第8页 / 共285页
资料共285页,剩余部分请下载后查看
MicroPython libraries
Python standard libraries and micro-libraries
Builtin functions and exceptions
array – arrays of numeric data
cmath – mathematical functions for complex numbers
gc – control the garbage collector
math – mathematical functions
sys – system specific functions
ubinascii – binary/ASCII conversions
ucollections – collection and container types
uerrno – system error codes
uhashlib – hashing algorithms
uheapq – heap queue algorithm
uio – input/output streams
ujson – JSON encoding and decoding
uos – basic “operating system” services
ure – simple regular expressions
uselect – wait for events on a set of streams
usocket – socket module
ussl – SSL/TLS module
ustruct – pack and unpack primitive data types
utime – time related functions
uzlib – zlib decompression
_thread – multithreading support
MicroPython-specific libraries
btree – simple BTree database
framebuf — Frame buffer manipulation
machine — functions related to the hardware
micropython – access and control MicroPython internals
network — network configuration
ucryptolib – cryptographic ciphers
uctypes – access binary data in a structured way
Libraries specific to the pyboard
pyb — functions related to the board
lcd160cr — control of LCD160CR display
Libraries specific to the WiPy
wipy – WiPy specific features
class TimerWiPy – control hardware timers
class TimerChannel — setup a channel for a timer
Libraries specific to the ESP8266
esp — functions related to the ESP8266
The MicroPython language
Glossary
The MicroPython Interactive Interpreter Mode (aka REPL)
Auto-indent
Auto-completion
Interrupting a running program
Paste Mode
Soft Reset
The special variable _ (underscore)
Raw Mode
Writing interrupt handlers
Tips and recommended practices
MicroPython Issues
Exceptions
General Issues
Maximising MicroPython Speed
Designing for speed
Identifying the slowest section of code
MicroPython code improvements
The Native code emitter
The Viper code emitter
Accessing hardware directly
MicroPython on Microcontrollers
Flash Memory
RAM
The Heap
String Operations
Postscript
Distribution packages, package management, and deploying applications
Overview
Distribution packages
upip package manager
Cross-installing packages
Cross-installing packages with freezing
Creating distribution packages
Application resources
References
Inline Assembler for Thumb2 architectures
Document conventions
Instruction Categories
Usage examples
References
MicroPython differences from CPython
Syntax
Spaces
Unicode
Core Language
Classes
Functions
Generator
Runtime
import
Builtin Types
Exception
bytearray
bytes
float
int
list
str
tuple
Modules
array
builtins
deque
json
struct
sys
MicroPython license information
Quick reference for the pyboard
General information about the pyboard
Local filesystem and SD card
Boot modes
Errors: flashing LEDs
Guide for using the pyboard with Windows
The pyboard hardware
Datasheets for the components on the pyboard
Datasheets for other components
MicroPython tutorial for the pyboard
Introduction to the pyboard
Running your first script
Getting a MicroPython REPL prompt
Turning on LEDs and basic Python concepts
The Switch, callbacks and interrupts
The accelerometer
Safe mode and factory reset
Making the pyboard act as a USB mouse
The Timers
Inline assembler
Power control
Tutorials requiring extra components
Tips, tricks and useful things to know
General board control
Delay and timing
Internal LEDs
Internal switch
Pins and GPIO
Servo control
External interrupts
Timers
RTC (real time clock)
PWM (pulse width modulation)
ADC (analog to digital conversion)
DAC (digital to analog conversion)
UART (serial bus)
SPI bus
I2C bus
CAN bus (controller area network)
Internal accelerometer
Quick reference for the ESP8266
General information about the ESP8266 port
Multitude of boards
Technical specifications and SoC datasheets
Scarcity of runtime resources
Boot process
Known Issues
MicroPython tutorial for ESP8266
Getting started with MicroPython on the ESP8266
Getting a MicroPython REPL prompt
The internal filesystem
Network basics
Network - TCP sockets
GPIO Pins
Pulse Width Modulation
Analog to Digital Conversion
Power control
Controlling 1-wire devices
Controlling NeoPixels
Temperature and Humidity
Next steps
Installing MicroPython
General board control
Networking
Delay and timing
Timers
Pins and GPIO
PWM (pulse width modulation)
ADC (analog to digital conversion)
Software SPI bus
Hardware SPI bus
I2C bus
Real time clock (RTC)
Deep-sleep mode
OneWire driver
NeoPixel driver
APA102 driver
DHT driver
WebREPL (web browser interactive prompt)
Quick reference for the WiPy
General information about the WiPy
No floating point support
Before applying power
WLAN default behaviour
Telnet REPL
Local file system and FTP access
FileZilla settings
Upgrading the firmware Over The Air
Boot modes and safe boot
The heartbeat LED
Details on sleep modes
Additional details for machine.Pin
Additional details for machine.I2C
Known issues
WiPy tutorials and examples
Introduction to the WiPy
Getting a MicroPython REPL prompt
Getting started with Blynk and the WiPy
WLAN step by step
Hardware timers
Reset and boot modes
General board control (including sleep modes)
Pins and GPIO
Timers
PWM (pulse width modulation)
ADC (analog to digital conversion)
UART (serial bus)
SPI bus
I2C bus
Watchdog timer (WDT)
Real time clock (RTC)
SD card
WLAN (WiFi)
Telnet and FTP server
Heart beat LED
Python Module Index
Index
MicroPython Documentation Release 1.9.4 Damien P. George, Paul Sokolovsky, and contributors Oct 25, 2018
CONTENTS 1 MicroPython libraries 1.1 1.2 MicroPython-specific libraries . . . 1.3 1.4 1.5 . . . . . . . 1 2 . Python standard libraries and micro-libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Builtin functions and exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.1 5 array – arrays of numeric data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.2 5 cmath – mathematical functions for complex numbers . . . . . . . . . . . . . . . . . . . . 1.1.3 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . gc – control the garbage collector 1.1.4 7 math – mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.5 9 sys – system specific functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.6 11 ubinascii – binary/ASCII conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.7 11 ucollections – collection and container types . . . . . . . . . . . . . . . . . . . . . . 1.1.8 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . uerrno – system error codes 1.1.9 13 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.10 uhashlib – hashing algorithms 14 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.11 uheapq – heap queue algorithm . . 14 1.1.12 uio – input/output streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.1.13 ujson – JSON encoding and decoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.1.14 uos – basic “operating system” services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.15 ure – simple regular expressions 21 . . . . . . . . . . . . . . . . . . . . . . . . 1.1.16 uselect – wait for events on a set of streams 23 1.1.17 usocket – socket module . . . . . 28 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.18 ussl – SSL/TLS module 28 1.1.19 ustruct – pack and unpack primitive data types . . . . . . . . . . . . . . . . . . . . . . . 29 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.20 utime – time related functions 32 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.21 uzlib – zlib decompression . . . . . 33 1.1.22 _thread – multithreading support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 btree – simple BTree database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.1 35 framebuf — Frame buffer manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.2 37 machine — functions related to the hardware . . . . . . . . . . . . . . . . . . . . . . . . 1.2.3 54 micropython – access and control MicroPython internals . . . . . . . . . . . . . . . . . 1.2.4 56 . . . . . . . . . . . . . . . . . . . . . . . . . . . network — network configuration . . . 1.2.5 66 ucryptolib – cryptographic ciphers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2.6 66 1.2.7 uctypes – access binary data in a structured way . . . . . . . . . . . . . . . . . . . . . . 71 Libraries specific to the pyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . pyb — functions related to the board . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3.1 71 lcd160cr — control of LCD160CR display . . . . . . . . . . . . . . . . . . . . . . . . . 108 1.3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Libraries specific to the WiPy . 1.4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 wipy – WiPy specific features . . . class TimerWiPy – control hardware timers . . . . . . . . . . . . . . . . . . . . . . . . . . 115 1.4.2 1.4.3 class TimerChannel — setup a channel for a timer . . . . . . . . . . . . . . . . . . . . . . . 116 Libraries specific to the ESP8266 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . i
1.5.1 esp — functions related to the ESP8266 . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 2 The MicroPython language . . 2.1 Glossary . 2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exceptions . General Issues . . 2.4 Maximising MicroPython Speed . . 2.3 Writing interrupt handlers . 2.3.1 2.3.2 MicroPython Issues . . 2.3.3 2.3.4 . 119 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 The MicroPython Interactive Interpreter Mode (aka REPL) . . . . . . . . . . . . . . . . . . . . . . . 121 2.2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Auto-indent . Auto-completion . 2.2.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 . Interrupting a running program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.2.3 Paste Mode . 2.2.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 2.2.5 Soft Reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 The special variable _ (underscore) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 2.2.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 2.2.7 Raw Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Tips and recommended practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Designing for speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131 2.4.1 2.4.2 Identifying the slowest section of code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 2.4.3 MicroPython code improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 2.4.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 The Native code emitter The Viper code emitter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 2.4.5 2.4.6 Accessing hardware directly . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 2.5 MicroPython on Microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 2.6 Distribution packages, package management, and deploying applications . . . . . . . . . . . . . . . 142 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 Distribution packages . upip package manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 Cross-installing packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Cross-installing packages with freezing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 Creating distribution packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Application resources . References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146 . 146 Document conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Instruction Categories . Usage examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 . . References 2.6.1 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 Inline Assembler for Thumb2 architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.7.1 2.7.2 2.7.3 2.7.4 . Flash Memory . . . RAM . . The Heap . . . String Operations . Postscript . 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 . . . 2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 MicroPython differences from CPython . . . . . Spaces . . Unicode . . . . Syntax . . 3.1.1 . 3.1.2 . Core Language . Classes 3.2.1 . Functions . 3.2.2 Generator . 3.2.3 3.2.4 Runtime . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1 3.2 ii 161 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
3.3 . . 3.2.5 Builtin Types . 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 import . . . . Exception . bytearray . bytes . . . . float . . int . . . list . . str . . tuple . . . . . array . . builtins deque . . . . json . . . struct sys . . . . . . . . . . . . . . . . 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176 . 176 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 . 178 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 . . . . . 3.4 Modules 4 MicroPython license information 181 5 Quick reference for the pyboard . . . . . . 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1 General information about the pyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 . 183 Local filesystem and SD card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 . 184 Boot modes . . Errors: flashing LEDs . . 185 Guide for using the pyboard with Windows . . . . . . . . . . . . . . . . . . . . . . . . . . 185 The pyboard hardware . . 185 Datasheets for the components on the pyboard . . . . . . . . . . . . . . . . . . . . . . . . . 185 Datasheets for other components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 5.2 MicroPython tutorial for the pyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Introduction to the pyboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 5.2.1 Running your first script . 186 5.2.2 Getting a MicroPython REPL prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 5.2.3 Turning on LEDs and basic Python concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 191 5.2.4 The Switch, callbacks and interrupts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 5.2.5 The accelerometer . 194 . 5.2.6 5.2.7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 Safe mode and factory reset 5.2.8 Making the pyboard act as a USB mouse . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 5.2.9 . 198 . 200 5.2.10 5.2.11 Power control . . 202 5.2.12 Tutorials requiring extra components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 5.2.13 Tips, tricks and useful things to know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214 5.3 General board control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 . 5.4 Delay and timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 . . 5.5 Internal LEDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 5.6 . . Internal switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 . . 5.7 Pins and GPIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 Servo control . 5.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216 External interrupts . 5.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 5.10 Timers . . 5.11 RTC (real time clock) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 5.12 PWM (pulse width modulation) . 5.13 ADC (analog to digital conversion) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The Timers Inline assembler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . iii
5.14 DAC (digital to analog conversion) 5.15 UART (serial bus) . 5.16 SPI bus . 5.17 I2C bus . . 5.18 CAN bus (controller area network) 5.19 Internal accelerometer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Quick reference for the ESP8266 . . . . . . . . . . . . . . . . . . . . . . . 6.1 General information about the ESP8266 port 6.1.1 Multitude of boards . 6.1.2 6.1.3 6.1.4 6.1.5 221 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 Technical specifications and SoC datasheets . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Scarcity of runtime resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Boot process . Known Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 6.2 MicroPython tutorial for ESP8266 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 Getting started with MicroPython on the ESP8266 . . . . . . . . . . . . . . . . . . . . . . 224 6.2.1 Getting a MicroPython REPL prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227 6.2.2 The internal filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231 6.2.3 Network basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 6.2.4 Network - TCP sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 6.2.5 GPIO Pins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235 6.2.6 Pulse Width Modulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 6.2.7 Analog to Digital Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 6.2.8 6.2.9 Power control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238 6.2.10 Controlling 1-wire devices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 6.2.11 Controlling NeoPixels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240 6.2.12 Temperature and Humidity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 6.2.13 Next steps . . . Installing MicroPython . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 6.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 . 6.4 General board control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 6.5 Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 . 6.6 Delay and timing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 . Timers . . . 6.7 Pins and GPIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.8 . . PWM (pulse width modulation) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244 6.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 6.10 ADC (analog to digital conversion) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 . 6.11 Software SPI bus . . . 6.12 Hardware SPI bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 6.13 I2C bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 6.14 Real time clock (RTC) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 . 6.15 Deep-sleep mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 . . 6.16 OneWire driver . 6.17 NeoPixel driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 . . 6.18 APA102 driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248 6.19 DHT driver . . . . . 6.20 WebREPL (web browser interactive prompt) . 248 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Quick reference for the WiPy 249 7.1 General information about the WiPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 7.1.1 7.1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 7.1.3 WLAN default behaviour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Telnet REPL . 7.1.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 7.1.5 Local file system and FTP access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 No floating point support Before applying power . . . . . . . iv
. . . . . . . . 7.2.1 7.2.2 7.2.3 7.2.4 WLAN step by step . 7.2.5 . 7.2.6 . Hardware timers . Reset and boot modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 WiPy tutorials and examples . . 250 FileZilla settings 7.1.6 Upgrading the firmware Over The Air . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 7.1.7 Boot modes and safe boot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 7.1.8 . 252 The heartbeat LED . 7.1.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 7.1.10 Details on sleep modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.11 Additional details for machine.Pin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 7.1.12 Additional details for machine.I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1.13 Known issues . . 253 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 . 255 Introduction to the WiPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting a MicroPython REPL prompt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256 Getting started with Blynk and the WiPy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 . 259 . 260 . 261 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 7.3 General board control (including sleep modes) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262 Pins and GPIO . 7.4 Timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 7.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 7.6 PWM (pulse width modulation) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 7.7 ADC (analog to digital conversion) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 . 7.8 UART (serial bus) . SPI bus . 7.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263 . 7.10 I2C bus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 7.11 Watchdog timer (WDT) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 . 7.12 Real time clock (RTC) . . . . 7.13 SD card . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 . 7.14 WLAN (WiFi) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 . 7.15 Telnet and FTP server 7.16 Heart beat LED . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Python Module Index Index 267 269 v
vi
分享到:
收藏