Ryan D'Costa
Published

Flood Detection and Warning System

We use Bolt IoT to directly measure flow rate, detect flood & soil moisture, and a sensor to find volumetric water content of soil.

IntermediateFull instructions provided3 hours786
Flood Detection and Warning System

Things used in this project

Hardware components

Piezo Sensor
ControlEverything.com Piezo Sensor
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Breadboard (generic)
Breadboard (generic)
×1

Software apps and online services

Bolt

Story

Read more

Code

Bolt For Sensors

HTML
<html>

<head>
  <title> Bolt Iot -- Flood detection</title>
  <script type="text/javascript" src="/serveFile?filename=bolt.js"> setDebug(true); </script> 
</head>

<body onload="init();" bgcolor="#FFFFFF">
  <button onclick="myLoop(1);">START SENSOR</button>
  <script>
   
    var avg_curr = 0; //avg of current minute
    var avg_prev=0;  //avg for previous minutes
    var ctr=0,ctr2=0;
    function myLoop (i) {          
      
      setTimeout(function () {   
      
      var input = new XMLHttpRequest();
      
      input.onreadystatechange = function() {
      
      if (input.readyState == 4 && input.status == 200) {
          //document.getElementById("output").innerHTML = input.responseText;   //for debuging
        
        var flag = typeof input.responseText != 'object' ? JSON.parse(input.responseText) : input.responseText; 
        alert(flag.value);  //It will print the value of second field    //for debuging
        
        if(flag.value != "failed")
          {
          var piezo = parseInt(flag.value);
          if(ctr == 4){ 
            var check = avg_curr - avg_prev;
            var alarm = new XMLHttpRequest();
            if (check >=25){
              /*sets the Digital pin 0 HIGH*/
              alarm.open("GET", "/digitalWrite?pin=0&state=HIGH",true);
              
            }
            else{
              alarm.open("GET", "/digitalWrite?pin=0&state=LOW",true);
            }
            alarm.send();
            ctr2++;
            avg_prev = (avg_prev+avg_curr)/ctr2;
            ctr =0;
           }
          else {
            ctr++;
            avg_curr = (avg_curr+piezo)/ctr;
          }
          } 
        }
      };
      input.open("GET", "/analogRead?pin=A3",true);
      input.send();

      //console.log(10);   //for debuging
      if (i) myLoop(i);    //  decrement i and call myLoop again if i > 0
      }, 1500)
    } 

    /*initiator function to set the IF condition and THEN action for IFTTT */
    function init()
    {
      var ifttt =  new XMLHttpRequest();
      iftttt.open("POST","/iftttSettings",true );
      ifttt.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      ifttt.send('key=https://maker.ifttt.com/trigger/attention_people/with/key/daqNS-hP1rjC9iYeUPVCx2&pin=0&state=HIGH&enable=1');
    }
</script>
</body>
</html>

Server Node Implementation

ActionScript
<html>
<head>
<title>Page Title</title>
</head>
<body>

<?php

// This part runs for half a day and then the script uploads to database 
  function read_data()
  {		
	var i = 0;

	while(1)
	{	
		$a = file_get_contents("http://192.168.43.84/analogRead?pin=A3");
		$b = file_get_contents("http://192.168.43.84/analogRead?pin=A0");
		
		$str = json_decode($a);
		$str1 = json_decode($b);
		
		$fp = fopen("1.csv","a");	

		foreach ($str as $key) 
		{
			fwrite($fp, $key);
			fwrite($fp, ",");	
		}
		i = i+150;
		fclose($fp); 

		$fp = fopen("2.csv","a");	
		foreach ($str1 as $key1) 
		{
			fwrite($fp, $key1);
			fwrite($fp, ",");	
		}
		fclose($fp); 
		
		sleep(150);
		if(i==43100)
		{
			break;
		}
	}
  }

  read_data();
  // This is for authenticating the database
  $connect = mysql_connect('localhost','root','password');

  if(!$connect)
  {
  	echo "No database found";
  }

  // your database name
  $cid = mysql_select_db(riversData,$connect);

  define('CSV_PATH','./');

  $csv_file = CSV_PATH."1.csv";
  $csv_file1 = CSV_PATH."2.csv";

  if(($handle = $fopen($csv_file,"r"))!==FALSE)
  {
  	fgetcsv($handle);
  	while(($data = fgetcsv($handle,1000,","))!==FALSE)
  	{
  		$num = count($data);
  		for($c=0;$c<$num;$c++)
  		{
  			$col[$c]=$data[$c];
  		}
  		$col1 = $col[0];
  		$col2 = $col[1];

  	// now upload your database using insert querries or bulk load
  		$query = "INSERT INTO river1node1(pressure,moisture,atTime,nodeId) VALUES(,'".$col2"',,,1)" 
  		$s = mysql_query($query,$connect);

  	}
  	fclose(handle);
  }

  if(($handle = $fopen($csv_file1,"r"))!==FALSE)
  {
  	fgetcsv($handle);
  	while(($data = fgetcsv($handle,1000,","))!==FALSE)
  	{
  		$num = count($data);
  		for($c=0;$c<$num;$c++)
  		{
  			$col[$c]=$data[$c];
  		}
  		$col1 = $col[0];
  		$col2 = $col[1];

  	// now upload your database using insert querries or bulk load
  		$query = "INSERT INTO river1node1(pressure,moisture,atTime,nodeId) VALUES(,,'".$col2."',,1)" 

  		$s = mysql_query($query,$connect);

  	}
  	fclose(handle);
  }

  mysql_close($connect);
?>

</body>
</html>

End User imp

HTML
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
</head>
<body>
	<style>body {
	    background: #eeeeee;
	}
	.panel {
	    display: inline-block;
	    background: #ffffff;
	    min-height: 100px;
	    height: 180px;
	    width: 400px;
	    box-shadow:0px 0px 5px 5px #C9C9C9;
	    -webkit-box-shadow:2px 2px 5px 5x #C9C9C9;
	    -moz-box-shadow:2px 2px 5px 5px #C9C9C9;
	    margin-top: 150px;
	    margin-left: 500px;
	    padding: 10px;
	}
	.panel1 {
	    min-width: 100px;
	    width: 100px;
	}
	.panel2 {
	    min-width: 245px;
	    width: 245px;
	}
	.menu1{
		box-shadow:0px 0px 5px 5px #C9C9C9;
	    -webkit-box-shadow:2px 2px 5px 5x #C9C9C9;
	    -moz-box-shadow:2px 2px 5px 5px #C9C9C9;
	    margin-left: 175px;
	    margin-top: 5px;
	    }
	.menu2{
		box-shadow:0px 0px 5px 5px #C9C9C9;
	    -webkit-box-shadow:2px 2px 5px 5x #C9C9C9;
	    -moz-box-shadow:2px 2px 5px 5px #C9C9C9;
	    margin-left: 175px;
	    margin-top: 15px;
	    }
	.menu3{
		box-shadow:0px 0px 5px 5px #C9C9C9;
	    -webkit-box-shadow:2px 2px 5px 5x #C9C9C9;
	    -moz-box-shadow:2px 2px 5px 5px #C9C9C9;
	    margin-left: 180px;
	    margin-top: 15px;
	    }
	.submit{
		margin-top: 20px;
		margin-left: 130px;
	}
	.title1{
		margin-left: 490px;
	}
	</style>
	<H1 class="title1">Natural Calamity Detection</H1>
<div class="panel">

	<label > River </label> <select class="menu1">
		  <option value="Ganges">Ganges</option>
		  <option value="Brahmaputra">Brahmaputra</option>
		  <option value="Yamuna">Yamuna</option>
		  <option value="Krishna">Krishna</option>
		  <option value="Godavari">Godavari</option>
		</select>
	<br>
	<label > State </label>
		<select class="menu2">
		  <option value="uttar_pradesh">Uttar Pradesh</option>
		  <option value="Assam">Assam</option>
		  <option value="Delhi">Delhi</option>
		  <option value="andhra">Andhra </option>
		</select>
	<br>
	<label > Area </label>
		<select class="menu3">
		  <option value="area1">area1</option>
		  <option value="area2">area2</option>
		  <option value="area3">area3</option>
		  <option value="area4">area4</option>
		</select><br>
	<form action="http://192.168.43.84/" method="GET">
	<input type="submit" value="Submit" class="submit">
	</form>
</div>
</body>
</html>

Credits

SaiA22

Posted by Ryan D'Costa

Comments