ESP8266 SHT21 sensor
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.

client-script.js 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // This is the client-side Javascript that receives the IPs of all available
  2. // local ESP-Weather modules, reads their values using the Websocket interface
  3. // and renders the graphs to visualize them.
  4. //
  5. // ----------------------------------------------------------------------------
  6. // "THE BEER-WARE LICENSE" (Revision 42):
  7. // <xythobuz@xythobuz.de> & <ghost-ghost@web.de> wrote this file. As long as
  8. // you retain this notice you can do whatever you want with this stuff. If we
  9. // meet some day, and you think this stuff is worth it, you can buy us a beer
  10. // in return. Thomas Buck & Christian Högerle
  11. // ----------------------------------------------------------------------------
  12. var arrSensor = Array();
  13. textAvailableSensors = "Available Sensors";
  14. textButtonNext = "Continue";
  15. $(document).ready(function() {
  16. $('#main-part').append(`
  17. <div class="row" id="contentDiv">
  18. <div class="col-md-5 col-lg-5">
  19. <div class="panel panel-primary">
  20. <div class="panel-heading" id="listSensorsHeading">
  21. ` + textAvailableSensors + ` (0/0)
  22. </div>
  23. <div class="panel-body">
  24. <ul class="list-group" id="listSensors"></ul>
  25. <div id="alertDiv"></div>
  26. <button class="btn btn-primary" disabled="" id="btnSubmit">
  27. ` + textButtonNext + `
  28. </button>
  29. </div>
  30. </div>
  31. </div>
  32. </div>`);
  33. // aktuelle Uhrzeit fuer die Graphen ermitteln
  34. var actTime = new Date();
  35. actTime = actTime.getHours() + ":" + (actTime.getMinutes() < 10 ? '0':'') + actTime.getMinutes();
  36. $('#listSensorsHeading').empty();
  37. $('#listSensorsHeading').html(textAvailableSensors + " (0/" + clients.length + ")");
  38. // Alle clients im Array iterieren und Websocket abfragen
  39. var count = [0];
  40. jQuery.each(clients, function(index, client) {
  41. webSocket(client, "2391", count, clients.length);
  42. });
  43. // Submit-Button fuer die Formulareingabe
  44. $("#btnSubmit").click(function(event) {
  45. $('#contentDiv').empty();
  46. generateView(arrSensor, actTime);
  47. });
  48. });
  49. function webSocket(wsUri, wsPort, count, clientsCount) {
  50. websocket = new WebSocket("ws://" + wsUri + ":" + wsPort + "/");
  51. websocket.onopen = function(evt) {};
  52. websocket.onclose = function(evt) {};
  53. websocket.onmessage = function(evt) {
  54. var jsonData = jQuery.parseJSON(evt.data);
  55. count[0]++;
  56. var sensor = {id: count[0], ip: wsUri, actualTemp: jsonData['T'], actualHum: jsonData['H']};
  57. var arrEEPROM = Array();
  58. jQuery.each(jsonData['EEPROM'], function(index, data) {
  59. arrEEPROM.push(data);
  60. });
  61. sensor.arrEEPROM = arrEEPROM;
  62. arrSensor.push(sensor);
  63. $('#listSensorsHeading').html(textAvailableSensors + " (" + sensor.id + "/" + clientsCount + ")");
  64. $('#listSensors').append('<li class="list-group-item">' +
  65. ' Sensor ' + sensor.id +
  66. ' | IP: ' + sensor.ip +
  67. ' | aktuelle Temperatur: ' + sensor.actualTemp +
  68. ' | aktuelle Luftfeuchtigkeit: ' + sensor.actualHum +
  69. '</li>');
  70. // Alle Sensoren erfolgreich abgefragt
  71. if(count[0] == clientsCount) {
  72. $('#btnSubmit').prop("disabled", false);
  73. }
  74. };
  75. websocket.onerror = function(evt) {
  76. if($('#websocketError').length ) {
  77. $('.alert-danger').append('Sensor mit der IP:' + wsUri + ' konnte nicht abgefragt werden! <br>');
  78. } else {
  79. $('#alertDiv').append('<div class="alert alert-danger" id="websocketError">' +
  80. '<strong>Fehler:</strong><br>Sensor mit der IP:' + wsUri + ' konnte nicht abgefragt werden! <br>' +
  81. '</div>');
  82. }
  83. console.log(evt.data);
  84. };
  85. }
  86. function generateView(arrSensor, actTime) {
  87. $('#contentDiv').append(`<div class="col-md-12 col-lg-12">
  88. <div class="panel panel-primary">
  89. <ul class="nav nav-pills">
  90. <li class="active"><a class="navtab" data-toggle="tab" href="#home">Home</a></li>
  91. </ul>
  92. <div class="panel-body">
  93. <div id="contentPanel">
  94. </div>
  95. </div>
  96. </div>
  97. </div>`);
  98. jQuery.each(arrSensor, function(index, sensor) {
  99. $('.nav-pills').append('<li><a class="navtab" data-toggle="tab" href="#' + sensor.id + '">Sensor ' + sensor.id + '</a></li>');
  100. });
  101. // Flag fuer gemeinsamer Graph -> true
  102. generateGraph(true, arrSensor, actTime);
  103. $(".navtab").click(function(event) {
  104. $('#contentPanel').empty();
  105. if(event.target.text == "Home") {
  106. // Flag fuer gemeinsamer Graph -> true
  107. generateGraph(true, arrSensor, actTime);
  108. } else {
  109. generateGraph(false, arrSensor[(event.target.text.split(" ")[1] - 1)], actTime);
  110. }
  111. });
  112. }
  113. function generateGraph(flag, sensor, actTime) {
  114. $('#contentPanel').append(`<div class="row">
  115. <div class="col-sm-12 col-md-12 col-lg-6">
  116. <canvas id="temperaturChart"></canvas>
  117. </div>
  118. <div class="col-sm-12 col-md-12 col-lg-6">
  119. <canvas id="humidityChart"></canvas>
  120. </div>
  121. </div>`);
  122. if(flag) { // ein Graph für alle Sensoren
  123. var length = 0;
  124. jQuery.each(sensor, function(index, tmp) {
  125. if(length < tmp.arrEEPROM.length) {
  126. length = tmp.arrEEPROM.length;
  127. }
  128. });
  129. var labels = Array();
  130. actHour = actTime.split(":")[0];
  131. for(var i = length; i > 0; i--) {
  132. labels.unshift(actHour + ":00");
  133. actHour = (actHour - 1).mod(24);
  134. }
  135. labels.push(actTime);
  136. var dataTemperature = Array();
  137. var dataHumidity = Array();
  138. var tmpDataTemperature = Array();
  139. var tmpDataHumidity = Array();
  140. jQuery.each(sensor, function(index, tmp) {
  141. for(var i = 0; i < (length - tmp.arrEEPROM.length); i++) {
  142. tmpDataTemperature.push([]);
  143. tmpDataHumidity.push([]);
  144. }
  145. jQuery.each(tmp.arrEEPROM, function(index, value) {
  146. tmpDataTemperature.push(value['T']);
  147. tmpDataHumidity.push(value['H']);
  148. });
  149. tmpDataTemperature.push(tmp.actualTemp);
  150. tmpDataHumidity.push(tmp.actualHum);
  151. var lineColor = getRandomColor();
  152. dataTemperature.push({label: "Sensor " + tmp.id, data: tmpDataTemperature, fill: false,
  153. borderWidth: 3, borderColor : lineColor,});
  154. dataHumidity.push({label: "Sensor " + tmp.id, data: tmpDataHumidity, fill: false,
  155. borderWidth: 3, borderColor : lineColor,});
  156. tmpDataTemperature = [];
  157. tmpDataHumidity = [];
  158. });
  159. } else { // Graph für einzelnen Sensor
  160. var labels = Array();
  161. var tmpDataTemperature = Array();
  162. var tmpDataHumidity = Array();
  163. actHour = actTime.split(":")[0];
  164. actHour = (actHour - sensor.arrEEPROM.length).mod(24);
  165. jQuery.each(sensor.arrEEPROM, function(index, value) {
  166. actHour = (actHour + 1).mod(24);
  167. labels.push(actHour + ":00");
  168. tmpDataTemperature.push(value['T']);
  169. tmpDataHumidity.push(value['H']);
  170. });
  171. labels.push(actTime);
  172. tmpDataTemperature.push(sensor.actualTemp);
  173. tmpDataHumidity.push(sensor.actualHum);
  174. var dataTemperature = [{label: 'Temperatur [C]', data: tmpDataTemperature,
  175. fill: false, borderWidth: 3, borderColor: '#337ab7',}];
  176. var dataHumidity = [{label: 'Luftfeuchtigkeit [%RH]', data: tmpDataHumidity,
  177. fill: false, borderWidth: 3, borderColor: '#337ab7',}];
  178. }
  179. var tempCtx = $('#temperaturChart');
  180. var humCtx = $('#humidityChart');
  181. var tempChart = new Chart(tempCtx, {
  182. type: 'line',
  183. data: {
  184. labels: labels,
  185. datasets: dataTemperature,
  186. },
  187. options: {
  188. title: {
  189. display: true,
  190. text: 'Temperaturverlauf'
  191. }
  192. }
  193. });
  194. var humCharts = new Chart(humCtx, {
  195. type: 'line',
  196. data: {
  197. labels: labels,
  198. datasets: dataHumidity,
  199. },
  200. options: {
  201. title: {
  202. display: true,
  203. text: 'Luftfeuchtigkeitverlauf'
  204. }
  205. }
  206. });
  207. }
  208. // Modulo-Bug: http://javascript.about.com/od/problemsolving/a/modulobug.htm
  209. Number.prototype.mod = function(n) {
  210. return ((this%n)+n)%n;
  211. }
  212. function getRandomColor() {
  213. var letters = '0123456789ABCDEF'.split('');
  214. var color = '#';
  215. for (var i = 0; i < 6; i++ ) {
  216. color += letters[Math.floor(Math.random() * 16)];
  217. }
  218. return color;
  219. }