You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Arduino Mega与NodeMCU ESP8266基于Firmata.js的UART通信故障排查

Troubleshooting Serial Reading with Firmata.js & NodeMCU ESP8266

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-data event triggers every time data arrives on the specified port, which is better suited for continuous reading than the one-time serialRead callback.
  • Adding .trim() cleans up extra newline characters from the Mega's println output 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:

  1. Open the StandardFirmataWifi sketch in the Arduino IDE (File > Examples > Firmata > StandardFirmataWifi)
  2. Look for lines like #define SERIAL_PORT_HARDWARE 0—ensure this is uncommented and matches the HW_SERIAL0 port you're using in code.
  3. 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_hostname in 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:02:09