我无法在NodeMCU Lua环境中读取串行引脚。我只能读取USB串行端口。
我已将串行适配器连接到rx,tx和g引脚。
我尝试了以下代码:
--arg
我在ESplorer控制台中输入文本,它确实读取了该文本。它不会读取我通过插入rx / tx / g引脚的串行适配器发送的任何内容。
uart.on("data","\n",function(data) print("receive from uart:", data) end, 0)
我断开了USB电缆的连接,并使用串行适配器为其供电。使用此代码未发送任何内容。我尝试了uart.write(0, "hello")
和uart.write(0,
。
答案 0 :(得分:0)
我需要拔下USB电缆。如果插入USB电缆并且您尝试使用针式串行端口,则设备会感到困惑。
在esp论坛上查看我的问题: https://www.esp8266.com/viewtopic.php?f=22&t=19768
答案 1 :(得分:0)
您必须使用不同于RX和TX的引脚,因为它们与将NodeMCU连接到PC的USB端口相同
借助https://github.com/scottwday/EspSoftSerial库,您可以将任意其他2个空闲的gpio引脚用作串行端口。该库专门用于NodeMCU所基于的ESP8266。
这样,您有2个串行端口,一个通过USB,另一个通过USB连接到其他设备。
一些简单的代码来实现下面的软件序列。
#include <SoftwareSerial.h>
#define BAUD_RATE 9600
SoftwareSerial Serial2(D8, D7, false, 8); //Here you choose the pins you connect the RX TX device to
//The first pin you choose is RX the second TX
// in my example these are the D8 and D7 pins on the nodeMCU
// D8=RX .... D7=TX
void setup() {
Serial.begin(BAUD_RATE);
Serial2.begin(BAUD_RATE);
Serial.println(" ### Hello ###");
Serial2.println(" ### Hello ###");
}
void loop() {
}
}