Search code examples
luanodemcuesp32

ESP32 GPIO commands


I just flashed a ESP32 with new custom build for lua files, designed from: https://nodemcu-build.com/ with dev-esp32<>BETA And it seems to be working, I can connect with ESPlorer and the firmware is installed as viewed in the startup code.

Im trying to toggle the BUILTIN_LED, but I get error using the GPIO commands. It worked out of the box with simple arduino code, did I mess something up or why is this not working?

Here is the log with error response from GPIO.mode command

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
> 

Solution

  • As per https://nodemcu.readthedocs.io/en/dev-esp32/modules/gpio/#gpiowrite (and the error message) the second parameter has to be an int; 1 or 0.

    Your code

    gpio.write(2, LOW)
    

    appears to send nil. Your LOW is not initialized anywhere, is it? What should definitely work fine is this:

    LOW = 0
    gpio.write(2, LOW)
    

    (or gpio.write(2, 0) of course)