问题
I'm using a NodeMCU board, V3 pinout, and the Arduino IDE. I need to oscillate one of the output pins, and digging around I found this page: https://github.com/nodemcu/nodemcu-firmware/blob/master/docs/en/modules/gpio.md
Very useful, especially the gpio.serout()
function, but I can't get it to work. This is my code:
#include <gpio.h>;
#define LED D5
void setup() {
gpio.mode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
Serial.write("Starting blinking.");
gpio.serout(LED, HIGH, 1000000, 10);
Serial.write("Done.");
}
The #include <gpio.h>;
is a guess of mine after the compiler threw the error 'gpio' was not declared in this scope
but the error remains. The file gpio.h
obviously imports fine, or it'd complain about that. All I can find is code snippets like the manual page linked to above, no complete sketches.
Any way to get to use these gpio
functions?
回答1:
Although there are several boards that are either named NodeMCU
(or referred to as NodeMCU
), the name NodeMCU
really refers to one of several possible firmwares that can be installed on an ESP8266-based chip/dev board.
NodeMCU is a popular SDK for the ESP8266 which provides you with a Lua interpreter and a number of different modules for controlling different functions and using different communication protocols. See the NodeMCU Documentation which lists and documents the various available extensions, including the gpio
module you reference in your question. See http://lua.org for information on the Lua programming language.
I very much like Lua, and I find using the NodeMCU SDK to be a lot of fun. However, there are other options for programming these boards.
A very good alternative is to use an Arduino-compatible SDK that allows you to program in C. If you are going to use the Arduino IDE than you will need to install this SDK. See the esp8266/Arduino github repository for installation instructions and to download the SDK.
Once this SDK is installed, you can control the pins on your board in exactly the same way as you would program any other Arduino, using the digitalWrite
function. So for your example, you could do something like:
int pin = 12; // or whatever pin you want
for (int i = 0; i < 10000; i++) {
digitalWrite(pin, HIGH);
delay(10);
digitalWrite(pin, LOW);
delay(10);
}
You should extract that into a function, and what I have written isn't as flexible as the gpio.serout
function, but it gives you a place to start. You can take a look at the gpio module (source code) and adapt its implementation of the serout
function if you need something more complex.
回答2:
Quick way to start using NodeMCU is to use the Arduino IDE. You can use the sequence of steps as described in the link. Once you do this, you can use the examples in the IDE itself. As a start, you can use the example in File->Examples->Basics->Blink. This will blink the onboard LED
回答3:
There is no need to use the "gpio.h" header file. To oscillate one of the I/O pins you can use the arduino code for the simple Blinking of the Led and directly burn it into the NODEMCU Board.
There are two different methods to program the NODEMCU:
- Arduino IDE
- Burning some firmware which acts as the operating system and inside which we write our code files.
As long we use the Arduino IDE for programming we need not worry about the firmware since the Arduino code gets converted to *.bin format which is burnt on the NODEMCU and the firmware which we are talking about helps us to program in different languages like MicroPython , Lua.
Everything is taken care of by the Arduino IDE. It creates its own version of the firmware and burns it on the board.
来源:https://stackoverflow.com/questions/42750573/nodemcu-gpio-programming