mirror of
https://github.com/dalathegreat/Battery-Emulator.git
synced 2025-10-04 02:09:30 +02:00
publish common info autoconfig
This commit is contained in:
parent
1a32022949
commit
91c7a001cc
1 changed files with 83 additions and 22 deletions
|
@ -26,22 +26,6 @@ static void publish_values(void) {
|
||||||
publish_cell_voltages();
|
publish_cell_voltages();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void publish_common_info(void) {
|
|
||||||
snprintf(mqtt_msg, sizeof(mqtt_msg),
|
|
||||||
"{\n"
|
|
||||||
" \"SOC\": %.3f,\n"
|
|
||||||
" \"StateOfHealth\": %.3f,\n"
|
|
||||||
" \"temperature_min\": %.3f,\n"
|
|
||||||
" \"temperature_max\": %.3f,\n"
|
|
||||||
" \"cell_max_voltage\": %d,\n"
|
|
||||||
" \"cell_min_voltage\": %d\n"
|
|
||||||
"}\n",
|
|
||||||
((float)SOC) / 100.0, ((float)StateOfHealth) / 100.0, ((float)((int16_t)temperature_min)) / 10.0,
|
|
||||||
((float)((int16_t)temperature_max)) / 10.0, cell_max_voltage, cell_min_voltage);
|
|
||||||
bool result = client.publish("battery/info", mqtt_msg, true);
|
|
||||||
//Serial.println(mqtt_msg); // Uncomment to print the payload on serial
|
|
||||||
}
|
|
||||||
|
|
||||||
static void publish_cell_voltages(void) {
|
static void publish_cell_voltages(void) {
|
||||||
static bool mqtt_first_transmission = true;
|
static bool mqtt_first_transmission = true;
|
||||||
|
|
||||||
|
@ -76,12 +60,12 @@ static void publish_cell_voltages(void) {
|
||||||
"\"object_id\": \"sensor_battery_voltage_cell%d\","
|
"\"object_id\": \"sensor_battery_voltage_cell%d\","
|
||||||
"\"origin\": {"
|
"\"origin\": {"
|
||||||
"\"name\": \"BatteryEmulator\","
|
"\"name\": \"BatteryEmulator\","
|
||||||
"\"sw\": \"4.4.0-mqtt\","
|
"\"sw\": \"5.1.0-mqtt\","
|
||||||
"\"url\": \"https://github.com/dalathegreat/Battery-Emulator\""
|
"\"url\": \"https://github.com/dalathegreat/Battery-Emulator\""
|
||||||
"},"
|
"},"
|
||||||
"\"state_class\": \"measurement\","
|
"\"state_class\": \"measurement\","
|
||||||
"\"name\": \"Battery Cell Voltage %d\","
|
"\"name\": \"Battery Cell Voltage %d\","
|
||||||
"\"state_topic\": \"battery/spec_data\","
|
"\"state_topic\": \"battery-emulator/spec_data\","
|
||||||
"\"unique_id\": \"battery-emulator_battery_voltage_cell%d\","
|
"\"unique_id\": \"battery-emulator_battery_voltage_cell%d\","
|
||||||
"\"unit_of_measurement\": \"V\","
|
"\"unit_of_measurement\": \"V\","
|
||||||
"\"value_template\": \"{{ value_json.cell_voltages[%d] }}\""
|
"\"value_template\": \"{{ value_json.cell_voltages[%d] }}\""
|
||||||
|
@ -111,12 +95,90 @@ static void publish_cell_voltages(void) {
|
||||||
snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "]\n}\n");
|
snprintf(mqtt_msg + msg_length, sizeof(mqtt_msg) - msg_length, "]\n}\n");
|
||||||
|
|
||||||
// Publish and print error if not OK
|
// Publish and print error if not OK
|
||||||
if (mqtt_publish_retain("battery/spec_data") == false) {
|
if (mqtt_publish_retain("battery-emulator/spec_data") == false) {
|
||||||
Serial.println("Cell voltage MQTT msg could not be sent");
|
Serial.println("Cell voltage MQTT msg could not be sent");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct SensorConfig {
|
||||||
|
const char* topic;
|
||||||
|
const char* name;
|
||||||
|
const char* value_template;
|
||||||
|
const char* unit;
|
||||||
|
const char* device_class;
|
||||||
|
};
|
||||||
|
|
||||||
|
SensorConfig sensorConfigs[] = {
|
||||||
|
{"homeassistant/sensor/battery-emulator/SOC/config", "Battery Emulator SOC", "{{ value_json.SOC }}", "%", "battery"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/StateOfHealth/config", "Battery Emulator StateOfHealth", "{{ value_json.StateOfHealth }}", "%", "battery"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/temperature_min/config", "Battery Emulator Temperature Min", "{{ value_json.temperature_min }}", "°C", "temperature"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/temperature_max/config", "Battery Emulator Temperature Max", "{{ value_json.temperature_max }}", "°C", "temperature"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/stat_batt_power/config", "Battery Emulator Stat Batt Power", "{{ value_json.stat_batt_power }}", "W", "power"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/battery_current/config", "Battery Emulator Battery Current", "{{ value_json.battery_current }}", "A", "current"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/cell_max_voltage/config", "Battery Emulator Cell Max Voltage", "{{ value_json.cell_max_voltage }}", "V", "voltage"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/cell_min_voltage/config", "Battery Emulator Cell Min Voltage", "{{ value_json.cell_min_voltage }}", "V", "voltage"},
|
||||||
|
{"homeassistant/sensor/battery-emulator/battery_voltage/config", "Battery Emulator Battery Voltage", "{{ value_json.battery_voltage }}", "V", "voltage"},
|
||||||
|
};
|
||||||
|
|
||||||
|
static void publish_common_info(void) {
|
||||||
|
static bool mqtt_first_transmission = true;
|
||||||
|
if (mqtt_first_transmission == true) {
|
||||||
|
mqtt_first_transmission = false;
|
||||||
|
for (int i = 0; i < sizeof(sensorConfigs) / sizeof(sensorConfigs[0]); i++) {
|
||||||
|
SensorConfig& config = sensorConfigs[i];
|
||||||
|
snprintf(mqtt_msg, sizeof(mqtt_msg),
|
||||||
|
"{"
|
||||||
|
"\"name\": \"%s\","
|
||||||
|
"\"state_topic\": \"battery-emulator/info\","
|
||||||
|
"\"json_attributes_topic\": \"battery-emulator/info\","
|
||||||
|
"\"unique_id\": \"battery-emulator_%d\","
|
||||||
|
"\"device\": {"
|
||||||
|
"\"identifiers\": ["
|
||||||
|
"\"battery-emulator\""
|
||||||
|
"],"
|
||||||
|
"\"manufacturer\": \"DalaTech\","
|
||||||
|
"\"model\": \"BatteryEmulator\","
|
||||||
|
"\"name\": \"BatteryEmulator\","
|
||||||
|
"\"sw_version\": \"4.4.0-mqtt\","
|
||||||
|
"\"url\": \"https://github.com/dalathegreat/Battery-Emulator\""
|
||||||
|
"},"
|
||||||
|
"\"value_template\": \"%s\","
|
||||||
|
"\"unit_of_measurement\": \"%s\","
|
||||||
|
"\"device_class\": \"%s\","
|
||||||
|
"\"state_class\": \"measurement\""
|
||||||
|
"}",
|
||||||
|
config.name, i, config.value_template, config.unit, config.device_class);
|
||||||
|
mqtt_publish_retain(config.topic);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
snprintf(mqtt_msg, sizeof(mqtt_msg),
|
||||||
|
"{\n"
|
||||||
|
" \"SOC\": %.3f,\n"
|
||||||
|
" \"StateOfHealth\": %.3f,\n"
|
||||||
|
" \"temperature_min\": %.3f,\n"
|
||||||
|
" \"temperature_max\": %.3f,\n"
|
||||||
|
" \"stat_batt_power\": %.3f,\n"
|
||||||
|
" \"battery_current\": %.3f,\n"
|
||||||
|
" \"cell_max_voltage\": %d,\n"
|
||||||
|
" \"cell_min_voltage\": %d\n"
|
||||||
|
" \"battery_voltage\": %d\n"
|
||||||
|
"}\n",
|
||||||
|
((float)SOC) / 100.0,
|
||||||
|
((float)StateOfHealth) / 100.0,
|
||||||
|
((float)((int16_t)temperature_min)) / 10.0,
|
||||||
|
((float)((int16_t)temperature_max)) / 10.0,
|
||||||
|
((float)((int16_t)stat_batt_power)) / 10.0,
|
||||||
|
((float)((int16_t)battery_current)) / 10.0,
|
||||||
|
cell_max_voltage,
|
||||||
|
cell_min_voltage,
|
||||||
|
battery_voltage / 10.0);
|
||||||
|
bool result = client.publish("battery-emulator/info", mqtt_msg, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Serial.println(mqtt_msg); // Uncomment to print the payload on serial
|
||||||
|
}
|
||||||
|
|
||||||
/* This is called whenever a subscribed topic changes (hopefully) */
|
/* This is called whenever a subscribed topic changes (hopefully) */
|
||||||
static void callback(char* topic, byte* payload, unsigned int length) {
|
static void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
Serial.print("Message arrived [");
|
Serial.print("Message arrived [");
|
||||||
|
@ -132,9 +194,8 @@ static void callback(char* topic, byte* payload, unsigned int length) {
|
||||||
static void reconnect() {
|
static void reconnect() {
|
||||||
// attempt one reconnection
|
// attempt one reconnection
|
||||||
Serial.print("Attempting MQTT connection... ");
|
Serial.print("Attempting MQTT connection... ");
|
||||||
// Create a random client ID
|
const char* hostname = WiFi.getHostname();
|
||||||
String clientId = "LilyGoClient-";
|
String clientId = "LilyGoClient-" + String(hostname);
|
||||||
clientId += String(random(0xffff), HEX);
|
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
|
if (client.connect(clientId.c_str(), mqtt_user, mqtt_password)) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue