Reputation: 93
I am unable to read the serial pins in the NodeMCU Lua environment. I have only been able to read the USB serial port.
I have connected a serial adapter to the rx, tx, and g pins.
I have tried this code:
uart.on("data","\n",function(data) print("receive from uart:", data) end, 0)
I enter text in the ESplorer console and it does read that. It doesn't read anything I send over a serial adapter plugged into the rx/tx/g pins.
uart.write(0, "hello")
I disconnected the USB cable and powered it with the serial adapter. Nothing was sent using this code. I tried uart.write(0,
and uart.write(1,
.
Upvotes: 2
Views: 2070
Reputation: 11
You have to use different pins then the RX and TX as they are the same as the USB port you are connecting your NodeMCU with to your PC
You can use any other 2 free gpio pins as a serial port with the help of https://github.com/scottwday/EspSoftSerial library. This library is specificly for ESP8266 on which your NodeMCU is based.
This way you have 2 serial ports, one through the usb and another one to connect to other devices.
Some simpel code to implement the Software serial below.
#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() {
}
}
Upvotes: 1
Reputation: 93
I needed to unplug the USB cable. The device gets confused if the USB cable is plugged in and you're trying to use the pin serial port.
See my question on the esp forums: https://www.esp8266.com/viewtopic.php?f=22&t=19768
Upvotes: 1