Hello, I am currently extremely frustrated, and really need help.
Currently I am using a 7 in 1 halisense soil sensor, which wen sent the following query:
0x01 0x03 0x00 0x00 0x00 0x07 0x04 0x08
Should reply the following:
0x01 0x03 0x0E 0x01 0xD0 0x01 0x4C 0x00 0x2C 0x00 0x5A 0x00 0x20 0x00 0x58 0x00 0x68 0x70 0x29
I have already connected the sensor via USB to my computer, and the values are coherent. However, when i try using it through my ESP32 Wroomkit, it doesnt work.
To communicate the MODBUSrs485 response from the sensor, i am using a MAX485 interface. This is the code i am using:
#define DE 32 // DE connected to GPIO 32
#define RE 33 // RE connected to GPIO 33
#define MODBUS_RX 16 // RX2 for ESP32
#define MODBUS_TX 17 // TX2 for ESP32
void setup() {
Serial.begin(9600); // For serial monitor
Serial1.begin(4800, SERIAL_8N1, MODBUS_RX, MODBUS_TX); // RS485 communication with sensor
pinMode(DE, OUTPUT);
pinMode(RE, OUTPUT);
digitalWrite(DE, LOW); // Start in receive mode
digitalWrite(RE, LOW); // Start in receive mode
}
void loop() {
// Query for multiple parameters
uint8_t query[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x07, 0x04, 0x08};
byte receivedData[20]; // Extended buffer for received data
// Print the sent query
Serial.print("Sent Query: ");
for (int i = 0; i < sizeof(query); i++) {
Serial.print(query[i], HEX);
Serial.print(" ");
}
Serial.println();
// Set to transmission mode
// Set to transmission mode
// Set to transmission mode
// Set to transmission mode
digitalWrite(DE, HIGH); // Enable Driver
digitalWrite(RE, HIGH); // Disable Receiver
delay(30); // Ensure mode switch is stable
// Send the query
Serial1.write(query, sizeof(query)); // Send the 8-byte query
delay(35); // Delay to allow time for the entire query to be transmitted
// Set to receive mode after sending
digitalWrite(DE, LOW); // Disable Driver
digitalWrite(RE, LOW); // Enable Receiver
delay(50); // Short delay to give the sensor time to respond
// Check and print the received data
int bytesAvailable = Serial1.available();
Serial.print("Bytes available: ");
Serial.println(bytesAvailable);
if (bytesAvailable > 0) {
Serial.print("Received Data: ");
for (int i = 0; i < bytesAvailable && i < 20; i++) { // Limit to 20 bytes
receivedData[i] = Serial1.read();
Serial.print(receivedData[i], HEX);
Serial.print(" ");
}
Serial.println();
} else {
Serial.println("No response or insufficient data received");
}
delay(5000); // Wait 10 seconds before sending the next query
}
The RE RO DE DI of the modbus are connected to the GPIO's stated in the code. Also every single device has the same ground (sensor, max485 and esp32). However, in the serial terminal I am recieving random, incoherent data. To ensure that the voltage levels werent a problem, the output of the max485 (which is 5V) is reduced to 3.15 V via a voltage regulator.
I dont know whats going wrong. I get random recieved data. Any help is welcome.