Monitor motorhome leisure battery and provide simple control of habitation

Dependencies:   net lpc1768 crypto clock web fram log

Committer:
andrewboyson
Date:
Fri Jun 10 18:32:21 2022 +0000
Revision:
10:a97a7cb7aa82
Parent:
9:d957af50fdc2
Corrected amp second count in html

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 9:d957af50fdc2 1 //Battery script
andrewboyson 0:b843d647695c 2 'use strict';
andrewboyson 0:b843d647695c 3
andrewboyson 9:d957af50fdc2 4 let batteryCountedCapacity = '';
andrewboyson 9:d957af50fdc2 5 let batteryCountedError = '';
andrewboyson 9:d957af50fdc2 6 let batteryCurrentMa = '';
andrewboyson 9:d957af50fdc2 7 let batteryCapacitySetpoint = '';
andrewboyson 9:d957af50fdc2 8 let batteryOutputState = '';
andrewboyson 9:d957af50fdc2 9 let batteryChargeEnabled = false;
andrewboyson 9:d957af50fdc2 10 let batteryDischargeEnabled = false;
andrewboyson 9:d957af50fdc2 11 let batteryTemperatureTenths = '';
andrewboyson 9:d957af50fdc2 12 let batteryTemperatureSetPoint = '';
andrewboyson 9:d957af50fdc2 13 let batteryHeaterPercent = '';
andrewboyson 9:d957af50fdc2 14 let batteryVoltageMv = '';
andrewboyson 9:d957af50fdc2 15 let batteryMeasuredPercent = '';
andrewboyson 9:d957af50fdc2 16 let batteryMeasuredError = '';
andrewboyson 9:d957af50fdc2 17 let batteryMeasuredAutoUpdate = false;
andrewboyson 0:b843d647695c 18
andrewboyson 0:b843d647695c 19 function parse()
andrewboyson 0:b843d647695c 20 {
andrewboyson 0:b843d647695c 21 let lines = Ajax.response.split('\n');
andrewboyson 9:d957af50fdc2 22 batteryCountedCapacity = Ajax.hexToSignedInt32(lines[ 0]);
andrewboyson 9:d957af50fdc2 23 batteryCountedError = Ajax.hexToSignedInt8 (lines[ 1]);
andrewboyson 9:d957af50fdc2 24 batteryCurrentMa = Ajax.hexToSignedInt32(lines[ 2]);
andrewboyson 9:d957af50fdc2 25 batteryCapacitySetpoint = Ajax.hexToSignedInt8 (lines[ 3]);
andrewboyson 9:d957af50fdc2 26 batteryOutputState = lines[ 4];
andrewboyson 9:d957af50fdc2 27 batteryChargeEnabled = Ajax.hexToBit (lines[ 5], 0);
andrewboyson 9:d957af50fdc2 28 batteryDischargeEnabled = Ajax.hexToBit (lines[ 5], 1);
andrewboyson 9:d957af50fdc2 29 batteryTemperatureTenths = Ajax.hexToSignedInt16(lines[ 6]);
andrewboyson 9:d957af50fdc2 30 batteryTemperatureSetPoint = Ajax.hexToSignedInt16(lines[ 7]);
andrewboyson 9:d957af50fdc2 31 batteryHeaterPercent = Ajax.hexToSignedInt8 (lines[ 8]);
andrewboyson 9:d957af50fdc2 32 batteryVoltageMv = Ajax.hexToSignedInt16(lines[ 9]);
andrewboyson 9:d957af50fdc2 33 batteryMeasuredPercent = Ajax.hexToSignedInt8 (lines[10]);
andrewboyson 9:d957af50fdc2 34 batteryMeasuredError = Ajax.hexToSignedInt8 (lines[11]);
andrewboyson 9:d957af50fdc2 35 batteryMeasuredAutoUpdate = Ajax.hexToSignedInt8 (lines[12]);
andrewboyson 0:b843d647695c 36 }
andrewboyson 0:b843d647695c 37 function display()
andrewboyson 0:b843d647695c 38 {
andrewboyson 0:b843d647695c 39 let elem;
andrewboyson 9:d957af50fdc2 40 elem = Ajax.getElementOrNull('txt-battery-counted-capacity-amp-seconds' ); if (elem) elem.textContent = batteryCountedCapacity;
andrewboyson 9:d957af50fdc2 41 elem = Ajax.getElementOrNull('val-battery-counted-error' ); if (elem) elem.value = batteryCountedError;
andrewboyson 9:d957af50fdc2 42 elem = Ajax.getElementOrNull('txt-battery-counted-capacity-amp-hours' ); if (elem) elem.textContent = Math.round(batteryCountedCapacity/3600);
andrewboyson 9:d957af50fdc2 43 elem = Ajax.getElementOrNull('txt-battery-counted-capacity-percent' ); if (elem) elem.textContent = Math.round(batteryCountedCapacity/36/280);
andrewboyson 9:d957af50fdc2 44 elem = Ajax.getElementOrNull('val-battery-counted-capacity-percent' ); if (elem) elem.value = Math.round(batteryCountedCapacity/36/280);
andrewboyson 9:d957af50fdc2 45 elem = Ajax.getElementOrNull('txt-battery-current-ma' ); if (elem) elem.textContent = batteryCurrentMa;
andrewboyson 9:d957af50fdc2 46 elem = Ajax.getElementOrNull('val-battery-capacity-setpoint-percent' ); if (elem) elem.value = batteryCapacitySetpoint;
andrewboyson 9:d957af50fdc2 47 elem = Ajax.getElementOrNull('txt-battery-output-state' ); if (elem) elem.textContent = batteryOutputState;
andrewboyson 9:d957af50fdc2 48 elem = Ajax.getElementOrNull('att-battery-charge-enabled' ); if (elem) elem.setAttribute('dir', batteryChargeEnabled ? 'rtl' : 'ltr');
andrewboyson 9:d957af50fdc2 49 elem = Ajax.getElementOrNull('att-battery-discharge-enabled' ); if (elem) elem.setAttribute('dir', batteryDischargeEnabled ? 'rtl' : 'ltr');
andrewboyson 9:d957af50fdc2 50 elem = Ajax.getElementOrNull('txt-battery-temperature-tenths' ); if (elem) elem.textContent = (batteryTemperatureTenths/10).toFixed(1);
andrewboyson 9:d957af50fdc2 51 elem = Ajax.getElementOrNull('val-battery-temperature-setpoint' ); if (elem) elem.value = (batteryTemperatureSetPoint/10).toFixed(1);
andrewboyson 9:d957af50fdc2 52 elem = Ajax.getElementOrNull('txt-battery-heater-output-percent' ); if (elem) elem.textContent = batteryHeaterPercent;
andrewboyson 9:d957af50fdc2 53 elem = Ajax.getElementOrNull('txt-battery-voltage-mv' ); if (elem) elem.textContent = batteryVoltageMv;
andrewboyson 9:d957af50fdc2 54 elem = Ajax.getElementOrNull('txt-battery-measured-percent' ); if (elem) elem.textContent = batteryMeasuredPercent;
andrewboyson 9:d957af50fdc2 55 elem = Ajax.getElementOrNull('txt-battery-measured-error' ); if (elem) elem.textContent = batteryMeasuredError;
andrewboyson 9:d957af50fdc2 56 elem = Ajax.getElementOrNull('att-battery-auto-update' ); if (elem) elem.setAttribute('dir', batteryMeasuredAutoUpdate ? 'rtl' : 'ltr');
andrewboyson 0:b843d647695c 57 }
andrewboyson 0:b843d647695c 58
andrewboyson 9:d957af50fdc2 59 Ajax.server = '/battery-ajax';
andrewboyson 0:b843d647695c 60 Ajax.onResponse = function() { parse(); display(); };
andrewboyson 0:b843d647695c 61 Ajax.init();