ESP8266 Treppenlichtsteuerung mit OTA zum Firmware Upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

input.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. let rangeValues = {};
  2. let xhrUpd = new XMLHttpRequest();
  3. xhrUpd.onreadystatechange = function () {
  4. if (xhrUpd.readyState == 4) {
  5. if (xhrUpd.status == 200) {
  6. console.log("xhrUpd: ", xhrUpd.responseText);
  7. }
  8. else {
  9. console.log("xhrUpd: status=", xhrUpd.status);
  10. }
  11. }
  12. }
  13. function reloadRangeValues() {
  14. let url = "/update";
  15. // if there are scheduled updates, send them
  16. if (Object.keys(rangeValues).length > 0) {
  17. let params = [];
  18. for (let p in rangeValues)
  19. params.push(encodeURIComponent(p) + "=" + encodeURIComponent(rangeValues[p]));
  20. params = params.join("&");
  21. xhrUpd.open("POST", url, true);
  22. xhrUpd.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  23. xhrUpd.send(params);
  24. rangeValues = {};
  25. }
  26. };
  27. function reloadTerminal() {
  28. const terminal = document.querySelector("#term");
  29. const autoscroll = document.querySelector("#scroll");
  30. fetch('/terminal', {
  31. method: 'POST',
  32. headers: {
  33. 'Content-Type': 'application/x-www-form-urlencoded',
  34. },
  35. body: ''
  36. })
  37. .then(response => response.text())
  38. .then(data => {
  39. if(data.length > 0) {
  40. terminal.innerHTML += data;
  41. if (autoscroll.checked)
  42. terminal.scrollTop = terminal.scrollHeight;
  43. }
  44. })
  45. .catch(error => console.log('Error:', error));
  46. };
  47. function updateParameters() {
  48. fetch('/parameters', {
  49. method: 'POST',
  50. headers: {
  51. 'Content-Type': 'application/x-www-form-urlencoded',
  52. },
  53. body: ''
  54. })
  55. .then(response => response.json())
  56. .then(json_str => {
  57. const active_pwm = Math.round(json_str.active_pwm / 4095 * 100);
  58. document.querySelector('#range_act_pwm').value = active_pwm;
  59. document.querySelector('#out_act_pwm').value = active_pwm;
  60. const idle_pwm_max = Math.round(json_str.idle_pwm_max / json_str.active_pwm * 100);
  61. document.querySelector('#range_idl_pwm').value = idle_pwm_max;
  62. document.querySelector('#out_idl_pwm').value = idle_pwm_max;
  63. document.querySelector('#range_tim_sta').value = json_str.time_per_stair;
  64. document.querySelector('#out_tim_sta').value = json_str.time_per_stair;
  65. document.querySelector('#range_tim_ldr').value = json_str.time_ldr;
  66. document.querySelector('#out_tim_ldr').value = json_str.time_ldr;
  67. document.querySelector('#range_ldr_shw').value = json_str.ldr_schwelle;
  68. document.querySelector('#out_ldr_shw').value = json_str.ldr_schwelle;
  69. })
  70. .catch(error => console.log('Error:', error));
  71. };
  72. document.addEventListener('DOMContentLoaded', () => {
  73. setInterval(reloadTerminal, 1000);
  74. setInterval(reloadRangeValues, 1000);
  75. updateParameters();
  76. // use data- attributes for action
  77. document.querySelectorAll('.control').forEach((button) => {
  78. button.onclick = () => {
  79. fetch('/action', {
  80. method: 'POST',
  81. headers: {
  82. 'Content-Type': 'application/x-www-form-urlencoded',
  83. },
  84. body: `control=${button.dataset.action}`
  85. })
  86. .then(response => console.log(response))
  87. .catch(error => console.log('Error:', error));
  88. }
  89. });
  90. document.querySelectorAll('.regler').forEach((regler) => {
  91. regler.oninput = () => {
  92. document.querySelector(`#${regler.dataset.output}`).innerHTML = regler.value;
  93. rangeValues[regler.id] = regler.value;
  94. }
  95. });
  96. document.querySelector('#clear_term').onclick = () => {
  97. document.querySelector("#term").innerHTML = '';
  98. };
  99. });