Class: Pzem016::InputRegisters

Inherits:
ModbusRegisters show all
Defined in:
lib/pzem016/pzem016.rb

Overview

Parsing logic for the “input registers”, also known as readonly. This is where the sample data shows up

Instance Attribute Summary

Attributes inherited from ModbusRegisters

#raw, #slave

Instance Method Summary collapse

Methods inherited from ModbusRegisters

#initialize, #report, #to_json

Constructor Details

This class inherits a constructor from Pzem016::ModbusRegisters

Instance Method Details

#alarm?Boolean

Returns true of power consumption has reached the alarm number of watts

Returns:

  • (Boolean)


163
164
165
# File 'lib/pzem016/pzem016.rb', line 163

def alarm?
  @raw[9] == 0xFFFF
end

#ampsObject

Extracts measured amps from the sample data



128
129
130
# File 'lib/pzem016/pzem016.rb', line 128

def amps
  (((@raw[2] << 16) | @raw[1]) * 0.001).round(3)
end

#hzObject

Extracts measured frequency from the sample data



149
150
151
# File 'lib/pzem016/pzem016.rb', line 149

def hz
  (@raw[7] * 0.1).round(3)
end

#powerFactorObject

Extracts measured power factor from the sample data



156
157
158
# File 'lib/pzem016/pzem016.rb', line 156

def powerFactor
  (@raw[8] * 0.01).round(3)
end

#readObject

Specific input register read logic for PZEM-016



114
115
116
# File 'lib/pzem016/pzem016.rb', line 114

def read
  @raw = @slave.input_registers[0..9]
end

#to_hashObject

Specific logic for generating a hash of the input registers in a PZEM-016



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/pzem016/pzem016.rb', line 170

def to_hash
  {
    "SlaveAddr": @slave.uid,
    "DeviceName": @name,
    "Volts": volts,
    "Amps": amps,
    "Watts": watts,
    "WH": wattHours,
    "HZ": hz,
    "PF": powerFactor,
    "Alarm": alarm?
  }
end

#voltsObject

Extracts measured volts from the sample data



121
122
123
# File 'lib/pzem016/pzem016.rb', line 121

def volts
  (@raw[0] * 0.1).round(3)
end

#wattHoursObject

Extracts cumulative watt-hours from the sample data



142
143
144
# File 'lib/pzem016/pzem016.rb', line 142

def wattHours
  (((@raw[6] << 16) | @raw[5]) * 0.1).round(3)
end

#wattsObject

Extracts measured watts from the sample data



135
136
137
# File 'lib/pzem016/pzem016.rb', line 135

def watts
  (((@raw[4] << 16) | @raw[3]) * 0.1).round(3)
end