micropython esp8266教程_ESP8266 快速开发指南
这是针对 ESP8266 版的快速指南,请大家注意与 pyboard 版的区别。简单介绍一组有关 ESP8266 开发板下使用 MicroPython 开发的范例程序。
machine 和频率控制
import machine
machine.freq() # get the current frequency of the CPU
machine.freq(160000000) # set the CPU frequency to 160 MHz
ESP模块
import esp
esp.osdebug(None) # turn off vendor O/S debugging messages
esp.osdebug(0) # redirect vendor O/S debugging messages to UART(0)
网络
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('essid', 'password') # connect to an AP
wlan.config('mac') # get the interface's MAC adddress
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point
一个用于连接到你的本地 WiFi 的函数:
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
网络连接后,就可以创建和使用 TCP/UDP 了。
GPIO
from machine import Pin
p0 = Pin(0, Pin.OUT) # create output pin on GPIO0
p0.high() # set pin to high
p0.low() # set pin to low
p0.value(1) # set pin to high
p2 = Pin(2, Pin.IN) # create input pin on GPIO2
print(p2.value()) # get value, 0 or 1
p4 = Pin(4, Pin.IN, Pin.PULL_UP) # enable internal pull-up resistor
p5 = Pin(5, Pin.OUT, value=1) # set pin high on creation
定时器
from machine import Timer
tim = Timer(-1)
tim.init(period=5000, mode=Timer.ONE_SHOT, callback=lambda t:print(1))
tim.init(period=2000, mode=Timer.PERIODIC, callback=lambda t:print(2))
延时
import time
time.sleep(1) # sleep for 1 second
time.sleep_ms(500) # sleep for 500 milliseconds
time.sleep_us(10) # sleep for 10 microseconds
start = time.ticks_ms() # get millisecond counter
delta = time.ticks_diff(start, time.ticks_ms()) # compute time difference
PWM
from machine import Pin, PWM
pwm0 = PWM(Pin(0)) # create PWM object from a pin
pwm0.freq() # get current frequency
pwm0.freq(1000) # set frequency
pwm0.duty() # get current duty cycle
pwm0.duty(200) # set duty cycle
pwm0.deinit() # turn off PWM on the pin
pwm2 = PWM(Pin(2), freq=500, duty=512) # create and configure in one go
ADC
from machine import ADC
adc = ADC(0) # create ADC object on ADC pin
adc.read() # read value, 0-1024
软件 SPI
SPI 有两种驱动。一种是软件方式 (bit-banging),可以使用任何 pins:
from machine import Pin, SPI
# construct an SPI bus on the given pins
# polarity is the idle state of SCK
# phase=0 means sample on the first edge of SCK, phase=1 means the second
spi = SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
spi.init(baudrate=200000) # set the baudrate
spi.read(10) # read 10 bytes on MISO
spi.read(10, 0xff) # read 10 bytes while outputing 0xff on MOSI
buf = bytearray(50) # create a buffer
spi.readinto(buf) # read into the given buffer (reads 50 bytes in this case)
spi.readinto(buf, 0xff) # read into the given buffer and output 0xff on MOSI
spi.write(b'12345') # write 5 bytes on MOSI
buf = bytearray(4) # create a buffer
spi.write_readinto(b'1234', buf) # write to MOSI and read from MISO into the buffer
spi.write_readinto(buf, buf) # write buf to MOSI and read MISO back into buf
硬件 SPI
硬件 SPI 速度更快 (最高 80Mhz),但是只能使用下面的 pins: MISO – GPIO12, MOSI – GPIO13, 以及 SCK – GPIO14。它的用法和上面的软件SPI相同,除了pin参数:
from machine import Pin, SPI
hspi = SPI(1, baudrate=80000000, polarity=0, phase=0)
(SPI(0) 仅用于内部的 FlashROM。)
I2C
from machine import Pin, I2C
# construct an I2C bus
i2c = I2C(scl=Pin(5), sda=Pin(4), freq=100000)
i2c.readfrom(0x3a, 4) # read 4 bytes from slave device with address 0x3a
i2c.writeto(0x3a, '12') # write '12' to slave device with address 0x3a
buf = bytearray(10) # create a buffer with 10 bytes
i2c.writeto(0x3a, buf) # write the given buffer to the slave
休眠
import machine
# configure RTC.ALARM0 to be able to wake the device
rtc = machine.RTC()
rtc.irq(trigger=rtc.ALARM0, wake=machine.DEEPSLEEP)
# check if the device woke from a deep sleep
if machine.reset_cause() == machine.DEEPSLEEP_RESET:
print('woke from a deep sleep')
# set RTC.ALARM0 to fire after 10 seconds (waking the device)
rtc.alarm(rtc.ALARM0, 10000)
# put the device to sleep
machine.deepsleep()
onewire 总线
from machine import Pin
import onewire
ow = onewire.OneWire(Pin(12)) # create a OneWire bus on GPIO12
ow.scan() # return a list of devices on the bus
ow.reset() # reset the bus
ow.readbyte() # read a byte
ow.read(5) # read 5 bytes
ow.writebyte(0x12) # write a byte on the bus
ow.write('123') # write bytes on the bus
ow.select_rom(b'12345678') # select a specific device by its ROM code
驱动 DS18B20
import time
ds = onewire.DS18B20(ow)
roms = ds.scan()
ds.convert_temp()
time.sleep_ms(750)
for rom in roms:
print(ds.read_temp(rom))
网络
import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True) # activate the interface
wlan.scan() # scan for access points
wlan.isconnected() # check if the station is connected to an AP
wlan.connect('essid', 'password') # connect to an AP
wlan.config('mac') # get the interface's MAC adddress
wlan.ifconfig() # get the interface's IP/netmask/gw/DNS addresses
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.active(True) # activate the interface
ap.config(essid='ESP-AP') # set the ESSID of the access point
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
NeoPixel 驱动
from machine import Pin
from neopixel import NeoPixel
pin = Pin(0, Pin.OUT) # set GPIO0 to output to drive NeoPixels
np = NeoPixel(pin, 8) # create NeoPixel driver on GPIO0 for 8 pixels
np[0] = (255, 255, 255) # set the first pixel to white
np.write() # write data to all pixels
r, g, b = np[0] # get first pixel colour
底层驱动
import esp
esp.neopixel_write(pin, grb_buf, is800khz)
APA102驱动
from machine import Pin
from apa102 import APA102
clock = Pin(14, Pin.OUT) # set GPIO14 to output to drive the clock
data = Pin(13, Pin.OUT) # set GPIO13 to output to drive the data
apa = APA102(clock, data, 8) # create APA102 driver on the clock and the data pin for 8 pixels
apa[0] = (255, 255, 255, 31) # set the first pixel to white with a maximum brightness of 31
apa.write() # write data to all pixels
r, g, b, brightness = apa[0] # get first pixel colour
底层驱动
import esp
esp.apa102_write(clock_pin, data_pin, rgbi_buf)
webrepl
import webrepl
webrepl.start()
webrepl.stop()
micropython esp8266教程_ESP8266 快速开发指南相关推荐
- micropython esp8266教程_ESP8266 刷写MicroPython固件
固件有很多得种类 这个是下载得页面 官方支持ESP8266,亲生的就是好高度集成 ESP8266是业内集成度最高的Wi-Fi芯片,最小封装尺寸仅为5mmx5mm.ESP8266高度集成了天线开关.射频 ...
- micropython esp8266教程_ESP8266 Micropython – 连接大学Wi-Fi(WPA2 Enterprise PEAP)
我有一块带ESP8266芯片的电路板,运行Micropython固件v1.8.7.我的要求是通过大学Wi-Fi使用WebREPL,它使用WPA2 Enterprise EAP-MSCHAPv2身份验证 ...
- Apache PDFbox快速开发指南
Apache PDFbox快速开发指南 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.介绍 Apache PDFbox是一个开源的.基于Java的. ...
- MSP432 快速开发指南:如何安装板载仿真器驱动(XDS110)
博主分享不易,请给一键三连哦(关注 + 点赞 + 收藏),你的鼓励是博主分享的动力. MSP432 快速开发指南:如何安装板载仿真器驱动(XDS110) 1 前言 1.1 内容简介 1.2 快速链接 ...
- esp8266灯上电闪一下_【零知ESP8266教程】快速入门2-点亮外部LED灯
[零知ESP8266教程]快速入门2-点亮外部LED灯 [复制链接] 一.工具原料 电脑,windows系统 ESP8266开发板 micro-usb线 LED灯1个 220Ω 电阻1个 面包板一个+ ...
- MSP432 快速开发指南:如何利用 DriverLib 进行快速开发
博主分享不易,请给一键三连哦(关注 + 点赞 + 收藏),你的鼓励是博主分享的动力. MSP432 快速开发指南:如何利用 DriverLib 进行快速开发 1 前言 1.1 内容简介 1.2 快速链 ...
- Oracle WebCenter 11g 快速开发指南--翻译(一)
第一章:开发者快速开发指南 作为一个开发者,如果你准备建立一个WebCenter Portal application ,你需要一个好的路线图,来指导你从什么地方开始,提供初始化的经验:本章首先解决开 ...
- 开放下载!《AliOS Things快速开发指南》
简介:<AliOS Things快速开发指南>手把手教你从环境准备到线上.线下开发调试,更有两大典型场景实践等你参与.你的物联网开发从这里开始!快来get新技能吧~ AliOS Thing ...
- DigWS 短消息和WapPush 快速开发指南-接口介绍
DigWS 短消息和WapPush 快速开发指南-接口介绍 SendSms:发送短消息 Parameter<?xml:namespace prefix = o /> Description ...
最新文章
- mysql与ofbiz,ofbiz+mysql安装求教
- 代理上网后localhost使用不了,只能使用127.0.0.1解决
- TCP协议连接的11种状态浅谈
- ITK:向转换工厂注册非默认转换
- 如何在Spring Boot应用程序中使用配置文件
- Revit二次开发 - C#程序员的佳好选择
- 【华为云技术分享】一行代码就能写一个日志打印组件,你信吗?为你揭晓LiteOS中日志打印组件的核心
- 对抗弱网下的音视频难题,声网正式开源抗丢包音频编解码器 Agora SOLO!
- 使用ubuntu 10.04中的中文乱码问题解决
- Python3.6机器学习sklearn中导入train_test_split库出错“Unresolved reference ‘train_test_split’”
- php连接mysql字符串函数_mysql 字符串函数
- Premiere视频导出格式
- AssertionError: Override list has odd length: [‘\r‘]; it must be a list of pairs
- 用Visio画流程图
- (三)树莓派系列教程:树莓派4B上编写Python程序(C语言),并运行
- SAP MM-MB52 物料库存查询简单操作
- P3554 LUK-Triumphal arch 解题报告
- UE4 打包问题总结
- 管理博文 畅购商城--oauth---09
- K8S污点taint的声明语法、污点的设置、查看和去除
热门文章
- Python中 __init__的通俗解释是什么?
- SAP 电商云 Spartacus UI 修改代码后,重新构建基于 SSR 版本的程序报错
- TypeScript 里 object 和 Object 的区别
- rxjs里的Observable对象subscribe方法的执行原理
- 如何访问SAP Spartacus里的config数据
- SAP应用有可能改造成Serverless架构么?
- Gradle的script API
- ABAP, Maven, CF App和Webpack的build
- SAP customer engagement center Fiori界面登录后的处理
- SAP UI5 walkthrough第一第二部分解析:data-sap-ui-libs=“sap.ui.commons,sap.ui.table“