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