Issue communicating via UART with a esp8266

I’ve got a NodeMCU 1.0 running in esphome configured with a UART bus and a series of switches with on and off messages. It was initially working fine but suddenly stopped working and I can no longer see any of the messages it sends via an Arduino Uno. The example arduino code provided prints nothing as switches are toggled.

D4 (GPIO02) on the 8266 is connected to pin 2 on the Uno.

SOLVED:

I simply left both devices connected separately to power and the issue is that they are not sharing the same ground. Powering the 8266 with the Uno solves the issue. This is done by connect the 5v on the Uno to the Vin on the 8266, and connecting a gnd on the Uno to a gnd on the 8266.

Here’s the following test configuration, log, and code:

1.) YAML configuration for the 8266
2.) Log output from the 8266
3.) The Arduino code I’m running on the Uno

1.) yaml:

esphome:
  name: notification-controller
  friendly_name: Notification Controller

esp8266:
  board: esp01_1m

# Enable logging
logger:

...
    
uart:
  tx_pin: GPIO02
  baud_rate: 115200

switch:
  - platform: uart
    name: "Dot A1"
    data:
      turn_on: "OnA1\r\n"
      turn_off: "OffA1\r\n"

  - platform: uart
    name: "Dot A2"
    data:
      turn_on: "OnA2\r\n"
      turn_off: "OffA2\r\n"

  - platform: uart
    name: "Dot A3"
    data:
      turn_on: "OnA3\r\n"
      turn_off: "OffA3\r\n"

  - platform: uart
    name: "Dot A4"
    data:
      turn_on: "OnA4\r\n"
      turn_off: "OffA4\r\n"

  - platform: uart
    name: "Dot A5"
    data:
      turn_on: "OnA5\r\n"
      turn_off: "OffA5\r\n"

  - platform: uart
    name: "Dot A6"
    data:
      turn_on: "OnA6\r\n"
      turn_off: "OffA6\r\n"

  - platform: uart
    name: "Dot A7"
    data:
      turn_on: "OnA7\r\n"
      turn_off: "OffA7\r\n"

  - platform: uart
    name: "Dot B1"
    data:
      turn_on: "OnB1\r\n"
      turn_off: "OffB1\r\n"

  - platform: uart
    name: "Dot B2"
    data:
      turn_on: "OnB2\r\n"
      turn_off: "OffB2\r\n"

  - platform: uart
    name: "Dot B3"
    data:
      turn_on: "OnB3\r\n"
      turn_off: "OffB3\r\n"

  - platform: uart
    name: "Dot B4"
    data:
      turn_on: "OnB4\r\n"
      turn_off: "OffB4\r\n"

  - platform: uart
    name: "Dot B5"
    data:
      turn_on: "OnB5\r\n"
      turn_off: "OffB5\r\n"

  - platform: uart
    name: "Dot B6"
    data:
      turn_on: "OnB6\r\n"
      turn_off: "OffB6\r\n"

  - platform: uart
    name: "Dot B7"
    data:
      turn_on: "OnB7\r\n"
      turn_off: "OffB7\r\n"

2.) Log

INFO Successfully connected to notification-controller @ 192.168.1.227 in 35.590s
INFO Successful handshake with notification-controller @ 192.168.1.227 in 2.710s
[17:39:31][I][app:100]: ESPHome version 2024.5.4 compiled on Jun 13 2024, 17:37:02
[17:39:31][C][wifi:580]: WiFi:
[17:39:31][C][wifi:408]:   Local MAC: E8:XX:6D:XX:18:XX
[17:39:31][C][wifi:413]:   SSID: [redacted]
[17:39:31][C][wifi:416]:   IP Address: 192.168.1.227
[17:39:31][C][wifi:419]:   BSSID: [redacted]
[17:39:31][C][wifi:421]:   Hostname: 'notification-controller'
[17:39:31][C][wifi:423]:   Signal strength: -72 dB ▂▄▆█
[17:39:31][C][wifi:427]:   Channel: 6
[17:39:31][C][wifi:428]:   Subnet: 255.255.255.0
[17:39:31][C][wifi:429]:   Gateway: 192.168.1.1
[17:39:31][C][wifi:430]:   DNS1: 192.168.1.1
[17:39:31][C][wifi:431]:   DNS2: 0.0.0.0
[17:39:31][C][logger:185]: Logger:
[17:39:31][C][logger:186]:   Level: DEBUG
[17:39:31][C][logger:188]:   Log Baud Rate: 115200
[17:39:31][C][logger:189]:   Hardware UART: UART0
[17:39:31][C][uart.arduino_esp8266:118]: UART Bus:
[17:39:31][C][uart.arduino_esp8266:119]:   TX Pin: GPIO2
[17:39:31][C][uart.arduino_esp8266:124]:   Baud Rate: 115200 baud
[17:39:31][C][uart.arduino_esp8266:125]:   Data Bits: 8
[17:39:31][C][uart.arduino_esp8266:126]:   Parity: NONE
[17:39:31][C][uart.arduino_esp8266:127]:   Stop bits: 1
[17:39:31][C][uart.arduino_esp8266:129]:   Using hardware serial interface.
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A1'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A2'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A3'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A4'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A5'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A6'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot A7'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B1'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B2'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B3'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B4'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B5'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B6'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][uart.switch:068]: UART Switch 'Dot B7'
[17:39:31][C][uart.switch:090]:   Restore Mode: always OFF
[17:39:31][C][captive_portal:088]: Captive Portal:
[17:39:31][C][mdns:115]: mDNS:
[17:39:31][C][mdns:116]:   Hostname: notification-controller
[17:39:31][C][ota:096]: Over-The-Air Updates:
[17:39:31][C][ota:097]:   Address: notification-controller.local:8266
[17:39:31][C][ota:100]:   Using Password.
[17:39:31][C][ota:103]:   OTA version: 2.
[17:39:31][C][api:139]: API Server:
[17:39:31][C][api:140]:   Address: notification-controller.local:6053
[17:39:31][C][api:142]:   Using noise encryption: YES
[17:39:45][D][switch:012]: 'Dot B2' Turning ON.
[17:39:45][D][switch:055]: 'Dot B2': Sending state ON
[17:39:45][D][uart.switch:021]: 'Dot B2': Sending on data...

3.) Arduino Code:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, -1);

void setup() {
  Serial.begin(115200);
  mySerial.begin(115200);

}

void loop() {
  static char message[128];
  static byte messageIndex = 0;

  while (mySerial.available()) {
    char incomingByte = mySerial.read();

    if (incomingByte == '\n' && messageIndex > 0 && message[messageIndex - 1] == '\r') {
      message[messageIndex - 1] = '\0';
      messageIndex = 0;

      Serial.println(message);

    } else if (messageIndex < sizeof(message) - 1) {
      message[messageIndex++] = incomingByte;
    } else {
      messageIndex = 0;
    }
  }
}

D4/GPIO2 is a poor choice of GPIO. See the table lower down on the page here: ESP8266 Pinout Reference: Which GPIO pins should you use? | Random Nerd Tutorials

You also do not appear to have a common ground connection between the ESP and Arduino. It will be relying on the USB ground. Did you disconnect one of those or are they connected to isolated devices?

So as you surmised the issue was the disconnected grounds. I’ll post an update with a photo of the correct wiring.

As for the choice of GPIO, this is the only other pin that can use hardware serial so I figured it was the correct choice. If I’m looking at the same table you have linked, it says that it is OK to use. What am I missing? Thanks for the help.

As long as you are aware of this:

Screenshot 2024-06-14 at 12-04-30 ESP8266 Pinout Reference Which GPIO pins should you use Random Nerd Tutorials

I did see that, and noted. It’s fine for my use case, the serial communication doesn’t begin for several seconds after the uno boots and I didn’t notice an issue. This is also a prototype build just to get a device working. I plan to write an external component so I can remove the need for multiple controllers and UART in the long run.