Next step, let’s send output to a GPIO pin. In this post, let’s keep it simple. Let’s connect a led to GPIO0 (D0 on the board) and turn it on and off. To test this yourself, connect a led, the cathode of the led to ground and the anode to GPIO0. If you are struggling with which is which, check these instructions.
Well, that was simple enough. We have connected a led and turned it off. Turning it on is just as simple:
As you have guessed, the first line sets the mode of the GPIO to output (instead of input as shown in my earlier post on this). The second line sets GPIO 0 (the first parameter) to 0 (second parameter).
This is too simple, so let’s start making it a little more complex by creating a neat init.lua which allows you to stop it. Remember that after you are done, your NodeMCU will be ‘in the field’ and you will not have a terminal attached. I have created the following init.lua which will automatically connect if a GPIO pin is not low.
- a GPIO configured with a switch or button to allow for an interrupt
(remember you cannot use GPIO0 for this because if this is connected to ground, your NodeMCU will expect a new firmware)
In my example, GPIO 1
- a GPIO configured with a led where the cathode is connect to ground and the anode to a GPIO or you can use the on board led which is connected to GPIO0. That’s what I did in this example. A low signal on GPIO0 will turn on the red led.
------------------ -- -- -- init.lua -- -- -- ------------------ -- 1:The wifi credentials. Change to match your wifi ssid = "YourSSID" pass = "YourPassword" -- 2:Configure wifi wifi.setmode(wifi.STATION) print("\nMy MAC address: "..wifi.sta.getmac()) wifi.sta.config(ssid,pass) -- 3:Prepare GPIOs pin_interrupt = 1 gpio.mode(pin_interrupt,gpio.INPUT) pin_error = 0 gpio.mode(pin_error,gpio.OUTPUT) -- 4:Read and write GPIOs gpio.write(pin_error,gpio.HIGH) interruptit = gpio.read(pin_interrupt) -- 5:Check whether to start the main program if interruptit==1 then print("\nStarting main") dofile("main.lua") else gpio.write(pin_error,gpio.LOW) print("\nGPIO1 low, so not starting main") end
The first section is the config section with your wifi credentials. The second section connects your NodeMCU to your wifi which I described in earlier posts.
The third section sets the variables and prepares the GPIOs. So pin_interrupt is the GPIO I use to interrupt the booting of the NodeMCU. I use pin_error as the GPIO to show with a led whether there is an error. The idea is that if the code is not running (or some other error which I will handle in a next post) the led is on. So no lights means it is working. This is with the led connected to GPIO0. If you want to use your own led you have to switch HIGH to LOW and vice versa when writing to pin_error.
The fourth section, I read my input and turn off the led. In the last section I check whether the main program needs to start and if not, turn on the led so I can see that the program is not running.
The file main.lua will contain the actual ‘program’ that the NodeMCU needs to do.