LED Temperature Humidity Sensor, Digit Display RS485 Modbus

I'm trying to read Modbus from the link below, But seem to be getting nothing, Even on the Modbus poll or Modbus slave program.

https://tr.aliexpress.com/item/33054683552.html

This is the code I'm trying

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
#include <ModbusMaster.h>


// instantiate ModbusMaster object
ModbusMaster node;

// variable
int tp_lcd;
int tp_modbus;
int hd_lcd;
int hd_modbus;

void setup() {  
  lcd.begin(20, 4);   // init display

  // init vars
  tp_lcd = 0;
  tp_modbus = 0;
  hd_lcd = 0;
  hd_modbus = 0;

  DisplayCurrentValues(); 

  // use Serial (port 0); initialize Modbus communication baud rate
  Serial.begin(9600);

  // communicate with Modbus slave ID 254 over Serial (port 0)
  node.begin(02, Serial);

}

void loop() {

  // read modbus data

  static uint32_t i;
  uint8_t j, result;
  uint16_t data[6];
  
  i++;
  
  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));
  
  // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));

  // read holding register - temperature
  result = node.readHoldingRegisters(0x0000, 1);
  if (result == node.ku8MBSuccess)
  {
      tp_modbus = node.getResponseBuffer(0);
  }

  // read holding register - humidity
  result = node.readHoldingRegisters(0x0001, 1);
  if (result == node.ku8MBSuccess)
  {
      hd_modbus = node.getResponseBuffer(0);
  }

  // display data on LCD
  CheckForDataChange();
  
}

void CheckForDataChange() {

  boolean data_has_changed = false;

  if (tp_lcd != tp_modbus) {
    data_has_changed = true;
    tp_lcd = tp_modbus;
  }

  if (hd_lcd != hd_modbus) {
    data_has_changed = true;
    hd_lcd = hd_modbus;
  }

  if (data_has_changed == true) {
    DisplayCurrentValues();
  }
    
}

void DisplayCurrentValues() {

  String tmpstr;

  lcd.clear();        // clear display

  tmpstr = "Temperature: " + String(tp_lcd);
  lcd.setCursor(0,0);
  lcd.print(tmpstr);

  tmpstr = "Humidity: " + String(hd_lcd);
  lcd.setCursor(0,1);
  lcd.print(tmpstr);
  
}

I've tried many ways, Has anybody else had any success with these modeules ?

The Serial port is RS-232, your device expect RS485. The two are not the same but they do make converts

blh64:
The Serial port is RS-232, your device expect RS485. The two are not the same but they do make converts

Sorry I forgot to add that for the PC software I'm using an RS485 to usb converter which shows as an serial port and with the slave code I can see this on the PC when I connect to it using the 485 to usb converter.

On the Nano's I'm using the RS485 to TTL converters modules, I know these are working as the 2 programs below work, The salve sends the value to the master and it displays the correct values on the LCD.

Slave

#include <ModbusRtu.h>

// registers in the slave
uint16_t au16data[16] = {
  3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, -1
};

Modbus slave(2, 0, 0); // this is slave @1 and RS-232 or USB-FTDI

void setup() {
  slave.begin( 9600 ); // baud-rate at 9600
}

void loop() {
  au16data[0] = analogRead(A0); // update data to be read by the master to adjust the PWM
  slave.poll( au16data, 16 );

}

Master

// include the library code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ModbusMaster.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
// instantiate ModbusMaster object
ModbusMaster node[2];
#define NODE_MAX 2 // maximum number of slave nodes (I2C addresses) to probe
#define START_NODE 1 // The starting I2C address of slave nodes
#define NODE_READ_DELAY 50;// Some delay between I2C node reads
int Data_in[2];

 int j, result;
uint8_t j1, result1;

// variable
float input_voltage1 = 0.0;
float input_voltage2 = 0.0;

int Battery1_modbus;
int Battery2_modbus;

void setup() {
  lcd.begin(20, 4);   // init display
  lcd.clear();        // clear display
  // init vars
  Battery1_modbus = 0;
  Battery2_modbus = 0;

  DisplayCurrentValues();

  // use Serial (port 0); initialize Modbus communication baud rate
  Serial.begin(9600);

  // communicate with Modbus slave ID 10 over Serial (port 0)
  node[0].begin(2, Serial);
 // node[1].begin(11, Serial);

}

void loop() {
/*
 * for (int nodeAddress = START_NODE; nodeAddress <= NODE_MAX; nodeAddress++) { // we are starting from Node address 2
    Wire.requestFrom(nodeAddress, PAYLOAD_SIZE);    // request data from node#
    if(Wire.available() == PAYLOAD_SIZE) {  // if data size is avaliable from nodes
      for (int i = 0; i < PAYLOAD_SIZE; i++) nodePayload[i] = Wire.read();  // get nodes data
      for (int j = 0; j < PAYLOAD_SIZE; j++) Serial.println(nodePayload[j]);   // print nodes data   
      Serial.println("*************************");      
      }
    }
    delay(NODE_READ_DELAY);
}
 */
 result = node[0].readInputRegisters(0, 1);
  if (result == node[0].ku8MBSuccess)
  {
    Battery1_modbus = node[0].getResponseBuffer(0);
   }
 
  delay(50);
  /*
  result1 = node[2].readInputRegisters(0, 1);
  if (result1 == node[1].ku8MBSuccess)
  {
    Battery2_modbus = node[1].getResponseBuffer(0);
  }
 
  */
  input_voltage1 = (Battery1_modbus * 5.0) / 1024.0; 
 // input_voltage2 = (Battery2_modbus * 5.0) / 1024.0; 
  DisplayCurrentValues();
}



void DisplayCurrentValues() {


  lcd.setCursor(0, 0);
  lcd.print(Battery1_modbus);
  lcd.print("    ");
  lcd.setCursor(0, 1);
  lcd.print(input_voltage1);
  lcd.print("    ");
  


}

That master code is communicating i2c. Where did that come from? Is that how to communicate with the RS485 to TTL converter module? A schematic (even hand drawn) would be helpful as you just introduced a whole lot of new information...

The I2C is only for the LCD, The above codes work sending data from the salve to the master.

I only set this code up so I know that the units work and are wired correctly.

I've found some software that works on the PC side of things with the temperature sensor poated in the above link called CAS Modbus scanner, This displays the data correctly so not sure why the others do no which does not really mater as I want to use Arduino to display the temperature which the final goal is I want to measure several of these in different lactations to a master and display them all on an LCD.

Still trying to figure out where it's going wrong on the Arduino end.

Here is a picture of my set up.

I've had another play around with the code and have manged some how to get it read and display the temperature, But the humidity remains 0.0, I think I've set the registers correctly.

On CAS Modbus scanner I can read both temperature and humidity.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // Set the LCD I2C address
#include <ModbusMaster.h>
// instantiate ModbusMaster object
ModbusMaster node;
// variable
int tp_lcd;
int tp_modbus;
int hd_lcd;
int hd_modbus;

void setup() {
  lcd.begin(20, 4);   // init display
  // init vars
  tp_lcd = 0;
  tp_modbus = 0;
  hd_lcd = 0;
  hd_modbus = 0;

  DisplayCurrentValues();

  // use Serial (port 0); initialize Modbus communication baud rate
  Serial.begin(9600);

  // communicate with Modbus slave ID 254 over Serial (port 0)
  node.begin(2, Serial);

}

void loop() {

  // read modbus data
  static uint32_t i;
  uint8_t j, result ;
  i++;

  // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  node.setTransmitBuffer(0, lowWord(i));
  // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  node.setTransmitBuffer(1, highWord(i));
  // read holding register - temperature
  result = node.readHoldingRegisters(0X0000, 1);
  if (result == node.ku8MBSuccess)
  {
    tp_modbus = node.getResponseBuffer(0);
  }

  // read holding register - humidity
  result = node.readHoldingRegisters(0X0001, 1);
  if (result == node.ku8MBSuccess)
  {
    hd_modbus = node.getResponseBuffer(0);
  }

  // display data on LCD
  CheckForDataChange();


}

void CheckForDataChange() {

  boolean data_has_changed = false;

  if (tp_lcd != tp_modbus) {
    data_has_changed = true;
    tp_lcd = tp_modbus;
  }

  if (hd_lcd != hd_modbus) {
    data_has_changed = true;
    hd_lcd = hd_modbus;
  }

  if (data_has_changed == true) {
    DisplayCurrentValues();
  }

}

void DisplayCurrentValues() {

  String tmpstr;
  float tmpfloat;

  lcd.clear();        // clear display

  tmpfloat = tp_lcd;
  tmpfloat = tmpfloat / 10;
  tmpstr = "Temperature:" + String(tmpfloat, 1);
  lcd.setCursor(0, 0);
  lcd.print(tmpstr);

  tmpfloat = hd_lcd;
  tmpfloat = tmpfloat / 10;
  tmpstr = "Humidity(%):" + String(tmpfloat, 1);
  lcd.setCursor(0, 1);
  lcd.print(tmpstr);

}