Add MAX17048 sensor component and update .gitignore for new files
This commit is contained in:
14
.gitignore
vendored
14
.gitignore
vendored
@@ -15,6 +15,15 @@
|
|||||||
# Whitelist subdirectory yaml files
|
# Whitelist subdirectory yaml files
|
||||||
!*/*.yaml
|
!*/*.yaml
|
||||||
!*/*.yml
|
!*/*.yml
|
||||||
|
!*/*.md
|
||||||
|
!*/*.sh
|
||||||
|
!*/*.js*
|
||||||
|
!*/*.txt*
|
||||||
|
!*/*.json*
|
||||||
|
!*/*.ui.yaml
|
||||||
|
!*/*.h
|
||||||
|
!*/*.py
|
||||||
|
|
||||||
|
|
||||||
# Whitelist specific folders (uncomment as needed)
|
# Whitelist specific folders (uncomment as needed)
|
||||||
!automations/
|
!automations/
|
||||||
@@ -24,7 +33,10 @@
|
|||||||
!esphome/ha-remote/
|
!esphome/ha-remote/
|
||||||
!esphome/ha-remote/components/
|
!esphome/ha-remote/components/
|
||||||
!esphome/ha-remote/components/**
|
!esphome/ha-remote/components/**
|
||||||
!custom_components/
|
!esphome/ha-remote/components/max17043/
|
||||||
|
!esphome/components/**
|
||||||
|
!esphome/components/max17043/**
|
||||||
|
!custom_components/**
|
||||||
# !packages/
|
# !packages/
|
||||||
# !themes/
|
# !themes/
|
||||||
# !node-red/
|
# !node-red/
|
||||||
|
|||||||
47
esphome/MAX17048_component.h
Normal file
47
esphome/MAX17048_component.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "esphome.h"
|
||||||
|
|
||||||
|
#define MAX17048_ADDRESS 0x36
|
||||||
|
#define MAX17048_VCELL 0x02 // voltage
|
||||||
|
#define MAX17048_SOC 0x04 // percentage
|
||||||
|
#define MAX17048_MODE 0x06
|
||||||
|
#define MAX17048_VERSION 0x08
|
||||||
|
#define MAX17048_CONFIG 0x0c
|
||||||
|
#define MAX17048_COMMAND 0xfe
|
||||||
|
|
||||||
|
class MAX17048Sensor : public PollingComponent, public Sensor {
|
||||||
|
public:
|
||||||
|
Sensor *voltage_sensor = new Sensor();
|
||||||
|
Sensor *percentage_sensor = new Sensor();
|
||||||
|
|
||||||
|
MAX17048Sensor() : PollingComponent(10000) {}
|
||||||
|
|
||||||
|
void setup() override {
|
||||||
|
// Initialize the device here. Usually Wire.begin() will be called in here,
|
||||||
|
// though that call is unnecessary if you have an 'i2c:' entry in your config
|
||||||
|
ESP_LOGD("custom", "Starting up MAX17048 sensor");
|
||||||
|
|
||||||
|
Wire.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t read16(uint8_t reg) {
|
||||||
|
uint16_t temp;
|
||||||
|
Wire.begin();
|
||||||
|
Wire.beginTransmission(MAX17048_ADDRESS);
|
||||||
|
Wire.write(reg);
|
||||||
|
Wire.endTransmission();
|
||||||
|
Wire.requestFrom(MAX17048_ADDRESS, 2);
|
||||||
|
temp = (uint16_t)Wire.read() << 8;
|
||||||
|
temp |= (uint16_t)Wire.read();
|
||||||
|
Wire.endTransmission();
|
||||||
|
return temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
void update() override {
|
||||||
|
float voltage = (float)(read16(MAX17048_VCELL)) * 78.125 / 1000000;
|
||||||
|
voltage_sensor->publish_state(voltage);
|
||||||
|
|
||||||
|
uint16_t percentage_tmp = read16(MAX17048_SOC);
|
||||||
|
float percentage = (float)(percentage_tmp) / 256;
|
||||||
|
percentage_sensor->publish_state(percentage);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user