ESP32 GPIO 命令
ESP32 GPIO commands
我刚刚为 lua 文件刷新了一个带有新自定义构建的 ESP32,设计自:
https://nodemcu-build.com/ 使用 dev-esp32<>BETA
它似乎工作正常,我可以连接 ESPlorer 并且固件已安装,如启动代码中所示。
我试图切换 BUILTIN_LED,但我在使用 GPIO 命令时遇到错误。
它使用简单的 arduino 代码开箱即用,我是不是搞砸了,或者为什么这不起作用?
这是来自 GPIO.mode 命令
的错误响应日志
Communication with MCU..Got answer! Communication with MCU established.
AutoDetect firmware...
Can't autodetect firmware, because proper answer not received (may be unknown firmware).
Please, reset module or continue.
ts Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6716
load:0x40078000,len:11116
ho 0 tail 12 room 4
load:0x40080400,len:5940
entry 0x40080700
[0;32mI (69) boot: Chip Revision: 1[0m
[0;32mI (69) boot_comm: chip revision: 1, min. bootloader chip revision: 0[0m
[0;32mI (41) boot: ESP-IDF v3.3.1 2nd stage bootloader[0m
[0;32mI (41) boot: compile time 17:08:58[0m
[0;32mI (41) boot: Enabling RNG early entropy source...[0m
[0;32mI (46) boot: SPI Speed : 40MHz[0m
[0;32mI (50) boot: SPI Mode : DIO[0m
[0;32mI (54) boot: SPI Flash Size : 4MB[0m
[0;32mI (58) boot: Partition Table:[0m
[0;32mI (61) boot: ## Label Usage Type ST Offset Length[0m
[0;32mI (69) boot: 0 nvs WiFi data 01 02 00009000 00006000[0m
[0;32mI (76) boot: 1 phy_init RF data 01 01 0000f000 00001000[0m
[0;32mI (84) boot: 2 factory factory app 00 00 00010000 00180000[0m
[0;32mI (91) boot: 3 lfs unknown c2 01 00190000 00010000[0m
[0;32mI (99) boot: 4 nodemcuspiffs unknown c2 00 001a0000 00070000[0m
[0;32mI (106) boot: End of partition table[0m
[0;32mI (110) boot_comm: chip revision: 1, min. application chip revision: 0[0m
[0;32mI (118) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1bdf4 (114164) map[0m
[0;32mI (165) esp_image: segment 1: paddr=0x0002be1c vaddr=0x3ffb0000 size=0x02fcc ( 12236) load[0m
[0;32mI (170) esp_image: segment 2: paddr=0x0002edf0 vaddr=0x40080000 size=0x00400 ( 1024) load[0m
[0;32mI (172) esp_image: segment 3: paddr=0x0002f1f8 vaddr=0x40080400 size=0x00e18 ( 3608) load[0m
[0;32mI (182) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x84a0c (543244) map[0m
[0;32mI (373) esp_image: segment 5: paddr=0x000b4a2c vaddr=0x40081218 size=0x0f074 ( 61556) load[0m
[0;32mI (398) esp_image: segment 6: paddr=0x000c3aa8 vaddr=0x400c0000 size=0x00064 ( 100) load[0m
[0;32mI (408) boot: Loaded app from partition at offset 0x10000[0m
[0;32mI (408) boot: Disabling RNG early entropy source...[0m
[0;32mI (409) cpu_start: Pro cpu up.[0m
[0;32mI (413) cpu_start: Application information:[0m
[0;32mI (418) cpu_start: Project name: NodeMCU[0m
[0;32mI (423) cpu_start: App version: a8b46af-dirty[0m
[0;32mI (428) cpu_start: Compile time: Apr 15 2020 17:09:01[0m
[0;32mI (434) cpu_start: ELF file SHA256: af54a39e6c0fb11d...[0m
[0;32mI (440) cpu_start: ESP-IDF: v3.3.1[0m
[0;32mI (445) cpu_start: Starting app cpu, entry point is 0x40081244[0m
[0;32mI (0) cpu_start: App cpu up.[0m
[0;32mI (456) heap_init: Initializing. RAM available for dynamic allocation:[0m
[0;32mI (462) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM[0m
[0;32mI (469) heap_init: At 3FFB9260 len 00026DA0 (155 KiB): DRAM[0m
[0;32mI (475) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM[0m
[0;32mI (481) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (488) heap_init: At 4009028C len 0000FD74 (63 KiB): IRAM[0m
[0;32mI (494) cpu_start: Pro cpu start user code[0m
[0;32mI (176) cpu_start: Starting scheduler on PRO CPU.[0m
[0;32mI (0) cpu_start: Starting scheduler on APP CPU.[0m
Mounting flash filesystem...
No LFS image loaded
I (337) wifi: wifi driver task: 3ffc26a8, prio:23, stack:3584, core=0
[0;32mI (337) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
[0;32mI (337) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
I (367) wifi: wifi firmware version: ac331d7
I (367) wifi: config NVS flash: enabled
I (367) wifi: config nano formating: disabled
I (367) wifi: Init dynamic tx buffer num: 32
I (377) wifi: Init data frame dynamic rx buffer num: 32
I (377) wifi: Init management frame dynamic rx buffer num: 32
I (387) wifi: Init management short buffer num: 32
I (387) wifi: Init static rx buffer size: 1600
I (397) wifi: Init static rx buffer num: 10
I (397) wifi: Init dynamic rx buffer num: 32
NodeMCU ESP32 built on nodemcu-build.com provided by frightanic.com
branch: dev-esp32
commit: a8b46af905b759506e9fd5eabdadbd8beb83e7c2
SSL: false
modules: file,gpio,net,node,tmr,wifi
build 2020-04-15-17-07-08 powered by Lua 5.1.4 on ESP-IDF v3.3.1 on SDK IDF
lua: cannot open init.lua
>
gpio.write(2, LOW)
gpio.write(2, LOW)
stdin:1: bad argument #2 to 'write' (number expected, got nil)
stack traceback:
[C]: in function 'write'
stdin:1: in main chunk
>
根据https://nodemcu.readthedocs.io/en/dev-esp32/modules/gpio/#gpiowrite(和错误信息),第二个参数必须是一个整数; 1 或 0。
您的代码
gpio.write(2, LOW)
似乎发送 nil
。您的 LOW
没有在任何地方初始化,是吗?绝对应该可以正常工作的是:
LOW = 0
gpio.write(2, LOW)
(当然是 gpio.write(2, 0)
)
bad argument #2 to 'write' (number expected, got nil)
这说明了问题所在:write
的第二个参数是 nil
,即 LOW
是 nil。您可以通过输入 print(LOW)
来确认它应该打印 nil
原因很简单:LOW
未在任何地方定义;
正如您在 documentation 中所读到的,NodeMCU 定义了 gpio.LOW 和 gpio.HIGH(在幕后,它们只是 0 和 1)
正如 Marcel Stör 指出的那样,gpio.LOW
和 gpio.HIGH
仅针对 ESP8266 版本的固件定义。对于 ESP32,您需要使用纯数字 0
和 1
代替(或定义您自己的变量)
我刚刚为 lua 文件刷新了一个带有新自定义构建的 ESP32,设计自: https://nodemcu-build.com/ 使用 dev-esp32<>BETA 它似乎工作正常,我可以连接 ESPlorer 并且固件已安装,如启动代码中所示。
我试图切换 BUILTIN_LED,但我在使用 GPIO 命令时遇到错误。 它使用简单的 arduino 代码开箱即用,我是不是搞砸了,或者为什么这不起作用?
这是来自 GPIO.mode 命令
的错误响应日志Communication with MCU..Got answer! Communication with MCU established.
AutoDetect firmware...
Can't autodetect firmware, because proper answer not received (may be unknown firmware).
Please, reset module or continue.
ts Jun 8 2016 00:22:57
rst:0x1 (POWERON_RESET),boot:0x12 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:6716
load:0x40078000,len:11116
ho 0 tail 12 room 4
load:0x40080400,len:5940
entry 0x40080700
[0;32mI (69) boot: Chip Revision: 1[0m
[0;32mI (69) boot_comm: chip revision: 1, min. bootloader chip revision: 0[0m
[0;32mI (41) boot: ESP-IDF v3.3.1 2nd stage bootloader[0m
[0;32mI (41) boot: compile time 17:08:58[0m
[0;32mI (41) boot: Enabling RNG early entropy source...[0m
[0;32mI (46) boot: SPI Speed : 40MHz[0m
[0;32mI (50) boot: SPI Mode : DIO[0m
[0;32mI (54) boot: SPI Flash Size : 4MB[0m
[0;32mI (58) boot: Partition Table:[0m
[0;32mI (61) boot: ## Label Usage Type ST Offset Length[0m
[0;32mI (69) boot: 0 nvs WiFi data 01 02 00009000 00006000[0m
[0;32mI (76) boot: 1 phy_init RF data 01 01 0000f000 00001000[0m
[0;32mI (84) boot: 2 factory factory app 00 00 00010000 00180000[0m
[0;32mI (91) boot: 3 lfs unknown c2 01 00190000 00010000[0m
[0;32mI (99) boot: 4 nodemcuspiffs unknown c2 00 001a0000 00070000[0m
[0;32mI (106) boot: End of partition table[0m
[0;32mI (110) boot_comm: chip revision: 1, min. application chip revision: 0[0m
[0;32mI (118) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1bdf4 (114164) map[0m
[0;32mI (165) esp_image: segment 1: paddr=0x0002be1c vaddr=0x3ffb0000 size=0x02fcc ( 12236) load[0m
[0;32mI (170) esp_image: segment 2: paddr=0x0002edf0 vaddr=0x40080000 size=0x00400 ( 1024) load[0m
[0;32mI (172) esp_image: segment 3: paddr=0x0002f1f8 vaddr=0x40080400 size=0x00e18 ( 3608) load[0m
[0;32mI (182) esp_image: segment 4: paddr=0x00030018 vaddr=0x400d0018 size=0x84a0c (543244) map[0m
[0;32mI (373) esp_image: segment 5: paddr=0x000b4a2c vaddr=0x40081218 size=0x0f074 ( 61556) load[0m
[0;32mI (398) esp_image: segment 6: paddr=0x000c3aa8 vaddr=0x400c0000 size=0x00064 ( 100) load[0m
[0;32mI (408) boot: Loaded app from partition at offset 0x10000[0m
[0;32mI (408) boot: Disabling RNG early entropy source...[0m
[0;32mI (409) cpu_start: Pro cpu up.[0m
[0;32mI (413) cpu_start: Application information:[0m
[0;32mI (418) cpu_start: Project name: NodeMCU[0m
[0;32mI (423) cpu_start: App version: a8b46af-dirty[0m
[0;32mI (428) cpu_start: Compile time: Apr 15 2020 17:09:01[0m
[0;32mI (434) cpu_start: ELF file SHA256: af54a39e6c0fb11d...[0m
[0;32mI (440) cpu_start: ESP-IDF: v3.3.1[0m
[0;32mI (445) cpu_start: Starting app cpu, entry point is 0x40081244[0m
[0;32mI (0) cpu_start: App cpu up.[0m
[0;32mI (456) heap_init: Initializing. RAM available for dynamic allocation:[0m
[0;32mI (462) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM[0m
[0;32mI (469) heap_init: At 3FFB9260 len 00026DA0 (155 KiB): DRAM[0m
[0;32mI (475) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM[0m
[0;32mI (481) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM[0m
[0;32mI (488) heap_init: At 4009028C len 0000FD74 (63 KiB): IRAM[0m
[0;32mI (494) cpu_start: Pro cpu start user code[0m
[0;32mI (176) cpu_start: Starting scheduler on PRO CPU.[0m
[0;32mI (0) cpu_start: Starting scheduler on APP CPU.[0m
Mounting flash filesystem...
No LFS image loaded
I (337) wifi: wifi driver task: 3ffc26a8, prio:23, stack:3584, core=0
[0;32mI (337) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
[0;32mI (337) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE[0m
I (367) wifi: wifi firmware version: ac331d7
I (367) wifi: config NVS flash: enabled
I (367) wifi: config nano formating: disabled
I (367) wifi: Init dynamic tx buffer num: 32
I (377) wifi: Init data frame dynamic rx buffer num: 32
I (377) wifi: Init management frame dynamic rx buffer num: 32
I (387) wifi: Init management short buffer num: 32
I (387) wifi: Init static rx buffer size: 1600
I (397) wifi: Init static rx buffer num: 10
I (397) wifi: Init dynamic rx buffer num: 32
NodeMCU ESP32 built on nodemcu-build.com provided by frightanic.com
branch: dev-esp32
commit: a8b46af905b759506e9fd5eabdadbd8beb83e7c2
SSL: false
modules: file,gpio,net,node,tmr,wifi
build 2020-04-15-17-07-08 powered by Lua 5.1.4 on ESP-IDF v3.3.1 on SDK IDF
lua: cannot open init.lua
>
gpio.write(2, LOW)
gpio.write(2, LOW)
stdin:1: bad argument #2 to 'write' (number expected, got nil)
stack traceback:
[C]: in function 'write'
stdin:1: in main chunk
>
根据https://nodemcu.readthedocs.io/en/dev-esp32/modules/gpio/#gpiowrite(和错误信息),第二个参数必须是一个整数; 1 或 0。
您的代码
gpio.write(2, LOW)
似乎发送 nil
。您的 LOW
没有在任何地方初始化,是吗?绝对应该可以正常工作的是:
LOW = 0
gpio.write(2, LOW)
(当然是 gpio.write(2, 0)
)
bad argument #2 to 'write' (number expected, got nil)
这说明了问题所在:write
的第二个参数是 nil
,即 LOW
是 nil。您可以通过输入 print(LOW)
来确认它应该打印 nil
原因很简单:LOW
未在任何地方定义;
正如您在 documentation 中所读到的,NodeMCU 定义了 gpio.LOW 和 gpio.HIGH(在幕后,它们只是 0 和 1)
正如 Marcel Stör 指出的那样,gpio.LOW
和 gpio.HIGH
仅针对 ESP8266 版本的固件定义。对于 ESP32,您需要使用纯数字 0
和 1
代替(或定义您自己的变量)