Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. <script language="javascript" type="text/javascript" src="https://www.flotcharts.org/flot/source/jquery.js"></script>
  2. <script language="javascript" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/flot/0.8.3/jquery.flot.min.js"></script>
  3.  
  4. <input type="button" id="button" value="click"/>
  5. <input type="text" id='service' placeholder='service' value="0x180D"/>
  6. <input type="text" id='characteristic' placeholder='characteristic' value="0x2a39"/>
  7. <div class="plot"></div>
  8. <input type="button" value="reset" onclick="reset()">
  9. <button onclick="download(JSON.stringify(data), 'myfilename.json', 'text/json')">Create file</button>
  10. <a href="javascript:alert('create the file, then download')" id="a">click here to download your file</a>
  11.  
  12.  
  13. <script>
  14. document.getElementById("button")
  15. .addEventListener('pointerup', function(event) {
  16. var service = parseInt(document.getElementById("service").value);
  17. var characteristic = parseInt(document.getElementById("characteristic").value);
  18.  
  19. // Call navigator.bluetooth.requestDevice
  20. navigator.bluetooth.requestDevice({
  21. filters: [{services: [service]}]
  22. }).then(device=>{
  23. window.device=device;
  24. console.log('Connecting to GATT Server...');
  25. return device.gatt.connect();
  26. }).then(server => {
  27. console.log('Getting Service...');
  28. window.server=server;
  29. return server.getPrimaryService(service);
  30. }).then(service => {
  31. console.log('Getting Characteristics...');
  32. if (true) {
  33. // Get all characteristics that match this UUID.
  34. return service.getCharacteristics(characteristic);
  35. }
  36. // Get all characteristics.
  37. return service.getCharacteristics();
  38. })
  39. .then(characteristics => {
  40. console.log(characteristics)
  41.  
  42. read_values(characteristics[0])
  43. })
  44. .catch(error => {
  45. console.error('Argh! ', error);
  46. });
  47. });
  48.  
  49.  
  50. function read_values(ch){
  51. ch.readValue().then(
  52. val => {
  53. console.log("val is", val);
  54.  
  55. /*for (let i = 0; i < val.byteLength; i++) {
  56. if(!data[i])
  57. data[i] = [];
  58. data[i].push([data[i].length, val.getInt8(i)])
  59. }*/
  60. while(data.length<7)
  61. data.push([])
  62.  
  63. addPoint(data[0], (val.getUint8(0)+255*val.getUint8(1))/100)
  64. addPoint(data[1], val.getUint8(2)+255*val.getUint8(3))
  65. addPoint(data[2], val.getUint8(4)*10)
  66. addPoint(data[3], val.getUint8(5)-100)
  67. addPoint(data[4], val.getUint8(6)-90)
  68. addPoint(data[5], val.getUint8(7)-90)
  69. addPoint(data[6], 360-(val.getUint8(8)+255*val.getUint8(9)))
  70.  
  71.  
  72.  
  73. }
  74. ).then(()=>{
  75. read_values(ch);
  76. });
  77. }
  78.  
  79. function addPoint(arr, val){
  80. arr.push([arr.length, val])
  81. }
  82.  
  83. var data;
  84. function reset(){
  85. data = new Array(7).fill().map(()=>[]);
  86. $(".plot").html('')
  87.  
  88. title=[
  89. 'wind speed',
  90. 'wind direction',
  91. 'battery',
  92. 'temprature',
  93. 'roll',
  94. 'pitch',
  95. 'eCompass'
  96. ]
  97. for(let i=0; i<data.length; i++){
  98. let subplot = $("<div class='subplot'>")
  99. $(".plot").append($("<div class='title'>"+title[i]+"</div>"))
  100. $(".plot").append(subplot)
  101. }
  102. }
  103. reset();
  104.  
  105. function download(text, name, type) {
  106. var a = document.getElementById("a");
  107. var file = new Blob([text], {type: type});
  108. a.href = URL.createObjectURL(file);
  109. a.download = name;
  110. }
  111.  
  112. setInterval(function() {
  113. for(let i=0; i<data.length; i++){
  114.  
  115. $.plot($(".subplot")[i], [
  116. data[i]
  117. ]);
  118. }
  119. },1000);
  120.  
  121. </script>
  122.  
  123. <style>
  124. .plot, .subplot{
  125. width: 100%;
  126. }
  127. .subplot{
  128. width: 25%;
  129. display: inline-block;
  130. height: 300px;
  131. }
  132. .title {
  133. display: inline-block;
  134. vertical-align: top;
  135. background: rgba(255,255,255,1);
  136. position: absolute;
  137. margin-left: 8%;
  138. }
  139. </style>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement