I have created my first custom input for an HX711 Load Cell, which imported fine and is in the inputs dropdown. My problem is adding the config options like GPIO pin, etc.. Can someone look at my code and maybe point me in the right direction?
Thank you in advance.
from mycodo.inputs.base_input import Input
#from mycodo.mycodo_client.measurements import convert_units
from hx711 import HX711 # Ensure this library is installed
INPUT_INFORMATION = {
'input_name_unique': 'HX711',
'input_name': 'HX711 Load Cell',
'input_manufacturer': 'Generic',
'input_library': 'hx711',
'measurements_name': 'Weight',
'measurements_dict': {
0: {'measurement': 'weight', 'unit': 'Grams'}
},
'url_manufacturer': 'https://github.com/robert-hh/hx711'
}
OPTIONS = {
'dt_pin': {
'type': 'int',
'default': 5,
'description': 'GPIO pin for DT (data)'
},
'sck_pin': {
'type': 'int',
'default': 6,
'description': 'GPIO pin for SCK (clock)'
},
'scale': {
'type': 'float',
'default': 1.0,
'description': 'Calibration scale factor'
},
'tare_offset': {
'type': 'float',
'default': 0.0,
'description': 'Tare offset in grams'
}
}
class InputModule(Input):
def __init__(self, input_config, testing=False):
super(InputModule, self).__init__(input_config, testing)
self.dt_pin = int(self.get_setting('dt_pin', OPTIONS['dt_pin']['default']))
self.sck_pin = int(self.get_setting('sck_pin', OPTIONS['sck_pin']['default'])) # Default GPIO 6
self.scale = float(self.get_setting('scale', OPTIONS['scale']['default'])) # Calibration factor
self.tare_offset = float(self.get_setting('tare_offset', OPTIONS['tare_offset']['default'])) # Optional tare
try:
self.hx = HX711(dout=self.dt_pin, pd_sck=self.sck_pin)
self.hx.set_scale_ratio(self.scale)
self.logger.info(f"HX711 initialized on DT={self.dt_pin}, SCK={self.sck_pin}")
except Exception as e:
self.logger.error(f"Failed to initialize HX711: {e}")
self.hx = None
def get_input_options(self):
return OPTIONS
def get_measurement(self):
if not self.hx:
self.logger.error("HX711 not initialized")
return None
try:
raw_weight = self.hx.get_weight_mean(5) # Average of 5 readings
weight = raw_weight - self.tare_offset
self.logger.debug(f"Raw: {raw_weight}, Tared: {weight}")
return {0: weight}
except Exception as e:
self.logger.error(f"Error reading HX711: {e}")
return None