Read energy meter with Z-uno shield via modbus

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
kivi
Posts: 8
Joined: 05 Sep 2020 12:26

Read energy meter with Z-uno shield via modbus

Post by kivi »

Hi all,
I need to read data from an energy meter via modbus with Z-Uno shield. I can't find any useful examples for a sketch, I tried made a one, but it isn' working, maybe no answer from meter. Wired to shield's 7-8 pin, jumpers set to RS485. My sketch is below (reads voltage only), is anyone a good idea or a solution? Thanks.

Code: Select all

// Global variables

int Buffer[9];
byte x;
int VoltReadCommand[] ={0x08, 0x04, 0x00, 0x00, 0x00, 0x02, 0x71, 0x52}; // energy meter is no. 008 on modbus, voltage is in 00 00 register

ZUNO_SETUP_CHANNELS(
  ZUNO_METER(ZUNO_METER_TYPE_ELECTRIC, METER_RESET_DISABLE, ZUNO_METER_ELECTRIC_SCALE_VOLTS, METER_SIZE_FOUR_BYTES, METER_PRECISION_TWO_DECIMALS, getterVOLTS, NULL)
);


void setup() {
  Serial1.begin(9600);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
}

void loop() {
 delay(20);
 
 // Set pin to HIGH > Transmit mode
 digitalWrite(2, HIGH); 
 delay(5);
 // Send command to ModBus Meter
for (int i=0; i < 8; i++) Serial1.write(VoltReadCommand[i]);
 delay(5);

// Set pin to LOW > Receive mode
 digitalWrite(2, LOW);
 delay(5);

// Read reply in Buffer[];
for (int i=0; i < 9; i++) Buffer[i] = Serial1.read();  

// Convert to Float

 ((byte*)&x)[3]= Buffer[3];
 ((byte*)&x)[2]= Buffer[4];
 ((byte*)&x)[1]= Buffer[5];
 ((byte*)&x)[0]= Buffer[6];

 delay(1500);
}

byte getterVOLTS() {
 return (x);
}
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Read energy meter with Z-uno shield via modbus

Post by PoltoS »

Your approach is correct I general except that you allocate one byte for x and write 3 bytes behind it. This results in corruption.

Also getter should be dword (and x too) since you define four bytes meter value
kivi
Posts: 8
Joined: 05 Sep 2020 12:26

Re: Read energy meter with Z-uno shield via modbus

Post by kivi »

Thanks, but should be an other bug too, it is reporting now -1.00V.
User avatar
PoltoS
Posts: 7565
Joined: 26 Jan 2011 19:36

Re: Read energy meter with Z-uno shield via modbus

Post by PoltoS »

Put a static number in getter and check the getter is defined correctly.

Then revert back to x and make sure x is correct after reading
kivi
Posts: 8
Joined: 05 Sep 2020 12:26

Re: Read energy meter with Z-uno shield via modbus

Post by kivi »

Hmm, If i put: return (55.33); in the controller says 0.55 V
or if x = x*100;
return (x);
result is -1.00V
Interesting.
kivi
Posts: 8
Joined: 05 Sep 2020 12:26

Re: Read energy meter with Z-uno shield via modbus

Post by kivi »

The bug is in the modbus communication, Serial1.available() > 0 is false when it wants to write or read.
zwaFisher
Posts: 25
Joined: 12 Jul 2020 13:53

Re: Read energy meter with Z-uno shield via modbus

Post by zwaFisher »

I would appreciate if you post your fina sketch.
Cheets
kivi
Posts: 8
Joined: 05 Sep 2020 12:26

Re: Read energy meter with Z-uno shield via modbus

Post by kivi »

Hi, not working yet. I am waiting for arrive a RS485 module for Arduino, that I can test without z-uno.
zwaFisher
Posts: 25
Joined: 12 Jul 2020 13:53

Re: Read energy meter with Z-uno shield via modbus

Post by zwaFisher »

Has it arrived?
kivi
Posts: 8
Joined: 05 Sep 2020 12:26

Re: Read energy meter with Z-uno shield via modbus

Post by kivi »

zwaFisher wrote:
16 Jan 2021 15:54
Has it arrived?
Yes, but unfortunately I have no time to play with it... :?
Post Reply