Thanks for the great lib. First of all, I am pretty new to Rust and also to the HX711, so this might be a super noob question.
I have a problem that readings are getting lower and lower with every iteration.
Currently I have this setup to send data from read from the HX711 to an MQTT broker: https://github.com/foxriver76/rust-scale/blob/main/src/main.rs
However, also with a minimal setup like the following this happens:
mod critical_section;
use esp_idf_svc::{
wifi::BlockingWifi,
wifi::EspWifi,
nvs::EspDefaultNvsPartition,
eventloop::EspSystemEventLoop,
};
use esp_idf_svc::hal::{
delay::{Delay},
gpio::PinDriver,
peripherals::Peripherals,
};
use embedded_svc::wifi::{ClientConfiguration, Configuration};
use esp_idf_svc::mqtt::client::*;
use core::time::Duration;
use loadcell::{hx711::HX711, LoadCell};
/// This configuration is picked up at compile time by `build.rs` from the
/// file `cfg.toml`.
#[toml_cfg::toml_config]
pub struct Config {
#[default("")]
wifi_ssid: &'static str,
#[default("")]
wifi_psk: &'static str,
#[default("localhost")]
mqtt_host: &'static str,
#[default("")]
mqtt_user: &'static str,
#[default("")]
mqtt_pass: &'static str,
#[default("")]
mqtt_client_id: &'static str,
}
fn main() {
esp_idf_svc::sys::link_patches();
// Bind the log crate to the ESP Logging facilities
esp_idf_svc::log::EspLogger::initialize_default();
let peripherals = Peripherals::take().unwrap();
let dt = PinDriver::input(peripherals.pins.gpio16).unwrap();
let sck = PinDriver::output(peripherals.pins.gpio4).unwrap();
let delay = Delay::new_default();
let mut load_sensor = HX711::new(sck, dt, delay);
log::info!("Start tare");
load_sensor.tare(16);
log::info!("Finished tare");
// TODO: scale
// factor = (real_weight - tare) / raw_measurement
load_sensor.set_scale(-1.0);
log::info!("Offset: {:.0} g", load_sensor.get_offset());
let mut vec = Vec::new();
loop {
if load_sensor.is_ready() {
let reading = load_sensor.read_scaled().unwrap();
vec.push(reading);
//let reading = load_sensor.read().unwrap(); // Use this to calibrate the load cell
log::info!("Weight: {:.0} g", reading);
//log::info!("Weight scaled : {:.0} g", reading_scaled);
// log::info!("Weight: {} g", reading); // Use this to get all the decimals
if vec.len() == 20 {
vec.sort_by(|a, b| a.partial_cmp(b).unwrap());
let median = vec.get(10).unwrap();
let payload = format!("{}", median);
// mqtt_client.enqueue(topic, QoS::AtMostOnce, false, payload.as_bytes()).unwrap();
//log::info!("Published \"{payload}\" to topic \"{topic}\"");
vec.clear();
}
// let sleep_secs = 2;
// log::info!("Now sleeping for {sleep_secs}s...");
//std::thread::sleep(Duration::from_secs(sleep_secs));
}
delay.delay_ms(5u32);
}
}
Output without any change on the scale:
I (408) rust_test: Start tare
I (1848) rust_test: Finished tare
I (1848) rust_test: Offset: -1680337 g
I (1938) rust_test: Weight: -97 g
I (2028) rust_test: Weight: -244 g
I (2128) rust_test: Weight: -292 g
I (2228) rust_test: Weight: -140 g
I (2328) rust_test: Weight: -162 g
I (2418) rust_test: Weight: -249 g
I (2518) rust_test: Weight: -230 g
I (2618) rust_test: Weight: -204 g
I (2718) rust_test: Weight: -201 g
I (2808) rust_test: Weight: -186 g
I (2908) rust_test: Weight: -130 g
I (3008) rust_test: Weight: -247 g
I (3108) rust_test: Weight: -185 g
I (3198) rust_test: Weight: -49 g
I (3298) rust_test: Weight: -178 g
I (3398) rust_test: Weight: -198 g
I (3498) rust_test: Weight: -101 g
I (3588) rust_test: Weight: -63 g
I (3688) rust_test: Weight: -57 g
I (3788) rust_test: Weight: 5 g
I (3888) rust_test: Weight: 87 g
I (3978) rust_test: Weight: -91 g
I (4078) rust_test: Weight: -84 g
I (4178) rust_test: Weight: 47 g
I (4278) rust_test: Weight: 47 g
I (4368) rust_test: Weight: -7 g
I (4468) rust_test: Weight: -27 g
I (4568) rust_test: Weight: -80 g
I (4668) rust_test: Weight: -152 g
I (4758) rust_test: Weight: -21 g
I (4858) rust_test: Weight: -137 g
I (4958) rust_test: Weight: -229 g
I (5058) rust_test: Weight: -151 g
I (5148) rust_test: Weight: -198 g
I (5248) rust_test: Weight: -269 g
I (5348) rust_test: Weight: -191 g
...
I (186348) rust_test: Weight: -928 g
I (186438) rust_test: Weight: -890 g
I (186538) rust_test: Weight: -884 g
I (186638) rust_test: Weight: -1002 g
I (186738) rust_test: Weight: -937 g
I (186828) rust_test: Weight: -888 g
I (186928) rust_test: Weight: -980 g
I (187028) rust_test: Weight: -976 g
I (187128) rust_test: Weight: -990 g
I (187218) rust_test: Weight: -1032 g
I (187318) rust_test: Weight: -1054 g
I (187418) rust_test: Weight: -1030 g
I (187518) rust_test: Weight: -1064 g
I (187608) rust_test: Weight: -1179 g
I (187708) rust_test: Weight: -1113 g
I (187808) rust_test: Weight: -1087 g
I (187908) rust_test: Weight: -1141 g
I (187998) rust_test: Weight: -1097 g
I (188098) rust_test: Weight: -1089 g
I (188198) rust_test: Weight: -1111 g
I (188298) rust_test: Weight: -1167 g
I (188388) rust_test: Weight: -1076 g
Any ideas why this happens? I am using two of these load cells wired like described in https://circuitjournal.com/50kg-load-cells-with-HX711
Thanks for the great lib. First of all, I am pretty new to Rust and also to the HX711, so this might be a super noob question.
I have a problem that readings are getting lower and lower with every iteration.
Currently I have this setup to send data from read from the HX711 to an MQTT broker: https://github.com/foxriver76/rust-scale/blob/main/src/main.rs
However, also with a minimal setup like the following this happens:
Output without any change on the scale:
Any ideas why this happens? I am using two of these load cells wired like described in https://circuitjournal.com/50kg-load-cells-with-HX711