Guest User

BOLTFORSENSORS

a guest
Apr 9th, 2016
117
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2.  
  3. <head>
  4. <title> Bolt Iot -- Flood detection</title>
  5. <script type="text/javascript" src="/serveFile?filename=bolt.js"> setDebug(true); </script>
  6. </head>
  7.  
  8. <body onload="init();" bgcolor="#FFFFFF">
  9. <button onclick="myLoop(1);">START SENSOR</button>
  10. <script>
  11.  
  12. var avg_curr = 0; //avg of current minute
  13. var avg_prev=0; //avg for previous minutes
  14. var ctr=0,ctr2=0;
  15. function myLoop (i) {
  16.  
  17. setTimeout(function () {
  18.  
  19. var input = new XMLHttpRequest();
  20.  
  21. input.onreadystatechange = function() {
  22.  
  23. if (input.readyState == 4 && input.status == 200) {
  24. //document.getElementById("output").innerHTML = input.responseText; //for debuging
  25.  
  26. var flag = typeof input.responseText != 'object' ? JSON.parse(input.responseText) : input.responseText;
  27. alert(flag.value); //It will print the value of second field //for debuging
  28.  
  29. if(flag.value != "failed")
  30. {
  31. var piezo = parseInt(flag.value);
  32. if(ctr == 4){
  33. var check = avg_curr - avg_prev;
  34. var alarm = new XMLHttpRequest();
  35. if (check >=25){
  36. /*sets the Digital pin 0 HIGH*/
  37. alarm.open("GET", "/digitalWrite?pin=0&state=HIGH",true);
  38.  
  39. }
  40. else{
  41. alarm.open("GET", "/digitalWrite?pin=0&state=LOW",true);
  42. }
  43. alarm.send();
  44. ctr2++;
  45. avg_prev = (avg_prev+avg_curr)/ctr2;
  46. ctr =0;
  47. }
  48. else {
  49. ctr++;
  50. avg_curr = (avg_curr+piezo)/ctr;
  51. }
  52. }
  53. }
  54. };
  55. input.open("GET", "/analogRead?pin=A3",true);
  56. input.send();
  57.  
  58. //console.log(10); //for debuging
  59. if (i) myLoop(i); // decrement i and call myLoop again if i > 0
  60. }, 1500)
  61. }
  62.  
  63. /*initiator function to set the IF condition and THEN action for IFTTT */
  64. function init()
  65. {
  66. var ifttt = new XMLHttpRequest();
  67. iftttt.open("POST","/iftttSettings",true );
  68. ifttt.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
  69. ifttt.send('key=https://maker.ifttt.com/trigger/attention_people/with/key/daqNS-hP1rjC9iYeUPVCx2&pin=0&state=HIGH&enable=1');
  70. }
  71. </script>
  72. </body>
  73. </html>
RAW Paste Data