Hello members and contributors,
Here I am again asking for help. I have a couple of doubts that I haven’t been able to clarify after many attempts and several months of work.
I’m trying to set up a soil moisture monitor with two sensors connected to a Cubecell AB01 V2 , each one linked to an RS485 XY module (automatic RE/DE management). The sensors are powered by an external 5V source, and the board is powered at 4.2V through the JST connector .
I’m using the SoftSerial.h library, but after many tests, the board does not enter Deep Sleep mode . After reading several posts in this forum, I understand that SoftSerial.h causes many issues and is not reliable.
Now, I’m trying to connect one sensor to the board’s native UART port (UART_RX 14; UART_TX 15) , but it does not communicate with the RS485 module . There is some confusion regarding the Rx and Tx pin numbers.
Could someone confirm if SoftSerial.h prevents the board from entering Deep Sleep mode?
Is there another library that allows me to use two digital serial ports?
Thanks, I really need to find a solution.
I’m sharing a test code for communication with a sensor.
#include "Arduino.h"
// Configuración de pines UART
#define UART_RX 14 // RX del CubeCell
#define UART_TX 15 // TX del CubeCell
// Trama MODBUS para leer 3 registros, ID = 1
const byte requestHTC[8] = { 0x01, 0x03, 0x00, 0x00, 0x00, 0x03, 0x05, 0xCB };
byte sensorResponse[12]; // Buffer para respuesta
void setup() {
Serial.begin(115200); // Consola USB para depuración
delay(500);
Serial.println("Iniciando comunicación RS485 con sensor CWM-HT...");
// Inicializar UART1 para comunicación con sensor RS485
Serial1.begin(9600, SERIAL_8N1, UART_RX, UART_TX);
delay(1000); // Espera para estabilización
}
void loop() {
// Limpiar buffer de entrada
while (Serial1.available()) {
Serial1.read();
}
// Enviar solicitud al sensor
Serial.println("\nEnviando solicitud al sensor...");
Serial1.write(requestHTC, sizeof(requestHTC));
// Esperar respuesta
delay(300); // El sensor suele responder en 100-200 ms
// Leer respuesta
int bytesRead = Serial1.readBytes(sensorResponse, 9); // Mínimo 9 bytes
if (bytesRead > 0) {
Serial.print("Respuesta recibida (");
Serial.print(bytesRead);
Serial.println(" bytes): ");
for (int i = 0; i < bytesRead; i++) {
Serial.printf("%02X ", sensorResponse[i]);
}
Serial.println();
// Validación rápida de la respuesta
if (bytesRead >= 9 && sensorResponse[0] == 0x01 && sensorResponse[1] == 0x03) {
// Interpretar humedad y temperatura
uint16_t humedad = (sensorResponse[3] << 8) | sensorResponse[4];
uint16_t temperatura = (sensorResponse[5] << 8) | sensorResponse[6];
Serial.print("Humedad: ");
Serial.print(humedad / 10.0);
Serial.println(" %");
Serial.print("Temperatura: ");
Serial.print(temperatura / 10.0);
Serial.println(" °C");
} else {
Serial.println("Datos recibidos no válidos.");
}
} else {
Serial.println("No se recibió respuesta del sensor.");
}
delay(2000); // Espera 2 segundos antes de la siguiente consulta
}