UART Data read not working

Hi,

I want to receive data by sending data to UPS with ESP8266 NodemCUV3 and RS232 to TTL.
In my tests on PC, I was able to send data to PC, but I could not receive data from PC.

Although I found many documents and examples, I could not succeed in sending data to ESP8266.

My sample codes and hardware pictures are as follows, I kindly request your valuable help.

substitutions:
  platform: ESP8266
  board: nodemcuv2
  deviceName: ha-ups-makelsan
  deviceNameId: ha_ups_makelsan
  deviceFriendlyName: Makelsan UPS

<<: !include common/device_base.yaml

esphome:
  includes:
    - common/scripts/uart/uart_read_line_sensor.h

logger:
  baud_rate: 0

uart:
  id: ${deviceNameId}_bus
  tx_pin: D1
  rx_pin: D3
  baud_rate: 2400
  data_bits: 8
  stop_bits: 1
  parity: none
  
text_sensor:
- platform: custom
  text_sensors:
    name: "UART Line"
    id: uart_readline
  lambda: |-
    auto uart_line = new UartReadLineSensor(id(${deviceNameId}_bus));
    App.register_component(uart_line);
    return {uart_line};

#interval:
#  - interval: 3s
#    then:
#      - uart.write: ""

switch:
  - platform: template
    id: senddata_00
    name: Send Data Q1 - 3
    turn_on_action:
      - uart.write:
         id: ${deviceNameId}_bus
         data: !lambda |-
          std::string str = "Q1";
          std::vector<uint8_t> vec(str.begin(), str.end());
          vec.push_back('\r');
          return vec;
  - platform: uart
    id: senddata_0
    name: Send Data .
    data: .
  - platform: uart
    id: senddata_Q1
    name: Send Data Q1.
    data: Q1.
#include "esphome.h"

class UartReadLineSensor : public Component, public UARTDevice, public TextSensor {
 public:
  UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }

  int readline(int readch, char *buffer, int len)
  {
    static int pos = 0;
    int rpos;

    if (readch > 0) {
      switch (readch) {
        case '\n': // Ignore new-lines
          break;
        case '\r': // Return on CR
          rpos = pos;
          pos = 0;  // Reset position index ready for next time
          return rpos;
        default:
          if (pos < len-1) {
            buffer[pos++] = readch;
            buffer[pos] = 0;
          }
      }
    }
    // No end of line has been found, so return -1.
    return -1;
  }

  void loop() override {
    const int max_line_length = 80;
    static char buffer[max_line_length];
    while (available()) {
      if(readline(read(), buffer, max_line_length) > 0) {
        publish_state(buffer);
      }
    }
  }
};

Source : Custom UART Text Sensor — ESPHome



UART Line Empty

Basen on the pictures it seems that you connected the esp tx to the com port tx.
The tx should go to the others rx. Transmit to receive.
So test cross the cables tx to other rx and vice versa.

Maybe it will solve the problem.

/Mattias

Hi, Thank you for your answer. As you said, I also tried to cross-opt the relevant pins, but in this case, data sending did not work either. So I think this link is correct. :pensive:

Sorry!

I missed that you had contact in one direction. Then I agree with you that you have probably connected it correct.
I have no experience with custom uart components. So cannot help you there.
You could try to use D2 instead of D3. D3 is connected to GPIO0 which is always pulled up (D3 is the flash button so if pulled low it will not boot). I dont know if that causes an issue. Probably not.
I hope you solve it.

/Mattias

Hi,

I tried before by changing the pins, even now I tried again but still no results :frowning:

The only question that remains in my mind is that the hardwares may be faulty. I hope there are no problems with the hardwares. :frowning:

Hi,

I realized that I had a hardware problem that I couldn’t understand.
The RS232 to TTL converter gets too hot.
When I connect the energy source, that is, the 3v source, to the different 3v leg on the nodemcu, the heating stops and it can send and receive data.
The same problem occurs again when I touch the hardware and when I connect the 3v leg to the previous leg, the problem is fixed again. In this way, I am considered to have completed the project.

I will try with a different hardware other than ESP8266 NodemCU and share the result and the final version of the software.

Great that you found your issue. Hardware issues is always a pain.
I hope you can find more stable hardware setup.

/Mattias

Hello again,

I think I managed to circumvent the problem using different hardware.
Now I can send and receive data without overheating.

I share the hardware links I use below.





Wemos D1 Mini Nodemcu Esp8266
RS232 to TTL
Makelsan POWERPACK SE SERİSİ 1 kVA

Many thanks again.

Solition URL