Today is...
Monday, August 19, 2019
Welcome to the Modbus Community, about
the world's leading automation protocol.
How to take data from smart energy meter to Arduino Nano using RS485?
Interfacing Arduino nano with Ziegler smart energy meter using RS 485 Modbus

Hi,

I have been working on a project. I have to take readings of Ziegler energy meter (http://www.zieglerinstruments.com/Product/Detail/192/Power-Energy-meter) on an Arduino nano using RS485 Modbus.

I have made this code:


#include<ModbusMaster.h>
#include<SoftwareSerial.h>

SoftwareSerial mySerial(2,1);

uint16_t m_startAddress[3] = {0x30001 , 0x30007 , 0x30013};
uint16_t m_length = 0x0001;
uint8_t result;
uint8_t ret;
int sta =0;

union
{
uint16_t u[2];
float f;
}meterdata;

ModbusMaster node;

void enableTx()
{
delay(2);
digitalWrite(7,HIGH);
}

void disableTx()
{
delay(2);
digitalWrite(7,LOW);
}

void setup()
{
pinMode(7,OUTPUT);
digitalWrite(7,LOW);

node.preTransmission(enableTx);
node.postTransmission(disableTx);
Serial.begin(9600);
node.begin(1,mySerial);
}

void loop()
{
result = node.readInputRegisters(m_startAddress[sta],m_length);
if(result == node.ku8MBSuccess)
{
if(sta==0)
{
meterdata.u[1] = node.getResponseBuffer(0);
meterdata.u[0] = node.getResponseBuffer(1);
Serial.print("Voltage: ");
Serial.print(meterdata.f);
Serial.print("V");
}
else if(sta==1)
{
meterdata.u[1] = node.getResponseBuffer(0);
meterdata.u[0] = node.getResponseBuffer(1);
Serial.print("Current: ");
Serial.print(meterdata.f);
Serial.print("A");
}
else if(sta==2)
{
meterdata.u[1] = node.getResponseBuffer(0);
meterdata.u[0] = node.getResponseBuffer(1);
Serial.print("Power: ");
Serial.print(meterdata.f);
Serial.print("W");
}
}
else{
Serial.print("Error");
Serial.println(result,HEX);
}
sta ++;
if(sta==3) sta=0;
delay(1000);
}


Can anyone tell me about the errors in this code?

You can go through this manual (http://www.zieglerinstruments.com/Upload/Documents/01%20Ziegler/Product%20Code/MFM/Ziegler-Delat-Energy%20Manual-E1R0.pdf)

Thanks for the help or advice in advance.

By José Luiz on 10 July, 2019 - 6:21 am

Good Morning!

I have no experience with Modbus. But with the above mentioned manual, I saw on page 35, table with end hex of the registers for Modbus. Example: the end 30001, for Modbus 0x0000; end 30007> 0x0006; and 30013, 0x000C.
Is this the problem?

Moderator's Note: This reply was originally in Portuguese. I used Google Translate.

Bom dia!
No tenho experiencia com modbus, mas consutando o manual citado, vi na pagina 35, tabela com end hex dos registradores para modbus. Exemplo: o end 30001, para modbus 0x0000; end 30007 > 0x0006; e 30013, 0x000C.
Ser¡ esse o problema ????

Hi,

I think we cant assign pin 1 to software serial library, bcoz its already in used for uart. you might forget to softwareserial.begin() in setup() function.