Advertisement
Tempist

Sensor Starter For Flood D/W [1/3]

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