I (obviously) second @RWB’s suggestion to use char arrays instead of String
.
Just one note about the readability of very long snprintf()
lines.
snprintf(publishStateString, sizeof(publishStateString), "%.2f:%.2f:%.2f:%u:%.2f:%.2f:%u:%u:%.2f:%u:%.2f:%.2f:%.2f:%.2f:%u:%u:%.2f:%.2f:%.2f:%.2f:%.2f:%.2f:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:%u:",bms.GetVoltage(),bms.GetCurrent(),bms.GetCurrent() * bms.GetVoltage(),bms.RelativeSOC(),(bms.AverageTimeTillEmpty()/60),(bms.RemainingBatteryCapacity() - bms.FullBatteryCapacity()) / (bms.GetCurrent() *1000),bms.RemainingBatteryCapacity(),bms.FullBatteryCapacity(),(bms.GetTemp()* 9 / 5 + 32),bms.CycleCount(),bms.CellVoltage1(),bms.CellVoltage2(),bms.CellVoltage3(),bms.CellVoltage4(),bms.StateOfHealth(),bms.MaxError(),mppt.GetSolarInputVoltage(),mppt.GetSolarInputCurrent(),mppt.GetLTC4015BatVReading(),mppt.GetLTC4015BatIReading(),mppt.GetLTC4015BatIReading() * mppt.GetLTC4015BatVReading(),(((mppt.GetLTC4015DieTemp() -12010)/45.6) *9.0 / 5.0 + 32.0),0,0,0,0,0,0,0,0,ChgFet,DsgFet,ShutdownLoVoltage,DischargingDisabled,ChargingDisabled,SleepMode,CellBalancing,0,ChargeInhibit,ChargeSuspend,ChargeTermination,0,0,0,0,0,CellUnderVoltage,CellOverVoltage,0,0,0,OverloadDuringDischargeLatch,0,ShortCircuitDuringDischargeLatch,OvertemperatureDuringCharge,OvertemperatureDuringDischarge,UndertemperatureDuringCharge,UndertemperatureDuringDischarge,CxChargeTerm,NtcPause,ChargerSuspended,AbsorbCharge,ConstantVoltage,ConstantCurrent,LinLimitActive,VinUvClActiveMPPT,VinHiEnoughToCharge,VinOverVoltageLockout,ThermalShutdown,OKToCharge,ChargerEnabled );
I think we can agree that this is rather hard to follow which field is going where
So you could write the same thing like this
snprintf(publishStateString, sizeof(publishStateString),
"%.2f:%.2f:%.2f:"
"%u:%.2f:%.2f:"
"%u:%u:%.2f:"
"%u:%.2f:%.2f:"
"%.2f:%.2f:%u:"
"%u:%.2f:%.2f:"
"%.2f:%.2f:%.2f:"
"%.2f:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:%u:"
"%u:%u:"
, bms.GetVoltage(), bms.GetCurrent(), bms.GetCurrent() * bms.GetVoltage()
, bms.RelativeSOC(), (bms.AverageTimeTillEmpty()/60),(bms.RemainingBatteryCapacity() - bms.FullBatteryCapacity()) / (bms.GetCurrent() *1000)
, bms.RemainingBatteryCapacity(), bms.FullBatteryCapacity(), (bms.GetTemp()* 9 / 5 + 32)
, bms.CycleCount(), bms.CellVoltage1(), bms.CellVoltage2()
, bms.CellVoltage3(), bms.CellVoltage4(), bms.StateOfHealth()
, bms.MaxError(), mppt.GetSolarInputVoltage(), mppt.GetSolarInputCurrent()
, mppt.GetLTC4015BatVReading(), mppt.GetLTC4015BatIReading(), mppt.GetLTC4015BatIReading() * mppt.GetLTC4015BatVReading()
, (((mppt.GetLTC4015DieTemp() -12010)/45.6) *9.0 / 5.0 + 32.0), 0, 0
, 0, 0, 0
, 0, 0, 0
, ChgFet, DsgFet, ShutdownLoVoltage
, DischargingDisabled, ChargingDisabled, SleepMode
, CellBalancing, 0, ChargeInhibit
, ChargeSuspend, ChargeTermination, 0
, 0, 0, 0
, 0, CellUnderVoltage, CellOverVoltage
, 0, 0, 0
, OverloadDuringDischargeLatch, 0, ShortCircuitDuringDischargeLatch
, OvertemperatureDuringCharge, OvertemperatureDuringDischarge, UndertemperatureDuringCharge
, UndertemperatureDuringDischarge, CxChargeTerm, NtcPause
, ChargerSuspended, AbsorbCharge, ConstantVoltage
, ConstantCurrent, LinLimitActive, VinUvClActiveMPPT
, VinHiEnoughToCharge, VinOverVoltageLockout, ThermalShutdown
, OKToCharge, ChargerEnabled);
Still a massive statement, but a bit easier to conceptualise IMO.