예제 #1
0
def easy_weight(weight_sensor):
    pin_dt = 5
    pin_sck = 6
    channel = 'A'
    reference_unit = 1
    offset = 0

    try:
        pin_dt = int(weight_sensor["pin_dt"])
        pin_sck = int(weight_sensor["pin_sck"])
        channel = weight_sensor["channel"]
        reference_unit = float(weight_sensor["reference_unit"])
        offset = int(weight_sensor["offset"])
    except Exception as e:
        print("HX711 missing param: " + str(e))

    try:
        GPIO.setmode(GPIO.BCM)  # set GPIO pin mode to BCM numbering
        # Create an object hx which represents your real hx711 chip
        hx = HX711(dout_pin=pin_dt, pd_sck_pin=pin_sck, select_channel=channel)
        hx.set_scale_ratio(scale_ratio=reference_unit)
        hx.set_offset(offset=offset)
        # use outliers_filter and do average over 30 measurements
        weight = hx.get_weight_mean(30)
        if weight != False:
            return round(weight, 1)

    except Exception as e:
        print("Fallback HX711 failed: " + str(e))

    finally:
        pass

    return None
예제 #2
0
def init_hx711(weight_sensor):
    # HX711 GPIO
    pin_dt = 5
    pin_sck = 6
    channel = 'A'
    errorEncountered = False
    try:
        try:
            debug = weight_sensor["debug"]
        except:
            debug = False

        try:
            pin_dt = int(weight_sensor["pin_dt"])
            pin_sck = int(weight_sensor["pin_sck"])
            channel = weight_sensor["channel"]
        except Exception as e:
            logger.error("init_hx711 missing param: " + str(e))

        logger.debug('Init HX711 DT: ' + str(pin_dt) + ' SCK: ' +
                     str(pin_sck) + ' Channel: ' + channel + ' started')
        loops = 0
        while not errorEncountered and loops < 3:
            loops += 1
            try:
                GPIO.setmode(GPIO.BCM)  # set GPIO pin mode to BCM numbering
                # Create an object hx which represents your real hx711 chip
                hx = HX711(dout_pin=pin_dt,
                           pd_sck_pin=pin_sck,
                           select_channel=channel)
                hx.set_debug_mode(flag=debug)
                errorEncountered = hx.reset(
                )  # Before we start, reset the hx711 (not necessary)
                if not errorEncountered:
                    logger.debug('Init HX711 DT: ' + str(pin_dt) + ' SCK: ' +
                                 str(pin_sck) + ' Channel: ' + channel +
                                 ' finished')
                    return hx
            except Exception as e:
                if str(e) == "no median for empty data":
                    logger.debug(
                        'HX711 DT: ' + str(pin_dt) + ' SCK: ' + str(pin_sck) +
                        ' Channel: ' + channel +
                        ' Could not read enough data from HX711 => Try again: '
                        + str(loops) + '/3')
                else:
                    logger.warning('HX711 DT: ' + str(pin_dt) + ' SCK: ' +
                                   str(pin_sck) + ' Channel: ' + channel +
                                   ' Initializing HX711 failed: ' + str(e))
            time.sleep(1)

        logger.error('Returning empty HX711 for DT: ' + str(pin_dt) +
                     ' SCK: ' + str(pin_sck) + ' Channel: ' + channel)
        return None
    except Exception as e:
        logger.exception("Unhandled Exception in init_hx711")
예제 #3
0
def init_hx711(weight_sensor, debug=False):
    # HX711 GPIO
    pin_dt = 5
    pin_sck = 6
    channel = 'A'
    errorEncountered = False
    try:
        pin_dt = int(weight_sensor["pin_dt"])
        pin_sck = int(weight_sensor["pin_sck"])
        channel = weight_sensor["channel"]
    except Exception as e:
        print("HX711 missing param: " + str(e))

    loops = 0
    while not errorEncountered and loops < 3:
        loops += 1
        try:
            GPIO.setmode(GPIO.BCM)  # set GPIO pin mode to BCM numbering
            # Create an object hx which represents your real hx711 chip
            hx = HX711(dout_pin=pin_dt,
                       pd_sck_pin=pin_sck,
                       select_channel=channel)
            if debug:
                hx.set_debug_mode(flag=debug)
            errorEncountered = hx.reset(
            )  # Before we start, reset the hx711 (not necessary)
            if not errorEncountered:
                return hx
        except Exception as e:
            if str(e) == "no median for empty data":
                print("Could not read any data from HX711 => Try again: " +
                      str(loops) + "/3")
            else:
                print("Initializing HX711 failed: " + str(e))
        time.sleep(1)

    print("Returning empty HX711")
    return None