Hello community,
I am building a weather station and bought a RS485 Modbus wind direction sensor. I am new to RS485, and have been diving a lot into modbus coding, registers, addresses and transmitter/reciever roles, guessing I could get to solve this problem, but here I am. Couldn't sort it out yet and that is the reason why I ask you for a bit of help (I feel I'm close!).
I have been deeply looking for very very similar posts, and even tried their codes (some of the had the same sensor settings in fact, same connection and RS module), but got no successful results.
Y have an ESP32 Devkit v4, MAX485 with 5V supply and the wind direction sensor offering A and B RS485 terminals connected to MAX485_
- DE and RE connected together for easier control of transmitter/receiver.
- DI to GPIO16 named in the code as RX_PIN
- R0 to GPIO 17 named in the code as TX_PIN
Sensors ground is not connected to ESP32/MAX485 ground, as the source is independent (one comes from a 12V battery and the other from the computer USB; final prototype has same source with a DC/DC controller but for coding and testing purposes I am doing it that way in order to check serial communication with the computer).
I tested the sensor connected via CH340 USB to the computer and get correct hexagesimal results with the drives given by the sensor's supplier, but when it comes to coding the ESP32 I am not able to get sensor readings.
#include <SoftwareSerial.h>
#define RX_PIN 16
#define TX_PIN 17
#define DERE_PIN 5
SoftwareSerial modbusSerial(RX_PIN, TX_PIN);
const byte request[] = {0x01, 0x03,0x00 ,0x00, 0x00 ,0x02, 0xC4, 0x0B};
byte values[11];
const uint32_t TIMEOUT = 500UL;
void setup() {
pinMode(DERE_PIN, OUTPUT);
digitalWrite(DERE_PIN, LOW);
modbusSerial.begin(4800, SWSERIAL_8N1);
Serial.begin(115200);
}
void loop() {
byte val1;
Serial.println("Vane: ");
val1 = vane();
delay(250);
Serial.println(val1);
Serial.println(" degrees");
Serial.println(sizeof(request));
delay(5000);
}
byte vane() {
uint32_t startTime = 0;
uint8_t byteCount = 0;
digitalWrite(DERE_PIN, HIGH);
delay(10);
if(modbusSerial.write(request,sizeof(request))==8){
digitalWrite(DERE_PIN, LOW);
for(byte i=0;i<7;i++){
Serial.print(modbusSerial.read(),HEX);
values[i] = modbusSerial.read();
Serial.print(values[i],HEX);
}
Serial.println();
}
This is what I get from serial:
And this is what I get from the USB RS485 adapter connected to PC.
Sorry but executable given by provider is in chinese and couldn't manage to translate it. I just followed their instructions (uploaded below).
Wind Vane instructions-RS485_EN.pdf (1,8 MB)
I would very appreciate your help. I feel stuck on this taking me a lot of time... Please help!!
Thank you in advance,
Gorka