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