ESP32 Modbus RS485 sensor not responding
Posted: Thu Nov 09, 2023 6:30 pm
Hello,
I want to work with a sensor that uses Modbus as a communication protocol, to be able to use ESP32, I am using a TTL MAX485 converter.
Unfortunately the sensor is not responsing, sometimes it responds with the same request I send, sometimes it displaying nothing in the serial monitor.
Please can anyone help ?
I want to work with a sensor that uses Modbus as a communication protocol, to be able to use ESP32, I am using a TTL MAX485 converter.
Unfortunately the sensor is not responsing, sometimes it responds with the same request I send, sometimes it displaying nothing in the serial monitor.
Please can anyone help ?
Code: Select all
#include <SoftwareSerial.h>
uint8_t TX_PIN = 27, RX_PIN = 26;
uint8_t LED_PIN = 2;
#define RE 32
#define DE 33
SoftwareSerial Soft_Serial(RX_PIN, TX_PIN);
#define Soft_Serial Serial2
byte no_ByteB, no_ByteV, incomingByteB[128], incomingByteV[128] = {0};
float temperature, dissolvedOxygen;
int doDec, tempDec, doDecp, tempDecp;
float doValue, tempValue;
void setup() {
Serial.begin(115200);
Soft_Serial.begin(9600);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT); // DE/RE Control pin of RS-485
pinMode(LED_PIN, OUTPUT);
Serial.println(" end setup ");
}
void disableEchoMode() {
byte command[] = {0x06, 0x06, 0x11, 0x00, 0x00, 0x01, 0x4C, 0x81};
Serial.println(" ** inside disableEchoMode function ** ");
digitalWrite(DE,HIGH); // Receive Enabled
digitalWrite(RE,HIGH);
Soft_Serial.write(command, sizeof(command));
delay(100);
digitalWrite(DE,LOW); // Transmit Enabled
digitalWrite(RE,LOW);
while (Soft_Serial.available() > 0) {
byte temp = Soft_Serial.read();
incomingByteB[no_ByteB] = temp;
no_ByteB++;
}
Serial.println("Response:");
for (int i = 0; i < no_ByteB; i++) {
Serial.print(String(incomingByteB[i], HEX) + " ");
}
no_ByteB = 0;
Serial.println(" \n ** End Bootcommand ** ");
}
void readModbus() {
byte sendBuffer[] = {0x01, 0x03, 0x00, 0x00, 0x00, 0x04, 0x45, 0xBE};
Serial.println(" ** inside readModbus function ** ");
digitalWrite(DE,HIGH); // Transmit Enabled
digitalWrite(RE,HIGH);
Soft_Serial.write(sendBuffer, sizeof(sendBuffer));
digitalWrite(DE,LOW); // Receive Enabled
digitalWrite(RE,LOW);
delay(100);
Serial.println(" Response inside readmodbus function ");
while (Soft_Serial.available() > 0) {
byte temp = Soft_Serial.read();
Serial.println("Response: " + String(temp, DEC) + " " + String(temp, HEX));
incomingByteV[no_ByteV] = temp;
no_ByteV++;
}
Serial.println(" \n ** End readModbus ** ");
}
//________________________________________LOOP_________________________________________________________________
void loop() {
disableEchoMode();
Serial.println(" *** ");
delay(8000);
readModbus();
//digitalWrite(LED_PIN, HIGH);
/*
Serial.println(" ** here response should be displayed ");
if (no_ByteV >= 12) {
// Check if there are at least 12 bytes in the response
// Extract the dissolved oxygen and temperature values
byte doValueH = incomingByteV[3];
byte doValueL = incomingByteV[4];
byte tempValueH = incomingByteV[7];
byte tempValueL = incomingByteV[8];
// Combine the high and low bytes to form 16-bit values
uint16_t doValueHex = (doValueH << 8) | doValueL;
uint16_t tempValueHex = (tempValueH << 8) | tempValueL;
// Extract the decimal points
byte doDecimals = incomingByteV[5];
byte tempDecimals = incomingByteV[9];
// Calculate the actual values
float doValue = static_cast<float>(doValueHex) / pow(10, doDecimals);
float tempValue = static_cast<float>(tempValueHex) / pow(10, tempDecimals);
// Display the results
Serial.print("Dissolved Oxygen: ");
Serial.print(doValue, 2); // Display with 2 decimal places
Serial.println(" mg/L");
Serial.print("Temperature: ");
Serial.print(tempValue, 1); // Display with 1 decimal place
Serial.println(" °C");
no_ByteV = 0; // Reset the byte count for the next reading
} */
delay(5000);
}
int hexToDec(String hexString) {
int decValue = 0, nextInt;
for (int i = 0; i < hexString.length(); i++) {
nextInt = (int)hexString.charAt(i) - 48; // Subtract 48 from ASCII value
decValue = (decValue << 4) + nextInt;
}
return decValue;
}