Reading data from energy meter through modbus protocol

Hi All,

Im trying to read holding register values of Elmeasure LG1129 and BM5140 energy meter using Modbus protocol through Arduino. I used RS485 to TTL converter between Energy meter and Arduino Mega but I haven’t succeeded. Please help me

This is the board i used for RS485 communication:
https://robokits.co.in/control-boards/interface-boards/max485-ttl-to-rs485-converter-module?gclid=EAIaIQobChMIhMDH_d2J5gIVizUrCh2MKwVHEAQYAiABEgKyZvD_BwE

Typical circuit diagram connection:

IMG_20191127_115511%20(1)

Some reference Code I got from forum and burned the same in Arduino:

#include <SimpleModbusMaster.h>

#define baud 9600
#define timeout 1000
#define polling 2000 // the scan rate
#define retry_count 10


#define TxEnablePin 2

#define TOTAL_NO_OF_REGISTERS 31


enum
{
  PACKET1,
  PACKET2,
  

  TOTAL_NO_OF_PACKETS // leave this last entry
};

// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];

// Masters register array
unsigned int regs[TOTAL_NO_OF_REGISTERS];

void setup()
{
  // Initialize each packet
  Serial.begin(9600);
Serial1.begin(9600);
  
  
  modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 40133, 2, 10); 
  modbus_construct(&packets[PACKET2], 1, READ_HOLDING_REGISTERS, 40135, 2, 12); 
  
  modbus_configure(&Serial1, baud, SERIAL_8E1, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
}

void loop()
{
  modbus_update();

  int A, B, C, D, E, F, G, H, I, J, K, L, M, N, O;

  A = regs[10];
  B = regs[12];

  
  Serial.print("A = ");
  Serial.println(A);


  Serial.print("B = ");
  Serial.println(B);

  Serial.println("============================================");
  delay(1000);

  
  Serial.print("successful_requests: ");
  Serial.println(packets[PACKET1].successful_requests);
  Serial.println("                        || ");
  Serial.print("failed_requests: ");
  Serial.print(packets[PACKET1].failed_requests);
  Serial.println("     || ");
  Serial.print("exception_errors: ");
  Serial.print(packets[PACKET1].exception_errors);
  Serial.println("     || ");
  Serial.print("connection: ");
  Serial.print(packets[PACKET1].connection);
  Serial.println("           || ");
 delay(1000);
}

Also Please clarify me this line of code what does number 10 represents although we have register number starting from 0 to 30

modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 40133, 2, 10); 

Hi This is a forum for Particle and its products and we really can’t offer much guidance on Arduino products. Can I suggest you register with this forum (or similar)

https://forum.arduino.cc

3 Likes

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

The code used in that post uses the ModbusMaster library which makes addressing individual devices straight forward.
If you look in post #51 in that thread you will find this

and with that the answer would be straight forward for any other slave ID.

However, since this is not an Arduino forum and hence has no affinity to the SimpleModbusMaster library you appear to be using it’s beyond our scope to make it fit a library currently not Particle user seems to be using.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

If you want another meter (aka node) you’d also need two node objects (with an unique ID for each of them).
To keep the code changes minimal I’d do this

// ModbuusMaster node; // repalce with
ModbusMaster *actNode;
ModbusMaster node[2] = { ModbusMaster(Serial2, ID0), ModbusMaster(Serial2, ID1) }; 
...
void setup() {
  ...
  node[0].begin();
  node[1].begin();
  ...
}

Then replace all node.* calls with actNode->* and before requesting data from a specific node just set the pointer accordingly (e.g. actNode = &node[0]; or actNode = &node[1];).

While there is a lot more potential in streamlining your code above, this should get you unstuck.

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Hi @ScruffR, we use that library with ESP32 to read up to 4 slaves, I think the code can be compiled with Particle photon just require to edit the IO accordingly.
here is full example --> ModbusMaster Multiple slaves
here is the library --> SensorModbusMaster

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Actually no, not ModBus.
However, as said earlier

So NodeMCU is not our focus but your question is very specific to that board and has nothing to do with Particle whatsoever.

Hi @priya I am also trying to interface energy meter from selec model No. EM2M but I am unable to read the data from it I am getting error as follow

17:40:13.381 -> Reading register: 30021 regSize: 1 sizeof(data): 2 XMITDELAY: 3000
17:40:13.449 -> 
17:40:18.405 -> LT: Response timed out. Trying again. 
17:40:23.422 -> LT: Failed. Response timed out. Adjust the XMITDELAY and/or ku8MBResponseTimeout? 
17:40:28.463 -> LT: Failed. Response timed out. Adjust the XMITDELAY and/or ku8MBResponseTimeout? 

can you please help me out I am following the code that you have pasted I am using an Arduino Mega board

@priya Yes the code is working and even though you deleted your post I was still able to access them and if you are sharing knowledge its good.