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 ?