├── .gitignore
├── 01_MicroPython-ESP32固件烧录
├── MicroPython-ESP32固件烧录.md
├── image
│ └── ad.png
└── 固件
│ ├── esp32spiram-20180919-v1.9.4-543-gdc77fdb7d.bin
│ └── flash.sh
├── 02_ESP32的REPL使用方法
├── ESP32的REPL使用方法.md
├── image
│ └── ad.png
└── src
│ ├── led.py
│ └── main.py
├── 03_GPIO输出与控制LED亮灭
├── GPIO输出控制小灯亮灭.md
├── image
│ └── analog_vs_digitial.jpeg
└── src
│ ├── RGB三色流水灯
│ ├── rgb_v1.py
│ ├── rgb_v2.py
│ ├── rgb_v3.py
│ └── rgb_v4.py
│ ├── led.py
│ ├── led_on.py
│ └── test_led.py
├── 04_延时函数与LED闪烁
├── image
│ └── helloworld.png
├── src
│ ├── blink_led_v1.py
│ ├── blink_led_v2.py
│ ├── delay_demo.py
│ └── led.py
└── 延时函数与LED闪烁.md
├── 05_PWM与呼吸灯
├── PWM与呼吸灯.md
├── image
│ └── pwm.jpg
└── src
│ ├── breath_led_v1.py
│ ├── breath_led_v2.py
│ ├── led.py
│ └── test_led.py
├── 06_GPIO输入与按键控制LED小灯
├── .vscode
│ └── settings.json
├── GPIO输入与用户按键控制LED开关.md
├── image
│ └── button-bouncing.png
└── src
│ ├── button_counter.py
│ ├── button_counter_by_irq.py
│ ├── button_ctrl_led.py
│ ├── button_read_value.py
│ ├── button_status.py
│ ├── led.py
│ └── 按键状态动态绘制_串口
│ ├── button_status_esp32_uart.py
│ └── button_status_pc_matplotlib.py
├── 07_IRQ外部中断与按键控制LED小灯
├── IRQ外部中断与按键控制LED小灯.md
└── src
│ ├── button.py
│ ├── button_irq_counter_v1.py
│ ├── button_irq_counter_v2.py
│ ├── button_irq_counter_v3.py
│ ├── button_irq_ctl_led_v1.py
│ ├── button_irq_ctl_led_v2.py
│ └── led.py
├── 08_ADC采样与电位计控制LED亮度
├── ADC采样与电位计控制LED亮度.md
├── image
│ └── dianweiji.png
└── src
│ ├── adc_ctl_led_light.py
│ ├── adc_demo.py
│ ├── adc_value_mean.py
│ ├── diy_voltmeter.py
│ └── photoregister_smart_light.py
├── 09_DAC数字转模拟
├── DAC数字转模拟.md
└── src
│ └── dac_demo_v1.py
├── 10_Timer定时器与LED闪烁
├── Timer定时器与LED闪烁.md
└── src
│ ├── print_hello_world_period.py
│ ├── print_timer_info.py
│ ├── timer_led_blink.py
│ └── timer_rgb_blink.py
├── 11_UART串口通信
├── UART串口通信.md
└── src
│ ├── 二进制Binary_ESP32发送PC接收
│ ├── binary_listener_pc_pyserial.py
│ ├── binary_publisher_esp32_uart.py
│ ├── decode_value.py
│ └── verify_test.py
│ ├── 二进制Binary_PC发送ESP32接收
│ ├── TODO
│ ├── binary_listener_esp32_uart.py
│ └── binary_publisher_pc_pyserial.py
│ ├── 二进制_ESP32自发自收
│ ├── binary_pub_and_recv.py
│ └── str_pub_and_recv.py
│ ├── 字节Byte_ESP32发送PC接收
│ ├── byte_listener_pc_pyserial.py
│ └── byte_publisher_esp32_uart.py
│ └── 字节Byte_PC发送ESP32接收
│ ├── byte_listener_esp32_uart.py
│ └── byte_publisher_pc_pyserial.py
├── 14_WebREPL与MIcroIDE配置教程
├── WebREPL与MIcroIDE配置教程.md
└── image
│ ├── bengbeng.jpeg
│ ├── boot-setup.png
│ ├── connect_wifi_success.png
│ ├── micro-ide-01.png
│ ├── microide-demo-02.png
│ ├── microide-demo-03.png
│ ├── microide-demo-05.png
│ ├── microide-demo-07.png
│ ├── microide-demo-08.png
│ ├── microide-demo-09.png
│ ├── microide-demo-10.png
│ ├── microide-demo-terminal.png
│ ├── microide-run-main.png
│ ├── set_password_for_webrepl.png
│ ├── webrepl_cfg_output.png
│ ├── webrepl_enable.png
│ ├── webrepl_setup.png
│ └── wifi_connect_choose_and_password.png
├── 15_MQTT入门之概念解析
├── MQTT入门之概念解析.md
└── image
│ ├── A1.png
│ ├── A2.png
│ ├── A3.png
│ ├── A4.png
│ ├── A5.jpeg
│ ├── A6-1.png
│ ├── A6-2.png
│ ├── A6-3.png
│ ├── A6-4.png
│ ├── A6-5.png
│ ├── A6-6.png
│ ├── MQTT_DHT_01-400x157.jpg
│ ├── MQTT_DHT_02-400x258.jpg
│ ├── mosquitto_pub_sub.png
│ └── paho-mqtt_pub_sub.png
├── 16_使用upip安装包
├── image
│ ├── emp-1zlab-pypi.png
│ └── install_emp_1zlab.png
└── 使用upip安装包.md
├── 17_ESP32接入WIFI热点
├── ESP32接入WIFI热点.md
└── image
│ ├── wifi_active.png
│ └── wifi_connect.png
├── 18_利用AMPY进行文件同步
└── 利用AMPY进行文件同步.md
├── 19_MQTT入门之项目实战
├── MQTT入门之项目实战.md
├── image
│ ├── MQTT_DHT_01-400x157.jpg
│ ├── MQTT_DHT_02-400x258.jpg
│ ├── mosquitto_pub_sub.png
│ └── paho-mqtt_pub_sub.png
└── src
│ ├── esp32
│ ├── publisher.py
│ └── subscriber.py
│ └── pc
│ ├── paho-mqtt-publisher.py
│ └── paho-mqtt-subsriber.py
├── LICENSE
├── README.md
└── libs
├── button.py
└── led.py
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 | MANIFEST
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 | .pytest_cache/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 | db.sqlite3
58 |
59 | # Flask stuff:
60 | instance/
61 | .webassets-cache
62 |
63 | # Scrapy stuff:
64 | .scrapy
65 |
66 | # Sphinx documentation
67 | docs/_build/
68 |
69 | # PyBuilder
70 | target/
71 |
72 | # Jupyter Notebook
73 | .ipynb_checkpoints
74 |
75 | # pyenv
76 | .python-version
77 |
78 | # celery beat schedule file
79 | celerybeat-schedule
80 |
81 | # SageMath parsed files
82 | *.sage.py
83 |
84 | # Environments
85 | .env
86 | .venv
87 | env/
88 | venv/
89 | ENV/
90 | env.bak/
91 | venv.bak/
92 |
93 | # Spyder project settings
94 | .spyderproject
95 | .spyproject
96 |
97 | # Rope project settings
98 | .ropeproject
99 |
100 | # mkdocs documentation
101 | /site
102 |
103 | # mypy
104 | .mypy_cache/
105 |
--------------------------------------------------------------------------------
/01_MicroPython-ESP32固件烧录/MicroPython-ESP32固件烧录.md:
--------------------------------------------------------------------------------
1 | # MicroPython-ESP32固件烧录-1Z实验室
2 |
3 | 教程搬家到:
4 | http://www.1zlab.com/article/micropython-esp32-firmware-upload/
--------------------------------------------------------------------------------
/01_MicroPython-ESP32固件烧录/image/ad.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/01_MicroPython-ESP32固件烧录/image/ad.png
--------------------------------------------------------------------------------
/01_MicroPython-ESP32固件烧录/固件/esp32spiram-20180919-v1.9.4-543-gdc77fdb7d.bin:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/01_MicroPython-ESP32固件烧录/固件/esp32spiram-20180919-v1.9.4-543-gdc77fdb7d.bin
--------------------------------------------------------------------------------
/01_MicroPython-ESP32固件烧录/固件/flash.sh:
--------------------------------------------------------------------------------
1 | sudo esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 esp32spiram-20180919-v1.9.4-543-gdc77fdb7d.bin
2 |
--------------------------------------------------------------------------------
/02_ESP32的REPL使用方法/ESP32的REPL使用方法.md:
--------------------------------------------------------------------------------
1 | # ESP32的REPL使用方法与文件同步-1Z实验室
2 | 教程搬家至:
3 | http://www.1zlab.com/article/micropython-esp32-repl/
--------------------------------------------------------------------------------
/02_ESP32的REPL使用方法/image/ad.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/02_ESP32的REPL使用方法/image/ad.png
--------------------------------------------------------------------------------
/02_ESP32的REPL使用方法/src/led.py:
--------------------------------------------------------------------------------
1 | '''
2 | 功能介绍: LED闪烁例程
3 | '''
4 | import utime
5 | import machine
6 |
7 | # 声明一个引脚 例如 D13 作为LED的引脚
8 | led_pin = machine.Pin(13, machine.Pin.OUT)
9 |
10 | while True:
11 | # 点亮LED -> 高电平
12 | led_pin.value(1)
13 | # 延时 500ms
14 | utime.sleep_ms(500)
15 | # 关闭LED -> 低电平
16 | led_pin.value(0)
17 | # 延时500ms
18 | utime.sleep_ms(500)
--------------------------------------------------------------------------------
/02_ESP32的REPL使用方法/src/main.py:
--------------------------------------------------------------------------------
1 | '''
2 | 功能介绍: LED闪烁例程
3 | '''
4 | import utime
5 | import machine
6 |
7 | # 声明一个引脚 例如 D13 作为LED的引脚
8 | led_pin = machine.Pin(13, machine.Pin.OUT)
9 |
10 | while True:
11 | # 点亮LED -> 高电平
12 | led_pin.value(1)
13 | # 延时 500ms
14 | utime.sleep_ms(500)
15 | # 关闭LED -> 低电平
16 | led_pin.value(0)
17 | # 延时500ms
18 | utime.sleep_ms(500)
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/GPIO输出控制小灯亮灭.md:
--------------------------------------------------------------------------------
1 | # GPIO控制小灯亮灭-1Z实验室
2 |
3 | 教程搬家至:
4 | http://www.1zlab.com/article/micropython-esp32-gpio-output-and-led-on-of-off/
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/image/analog_vs_digitial.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/03_GPIO输出与控制LED亮灭/image/analog_vs_digitial.jpeg
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/RGB三色流水灯/rgb_v1.py:
--------------------------------------------------------------------------------
1 | '''
2 | 流水灯 RGB彩灯
3 |
4 | 版本 v1
5 | 版本备注:
6 | 原始的方法, 逐行, 不借助函数。
7 | '''
8 |
9 | import machine
10 | import utime
11 |
12 | # 定义R 红色LED的引脚
13 | pinR = machine.Pin(18, machine.Pin.OUT)
14 | # 定义G 绿色LED的引脚
15 | pinG = machine.Pin(19, machine.Pin.OUT)
16 | # 定义B 蓝色LED的引脚
17 | pinB = machine.Pin(21, machine.Pin.OUT)
18 |
19 |
20 | while True:
21 | # 第一步:红色LED闪烁
22 | pinR.value(1) # 红色LED的管脚输出高电平
23 | utime.sleep_ms(500) # 延时500ms
24 | pinR.value(0) # 红色LED的管脚输出低电平
25 | utime.sleep_ms(500) # 延时500ms
26 |
27 | # 第二步:绿色LED闪烁
28 | pinG.value(1) # 绿色LED管脚输出高电平
29 | utime.sleep_ms(500)
30 | pinG.value(0) # 绿色LED管脚输出低电平
31 | utime.sleep_ms(500)
32 |
33 | # 第三步:蓝色LED闪烁
34 | pinB.value(1) # 蓝色LED管脚输出低电平
35 | utime.sleep_ms(500)
36 | pinB.value(0) # 蓝色LED管脚输出低电平
37 | utime.sleep_ms(500)
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/RGB三色流水灯/rgb_v2.py:
--------------------------------------------------------------------------------
1 | '''
2 | 流水灯 RGB彩灯
3 |
4 | 版本 v2
5 | 版本备注:
6 | 使用函数 led_blink() 简化代码
7 | '''
8 |
9 | import machine
10 | import utime
11 |
12 | # 定义R 红色LED的引脚
13 | pinR = machine.Pin(18, machine.Pin.OUT)
14 | # 定义G 绿色LED的引脚
15 | pinG = machine.Pin(19, machine.Pin.OUT)
16 | # 定义B 蓝色LED的引脚
17 | pinB = machine.Pin(21, machine.Pin.OUT)
18 |
19 |
20 | def led_blink(led_pin, delay_ms=500):
21 | '''
22 | 控制led的引脚(led_pin)进行闪烁
23 | 时间间隔为 delay_ms , 默认为500ms
24 | '''
25 | led_pin.value(1) # LED的管脚输出高电平
26 | utime.sleep_ms(delay_ms) # 延时500ms
27 | led_pin.value(0) # LED的管脚输出低电平
28 | utime.sleep_ms(delay_ms) # 延时500ms
29 |
30 |
31 | while True:
32 | # 第一步:红色LED闪烁
33 | led_blink(pinR)
34 |
35 | # 第二步:绿色LED闪烁
36 | led_blink(pinG)
37 |
38 | # 第三步:蓝色LED闪烁 时间间隔设定为1000ms = 1s
39 | led_blink(pinB, delay_ms=1000)
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/RGB三色流水灯/rgb_v3.py:
--------------------------------------------------------------------------------
1 | '''
2 | 流水灯 RGB彩灯
3 |
4 | 版本 v3
5 | 版本备注:
6 | 通过设定跑马灯的数组,简化流程
7 | '''
8 |
9 | import machine
10 | import utime
11 |
12 |
13 | '''
14 | # -------静态初始化led_pins数组---------
15 | # 定义R 红色LED的引脚
16 | pinR = machine.Pin(18, machine.Pin.OUT)
17 | # 定义G 绿色LED的引脚
18 | pinG = machine.Pin(19, machine.Pin.OUT)
19 | # 定义B 蓝色LED的引脚
20 | pinB = machine.Pin(21, machine.Pin.OUT)
21 |
22 | # 初始化led数组
23 | led_pins = [pinR, pinG, pinB]
24 | '''
25 |
26 | # -----动态加载led_pins数组-----
27 |
28 | led_pin_nums = [18, 19, 21] # 定义led对应的管脚
29 | led_pins = [] # 存储引脚对象数组
30 |
31 | for pin_num in led_pin_nums:
32 | # 声明一个新的pin对象
33 | pin = machine.Pin(pin_num, machine.Pin.OUT)
34 | # 将pin添加到数组led_pins的末尾
35 | led_pins.append(pin)
36 |
37 |
38 | def led_blink(led_pin, delay_ms=500):
39 | '''
40 | 控制led的引脚(led_pin)进行闪烁
41 | 时间间隔为 delay_ms , 默认为500ms
42 | '''
43 | led_pin.value(1) # LED的管脚输出高电平
44 | utime.sleep_ms(delay_ms) # 延时500ms
45 | led_pin.value(0) # LED的管脚输出低电平
46 | utime.sleep_ms(delay_ms) # 延时500ms
47 |
48 |
49 | while True:
50 |
51 | for pin in led_pins:
52 | # 遍历数组led_pins中所有的pin实例,并执行闪烁函数
53 | led_blink(pin)
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/RGB三色流水灯/rgb_v4.py:
--------------------------------------------------------------------------------
1 | '''
2 | 流水灯 RGB彩灯
3 |
4 | 版本 v4
5 | 版本备注:
6 | 利用列表推导式,初始化数组,简化流程
7 | '''
8 |
9 | import machine
10 | import utime
11 |
12 |
13 |
14 | led_pin_nums = [18, 19, 21] # 定义led对应的管脚
15 | # 使用Python的推到式生成 LED数组
16 | led_pins = [machine.Pin(pin_num, machine.Pin.OUT) for pin_num in led_pin_nums]
17 |
18 |
19 | def led_blink(led_pin, delay_ms=500):
20 | '''
21 | 控制led的引脚(led_pin)进行闪烁
22 | 时间间隔为 delay_ms , 默认为500ms
23 | '''
24 | led_pin.value(1) # LED的管脚输出高电平
25 | utime.sleep_ms(delay_ms) # 延时500ms
26 | led_pin.value(0) # LED的管脚输出低电平
27 | utime.sleep_ms(delay_ms) # 延时500ms
28 |
29 |
30 | while True:
31 |
32 | for pin in led_pins:
33 | # 遍历数组led_pins中所有的pin实例,并执行闪烁函数
34 | led_blink(pin)
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/led.py:
--------------------------------------------------------------------------------
1 | from machine import Pin
2 |
3 | class LED:
4 | def __init__(self, led_id):
5 | # LED字典
6 | # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
7 | led_list = [(2, False, True),(13, True, False)]
8 |
9 | if led_id >= len(led_list) or led_id < 0:
10 | print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
11 | return None
12 |
13 | gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
14 | self.led_pin = Pin(gpio_id, Pin.OUT)
15 |
16 | def on(self):
17 | '''
18 | 打开LED
19 | '''
20 | self.led_pin.value(self.LED_ON)
21 |
22 | def off(self):
23 | '''
24 | 关闭LED
25 | '''
26 | self.led_pin.value(self.LED_OFF)
27 |
28 | def toggle(self):
29 | '''
30 | 切换LED的状态
31 | OFF -> ON
32 | ON -> OFF
33 | '''
34 | self.led_pin.value(not self.led_pin.value())
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/led_on.py:
--------------------------------------------------------------------------------
1 | from machine import Pin
2 | # 创建一个Pin的对象,传入GPIO的编号
3 | led_pin = Pin(3, Pin.OUT)
4 | # Pin13 输出高电平 打开LED
5 | LED_ON = 0 # LED亮的时候的电平
6 | LED_OFF = 1 # LED灭的时候的电平
7 |
8 | # 小灯灭
9 | # led_pin.value(LED_OFF)
10 |
11 | # 小灯亮
12 | led_pin.value(LED_ON)
--------------------------------------------------------------------------------
/03_GPIO输出与控制LED亮灭/src/test_led.py:
--------------------------------------------------------------------------------
1 | from led import LED
2 | # 提示: 你也可以尝试LED(1)
3 | # 创建一个LED对象
4 | led0 = LED(0)
5 |
6 | # 开启LED
7 | led0.on()
8 |
9 | # 关闭LED
10 | # led0.off()
11 |
12 | # 切换LED的状态
13 | # led0.toggle()
--------------------------------------------------------------------------------
/04_延时函数与LED闪烁/image/helloworld.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/04_延时函数与LED闪烁/image/helloworld.png
--------------------------------------------------------------------------------
/04_延时函数与LED闪烁/src/blink_led_v1.py:
--------------------------------------------------------------------------------
1 | '''
2 | 功能介绍: LED闪烁例程
3 |
4 | 版本: v1
5 | 版本说明:
6 | 逐行控制高低电平与延迟
7 | '''
8 | import utime
9 | from led import LED
10 |
11 | # 声明一个LED对象 (P2)
12 | led = LED(0)
13 |
14 | while True:
15 | # 点亮LED
16 | led.on()
17 | # 延时 500ms
18 | utime.sleep_ms(500)
19 | # 关闭LED
20 | led.off()
21 | # 延时500ms
22 | utime.sleep_ms(500)
--------------------------------------------------------------------------------
/04_延时函数与LED闪烁/src/blink_led_v2.py:
--------------------------------------------------------------------------------
1 | '''
2 | 功能介绍: LED闪烁例程
3 |
4 | 版本 v2
5 | 版本说明: 利用函数实现led闪烁
6 | '''
7 | import utime
8 | from led import LED
9 |
10 | # 声明一个LED (P2)
11 | led = LED(0)
12 |
13 | def led_blink(led_pin, delay_ms=500):
14 | '''
15 | 控制led的引脚(led_pin)进行闪烁
16 | 时间间隔为 delay_ms , 默认为500ms
17 | '''
18 | global led
19 | led.toggle() # 切换LED的状态
20 | utime.sleep_ms(delay_ms) # 延时500ms
21 |
22 | while True:
23 | led_blink(led_pin, delay_ms=500)
24 | # led_blink(led_pin, delay_ms=100)
--------------------------------------------------------------------------------
/04_延时函数与LED闪烁/src/delay_demo.py:
--------------------------------------------------------------------------------
1 |
2 | '''
3 | 演示延时功能
4 | 每隔1s(1000 ms) 打印一个hello_world
5 | '''
6 |
7 | import utime
8 |
9 |
10 | while True:
11 | print('Hello World!!!')
12 | utime.sleep_ms(1000)
13 |
--------------------------------------------------------------------------------
/04_延时函数与LED闪烁/src/led.py:
--------------------------------------------------------------------------------
1 | from machine import Pin
2 |
3 | class LED:
4 | def __init__(self, led_id):
5 | # LED字典
6 | # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
7 | led_list = [(2, False, True),(13, True, False)]
8 |
9 | if led_id >= len(led_list) or led_id < 0:
10 | print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
11 | return None
12 |
13 | gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
14 | self.led_pin = Pin(gpio_id, Pin.OUT)
15 |
16 | def on(self):
17 | '''
18 | 打开LED
19 | '''
20 | self.led_pin.value(self.LED_ON)
21 |
22 | def off(self):
23 | '''
24 | 关闭LED
25 | '''
26 | self.led_pin.value(self.LED_OFF)
27 |
28 | def toggle(self):
29 | '''
30 | 切换LED的状态
31 | OFF -> ON
32 | ON -> OFF
33 | '''
34 | self.led_pin.value(not self.led_pin.value())
--------------------------------------------------------------------------------
/04_延时函数与LED闪烁/延时函数与LED闪烁.md:
--------------------------------------------------------------------------------
1 | # 延时函数与LED闪烁
2 |
3 | 教程搬家至
4 | http://www.1zlab.com/article/micropython-esp32-delay-and-led-blink/
--------------------------------------------------------------------------------
/05_PWM与呼吸灯/PWM与呼吸灯.md:
--------------------------------------------------------------------------------
1 | # PWM与呼吸灯-MicroPython-ESP32-1Z实验室
2 |
3 | 教程搬家到:
4 | http://www.1zlab.com/article/pwm-control-and-breath-led/
--------------------------------------------------------------------------------
/05_PWM与呼吸灯/image/pwm.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/05_PWM与呼吸灯/image/pwm.jpg
--------------------------------------------------------------------------------
/05_PWM与呼吸灯/src/breath_led_v1.py:
--------------------------------------------------------------------------------
1 | '''
2 | 呼吸灯测试程序
3 | v1
4 | '''
5 | import machine
6 | import utime, math
7 |
8 | # 初始化一个PWM对象叫做 led_pwm
9 | led_pwm = machine.PWM(machine.Pin(12), freq=1000)
10 |
11 | def pulse(led_pwm, delay):
12 | # 呼吸灯核心代码
13 | # 借用sin正弦函数,将PWM范围控制在 0 - 1000范围内
14 | for i in range(20):
15 | led_pwm.duty(int(math.sin(i / 10 * math.pi) * 500 + 500))
16 | # 延时delay个ms
17 | utime.sleep_ms(delay)
18 | try:
19 | while True:
20 | pulse(led_pwm, 50)
21 | except:
22 | # 销毁PWM对象
23 | led_pwm.deinit()
--------------------------------------------------------------------------------
/05_PWM与呼吸灯/src/breath_led_v2.py:
--------------------------------------------------------------------------------
1 | '''
2 | 呼吸灯测试程序
3 | v2 使用led.py
4 | '''
5 | import machine
6 | import utime, math
7 | from led import LED
8 |
9 | # 初始化一个PWM对象叫做 led_pwm
10 | led = LED(0)
11 |
12 | def pulse(led, delay):
13 | # 呼吸灯核心代码
14 | # 借用sin正弦函数,将PWM范围控制在 0 - 1000范围内
15 | for i in range(20):
16 | value = int(math.sin(i / 10 * math.pi) * 500 + 500)
17 | # 设置LED的亮度
18 | led.intensity(value)
19 | # 延时delay个ms
20 | utime.sleep_ms(delay)
21 | try:
22 | while True:
23 | pulse(led, 50)
24 | except:
25 | # 销毁PWM对象
26 | led.deinit()
--------------------------------------------------------------------------------
/05_PWM与呼吸灯/src/led.py:
--------------------------------------------------------------------------------
1 | '''
2 | LED类
3 | v2 添加LED亮度控制
4 | '''
5 | from machine import Pin, PWM
6 |
7 | class LED:
8 | def __init__(self, led_id):
9 | # LED字典
10 | # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
11 | led_list = [(2, False, True),(13, True, False)]
12 |
13 | if led_id >= len(led_list) or led_id < 0:
14 | print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
15 | return None
16 |
17 | gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
18 | self.pin = Pin(gpio_id, Pin.OUT)
19 | self.pwm = PWM(self.pin, freq=1000)
20 |
21 | def on(self):
22 | '''
23 | 打开LED
24 | '''
25 | self.pin.value(self.LED_ON)
26 |
27 | def off(self):
28 | '''
29 | 关闭LED
30 | '''
31 | self.pin.value(self.LED_OFF)
32 |
33 | def toggle(self):
34 | '''
35 | 切换LED的状态
36 | OFF -> ON
37 | ON -> OFF
38 | '''
39 | self.pin.value(not self.pin.value())
40 |
41 | def intensity(self, value):
42 | '''
43 | 设置LED的亮度
44 | '''
45 | if self.LED_ON == True:
46 | self.pwm.duty(value)
47 | else:
48 | self.pwm.duty(1023 - value)
49 |
50 | def deinit(self):
51 | '''
52 | 销毁资源
53 | '''
54 | self.pwm.deinit()
--------------------------------------------------------------------------------
/05_PWM与呼吸灯/src/test_led.py:
--------------------------------------------------------------------------------
1 | from led import LED
2 |
3 | # 实例化一个LED
4 | led = LED(0)
5 | # 设置LED的亮度
6 | led.intensity(500)
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "python.linting.enabled": false
3 | }
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/GPIO输入与用户按键控制LED开关.md:
--------------------------------------------------------------------------------
1 | # GPIO输入与用户按键控制LED开关-1Z实验室
2 |
3 |
4 | 教程搬家到:
5 |
6 | http://www.1zlab.com/article/micropython-esp32-gpio-output-button-control-led/
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/image/button-bouncing.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/06_GPIO输入与按键控制LED小灯/image/button-bouncing.png
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/button_counter.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键计数器 每按一下,数值加1
3 |
4 | 存在按键抖动的问题
5 | '''
6 | from machine import Pin
7 | import utime
8 |
9 | # 用户按键GPIO
10 | # PyESPCar上自带的用户按键在39号管脚
11 | USER_BTN = 39
12 | # 按键引脚对象
13 | button = Pin(USER_BTN, Pin.IN)
14 |
15 | # 定义按键按下的值 (取决于按键模块的设计, 有可能相反)
16 | BTN_DOWN = 0 # 按键按下对应的取值
17 | BTN_UP = 1 # 按键抬起对应的状态
18 |
19 | # 记录上一次按键的状态
20 | last_btn_status = BTN_UP
21 | counter = 0 # 计数器
22 |
23 | print("按下按键, 会计数哦")
24 | while True:
25 | # 获取按钮状态
26 | btn_status = button.value()
27 |
28 | if btn_status == BTN_DOWN and last_btn_status == BTN_UP:
29 | print("按键按下")
30 | counter += 1
31 | print("Counter += 1 ; Counter = %d"%(counter))
32 |
33 | last_btn_status = btn_status
34 | # 延时100ms
35 | utime.sleep_ms(100)
36 |
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/button_counter_by_irq.py:
--------------------------------------------------------------------------------
1 | '''
2 | 利用按键中断, 实现按键计数器 每按一下,数值加1
3 | '''
4 |
5 | from machine import Pin
6 | import utime
7 |
8 | # 用户按键GPIO
9 | # PyESPCar上自带的用户按键在39号管脚
10 | USER_BTN = 39
11 | # 引脚
12 | button = Pin(USER_BTN, Pin.IN)
13 |
14 | # 定义按键按下的值 (取决于按键模块的设计, 有可能相反)
15 | # BTN_DOWN = 0 # 按键按下对应的取值
16 | # BTN_UP = 1 # 按键抬起对应的状态
17 |
18 | counter = 0 # 计数器
19 |
20 | def counter_callback(pin):
21 | '''
22 | 计数器回调函数
23 | '''
24 |
25 | global counter
26 |
27 | # 添加软件消抖
28 | utime.sleep_ms(150)
29 | if pin.value() == 0:
30 | counter += 1
31 | print("Counter += 1 ; Counter = %d"%(counter))
32 |
33 |
34 | button.irq(trigger=Pin.IRQ_FALLING, handler=counter_callback)
35 | print("按下按键, 会计数哦")
36 |
37 | while True:
38 | print("do something...")
39 | utime.sleep_ms(500)
40 |
41 |
42 |
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/button_ctrl_led.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键控制LED亮灭
3 | 状态转换
4 | '''
5 | from machine import Pin
6 | import utime
7 | from led import LED
8 | # 按键
9 | # 用户按键GPIO
10 | # PyESPCar上自带的用户按键在39号管脚
11 | USER_BTN = 39
12 | # 按键引脚对象
13 | button = Pin(USER_BTN, Pin.IN)
14 | # 创建一个LED对象
15 | led = LED(0)
16 |
17 |
18 | # 定义按键按下的值 (取决于按键模块的设计, 有可能相反)
19 | BTN_DOWN = 0 # 按键按下对应的取值
20 | BTN_UP = 1 # 按键抬起对应的状态
21 | last_btn_status = None
22 |
23 | while True:
24 | # 获取按钮状态
25 | btn_status = button.value()
26 |
27 | if btn_status == BTN_DOWN and last_btn_status == BTN_UP:
28 | # 切换LED状态
29 | led.toggle()
30 | print("按键按下,LED状态转换 LED: {}".format(led.pin.value() == led.LED_ON))
31 |
32 | last_btn_status = btn_status
33 | # 延时500ms
34 | utime.sleep_ms(150)
35 |
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/button_read_value.py:
--------------------------------------------------------------------------------
1 | from machine import Pin
2 |
3 | # 用户按键GPIO
4 | # PyESPCar上自带的用户按键在39号管脚
5 | USER_BTN = 39
6 | # 按键引脚对象
7 | button = Pin(USER_BTN, Pin.IN)
8 |
9 | # 打印当前按键的电平 0 / 1
10 | button.value()
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/button_status.py:
--------------------------------------------------------------------------------
1 | '''
2 | 打印按键的状态
3 | '''
4 | from machine import Pin
5 | import utime
6 |
7 | # 用户按键GPIO
8 | # PyESPCar上自带的用户按键在39号管脚
9 | USER_BTN = 39
10 | # 按键引脚对象
11 | button = Pin(USER_BTN, Pin.IN)
12 |
13 | # 定义按键按下的值 (取决于按键模块的设计, 有可能相反)
14 | BTN_DOWN = 0 # 按键按下对应的取值
15 | BTN_UP = 1 # 按键抬起对应的状态
16 |
17 | while True:
18 | # 获取按钮状态
19 | btn_status = button.value()
20 |
21 | if btn_status == BTN_DOWN:
22 | print("按键状态:按下 <<<<<<<<")
23 | else:
24 | print("按键状态:抬起 ========")
25 | # 延时500ms
26 | utime.sleep_ms(100)
27 |
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/led.py:
--------------------------------------------------------------------------------
1 | '''
2 | LED类
3 | v2 添加LED亮度控制
4 | '''
5 | from machine import Pin, PWM
6 |
7 | class LED:
8 | def __init__(self, led_id):
9 | # LED字典
10 | # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
11 | led_list = [(2, False, True),(13, True, False)]
12 |
13 | if led_id >= len(led_list) or led_id < 0:
14 | print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
15 | return None
16 |
17 | gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
18 | self.pin = Pin(gpio_id, Pin.OUT)
19 | self.pwm = PWM(self.pin, freq=1000)
20 |
21 | def on(self):
22 | '''
23 | 打开LED
24 | '''
25 | self.pin.value(self.LED_ON)
26 |
27 | def off(self):
28 | '''
29 | 关闭LED
30 | '''
31 | self.pin.value(self.LED_OFF)
32 |
33 | def toggle(self):
34 | '''
35 | 切换LED的状态
36 | OFF -> ON
37 | ON -> OFF
38 | '''
39 | self.pin.value(not self.pin.value())
40 |
41 | def intensity(self, value):
42 | '''
43 | 设置LED的亮度
44 | '''
45 | if self.LED_ON == True:
46 | self.pwm.duty(value)
47 | else:
48 | self.pwm.duty(1023 - value)
49 |
50 | def deinit(self):
51 | '''
52 | 销毁资源
53 | '''
54 | self.pwm.deinit()
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/按键状态动态绘制_串口/button_status_esp32_uart.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | 获取按键的状态并通过串口发送
4 |
5 | 接线:
6 | ESP32,TDX,D12 -> RXD,CP2102 (USB转TTL)
7 | ESP32,RXD,D13 -> TXD,CP2102 (USB转TTL)
8 | '''
9 |
10 | from machine import UART,Pin
11 | import utime
12 | import struct
13 |
14 | # 初始化串口 UART
15 | # 波特率 115200
16 | # rx -> Pin 13
17 | # tx -> Pin 12
18 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
19 | # 引脚
20 | button = Pin(5, Pin.IN)
21 |
22 | # 定义按键按下的值 (取决于按键模块的设计, 有可能相反)
23 | BTN_DOWN = 0 # 按键按下对应的取值
24 | BTN_UP = 1 # 按键抬起对应的状态
25 |
26 |
27 | while True:
28 | # 获取按钮状态
29 | btn_status = button.value()
30 | info = "{}\n".format(int(btn_status == BTN_DOWN))
31 | # 串口发送
32 | uart.write(info)
33 | # 打印原始的字节数据()
34 | print("Send字符: "+info+"\n")
35 | utime.sleep_ms(10)
36 |
37 |
--------------------------------------------------------------------------------
/06_GPIO输入与按键控制LED小灯/src/按键状态动态绘制_串口/button_status_pc_matplotlib.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | import serial
3 | import struct
4 | from matplotlib import pyplot as plt
5 | import threading, multiprocessing
6 | import time
7 | # 串口号 默认为 /dev/ttyUSB0
8 | ser_dev = '/dev/ttyUSB1'
9 |
10 | # 创建一个串口实例
11 | ser = serial.Serial(ser_dev,115200, timeout=1, bytesize=8)
12 |
13 |
14 | data_window = 1000 # 保留最近的400个数据点
15 | data_buffer = [0]*data_window # 数据缓存池
16 |
17 |
18 | def update_canvas():
19 | global data_buffer
20 | '''
21 | 更新绘制数据
22 | '''
23 | while True:
24 | print("Begin 更新画面")
25 | plt.plot(data_buffer)
26 | plt.draw()
27 | plt.pause(0.04)
28 | print("END 更新画面")
29 |
30 | def update_btn_status():
31 | global ser
32 | global data_buffer
33 |
34 | while True:
35 | # 读入一行数据 \n是换行符号 数据样式类似 '1\n' 或者 '0\n'
36 | info = ser.readline()
37 | # 讲接收的字节流转换成容易读取的样式
38 | print("Recv字符: "+info)
39 | # 注意接收过来的是字符串格式的数据
40 | # '1' -> 按键按下
41 | # ‘0’ -> 按键抬起
42 | print("按键状态 是否按下: {}".format(info[0]=='1'))
43 | # print("按键状态 是否按下: {}".format(int(info[0])==1))
44 |
45 | data_buffer.pop(0)
46 | btn_status = int(info[0])
47 | data_buffer.append(btn_status)
48 | time.sleep(0.001)
49 |
50 | plt.ylim((-1, 2))
51 | plt.ion()
52 | plt.show()
53 |
54 |
55 | thread_canvas = threading.Thread(target=update_canvas)
56 | thread_canvas.start()
57 |
58 | thread_serial = threading.Thread(target=update_btn_status)
59 | thread_serial.start()
60 |
61 |
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/IRQ外部中断与按键控制LED小灯.md:
--------------------------------------------------------------------------------
1 | # IRQ外部中断与按键控制LED小灯
2 |
3 | 教程搬家到:
4 | http://www.1zlab.com/article/micropython-esp32-irq-interrupt-and-button-control-led/
5 |
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/button.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键类Button
3 | '''
4 | from machine import Pin
5 | import utime
6 |
7 | class Button(object):
8 | '''
9 | 按键对象
10 | '''
11 | def __init__(self,button_idx, callback=None):
12 | # 按键字典
13 | # 数据结构: (GPIO编号,按键抬起的电平, 按键按下的电平)
14 | button_list = [(39, False, True)]
15 |
16 | if button_idx < 0 or button_idx >= len(button_list):
17 | print("ERROR: Wrong Button Index")
18 | print("Valid Button Index: {} - {}".format(0, len(button_list)-1))
19 | return None
20 |
21 | gpio_id, self.BUTTON_RELEASE, self.BUTTON_PRESS, = button_list[button_idx]
22 | # 按键
23 | self.pin = Pin(gpio_id, Pin.IN)
24 | # 回调函数
25 | self.callback = callback
26 | # 设置外部中断
27 | if self.BUTTON_PRESS == True:
28 | self.pin.irq(trigger=Pin.IRQ_RISING, handler=self.irq_handler)
29 | else:
30 | self.pin.irq(trigger=Pin.IRQ_FALLING, handler=self.irq_handler)
31 |
32 | # 标志位 当前是否可以相应按键中断
33 | self.flag = True
34 |
35 | def irq_handler(self, irq_pin):
36 | '''
37 | 外部中断的相应函数
38 | '''
39 | # 如果当前正在处理中断,则忽略
40 | if not self.flag:
41 | return
42 | # 添加软件滤波
43 | utime.sleep_ms(50)
44 | if self.pin.value() == self.BUTTON_PRESS:
45 | # 判断当前按键状态是不是按下,如果是,则执行回调函数
46 | if self.flag and self.callback is not None:
47 | self.flag = False
48 | # 执行回调函数
49 | self.callback(self.pin)
50 | self.flag = True
51 |
52 | def deinit(self):
53 | '''
54 | 销毁资源
55 | '''
56 | self.pin.irq(trigger=0, handler=None) # 销毁外部中断的资源
57 |
58 |
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/button_irq_counter_v1.py:
--------------------------------------------------------------------------------
1 | '''
2 | [功能描述]
3 | 按键外部中断 记录中断的次数
4 |
5 | [备注]
6 | 按键模块 未按下的电平是1, 按下电平是0, 所以要检测是电平的下降沿 IRQ_FALLING
7 |
8 | [存在问题]
9 | 按下一次,但是计数器增加多个,这个是因为按键在按下的时候产生了 抖动
10 | 所以我们需要做消抖操作!!!
11 | '''
12 | from machine import Pin
13 |
14 | # 用户按键GPIO
15 | # PyESPCar上自带的用户按键在39号管脚
16 | USER_BTN = 39
17 | # 引脚
18 | button = Pin(USER_BTN, Pin.IN)
19 |
20 | counter = 0 # 中断计数器
21 | def button_irq_handle(pin):
22 | '''
23 | 外部中断处理函数
24 | 回调函数传入的参数为产生中断的Pin管脚对象
25 | '''
26 | global counter
27 | print('外部中断 下降沿触发, 中断计数:{}'.format(counter))
28 | print('当前管脚电平 : {}'.format(pin.value()))
29 | counter += 1
30 |
31 | # 下降沿触发
32 | button.irq(trigger=Pin.IRQ_RISING, handler=button_irq_handle)
33 |
34 |
35 | try:
36 | while True:
37 | pass
38 | except:
39 | # 释放按键的IRQ资源
40 | button.irq(trigger=0, handler=None)
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/button_irq_counter_v2.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键IRQ计数器 V2
3 | '''
4 | from button import Button
5 |
6 | counter = 0
7 | def callback(irq_pin):
8 | '''
9 | 切换LED的状态
10 | '''
11 | global counter
12 |
13 | counter += 1
14 | print('Counter: {}'.format(counter))
15 |
16 |
17 | # 创建一个Button对象,设置回调函数为callback
18 | button = Button(0, callback)
19 |
20 | try:
21 | while True:
22 | pass
23 | except:
24 | button.deinit()
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/button_irq_counter_v3.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键IRQ计数器 V3
3 | '''
4 | from button import Button
5 |
6 | counter = 0
7 | def callback(irq_pin):
8 | '''
9 | 切换LED的状态
10 | '''
11 | global counter
12 |
13 | counter += 1
14 |
15 |
16 |
17 | # 创建一个Button对象,设置回调函数为callback
18 | button = Button(0, callback)
19 |
20 | # 记录上一次counter的取值
21 | old_counter = counter
22 | try:
23 | while True:
24 | if old_counter != counter:
25 | print('Counter: {}'.format(counter))
26 | old_counter = counter
27 | except:
28 | button.deinit()
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/button_irq_ctl_led_v1.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键IRQ(外部中断)切换LED状态V1
3 | '''
4 | from machine import Pin
5 | import utime
6 | from led import LED
7 |
8 | # 定义按键按下的值 (取决于按键模块的设计, 有可能相反)
9 | BTN_DOWN = 1 # 按键按下对应的取值
10 | BTN_UP = 0 # 按键抬起对应的状态
11 |
12 | # 用户按键GPIO
13 | # PyESPCar上自带的用户按键在39号管脚
14 | USER_BTN = 39
15 | # 引脚
16 | button = Pin(USER_BTN, Pin.IN)
17 |
18 | # 创建一个LED对象
19 | led = LED(0)
20 |
21 | def button_irq_handle(button):
22 | '''
23 | 外部中断处理函数
24 | 回调函数传入的参数为产生中断的Pin管脚对象
25 | '''
26 | global BTN_DOWN
27 | global led
28 | # 延时消抖
29 | utime.sleep_ms(50)
30 | if button.value() == BTN_DOWN:
31 | # 状态取反
32 | led.toggle()
33 |
34 | # 下降沿触发
35 | button.irq(trigger=Pin.IRQ_FALLING, handler=button_irq_handle)
36 |
37 | try:
38 | while True:
39 | pass
40 | except:
41 | # 释放按键的IRQ资源
42 | button.irq(trigger=0, handler=None)
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/button_irq_ctl_led_v2.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键IRQ切换LED状态V2
3 |
4 | 简直完美
5 | '''
6 | from led import LED
7 | from button import Button
8 |
9 |
10 | # 创建LED对象
11 | led = LED(0)
12 |
13 | def callback(irq_pin):
14 | '''
15 | 切换LED的状态
16 | '''
17 | global led
18 | led.toggle()
19 |
20 | # 创建一个Button对象,设置回调函数为callback
21 | button = Button(0, callback)
22 |
23 | try:
24 | while True:
25 | pass
26 | except:
27 | button.deinit()
--------------------------------------------------------------------------------
/07_IRQ外部中断与按键控制LED小灯/src/led.py:
--------------------------------------------------------------------------------
1 | '''
2 | LED类
3 | v2 添加LED亮度控制
4 | '''
5 | from machine import Pin, PWM
6 |
7 | class LED:
8 | def __init__(self, led_id):
9 | # LED字典
10 | # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
11 | led_list = [(2, False, True),(13, True, False)]
12 |
13 | if led_id >= len(led_list) or led_id < 0:
14 | print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
15 | return None
16 |
17 | gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
18 | self.pin = Pin(gpio_id, Pin.OUT)
19 | self.pwm = PWM(self.pin, freq=1000)
20 |
21 | def on(self):
22 | '''
23 | 打开LED
24 | '''
25 | self.pin.value(self.LED_ON)
26 |
27 | def off(self):
28 | '''
29 | 关闭LED
30 | '''
31 | self.pin.value(self.LED_OFF)
32 |
33 | def toggle(self):
34 | '''
35 | 切换LED的状态
36 | OFF -> ON
37 | ON -> OFF
38 | '''
39 | self.pin.value(not self.pin.value())
40 |
41 | def intensity(self, value):
42 | '''
43 | 设置LED的亮度
44 | '''
45 | if self.LED_ON == True:
46 | self.pwm.duty(value)
47 | else:
48 | self.pwm.duty(1023 - value)
49 |
50 | def deinit(self):
51 | '''
52 | 销毁资源
53 | '''
54 | self.pwm.deinit()
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/ADC采样与电位计控制LED亮度.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # ADC采样与电位计控制LED亮度
4 |
5 | 教程搬家至:
6 | http://www.1zlab.com/article/micropython-esp32-adc-sample-and-encoder-control-led/
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/image/dianweiji.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/08_ADC采样与电位计控制LED亮度/image/dianweiji.png
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/src/adc_ctl_led_light.py:
--------------------------------------------------------------------------------
1 | '''
2 | 电位计采样,控制LED的亮度
3 | '''
4 | from machine import ADC,Pin,PWM
5 | import utime
6 |
7 | # 2号引脚作为led引脚
8 | led_pin = Pin(2, Pin.OUT)
9 | led_pwm = PWM(led_pin)
10 | led_pwm.duty(0)
11 | led_pwm.freq(1000)
12 |
13 | # 设置D34号引脚作为ADC采样引脚
14 | pin_read = Pin(34,Pin.IN)
15 | # 声明ADC对象
16 | adc = ADC(pin_read)
17 | # 设置衰减比 满量程3.3v
18 | adc.atten(ADC.ATTN_11DB)
19 | # 设置数据宽度为10bit
20 | adc.width(ADC.WIDTH_10BIT)
21 |
22 | def mean_filter(adc, sample_times = 10):
23 | # 做一个简单的均值滤波
24 | value_sum = 0
25 | for i in range(sample_times):
26 | value_sum += adc.read()
27 | # 计算ADC采样的均值
28 | value_mean = value_sum / sample_times
29 | return value_mean
30 |
31 | last_value = 0
32 | while True:
33 | try:
34 | value_mean = mean_filter(adc)
35 | # 判断是否发生了变化
36 | if abs(last_value-value_mean) > 2:
37 | # 打印日志
38 | print("电位计采样: %d"%value_mean)
39 | # 根据adc采样 设定LED的duty
40 | # ADC采样与PWM占空比的范围都设定的是0-1023
41 | led_pwm.duty(int(value_mean))
42 | # 更新last value
43 | last_value = value_mean
44 | # 延时100ms
45 | utime.sleep_ms(100)
46 | except:
47 | # 释放PWM资源
48 | led_pwm.deinit()
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/src/adc_demo.py:
--------------------------------------------------------------------------------
1 | '''
2 | 点位计 ADC采样
3 | 定时打印ADC采样的结果
4 | '''
5 | from machine import ADC,Pin
6 | import utime
7 |
8 |
9 | # 设置D34号引脚作为ADC采样引脚
10 | pin_read = Pin(34,Pin.IN)
11 | # 声明ADC对象
12 | adc = ADC(pin_read)
13 | # 设置衰减比 满量程3.3v
14 | adc.atten(ADC.ATTN_11DB)
15 | # 设置数据宽度为10bit
16 | adc.width(ADC.WIDTH_10BIT)
17 |
18 | while True:
19 | # 数据采样 数值范围0-1023
20 | value = adc.read()
21 | # 打印日志
22 | print("Value: %d"%value)
23 | # 延时500ms
24 | utime.sleep_ms(500)
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/src/adc_value_mean.py:
--------------------------------------------------------------------------------
1 | '''
2 | 检测ADC采样值是否发生变化,如果发生变化就打印出来
3 | 采用均值滤波
4 | '''
5 | from machine import ADC,Pin
6 | import utime
7 |
8 |
9 | # 设置D34号引脚作为ADC采样引脚
10 | pin_read = Pin(34,Pin.IN)
11 | # 声明ADC对象
12 | adc = ADC(pin_read)
13 | # 设置衰减比 满量程3.3v
14 | adc.atten(ADC.ATTN_11DB)
15 | # 设置数据宽度为10bit
16 | adc.width(ADC.WIDTH_10BIT)
17 |
18 | last_value = 0
19 | sample_times = 10
20 | while True:
21 | # 做一个简单的均值滤波
22 | value_sum = 0
23 | for i in range(sample_times):
24 | value_sum += adc.read()
25 | # 计算ADC采样的均值
26 | value_mean = value_sum / sample_times
27 | # 判断是否发生了变化
28 | if abs(last_value-value_mean) > 2:
29 | # 打印日志
30 | print("电位计采样: %d"%value_mean)
31 | # 更新last value
32 | last_value = value_mean
33 | # 延时100ms
34 | utime.sleep_ms(100)
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/src/diy_voltmeter.py:
--------------------------------------------------------------------------------
1 | '''
2 | 利用ADC 自制电压表
3 |
4 | 数字的数值范围 ,0-4095
5 | 测量范围 0v - 3.3v 注意: 不能越界
6 | '''
7 | from machine import ADC,Pin
8 | import utime
9 |
10 | # 设置D34号引脚作为ADC采样引脚
11 | pin_read = Pin(34,Pin.IN)
12 | # 声明ADC对象
13 | adc = ADC(pin_read)
14 |
15 |
16 | def digtal2volt(digital_value):
17 | '''
18 | 数字信号转换为实际的电压值
19 | '''
20 | volt = (digital_value / 4095) * 3.3
21 | return volt
22 |
23 |
24 | while True:
25 | # 数据采样 模拟信号 -> 数字信号
26 | value = adc.read()
27 | # 打印日志
28 | print("Value: %d -> Volt: %.2fv"%(value, digtal2volt(value)))
29 | # 延时1s
30 | utime.sleep_ms(1000)
31 |
--------------------------------------------------------------------------------
/08_ADC采样与电位计控制LED亮度/src/photoregister_smart_light.py:
--------------------------------------------------------------------------------
1 | '''
2 | Smart LED 基于光敏电阻的智能照明
3 | 如果光照强度小于某个阈值,就开灯。
4 | '''
5 |
6 |
7 | from machine import ADC,Pin
8 | import utime
9 |
10 | led_pin = Pin(2, Pin.OUT)
11 | # 设置D33号引脚作为ADC采样引脚
12 | pin_read = Pin(33, Pin.IN)
13 |
14 | # 声明ADC对象
15 | adc = ADC(pin_read)
16 | adc.atten(ADC.ATTN_11DB)
17 | adc.width(ADC.WIDTH_12BIT)
18 |
19 | # 设定阈值
20 | boudary = 2000
21 |
22 | while True:
23 | # 数据采样 模拟信号 -> 数字信号
24 | # 读取光照强度 illumination intensity
25 | intensity = adc.read()
26 |
27 | is_dark = intensity > boudary
28 | if is_dark:
29 | led_pin.value(1)
30 | else:
31 | led_pin.value(0)
32 | # 如果光照强度
33 |
34 | # 打印日志
35 | print("is Too Dark: {}".format(is_dark))
36 | utime.sleep_ms(1000)
37 |
38 |
--------------------------------------------------------------------------------
/09_DAC数字转模拟/DAC数字转模拟.md:
--------------------------------------------------------------------------------
1 | # DAC数字转模拟
2 |
3 | 教程搬家至:
4 | http://www.1zlab.com/article/micropython-esp32-dac/
--------------------------------------------------------------------------------
/09_DAC数字转模拟/src/dac_demo_v1.py:
--------------------------------------------------------------------------------
1 | '''
2 | DAC的演示实例-DAC输出正弦波
3 | '''
4 | from machine import DAC,Pin
5 | import math
6 |
7 | # 创建一个缓冲数组,用于存放一个sin波形
8 | buf = bytearray(100)
9 | for i in range(len(buf)):
10 | buf[i] = 128 + int(127 * math.sin(2*math.pi * i/len(buf)))
11 |
12 |
13 | dac = DAC(Pin(25), bits=12) # bits可选 8/12
14 |
15 |
16 |
17 | def write_sin_wave():
18 | # 设定频率为400HZ
19 | dac.write(buf, 400*len(buf), mode=DAC.CIRCULAR)
20 |
21 |
--------------------------------------------------------------------------------
/10_Timer定时器与LED闪烁/Timer定时器与LED闪烁.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | # Timer定时器与LED闪烁
4 |
5 | 教程搬家至:
6 | http://www.1zlab.com/article/micropython-esp32-timer-and-led-blink/
--------------------------------------------------------------------------------
/10_Timer定时器与LED闪烁/src/print_hello_world_period.py:
--------------------------------------------------------------------------------
1 | '''
2 | 利用定时器周期的打印HelloWorld
3 | '''
4 | from machine import Timer
5 |
6 | timer_id = 10000 # TODO 这里timer id好像可以是任何值 -1会报错
7 | timer = Timer(timer_id) # 创建定时器对象
8 |
9 | def hello_world(t):
10 | print("Hello World")
11 |
12 | # 初始化定时器
13 | timer.init(period=1000, mode=Timer.PERIODIC , callback=hello_world)
14 |
15 |
16 | try:
17 | while True:
18 | # do nothing 什么也不做
19 | pass
20 | except:
21 | # 销毁定时器
22 | timer.deinit()
23 |
--------------------------------------------------------------------------------
/10_Timer定时器与LED闪烁/src/print_timer_info.py:
--------------------------------------------------------------------------------
1 | '''
2 | 定时器演示实例, 打印定时器,讲解定时器的属性。
3 | '''
4 |
5 | from machine import Timer
6 | import utime
7 |
8 |
9 | timer=Timer(1)
10 |
11 |
12 | def timer_print(timer):
13 | # 打印当前的计数
14 | print("Timer InteruptCount , counter= %d"%(timer.value()))
15 | print(timer)
16 |
17 |
18 | '''
19 | 定时器 模式:
20 | Timer.ONE_SHOT --- 0 -- # 执行一次
21 | Timer.PERIODIC --- 1 -- # 循环执行
22 | '''
23 | # timer.init(period=3000, mode=Timer.PERIODIC , callback=timer_print) #这里直接在回调函数中打印tim状态信息
24 | timer.init(period=1000, mode=Timer.PERIODIC , callback=lambda t: print("Counter: %d"%(t.value())))
25 |
26 |
27 | try:
28 | while True:
29 | print("do something..., counter = %d"%(timer.value()))
30 | utime.sleep_ms(100)
31 | except:
32 | # 必须要有这个try except ,要不然 键盘中段不能让定时器停止
33 | # 禁用此定时器
34 | timer.deinit()
35 |
36 | '''
37 | 样例打印:
38 | Timer(3ffe67a0; alarm_en=1, auto_reload=1, counter_en=1)
39 | Timer(3ffe67a0; alarm_en=1, auto_reload=1, counter_en=1)
40 | 。。。
41 |
42 | 3ffe67a0:创建定时器分配的内存空间首地址
43 | alarm_en : alarm_en:ONE_SHOT模式下,回调函数调用完成之后alarm_en=0,否则alarm_en=1;PERIODIC模式下,alarm_en=1。
44 | auto_reload: alarm_en:ONE_SHOT模式下,回调函数调用完成之后alarm_en=0,否则alarm_en=1;PERIODIC模式下,alarm_en=1。
45 | counter_en:参数为0时表示计数器没在计数(没有初始化之前或者调用deinit()之后),参数为1时表示计数器正在计数
46 | '''
--------------------------------------------------------------------------------
/10_Timer定时器与LED闪烁/src/timer_led_blink.py:
--------------------------------------------------------------------------------
1 | '''
2 | 定时器控制LED闪烁
3 | '''
4 | from machine import Timer,Pin
5 | import utime
6 |
7 |
8 | def led_toggle(led_pin):
9 | '''
10 | LED状态翻转
11 | '''
12 | status = led_pin.value()
13 | status = (status + 1) % 2
14 | led_pin.value(status)
15 |
16 |
17 | def led_blink_timed(timer, led_pin, freq=10):
18 | '''
19 | led 按照特定的频率进行闪烁
20 | LED闪烁周期 = 1000ms / 频率
21 | 状态变换间隔(period) = LED闪烁周期/ 2
22 | '''
23 | # 计算状态变换间隔时间 ms
24 | period = int(0.5 * 1000 / freq)
25 | # 初始化定时器
26 | timer.init(period=period, mode=Timer.PERIODIC, callback=lambda t:led_toggle(led_pin))
27 |
28 |
29 | # 声明引脚 D12 作为LED的引脚
30 | led_pin = Pin(12, Pin.OUT)
31 | timer = Timer(1) # 创建定时器对象
32 | led_blink_timed(timer, led_pin, freq=20)
33 |
34 |
35 |
36 |
37 | try:
38 | while True:
39 | # do nothing 什么也不做
40 | pass
41 | except:
42 | # 销毁定时器
43 | timer.deinit()
44 |
--------------------------------------------------------------------------------
/10_Timer定时器与LED闪烁/src/timer_rgb_blink.py:
--------------------------------------------------------------------------------
1 | '''
2 | 利用定时器让RGB彩灯按照不同的频率闪烁
3 | '''
4 |
5 | from machine import Timer,Pin
6 | import utime
7 |
8 |
9 | def led_toggle(led_pin):
10 | '''
11 | LED状态翻转
12 | '''
13 | status = led_pin.value()
14 | status = (status + 1) % 2
15 | led_pin.value(status)
16 |
17 |
18 | def led_blink_timed(timer, led_pin, freq=10):
19 | '''
20 | led 按照特定的频率进行闪烁
21 | LED闪烁周期 = 1000ms / 频率
22 | 状态变换间隔(period) = LED闪烁周期/ 2
23 | '''
24 | # 计算状态变换间隔时间 ms
25 | period = int(0.5 * 1000 / freq)
26 | # 初始化定时器
27 | timer.init(period=period, mode=Timer.PERIODIC, callback=lambda t:led_toggle(led_pin))
28 |
29 |
30 | # 声明引脚 D32 作为LED的引脚
31 | red_pin = Pin(12, Pin.OUT) # 定义红色LED引脚
32 | red_timer = Timer(1) # 创建定时器对象
33 | led_blink_timed(red_timer, red_pin, freq=10)
34 |
35 | green_pin = Pin(14, Pin.OUT) # 定义绿色LED引脚
36 | green_timer = Timer(2)
37 | led_blink_timed(green_timer, green_pin, freq=15)
38 |
39 | blue_pin = Pin(27, Pin.OUT) # 定义蓝色LED引脚
40 | blue_timer = Timer(3)
41 | led_blink_timed(blue_timer, blue_pin, freq=20)
42 |
43 |
44 |
45 | try:
46 | while True:
47 | # do nothing 什么也不做
48 | pass
49 | except:
50 | # 销毁定时器
51 | red_timer.deinit()
52 | green_timer.deinit()
53 | blue_timer.deinit()
54 |
--------------------------------------------------------------------------------
/11_UART串口通信/UART串口通信.md:
--------------------------------------------------------------------------------
1 |
2 | # UART串口通信-MicroPython-ESP32-1Z实验室
3 |
4 | 教程搬家至:
5 | http://www.1zlab.com/article/micropython-esp32-uart-communication/
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_ESP32发送PC接收/binary_listener_pc_pyserial.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | PC上使用PySerial解析二进制数据
4 |
5 | ## 注意
6 | 运行此程序的时候,需要修改设备的权限,
7 | sudo chmod 777 /dev/ttyUSB? ,其中 ? = 0,1,2,...
8 | 或者使用管理员权限运行脚本
9 | sudo python xxxxx.py
10 |
11 |
12 | ## 样例输出
13 | Recv字节流: aa:ae:00:00:27:1c:ff:ff:f6:dd:3f:a5:47:11:0a
14 | 数据解析: x= 100.00, y= -24.00, value=1.291231
15 |
16 | '''
17 |
18 | import serial
19 | import struct
20 | # 串口号 默认为 /dev/ttyUSB0
21 | ser_dev = '/dev/ttyUSB1'
22 |
23 |
24 | FRAME_BYTE_LEN = 15 # 通信协议里面每一帧字节的长度
25 |
26 | def depack_bin_data(byte_raw):
27 | '''
28 | 将数据解包
29 | 根据通信协议,解析数据, 并校验帧头
30 | '''
31 | global FRAME_BYTE_LEN
32 |
33 | if len(byte_raw) != FRAME_BYTE_LEN:
34 | # 检查数据帧长度是否满足条件
35 | raise ValueError("ERROR: 长度不满足条件")
36 |
37 | (verify_byte1, verify_byte2, x, y, value, _) = struct.unpack('>BBiifB', byte_raw)
38 | if verify_byte1 == 0xAA and verify_byte2 == 0xAE:
39 | x /= 100 # 恢复为实际数值范围
40 | y /= 100 # 恢复为实际数值范围
41 | return (x, y, value)
42 | else:
43 | raise ValueError("ERROR: 数据帧头校验失败")
44 |
45 | # 创建一个串口实例
46 | ser = serial.Serial(ser_dev,115200, timeout=1, bytesize=8)
47 |
48 | while True:
49 | # 读取的是byte类型 ,例如
50 | # b'hello\n'
51 | # ser.read() # 注意:串口读取单个字节 (一个字节是16位)
52 | # ser.read(n) # 读入N个字节
53 | # print(ser.read())
54 |
55 | # 读入一行数据 以EOF标识(\n)作为参考
56 | byte_raw = ser.readline()
57 | # 讲接收的字节流转换成容易读取的样式
58 | byte_str = ':'.join(["{:02x}".format(char_byte) for char_byte in bytearray(byte_raw)])
59 | # 打印原始的字节数据()
60 | print("Recv字节流: "+byte_str)
61 |
62 |
63 | try:
64 | # 读取解析的数据
65 | x,y,value = depack_bin_data(byte_raw)
66 | print("数据解析: "+"x= %.2f, y= %.2f, value=%f\n"%(x, y, value))
67 |
68 | except ValueError as e:
69 | # 若帧头不匹配
70 | print(e)
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_ESP32发送PC接收/binary_publisher_esp32_uart.py:
--------------------------------------------------------------------------------
1 | '''
2 | 串口发送信息 二进制数据
3 | 接线:
4 | ESP32,TDX,D12 -> RXD,CP2102 (USB转TTL)
5 | ESP32,RXD,D13 -> TXD,CP2102 (USB转TTL)
6 | '''
7 |
8 | from machine import UART,Pin
9 | import struct
10 | import utime
11 |
12 |
13 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
14 |
15 |
16 |
17 |
18 | def pack_bin_data(x,y,value):
19 | '''
20 | h: unsigned short bit=2
21 | b: unsigned char (byte): bit =1
22 | i: integer bit=4
23 | f: float bit=4
24 |
25 | b*2 + i*2 + f*1
26 | = 1*2 + 4*2 + 4*1 = 14byte
27 |
28 | '''
29 | bin_data = struct.pack(">BBiifB",
30 | 0xAA,
31 | 0xAE,
32 | int(x * 100), # x坐标,精确到小数点后两位
33 | int(y * 100 ), # y坐标,精确到小数点后两位
34 | float(value), # 传感器读入的值,float类型
35 | 0x0A) # 结束符 \n = 0x0A
36 | print(bin_data)
37 | return bin_data
38 |
39 | x = 100.123
40 | y = -23.398
41 | value = 1.2912312352
42 |
43 | try:
44 | while True:
45 | # print(uart)
46 | uart.write(pack_bin_data(x, y, value))
47 | utime.sleep_ms(1000)
48 | except:
49 | print("程序意外终端, Bye")
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_ESP32发送PC接收/decode_value.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | import struct
3 |
4 | data = b"\xaa\xae\x00\x00'\x1c\xff\xff\xf6\xdd?\xa5G\x11"
5 |
6 | print(data)
7 | (_,_,x,y,value) = struct.unpack('>BBiif',data)
8 |
9 | print(struct.unpack('>BBiif',data))
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_ESP32发送PC接收/verify_test.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 |
3 | def verify_data(byte_buffer, new_byte):
4 | # 校验
5 | veri_code = bytearray(b'\xaa\xae')
6 | # 校验字节长度
7 | veri_code_len = len(veri_code)
8 |
9 | byte_buffer.append(new_byte[0])
10 | while len(byte_buffer) > veri_code_len:
11 | byte_buffer.pop()
12 |
13 | if len(byte_buffer) < veri_code_len:
14 | # 长度不足
15 | return False
16 |
17 | # print(veri_code)
18 | # print(byte_buffer)
19 |
20 | return veri_code == byte_buffer
21 |
22 |
23 | print(verify_data(bytearray(b'\xaa'),b'\xae'))
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_PC发送ESP32接收/TODO:
--------------------------------------------------------------------------------
1 |
2 | PC发送:
3 | Send字节流: 长度15
4 | aa:ae:00:00:27:1c:ff:ff:f6:dd:3f:a5:47:11:0a
5 |
6 |
7 | ESP32接收: 接收12
8 | aa:0d:00:12:f4:ff:5f:f7:2a:47:44:e1
9 |
10 |
11 |
12 | ? MicroPython 接收字节流有问题?
13 | 字符跟str是UTF-8字节流
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_PC发送ESP32接收/binary_listener_esp32_uart.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | ## 功能描述
4 | # 串口解析信息 二进制数据
5 | ## 接线:
6 | # ESP32,TDX,D12 -> RXD,CP2102 (USB转TTL)
7 | # ESP32,RXD,D13 -> TXD,CP2102 (USB转TTL)
8 |
9 | # TODO 数据解析
10 | # 运行过程中遭遇跳过 ? ESP32的问题
11 | '''
12 | from machine import UART,Pin
13 | import struct
14 | import utime
15 |
16 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
17 |
18 |
19 | def depack_bin_data(byte_raw):
20 | '''
21 | 将数据解包
22 | 根据通信协议,解析数据, 并校验帧头
23 | '''
24 | FRAME_BYTE_LEN = 15
25 |
26 | if len(byte_raw) != FRAME_BYTE_LEN:
27 | # 检查数据帧长度是否满足条件
28 | raise ValueError("ERROR: 长度不满足条件")
29 |
30 | (verify_byte1, verify_byte2, x, y, value, _) = struct.unpack('>BBiifB', byte_raw)
31 | if verify_byte1 == 0xAA and verify_byte2 == 0xAE:
32 | x /= 100 # 恢复为实际数值范围
33 | y /= 100 # 恢复为实际数值范围
34 | return (x, y, value)
35 | else:
36 | raise ValueError("ERROR: 数据帧头校验失败")
37 |
38 |
39 |
40 | while True:
41 | if uart.any():
42 | print("Recv Raw Data")
43 | byte_raw = uart.readline()
44 | print(byte_raw)
45 | print(len(byte_raw))
46 | # 讲接收的字节流转换成容易读取的样式
47 | # byte_str = ':'.join(['{:02x}'.format(char_byte) for char_byte in byte_raw])
48 | try:
49 | (x, y,value) = depack_bin_data(byte_raw)
50 | print("x= %.2f y=%.2f value=%f"%(x, y, value))
51 | except ValueError as e:
52 | print(e)
53 | # print(byte_raw)
54 | # print('.')
55 | utime.sleep_ms(1000)
56 |
57 |
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制Binary_PC发送ESP32接收/binary_publisher_pc_pyserial.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | PC使用PySerial发送数据
4 |
5 | ## 注意
6 | 运行此程序的时候,需要修改设备的权限,
7 | sudo chmod 777 /dev/ttyUSB? ,其中 ? = 0,1,2,...
8 | 或者使用管理员权限运行脚本
9 | sudo python xxxxx.py
10 |
11 | '''
12 | import serial
13 | import struct
14 | import time
15 |
16 | # 串口号 默认为 /dev/ttyUSB0
17 | ser_dev = '/dev/ttyUSB1'
18 | # 创建一个串口实例
19 | ser = serial.Serial(ser_dev,115200, timeout=1, bytesize=8)
20 |
21 |
22 | def pack_bin_data(x,y,value):
23 | '''
24 | h: unsigned short bit=2
25 | b: unsigned char (byte): bit =1
26 | i: integer bit=4
27 | f: float bit=4
28 |
29 | b*2 + i*2 + f*1
30 | = 1*3 + 4*2 + 4*1 = 15byte
31 |
32 | '''
33 |
34 | bin_data = struct.pack(">BBiifB",
35 | 0xAA,
36 | 0xAE,
37 | int(x * 100), # x坐标,精确到小数点后两位
38 | int(y * 100 ), # y坐标,精确到小数点后两位
39 | float(value), # 传感器读入的值,float类型
40 | 0x0A) # 结束符 '\n = 0x0A
41 | return bin_data
42 |
43 | x = 100.123
44 | y = -23.398
45 | value = 1.2912312352
46 |
47 | byte_raw = pack_bin_data(x,y,value)
48 |
49 | while True:
50 | ser.write(byte_raw)
51 | # ser.write(b'\xAA')
52 | # ser.write('haha')
53 | # 讲接收的字节流转换成容易读取的样式
54 | byte_str = ':'.join(["{:02x}".format(char_byte) for char_byte in bytearray(byte_raw)])
55 | # 打印原始的字节数据()
56 | print("Send字节流: "+byte_str+"\n")
57 |
58 | # 每隔10s发送一次数据
59 | time.sleep(3)
60 |
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制_ESP32自发自收/binary_pub_and_recv.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | ## 功能描述
4 | # 串口解析信息 二进制数据
5 | ## 接线:
6 | # ESP32,TDX,D13 -> RXD,CP2102 (USB转TTL)
7 | # ESP32,RXD,D12 -> TXD,CP2102 (USB转TTL)
8 | '''
9 | from machine import UART,Pin
10 | import struct
11 | import utime
12 | # 初始化串口 UART
13 | # 波特率 115200
14 | # rx -> Pin 13
15 | # tx -> Pin 12
16 |
17 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
18 |
19 | def pack_bin_data(x,y,value):
20 | '''
21 | h: unsigned short bit=2
22 | b: unsigned char (byte): bit =1
23 | i: integer bit=4
24 | f: float bit=4
25 |
26 | b*2 + i*2 + f*1
27 | = 1*2 + 4*2 + 4*1 = 14byte
28 |
29 | '''
30 | bin_data = struct.pack(">BBiifB",
31 | 0xAA,
32 | 0xAE,
33 | int(x * 100), # x坐标,精确到小数点后两位
34 | int(y * 100 ), # y坐标,精确到小数点后两位
35 | float(value), # 传感器读入的值,float类型
36 | 0x0A) # 结束符 \n = 0x0A
37 | print(bin_data)
38 | return bin_data
39 |
40 | x = 100.123
41 | y = -23.398
42 | value = 1.2912312352
43 |
44 | while True:
45 | print('Send Data')
46 | print(pack_bin_data(x, y, value))
47 | uart.write(pack_bin_data(x, y, value))
48 | utime.sleep_ms(1000)
49 | print("Recv Data")
50 | # byte_raw = uart.readline()
51 | byte_raw = uart.read()
52 | print(len(byte_raw))
53 | print(byte_raw)
54 | # 讲接收的字节流转换成容易读取的样式
55 | # byte_str = ':'.join(["{:02x}".format(char_byte) for char_byte in byte_raw])
56 | # print(byte_str)
57 | # print(byte_raw)
58 | utime.sleep_ms(1000)
59 |
--------------------------------------------------------------------------------
/11_UART串口通信/src/二进制_ESP32自发自收/str_pub_and_recv.py:
--------------------------------------------------------------------------------
1 | '''
2 | ESP32串口通信-字符串数据自发实验
3 |
4 | 接线 将开发板的 13号引脚与12号引脚用杜邦线相连接。
5 |
6 | '''
7 | from machine import UART,Pin
8 | import utime
9 |
10 | # 初始化一个UART对象
11 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
12 |
13 | count = 1
14 |
15 | while True:
16 | print('\n\n===============CNT {}==============='.format(count))
17 |
18 | # 发送一条消息
19 | print('Send: {}'.format('hello {}\n'.format(count)))
20 | print('Send Byte :') # 发送字节数
21 | uart.write('hello {}\n'.format(count))
22 | # 等待1s钟
23 | utime.sleep_ms(1000)
24 |
25 | if uart.any():
26 | # 如果有数据 读入一行数据返回数据为字节类型
27 | # 例如 b'hello 1\n'
28 | bin_data = uart.readline()
29 | # 将手到的信息打印在终端
30 | print('Echo Byte: {}'.format(bin_data))
31 |
32 | # 将字节数据转换为字符串 字节默认为UTF-8编码
33 | print('Echo String: {}'.format(bin_data.decode()))
34 | # 计数器+1
35 | count += 1
36 | print('---------------------------------------')
--------------------------------------------------------------------------------
/11_UART串口通信/src/字节Byte_ESP32发送PC接收/byte_listener_pc_pyserial.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | PC上使用PySerial解析数据
4 | 接收byte字节数据
5 |
6 | 运行此程序的时候,需要修改设备的权限,
7 |
8 | sudo chmod 777 /dev/ttyUSB1
9 |
10 | '''
11 | import serial
12 |
13 | # 串口号 默认为 /dev/ttyUSB0
14 | ser_dev = '/dev/ttyUSB1'
15 |
16 |
17 | '''
18 | # 执行一次
19 | with serial.Serial(ser_dev,115200, timeout=1) as ser:
20 | print(ser)
21 | info = ser.readline()
22 | print(info)
23 | '''
24 |
25 | ser = serial.Serial(ser_dev,115200, timeout=1)
26 |
27 | while True:
28 | # 读取的是byte类型 ,例如
29 | # b'hello\n'
30 | # ser.read() # 读取单个字节
31 | # ser.read(n) # 读入N个字节
32 | info = ser.readline() # 读入一行数据
33 |
34 | # 打印接收的数据
35 | print(info)
--------------------------------------------------------------------------------
/11_UART串口通信/src/字节Byte_ESP32发送PC接收/byte_publisher_esp32_uart.py:
--------------------------------------------------------------------------------
1 | '''
2 | 串口发送信息 (字符chuan)
3 | 接线:
4 | ESP32,TDX,D12 -> RXD,CP2102 (USB转TTL)
5 | ESP32,RXD,D13 -> TXD,CP2102 (USB转TTL)
6 | '''
7 |
8 | from machine import UART,Pin
9 | import utime
10 |
11 | # 初始化串口 UART
12 | # 波特率 115200
13 | # rx -> Pin 13
14 | # tx -> Pin 12
15 |
16 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
17 | while True:
18 | # 句子后面需要添加结束符\n
19 | # 以方便PC上进行数据解析
20 | # uart.write("Fange: HelloWorld\r\n")
21 | uart.write("hello\n")
22 | # 时间间隔1s
23 | utime.sleep_ms(100)
--------------------------------------------------------------------------------
/11_UART串口通信/src/字节Byte_PC发送ESP32接收/byte_listener_esp32_uart.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | 串口发送信息 (字符chuan)
4 | 接线:
5 | ESP32,TDX,D12 -> RXD,CP2102 (USB转TTL)
6 | ESP32,RXD,D13 -> TXD,CP2102 (USB转TTL)
7 |
8 | TODO 信息丢失或者损坏
9 | 栈不够用等一系列问题
10 |
11 | TODO byte解码问题, 失败
12 | '''
13 |
14 | from machine import UART,Pin
15 | import utime
16 |
17 | # 初始化串口 UART
18 | # 波特率 115200
19 | # rx -> Pin 13
20 | # tx -> Pin 12
21 |
22 | # 注意,我们这里拓展了buffer的尺寸
23 | uart = UART(2, baudrate=115200, rx=13,tx=12,timeout=10)
24 | # 注意 buffer_size暂时还不支持
25 | # uart = UART(2, baudrate=115200, bits=8, parity=0, rx=13,tx=12,buffer_size=4096,timeout=10)
26 |
27 | while True:
28 | if uart.any():
29 | # 如果有数据的话, 就读入一行
30 | info_byte = uart.readline()
31 | print(info_byte)
32 | # info_str = info_byte.decode('uft8')
33 | # print(info_str)
34 |
35 | # Unicode Error
36 | # info_str = info_byte.decode('ascii')
37 |
38 | # not Implement 没有实现这个功能
39 | # info_str = str(info_byte,encoding="ascii")
40 |
41 | # print(info_byte.decode(encoding="ascii"))
42 | # 时间间隔1s
43 | utime.sleep_ms(100)
--------------------------------------------------------------------------------
/11_UART串口通信/src/字节Byte_PC发送ESP32接收/byte_publisher_pc_pyserial.py:
--------------------------------------------------------------------------------
1 | # -*- coding:utf-8 -*-
2 | '''
3 | PC上使用PySerial发送字节数据
4 |
5 | 运行此程序的时候,需要修改设备的权限,
6 |
7 | sudo chmod 777 /dev/ttyUSB1
8 | '''
9 | import serial
10 | import time
11 | # 串口号 默认为 /dev/ttyUSB0
12 | ser_dev = '/dev/ttyUSB1'
13 |
14 | ser = serial.Serial(ser_dev,115200, timeout=1)
15 |
16 | count = 1
17 | MAX_NUM = 10
18 | while True:
19 |
20 | info_str = 'INFO-%d\n'%(count)
21 | print(info_str)
22 | # 以字节的方式发出 编码方式,默认为UTF8 --ERROR
23 | # info_byte = bytes(info_str, encoding='utf8')
24 |
25 | info_byte = b'INFO-%d\n'%(count)
26 |
27 | # print("Byte : {}".format(info_byte))
28 | ser.write(info_byte) # 通过串口发送BYTE
29 |
30 | count = (count+1)%MAX_NUM
31 | # 停顿一秒
32 | time.sleep(2)
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/WebREPL与MIcroIDE配置教程.md:
--------------------------------------------------------------------------------
1 | # WebREPL与MicroIDE配置教程-1Z实验室
2 |
3 | 教程搬家至:
4 | http://www.1zlab.com/article/micropython-esp32-webrepl-emp-ide/
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/bengbeng.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/bengbeng.jpeg
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/boot-setup.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/boot-setup.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/connect_wifi_success.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/connect_wifi_success.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/micro-ide-01.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/micro-ide-01.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-02.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-02.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-03.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-03.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-05.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-05.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-07.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-07.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-08.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-08.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-09.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-09.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-10.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-demo-terminal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-demo-terminal.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/microide-run-main.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/microide-run-main.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/set_password_for_webrepl.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/set_password_for_webrepl.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/webrepl_cfg_output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/webrepl_cfg_output.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/webrepl_enable.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/webrepl_enable.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/webrepl_setup.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/webrepl_setup.png
--------------------------------------------------------------------------------
/14_WebREPL与MIcroIDE配置教程/image/wifi_connect_choose_and_password.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/14_WebREPL与MIcroIDE配置教程/image/wifi_connect_choose_and_password.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/MQTT入门之概念解析.md:
--------------------------------------------------------------------------------
1 | # MQTT入门之概念解析-MicroPython-ESP32-1Z实验室
2 |
3 | 教程搬家至:
4 | http://www.1zlab.com/article/micropython-esp32-introduction-to-mqtt/
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A1.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A2.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A3.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A4.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A5.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A5.jpeg
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A6-1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A6-1.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A6-2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A6-2.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A6-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A6-3.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A6-4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A6-4.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A6-5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A6-5.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/A6-6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/A6-6.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/MQTT_DHT_01-400x157.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/MQTT_DHT_01-400x157.jpg
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/MQTT_DHT_02-400x258.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/MQTT_DHT_02-400x258.jpg
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/mosquitto_pub_sub.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/mosquitto_pub_sub.png
--------------------------------------------------------------------------------
/15_MQTT入门之概念解析/image/paho-mqtt_pub_sub.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/15_MQTT入门之概念解析/image/paho-mqtt_pub_sub.png
--------------------------------------------------------------------------------
/16_使用upip安装包/image/emp-1zlab-pypi.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/16_使用upip安装包/image/emp-1zlab-pypi.png
--------------------------------------------------------------------------------
/16_使用upip安装包/image/install_emp_1zlab.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/16_使用upip安装包/image/install_emp_1zlab.png
--------------------------------------------------------------------------------
/16_使用upip安装包/使用upip安装包.md:
--------------------------------------------------------------------------------
1 | # 使用upip安装包
2 |
3 | 教程搬家至: http://www.1zlab.com/article/micropython-esp32-upip-install-package/
--------------------------------------------------------------------------------
/17_ESP32接入WIFI热点/ESP32接入WIFI热点.md:
--------------------------------------------------------------------------------
1 | # ESP32接入WIFI热点
2 |
3 | 教程搬家至:
4 |
5 | http://www.1zlab.com/article/micropython-esp32-connect-to-wifi/
--------------------------------------------------------------------------------
/17_ESP32接入WIFI热点/image/wifi_active.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/17_ESP32接入WIFI热点/image/wifi_active.png
--------------------------------------------------------------------------------
/17_ESP32接入WIFI热点/image/wifi_connect.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/17_ESP32接入WIFI热点/image/wifi_connect.png
--------------------------------------------------------------------------------
/18_利用AMPY进行文件同步/利用AMPY进行文件同步.md:
--------------------------------------------------------------------------------
1 | # 利用AMPY进行文件同步-1Z实验室
2 |
3 | 教程搬家至
4 | http://www.1zlab.com/article/micropython-esp32-ampy/
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/MQTT入门之项目实战.md:
--------------------------------------------------------------------------------
1 | # MQTT入门之项目实战-MicroPython-ESP32-1Z实验室
2 |
3 |
4 | 教程搬家至
5 | http://www.1zlab.com/article/micropython-esp32-mqtt-in-action/
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/image/MQTT_DHT_01-400x157.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/19_MQTT入门之项目实战/image/MQTT_DHT_01-400x157.jpg
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/image/MQTT_DHT_02-400x258.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/19_MQTT入门之项目实战/image/MQTT_DHT_02-400x258.jpg
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/image/mosquitto_pub_sub.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/19_MQTT入门之项目实战/image/mosquitto_pub_sub.png
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/image/paho-mqtt_pub_sub.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/1zlab/1ZLAB_MicroPython_ESP32_Tutorial/317926f13a6b8baf612c2138a0959ace3436f80d/19_MQTT入门之项目实战/image/paho-mqtt_pub_sub.png
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/src/esp32/publisher.py:
--------------------------------------------------------------------------------
1 | from umqtt.simple import MQTTClient
2 | import time
3 |
4 | SERVER = '192.168.43.16'
5 | CLIENT_ID = 'PYESPCAR_A0'
6 | TOPIC = b'pyespcar_basic_control'
7 |
8 | client = MQTTClient(CLIENT_ID, SERVER)
9 | client.connect()
10 |
11 |
12 | while True:
13 | client.publish(TOPIC, 'helloworld')
14 | time.sleep(1)
15 |
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/src/esp32/subscriber.py:
--------------------------------------------------------------------------------
1 | from umqtt.simple import MQTTClient
2 | import time
3 |
4 | SERVER = '192.168.43.16'
5 | CLIENT_ID = 'PYESPCAR_A0'
6 | TOPIC = b'pyespcar_ctl_msg'
7 |
8 | def mqtt_callback(topic, msg):
9 | print('topic: {}'.format(topic))
10 | print('msg: {}'.format(msg))
11 |
12 |
13 | client = MQTTClient(CLIENT_ID, SERVER)
14 | client.set_callback(mqtt_callback)
15 | client.connect()
16 |
17 | client.subscribe(TOPIC)
18 |
19 |
20 | while True:
21 | # 查看是否有数据传入
22 | # 有的话就执行 mqtt_callback
23 | client.check_msg()
24 | time.sleep(1)
25 |
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/src/pc/paho-mqtt-publisher.py:
--------------------------------------------------------------------------------
1 | import paho.mqtt.client as mqtt
2 | import time
3 |
4 | HOST_IP = 'localhost' # Server的IP地址
5 | HOST_PORT = 1883 # mosquitto 默认打开端口
6 | TOPIC_ID = 'pyespcar_basic_control' # TOPIC的ID
7 |
8 | # 创建一个客户端
9 | client = mqtt.Client()
10 | # 连接到服务器(本机)
11 | client.connect(HOST_IP, HOST_PORT, 60)
12 |
13 | count = 0
14 | while True:
15 | count += 1
16 | # 待发送的数据
17 | message = 'MOVE FRORWORD,{}'.format(count)
18 | # 通过mqtt协议发布数据给server
19 | client.publish(TOPIC_ID, message)
20 | # 打印日志
21 | print('SEND: {}'.format(message))
22 | # 延时1s
23 | time.sleep(1)
--------------------------------------------------------------------------------
/19_MQTT入门之项目实战/src/pc/paho-mqtt-subsriber.py:
--------------------------------------------------------------------------------
1 | import paho.mqtt.client as mqtt
2 |
3 | def on_message(client, userdata, msg):
4 | '''处理message回调'''
5 | print('topic: {}'.format(msg.topic))
6 | print('message: {}'.format(str(msg.payload)))
7 |
8 | # 建立一个MQTT的客户端
9 | client = mqtt.Client()
10 | # 绑定数据接收回调函数
11 | client.on_message = on_message
12 |
13 | HOST_IP = 'localhost' # Server的IP地址
14 | HOST_PORT = 1883 # mosquitto 默认打开端口
15 | TOPIC_ID = 'pyespcar_basic_control' # TOPIC的ID
16 |
17 | # 连接MQTT服务器
18 | client.connect(HOST_IP, HOST_PORT, 60)
19 | # 订阅主题
20 | client.subscribe(TOPIC_ID)
21 |
22 | # 阻塞式, 循环往复,一直处理网络数据,断开重连
23 | client.loop_forever()
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # MicroPython-ESP32从入门到差不多
2 |
3 |
4 |
5 | ## 概要
6 |
7 | 
8 | ## 目录
9 |
10 | * [MicroPython-ESP32固件烧录](./01_MicroPython-ESP32固件烧录/MicroPython-ESP32固件烧录.md)
11 | * [ESP32的REPL使用方法](./02_ESP32的REPL使用方法/ESP32的REPL使用方法.md)
12 | * [ESP32接入WIFI热点](./17_ESP32接入WIFI热点/ESP32接入WIFI热点.md)
13 | * [使用upip安装包](./16_使用upip安装包/使用upip安装包.md)
14 | * [WebREPL与MIcroIDE配置教程](./14_WebREPL与MIcroIDE配置教程/WebREPL与MIcroIDE配置教程.md)
15 | * [GPIO输出控制小灯亮灭](./03_GPIO输出与控制LED亮灭/GPIO输出控制小灯亮灭.md)
16 | * [延时函数与LED闪烁](./04_延时函数与LED闪烁/延时函数与LED闪烁.md)
17 | * [PWM与呼吸灯](./05_PWM与呼吸灯/PWM与呼吸灯.md)
18 | * [GPIO输入与用户按键控制LED开关](./06_GPIO输入与按键控制LED小灯/GPIO输入与用户按键控制LED开关.md)
19 | * [IRQ外部中断与按键控制LED小灯](./07_IRQ外部中断与按键控制LED小灯/IRQ外部中断与按键控制LED小灯.md)
20 | * [ADC采样与电位计控制LED亮度](./08_ADC采样与电位计控制LED亮度/ADC采样与电位计控制LED亮度.md)
21 | * [DAC数字转模拟](./09_DAC数字转模拟/DAC数字转模拟.md)
22 | * [Timer定时器与LED闪烁](./10_Timer定时器与LED闪烁/Timer定时器与LED闪烁.md)
23 | * [UART串口通信](./11_UART串口通信/UART串口通信.md)
24 |
25 | * [MQTT入门之概念解析](./15_MQTT入门之概念解析/MQTT入门之概念解析.md)
26 | * [MQTT入门之项目实战](./19_MQTT入门之项目实战/MQTT入门之项目实战.md)
27 | * [利用AMPY进行文件同步](./18_利用AMPY进行文件同步/利用AMPY进行文件同步.md)
28 |
29 |
30 | ## 项目实战
31 | * [PyESPCar-基于ESP32的WIFI小车](https://github.com/1zlab/1ZLAB_PyEspCar)
32 |
33 | 
34 | * [舵机云台人脸追踪](https://github.com/1zlab/1ZLAB_Face_Track_Robot)
35 |
36 | 
37 |
38 | ## 推广
39 | **1Z实验室出品**
40 | **1zlab: make things easy** 致力于在机器人+计算机视觉+人工智能的重叠区域, 制作小白友好的教程.
41 |
42 |
43 | 
44 |
--------------------------------------------------------------------------------
/libs/button.py:
--------------------------------------------------------------------------------
1 | '''
2 | 按键类Button
3 | '''
4 | from machine import Pin
5 | import utime
6 |
7 | class Button(object):
8 | '''
9 | 按键对象
10 | '''
11 | def __init__(self,button_idx, callback=None):
12 | # 按键字典
13 | # 数据结构: (GPIO编号,按键抬起的电平, 按键按下的电平)
14 | button_list = [(39, False, True)]
15 |
16 | if button_idx < 0 or button_idx >= len(button_list):
17 | print("ERROR: Wrong Button Index")
18 | print("Valid Button Index: {} - {}".format(0, len(button_list)-1))
19 | return None
20 |
21 | gpio_id, self.BUTTON_RELEASE, self.BUTTON_PRESS, = button_list[button_idx]
22 | # 按键
23 | self.pin = Pin(gpio_id, Pin.IN)
24 | # 回调函数
25 | self.callback = callback
26 | # 设置外部中断
27 | if self.BUTTON_PRESS == True:
28 | self.pin.irq(trigger=Pin.IRQ_RISING, handler=self.irq_handler)
29 | else:
30 | self.pin.irq(trigger=Pin.IRQ_FALLING, handler=self.irq_handler)
31 |
32 | # 标志位 当前是否可以相应按键中断
33 | self.flag = True
34 |
35 | def irq_handler(self, irq_pin):
36 | '''
37 | 外部中断的相应函数
38 | '''
39 | # 如果当前正在处理中断,则忽略
40 | if not self.flag:
41 | return
42 | # 添加软件滤波
43 | utime.sleep_ms(50)
44 | if self.pin.value() == self.BUTTON_PRESS:
45 | # 判断当前按键状态是不是按下,如果是,则执行回调函数
46 | if self.flag and self.callback is not None:
47 | self.flag = False
48 | # 执行回调函数
49 | self.callback(self.pin)
50 | self.flag = True
51 |
52 | def deinit(self):
53 | '''
54 | 销毁资源
55 | '''
56 | self.pin.irq(trigger=0, handler=None) # 销毁外部中断的资源
57 |
58 |
--------------------------------------------------------------------------------
/libs/led.py:
--------------------------------------------------------------------------------
1 | '''
2 | LED类
3 | v2 添加LED亮度控制
4 | '''
5 | from machine import Pin, PWM
6 |
7 | class LED:
8 | def __init__(self, led_id):
9 | # LED字典
10 | # 数据结构: (gpio管脚编号, LED灭的电平, LED亮的电平)
11 | led_list = [(2, False, True),(13, True, False)]
12 |
13 | if led_id >= len(led_list) or led_id < 0:
14 | print('ERROR:LED编号无效, 有效ID:{} - {}'.format(0, len(led_list-1)))
15 | return None
16 |
17 | gpio_id, self.LED_OFF, self.LED_ON = led_list[led_id]
18 | self.pin = Pin(gpio_id, Pin.OUT)
19 | self.pwm = PWM(self.pin, freq=1000)
20 |
21 | def on(self):
22 | '''
23 | 打开LED
24 | '''
25 | self.pin.value(self.LED_ON)
26 |
27 | def off(self):
28 | '''
29 | 关闭LED
30 | '''
31 | self.pin.value(self.LED_OFF)
32 |
33 | def toggle(self):
34 | '''
35 | 切换LED的状态
36 | OFF -> ON
37 | ON -> OFF
38 | '''
39 | self.pin.value(not self.pin.value())
40 |
41 | def intensity(self, value):
42 | '''
43 | 设置LED的亮度
44 | '''
45 | if self.LED_ON == True:
46 | self.pwm.duty(value)
47 | else:
48 | self.pwm.duty(1023 - value)
49 |
50 | def deinit(self):
51 | '''
52 | 销毁资源
53 | '''
54 | self.pwm.deinit()
--------------------------------------------------------------------------------