RS485_Soil_Sensor_Temperature_Humidity_EC_PH_SKU

Hi I was trying to change the addess on my sensor https://wiki.dfrobot.com/RS485_Soil_Sensor_Temperature_Humidity_EC_PH_SKU_SEN0604#target_9 I used this code and I cannot read any value anymore: “// Funzione per calcolare il CRC16 secondo il protocollo Modbus
unsigned int CRC16_2(unsigned char *buf, int len) {
unsigned int crc = 0xFFFF;
for (int pos = 0; pos < len; pos++) {
crc ^= (unsigned int)buf[pos];
for (int i = 0; i < 8; i++) {
if (crc & 0x0001) {
crc >>= 1;
crc ^= 0xA001;
} else {
crc >>= 1;
}
}
}
// Riorganizza in modo che il byte alto venga inviato per primo
crc = ((crc & 0x00FF) << 8) | ((crc & 0xFF00) >> 8);
return crc;
}
void setup() {
// Inizializza la comunicazione seriale a 9600 bps
Serial.begin(9600);
// Attendi che la porta seriale sia pronta
delay(2000);
// Chiamata alla funzione per cambiare l'indirizzo
changeSensorAddress(0x01, 0x02); // da 0x01 (indirizzo attuale) a 0x02 (nuovo indirizzo)
}
void loop() {
// Il comando viene eseguito una sola volta in setup
// Dopo la modifica si ferma lo sketch (oppure puoi estendere le funzionalità)
while(true);
}
/// Funzione per cambiare l'indirizzo del sensore sul bus RS485
/// @param currentAddr L'indirizzo corrente del sensore (es. 0x01)
/// @param newAddr Il nuovo indirizzo da impostare (da 1 a 254, es. 0x02)
void changeSensorAddress(uint8_t currentAddr, uint8_t newAddr) {
// Costruzione del comando Modbus per la funzione "Write Single Register" (0x06)
// Registro per l'indirizzo del dispositivo: 07D0H
// Il comando è composto da 8 byte:
// [Indirizzo Slave, Funzione, Indirizzo Registro High, Indirizzo Registro Low, Valore High, Valore Low, CRC High, CRC Low]
uint8_t command[8];
command[0] = currentAddr; // Indirizzo corrente del sensore
command[1] = 0x06; // Funzione 0x06 (Scrittura di un singolo registro)
command[2] = 0x07; // Registro 07D0H -> parte alta
command[3] = 0xD0; // Registro 07D0H -> parte bassa
// Il valore da scrivere (nuovo indirizzo) viene inviato in 2 byte.
// Esempio: se vogliamo impostare l'indirizzo a 0x02, il valore sarà 0x0002.
command[4] = 0x00; // Byte alto: 0x00
command[5] = newAddr; // Byte basso: nuovo indirizzo (es. 0x02)
// Calcola il CRC sui primi 6 byte
unsigned int crc = CRC16_2(command, 6);
command[6] = crc >> 8; // Byte alto del CRC
command[7] = crc & 0xFF; // Byte basso del CRC
// Invia il comando sul bus RS485
Serial.write(command, 8);
// Attendi qualche istante per permettere al sensore di elaborare il comando
delay(1000);
// Eventuale log di conferma
Serial.println("Command sent.");
}
” Can I somehow reset the default configuration or correct this?
Steps to Resolve the Issue:
1. Reset the Sensor to Default:
- Some sensors can be reset by power cycling them (turn off and on) or holding a specific button during startup. Refer to the sensor's manual for exact steps.
- If the sensor supports Modbus broadcast (address `0x00`), you might be able to send a reset command to revert to the default address.
2. Verify the Register Address:
- Double-check the register address `0x07D0` in the [sensor's documentation](https://wiki.dfrobot.com/RS485_Soil_Sensor_Temperature_Humidity_EC_PH_SKU_SEN0604#target_9). If incorrect, update the code with the correct register.
3. Test Communication with Default Address:
- Modify your code to communicate with the default address (`0x01`) and see if the sensor responds. This will confirm whether the issue is with the address change or another factor (e.g., wiring).
4. Check RS485 Communication:
- Ensure the RS485 bus is properly wired (A/B lines, termination resistors, and correct baud rate).
- Use a tool like a Modbus sniffer or another device to verify communication on the bus.
5. Alternative Approach:
- If the sensor is unresponsive, try connecting it to a known-working Modbus master (e.g., a PLC or software like Modbus Poll) to test its functionality.
Example Code to Test Default Address:
```cpp
void setup() {
Serial.begin(9600);
delay(2000);
}
void loop() {
// Example: Read a register (e.g., temperature) from the default address 0x01
uint8_t command[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x01, 0x84, 0x0A}; // Example read command
Serial.write(command, sizeof(command));
delay(1000);
// Check for a response
if (Serial.available()) {
while (Serial.available()) {
Serial.print(Serial.read(), HEX);
Serial.print(" ");
}
Serial.println();
}
delay(5000);
}
```
If the sensor still doesn't respond, it may require a hardware reset or further troubleshooting. Let me know if you need help with any of these steps!

Some tools like ModScan, Modbus Doctor, or Arduino-based Modbus scanner sketches can help scan the entire range (1–254) and see if the sensor responds at another address (in case something weird happens like a bad CRC causing an incorrect value).
