Skip to content

read() returns values differently from raw calculation #4

@utdrmac

Description

@utdrmac

In my code, I'm taking read(10) along with read_raw() and storing to InfluxDB. Every now-and-then, the read() calculation is extremely far off from a self-calculated value using raw().

My scale is under constant load; under a beehive. The offset value for init was calculated with nothing on the scale. The scalar was determined with a 5lb dumbbell, thus my unit is Lbs.

My code:

    hx = HX711_GPIO(gpio_data, gpio_clk, tare=False, offset=-155118, scalar=8949.0)

    # loop forever
    while True:
        # Take 10 ADC readings
        reading = hx.read(10)
        reading_raw = hx.read_raw()

        reading = round(reading, 2)

        # InfluxDB Line Format
        weight_line = f"weight value={reading},raw={reading_raw} {tsn}\n"
        mqtt.publish(weight_line)

        time.sleep(5)

Resulting grafana graph:
Screen Shot 2023-05-03 at 1 26 45 PM

For completeness, the raw dataline above is calculated within Grafana when extracting the data from InfluxDB:

from(bucket: "beehive")
  |> range(start: v.timeRangeStart, stop: v.timeRangeStop)
  |> filter(fn: (r) => r["_measurement"] == "weight")
  |> filter(fn: (r) => r["_field"] == "raw")
  |> map(fn: (r) => ({r with _value: (r._value + 155118.00) / 8949.00}))
  |> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
  |> yield(name: "mean")

You can see the map() line where I perform the same calculation as here. This calculation should return the same value as read(), but as you can see on this zoomed in graph, the value from read(10) is much higher than my raw-manual calculation (63.11 vs 55.18).

Screen Shot 2023-05-03 at 1 30 06 PM

I can see the datapoint in Influx as 63.11, so I know this is not some artifact of Grafana extrapolation.

Screen Shot 2023-05-03 at 1 36 23 PM

Any thoughts on why this happens sometimes with read()?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions