Hello
So far, I have established communication with only one sensor by using esp8266 and the following module and rs485 connection. The rs485 to ttl conversion module and my code is as follows.
#include <SoftwareSerial.h>
const byte ph[] = {0x02, 0x03, 0x00, 0x06, 0x00, 0x01, 0x64, 0x38}; // Soil PH Sensor
byte ph_values[11];
#define RE 5
#define DE 4
#define PUB_DELAY 50
void setup() {
mod.begin(9600, SWSERIAL_8N1, 14, 12, false, 256);
pinMode(RE, OUTPUT);
pinMode(DE, OUTPUT);
// put your setup code here, to run once:
}
void loop() {
while (true){
digitalWrite(DE, HIGH);
digitalWrite(RE, HIGH);
delay(100);
if (mod.write(ph, sizeof(ph)) == 8)
{
mod.flush();
digitalWrite(DE, LOW);
digitalWrite(RE, LOW);
mod.readBytes(ph_values, 7);
// Serial.print("Multi PH Values : ");
for (byte i = 0; i < 7; i++)
{
// Serial.print(ph_values[i], HEX);
Serial.print(" ");
}
Serial.println();
}
if (ph_values[0] == 0x02 && ph_values[1] == 0x03 && ph_values[2] == 0x02){
float ph_reg3and4 = ((ph_values[3] << 8) + ph_values[4]);
ph_reg3and4 = ph_reg3and4/100;
pubPH = ph_reg3and4;
if (ph_reg3and4 > -1 && ph_reg3and4 < 15){
Serial.print("PH : "+(String)ph_reg3and4+"");
Serial.println();
delay(100);
int ph;
ph = (int) (ph_reg3and4 * 10L);
if (phcomp != ph){
phcomp = ph;
Serial.println("Published");
}
break;
}
}else{
Serial.print("PH cant measure");
break;
}
}
I want to communicate with several slaves through esp8266. I know I have to use the ModbusRTU library. But I don't know how to write the code. Thank you for your guidance.
