hello, can i ask how do i resolve this issue, and how can i connect the same pin in DE RE. thank you!!
#include <ModbusMaster.h>
// Modbus RTU requests for reading NPK values
const byte nitro[] = {0x01, 0x03, 0x00, 0x1E, 0x00, 0x01, 0xE4, 0x0C};
const byte phos[] = {0x01, 0x03, 0x00, 0x1F, 0x00, 0x01, 0xB5, 0xCC};
const byte pota[] = {0x01, 0x03, 0x00, 0x20, 0x00, 0x01, 0x85, 0xC0};
// Create a ModbusMaster object
ModbusMaster sensor;
// Define the Serial interface for RS485 communication
#define SerialRS485 Serial1 // Use Serial1 for RS485 (TX on GPIO 17, RX on GPIO 16)
#define DE_RE_PIN 23 // GPIO pin for DE and RE control
void setup() {
// Set the baud rate for the Serial monitor
Serial.begin(115200);
// Set the baud rate for RS485 communication
SerialRS485.begin(9600, SERIAL_8N1);
// Set DE and RE pins as output
pinMode(DE_RE_PIN, OUTPUT);
digitalWrite(DE_RE_PIN, LOW); // Start in receive mode
// Initialize Modbus communication
sensor.begin(1, SerialRS485); // 1 is the slave ID of your sensor
}
void loop() {
// Read values
byte val1, val2, val3;
val1 = nitrogen();
delay(250);
val2 = phosphorous();
delay(250);
val3 = potassium();
delay(250);
// Print values to the serial monitor
Serial.print("Nitrogen: ");
Serial.print(val1);
Serial.println(" mg/kg");
Serial.print("Phosphorous: ");
Serial.print(val2);
Serial.println(" mg/kg");
Serial.print("Potassium: ");
Serial.print(val3);
Serial.println(" mg/kg");
delay(2000);
}
byte nitrogen() {
digitalWrite(DE_RE_PIN, HIGH); // Enable transmitter
delay(1); // Allow time to switch modes
SerialRS485.write(nitro, sizeof(nitro)); // Send nitrogen request
delay(100); // Wait for the response
digitalWrite(DE_RE_PIN, LOW); // Enable receiver
delay(1); // Allow time to switch modes
// Read response
byte response[8];
for (byte i = 0; i < 7; i++) {
response[i] = SerialRS485.read();
Serial.print(response[i], HEX);
}
Serial.println();
return response[4]; // Return nitrogen value
}
byte phosphorous() {
digitalWrite(DE_RE_PIN, HIGH); // Enable transmitter
delay(1); // Allow time to switch modes
SerialRS485.write(phos, sizeof(phos)); // Send phosphorous request
delay(100); // Wait for the response
digitalWrite(DE_RE_PIN, LOW); // Enable receiver
delay(1); // Allow time to switch modes
// Read response
byte response[8];
for (byte i = 0; i < 7; i++) {
response[i] = SerialRS485.read();
Serial.print(response[i], HEX);
}
Serial.println();
return response[4]; // Return phosphorous value
}
byte potassium() {
digitalWrite(DE_RE_PIN, HIGH); // Enable transmitter
delay(1); // Allow time to switch modes
SerialRS485.write(pota, sizeof(pota)); // Send potassium request
delay(100); // Wait for the response
digitalWrite(DE_RE_PIN, LOW); // Enable receiver
delay(1); // Allow time to switch modes
// Read response
byte response[8];
for (byte i = 0; i < 7; i++) {
response[i] = SerialRS485.read();
Serial.print(response[i], HEX);
}
Serial.println();
return response[4]; // Return potassium value
}




