Arduino Mega与NodeMCU ESP8266基于Firmata.js的UART通信故障排查
Let's fix your serial data reading issue step by step—there are a few key areas to check, starting with the most likely culprit:
1. Fix the Hardware Wiring (Critical!)
Serial communication relies on cross-connected TX/RX pins, and it looks like your current wiring is reversed:
- Current (incorrect) setup: Arduino Mega RX0 (pin 0) → NodeMCU RX (pin 21); Mega TX0 (pin 1) → NodeMCU TX (pin 22)
- Correct setup: Arduino Mega TX0 (pin 1) → NodeMCU RX (pin 21); Mega RX0 (pin 0) → NodeMCU TX (pin 22)
TX pins send data, so they must connect directly to RX pins on the other device. Reversing these means no data can pass between the two boards—this is almost certainly why you're not seeing any serial output.
2. Verify Arduino Mega is Sending Data
First, confirm your Mega is actually outputting serial data. Upload this simple test sketch to your Mega:
void setup() { Serial.begin(115200); // Match the baud rate in your Firmata.js code } void loop() { Serial.println("Hello from Arduino Mega!"); delay(1000); // Send a message every second for testing }
This ensures consistent, predictable data is being sent over the UART.
3. Adjust Firmata.js Serial Reading Logic
Your current code uses board.serialRead, but switching to the serial-data event listener is more reliable for continuous reads. Update your code like this:
var EtherPortClient = require("etherport-client").EtherPortClient; var Firmata = require("firmata"); // Ensure you've installed this with `npm install firmata` var board = new Firmata(new EtherPortClient({ host: "ip_hostname", port: 3030 })); board.on("ready", () => { console.log("READY"); const HW_SERIAL0 = board.SERIAL_PORT_IDs.HW_SERIAL0; // Log the port ID to confirm we're targeting the right one console.log("Using serial port ID:", HW_SERIAL0); // Configure the serial port with matching baud rate board.serialConfig({ portId: HW_SERIAL0, baud: 115200 }); // Listen for incoming serial data events board.on("serial-data", (portId, data) => { if (portId === HW_SERIAL0) { const message = Buffer.from(data).toString('ascii').trim(); console.log('Serial port reading'); console.log('Received:', message); } }); console.log('Connected board...'); });
- The
serial-dataevent triggers every time data arrives on the specified port, which is better suited for continuous reading than the one-timeserialReadcallback. - Adding
.trim()cleans up extra newline characters from the Mega'sprintlnoutput for cleaner logs.
4. Confirm StandardFirmataWifi Supports Your Serial Port
Double-check that your NodeMCU's StandardFirmataWifi firmware has support enabled for the hardware serial port you're using:
- Open the StandardFirmataWifi sketch in the Arduino IDE (File > Examples > Firmata > StandardFirmataWifi)
- Look for lines like
#define SERIAL_PORT_HARDWARE 0—ensure this is uncommented and matches theHW_SERIAL0port you're using in code. - If you were using software serial (unlikely here since you're referencing
HW_SERIAL0), you'd need to enable that in the firmware too, but hardware serial is preferred for reliability.
5. Validate Network Connectivity
- Ensure your NodeMCU is connected to the same WiFi network as your web application.
- Confirm the
ip_hostnamein your Firmata.js code is the correct IP address of your NodeMCU (you can find this in the NodeMCU's serial monitor after it connects to WiFi). - Check that port 3030 is open and not blocked by any firewalls on your network or machine.
After making these changes, your console should start showing the expected output:
READY Using serial port ID: 0 Connected board... Serial port reading Received: Hello from Arduino Mega! Serial port reading Received: Hello from Arduino Mega!
内容的提问来源于stack exchange,提问作者Maximus

