Hi Guys,
I picked up one of these mini scales recently, Ive been trying to add it to one of my ESP32 devices but I’m really struggling to understand how to achieve this.
The device communicates over the i2c bus, my code is compiling and it identifies the scale on the address it is expected to be on.
My question is how do I then turn this into a sensor.
I did find an article on the ESPHome website suggesting that the hx711 device can be used as a sensor however there is no reference how I can make it communicate with the i2c bus.
esphome:
name: bedroom-home-status-light
friendly_name: Bedroom Home Status Light
esp32:
board: m5stack-atom
framework:
type: arduino
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "*="
ota:
- platform: esphome
password: "*"
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Bedroom-Home-Status-Light"
password: "*"
light:
- platform: fastled_clockless # 5x5 RGB Matrix (WS2812C)
name: "Light"
pin: 27
chipset: WS2812
num_leds: 25
rgb_order: GRB
effects:
- pulse:
name: "Fast Pulse"
transition_length: 0.5s
update_interval: 0.5s
min_brightness: 0%
max_brightness: 100%
i2c:
- id: bus_a
sda: 25
scl: 21
scan: true
I did attempt the following but this failed to compile:
sensor:
- platform: hx711
name: "HX711 Value"
update_interval: 60s
i2c_id: bus_a
address: 0x26
Any ideas?
doezel
December 4, 2024, 10:35am
2
Hi!
were you able to solve this? I have an hx711 connected to a m5Stack poesp32. The scale gets detected on address 0x26 but I can’t read it.
Karosm
(Karosm)
December 4, 2024, 1:12pm
3
If you are using those mini scales, even if hx711 is one component of that scale, connection is done by STM32 microcontroller through I2C and it has nothing to do with hx711 serial protocol.
doezel
December 4, 2024, 1:27pm
4
Thanks, you’re right.
So I want to use the i2c read_register functionality to get the weight from the scale. Any ideas on how to make this work?
i2c:
- id: bus_a
sda: GPIO16
scl: GPIO17
scan: true
i2c_device:
id: i2cdev
address: 0x26
sensor:
- platform: custom
lambda: |-
uint8_t* weight=0;
if (id(i2cdev).read_register(0x60,weight,4,false)) {
return {weight};
}
sensors:
- name: weight
id: weight
Because now I get this error when compiling:
Compiling .pioenvs/poesp32/src/main.cpp.o
/config/esphome/poesp32.yaml: In lambda function:
/config/esphome/poesp32.yaml:43:21: error: could not convert '{weight}' from '<brace-enclosed initializer list>' to 'std::vector<esphome::sensor::Sensor*>'
return {weight};
^
*** [.pioenvs/poesp32/src/main.cpp.o] Error 1
Karosm
(Karosm)
December 4, 2024, 1:39pm
5
No, but you can study this:
1 Like
doezel
December 4, 2024, 4:12pm
6
Well, I figured it out! (And added the Tare Button)
This reads the weight in grams every second.
i2c:
- id: bus_a
sda: GPIO16
scl: GPIO17
scan: true
i2c_device:
id: i2cdev
address: 0x26
sensor:
- platform: template
name: "Weight Sensor"
update_interval: 1s
unit_of_measurement: "g"
device_class: "weight"
state_class: "measurement"
lambda: |-
uint8_t data[4];
float c;
uint8_t *p;
id(i2cdev).read_bytes(0x10,data,4);
p = (uint8_t *)&c;
memcpy(p, data, 4);
return c;
button:
- platform: template
id: SetOffset
name: "Tare"
on_press:
then:
- lambda: |-
id(i2cdev).write_byte(0x50, 1);
I got the code from the M5Stack sample here , from the getWeight function:
uint8_t color[4] = {0};
uint32_t colorHEX = 0;
if (readBytes(_addr, UNIT_SCALES_RGB_LED_REG, color, 3)) {
colorHEX = color[0];
colorHEX = (colorHEX << 8) | color[1];
colorHEX = (colorHEX << 8) | color[2];
};
return colorHEX;
}
float UNIT_SCALES::getWeight() {
uint8_t data[4];
float c;
uint8_t *p;
if (readBytes(_addr, UNIT_SCALES_CAL_DATA_REG, data, 4)) {
p = (uint8_t *)&c;
memcpy(p, data, 4);
};
return c;
}
2 Likes