Skip to content

Instantly share code, notes, and snippets.

@KaiGrassnick
Created November 24, 2022 10:12
Show Gist options
  • Save KaiGrassnick/bb6a01d87941ad01143f42a4abb971c0 to your computer and use it in GitHub Desktop.
Save KaiGrassnick/bb6a01d87941ad01143f42a4abb971c0 to your computer and use it in GitHub Desktop.
dbus-mqtt-battery Simple Test Sample MQTT Battery Faker
// npm install mqtt --save
const mqtt = require('mqtt')
const host = '10.254.254.30'
const port = '1883'
const clientId = 'e92158e947'
const connectUrl = `mqtt://${host}:${port}`
const client = mqtt.connect(connectUrl, {
clientId,
clean: true,
connectTimeout: 4000,
username: '',
password: '',
reconnectPeriod: 1000,
})
const baseTopic = 'device/' + clientId
const subscribeTopic = baseTopic + '/DBus'
const publishTopic = baseTopic + '/Status'
client.on('connect', () => {
console.log('Connected')
client.subscribe([subscribeTopic], () => {
console.log(`Subscribe to topic '${subscribeTopic}'`)
})
let battery = {
'clientId': clientId,
'connected': 1,
'version': 'v0.1',
'services': {
'b1': 'battery'
}
}
client.publish(publishTopic, JSON.stringify(battery), {qos: 0, retain: false}, (error) => {
if (error) {
console.error(error)
}
})
})
client.on('message', (topic, payload) => {
console.log('Received Message:', topic, payload.toString())
if (topic.endsWith('DBus')) {
let jsonPayload = JSON.parse(payload.toString());
let deviceInstanceId = jsonPayload.deviceInstance.b1;
let portalId = jsonPayload.portalId;
// updateValue(portalId, deviceInstanceId, 'ProductId', 65535);
updateValue(portalId, deviceInstanceId, 'CustomName', 'DIY Battery');
updateValue(portalId, deviceInstanceId, 'Dc/0/Voltage', 57.40);
updateValue(portalId, deviceInstanceId, 'Dc/0/Current', 0.00);
updateValue(portalId, deviceInstanceId, 'Dc/0/Power', 0.00);
updateValue(portalId, deviceInstanceId, 'Dc/0/Temperature', 25.12);
// updateValue(portalId, deviceInstanceId, 'Dc/0/MidVoltage', 57.20);
// updateValue(portalId, deviceInstanceId, 'Dc/0/MidVoltageDeviation', 0.2);
updateValue(portalId, deviceInstanceId, 'ConsumedAmphours', 0);
updateValue(portalId, deviceInstanceId, 'Soc', 100);
updateValue(portalId, deviceInstanceId, 'Capacity', 580.12345);
updateValue(portalId, deviceInstanceId, 'InstalledCapacity', 600);
// updateValue(portalId, deviceInstanceId, 'SystemSwitch', 0);
updateValue(portalId, deviceInstanceId, 'Balancing', 0);
updateValue(portalId, deviceInstanceId, 'Info/MaxChargeCurrent', 250.00);
updateValue(portalId, deviceInstanceId, 'Info/MaxDischargeCurrent', 250.00);
updateValue(portalId, deviceInstanceId, 'Info/MaxChargeVoltage', 57.40);
updateValue(portalId, deviceInstanceId, 'Info/BatteryLowVoltage', 42.00);
// values: 0 = OK, 1 = Warning, 2 = Alarm
updateValue(portalId, deviceInstanceId, 'Alarms/LowVoltage', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/HighVoltage', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/LowSoc', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/HighChargeCurrent', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/HighDischargeCurrent', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/HighCurrent', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/CellImbalance', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/HighChargeTemperature', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/LowChargeTemperature', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/LowCellVoltage', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/LowTemperature', 0);
updateValue(portalId, deviceInstanceId, 'Alarms/HighTemperature', 0);
updateValue(portalId, deviceInstanceId, 'System/MinVoltageCellId', 1);
updateValue(portalId, deviceInstanceId, 'System/MaxVoltageCellId', 13);
updateValue(portalId, deviceInstanceId, 'System/MinCellVoltage', 3.2121234);
updateValue(portalId, deviceInstanceId, 'System/MaxCellVoltage', 3.574);
updateValue(portalId, deviceInstanceId, 'System/MinTemperatureCellId', 1);
updateValue(portalId, deviceInstanceId, 'System/MaxTemperatureCellId', 13);
updateValue(portalId, deviceInstanceId, 'System/MinCellTemperature', 22.00);
updateValue(portalId, deviceInstanceId, 'System/MaxCellTemperature', 25.00);
updateValue(portalId, deviceInstanceId, 'System/NrOfBatteries', 2);
updateValue(portalId, deviceInstanceId, 'System/BatteriesParallel', 2);
updateValue(portalId, deviceInstanceId, 'System/BatteriesSeries', 0);
updateValue(portalId, deviceInstanceId, 'System/NrOfCellsPerBattery', 16);
updateValue(portalId, deviceInstanceId, 'System/NrOfModulesBlockingCharge', 0);
updateValue(portalId, deviceInstanceId, 'System/NrOfModulesBlockingDischarge', 0);
updateValue(portalId, deviceInstanceId, 'System/NrOfModulesOnline', 2);
updateValue(portalId, deviceInstanceId, 'System/NrOfModulesOffline', 0);
updateValue(portalId, deviceInstanceId, 'Io/AllowToCharge', 1);
updateValue(portalId, deviceInstanceId, 'Io/AllowToDischarge', 1);
// updateValue(portalId, deviceInstanceId, 'Io/ExternalRelay', 0);
updateValue(portalId, deviceInstanceId, 'History/ChargeCycles', 0);
updateValue(portalId, deviceInstanceId, 'History/TotalAhDrawn', 10);
}
})
function updateValue(portalId, deviceInstanceId, path, value) {
let updateTopic = 'W/' + portalId + '/battery/' + deviceInstanceId + '/' + path
let updateObject = {
'value': value
}
client.publish(updateTopic, JSON.stringify(updateObject), {qos: 0, retain: false}, (error) => {
if (error) {
console.error(error)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment