#reading seesaw i2c w/ rust embedded
1 messages · Page 1 of 1 (latest)
i'm using the stemma soil sensor and trying to read the temp register as a test
here's my code running on the esp32:
fn main() -> Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
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 sda = peripherals.pins.gpio25;
let scl = peripherals.pins.gpio26;
let config = I2cConfig::new().baudrate(100.kHz().into());
let mut i2c = I2cDriver::new(peripherals.i2c0, sda, scl, &config)?;
let mut result: Vec<u8> = vec![0; 4];
let sensor_addr = 54;
let to_write: Vec<u8> = vec![0, 4];
let err = i2c.write_read(sensor_addr, to_write.as_slice(), result.as_mut_slice(), 5);
if err.is_err_and(|e| {
esp_nofail!(e.code());
true
}) {
log::info!("Got result! {:#?}", result);
}
let num_result = i32::from_be_bytes(result[0..4].try_into().unwrap());
log::info!("Got result! {}F", num_result);
let wakeup_source = peripherals.pins.gpio4;
unsafe {
esp_sleep_enable_ext0_wakeup(wakeup_source.pin(), 1);
}
std::thread::sleep(std::time::Duration::from_secs(2));
log::info!("Entering deep sleep");
unsafe { esp_deep_sleep_start() }
}
i've converted some code to Java/Kotlin, so i might be able to help -- what's the error?
Cool I will send output/explain more when im back at my computer but im not getting an error, but the data im getting back seems wrong? Its very possible im parsing the bytes wrong
Alright so i'm getting back the following bytes, ```
[
0,
25,
149,
156,
]
which converted to a signed int 32 is `1676700`
That int is in no temperature unit I know of
I looked into the CircuitPython seesaw code to see how they are calculating the temp, and they randomly multiply by some decimal? https://github.com/adafruit/Adafruit_CircuitPython_seesaw/blob/main/adafruit_seesaw/seesaw.py#L389
no idea what the significance of multiplying by 0.00001525878 is
but when I multiply my result by that, I do indeed get 25.5 (which I am assuming is celsius) and that sounds roughly correct (my room temp is like 23/24C according to external temp reader)
I guess i'll just hardcode that same number in my code but would love an explanation if someone knows!
alright well the C version at least makes it a little less random, the number is basically a 32 bit binary number with a 1 shifted 16 places in https://github.com/adafruit/Adafruit_Seesaw/blob/master/Adafruit_seesaw.cpp#L815
ah yeah its the scaling factor of the actual sensor
sensor's raw data is probably using 16 bits for the fractional part