ESP32 - HardwareSerial, change Rx pin periodically

brunot
Posts: 2
Joined: Sat Jun 29, 2024 5:52 am

ESP32 - HardwareSerial, change Rx pin periodically

Postby brunot » Sat Jun 29, 2024 6:12 am

Hello,
I am in France. We have electricity counters with serial outputs for data.
I have 3 of those counters, for which I want to collect the data using an ESP32
My software works ok for those 3 counters, individually or by 2 (using uart1 and uart2), I am using the HardwareSerial library.
I initially wanted to also use UART0 to collect the 3rd counter, but it seems not so simple (maybe have to go through bootloader modification to be able to use UART0 for my purpose (and using other pins that the default 1 & 3).

Anyway, I decided to only use UART1 and UART2 as 1 of the counter needs to be monitored all the time, but for the 2 others, 1 is only required during the day, and the other during the night.
I only need the Rx Pin on those counters as I can only read data.
I have :
1) main counter : GPIO 16 - UART1 - all the time
2) photovoltaic counter : GPIO 26 - UART2 - day
3) water heater counter : GPIO 15 - UART2 - night

in my software, creating and configuring any of those inputs works, I can receive data
but when the SW switches counter (on UART2) : no more data from UART2.

init :
...
this->port = new HardwareSerial(UART_nbr);
this->port->begin(baudrate, TIC_PROTOCOL, RX_pin);
...
=> this works ok with any of my 3 inputs, and any of the 2 UART 1 or 2.

What my "changeInput" code looks like (only the relevant part - of course baudrate & RX_pin values are different)
...
this->port->end();
this->port->begin(baudrate, TIC_PROTOCOL, RX_pin);
...
After this "changeInput" is executed, no more data from the UART.

I also tried to add a delay(1000); between the "end" and consecutive begin, no more result.

I also tried for "changeInput"
...
delete this->port;
this->port = new HardwareSerial(UART_nbr);
this->port->begin(baudrate, TIC_PROTOCOL, RX_pin);
...
=> No more result.

Any idea of what I am doing wrong and how I should handle this ?
Btw, I am updating/debugging the ESP32 over wifi therefore not really needing the Serial of UART0 for that purpose.

Thanks for any help

lbernstone
Posts: 829
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 - HardwareSerial, change Rx pin periodically

Postby lbernstone » Mon Jul 01, 2024 4:42 pm

Have you tried doing the end and begin outside of the class? You need to distinguish whether this is an error of the underlying framework, or whether it is a problem with how you are using it.

brunot
Posts: 2
Joined: Sat Jun 29, 2024 5:52 am

Re: ESP32 - HardwareSerial, change Rx pin periodically

Postby brunot » Sun Jul 14, 2024 5:59 am

Hi,

sorry, I thought I wrote an answer twice here, I guess I never finised my post.. Anyway..
So I did not understand why it might not work inside a class instance, but I ran some tests.

In the end, I figured out that it works perfectly, even inside a class instance.
The only issue was my triggering condition for the change of pin : it was changing at every "loop" once the initial condition was reached.
I fixed it and got no more issue.

Sorry for the trouble.

Who is online

Users browsing this forum: No registered users and 46 guests