Guest User

Server_Node_Implementation

a guest
Apr 9th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <html>
  2. <head>
  3. <title>Page Title</title>
  4. </head>
  5. <body>
  6.  
  7. <?php
  8.  
  9. // This part runs for half a day and then the script uploads to database
  10.   function read_data()
  11.   {    
  12.     var i = 0;
  13.  
  14.     while(1)
  15.     {  
  16.         $a = file_get_contents("http://192.168.43.84/analogRead?pin=A3");
  17.         $b = file_get_contents("http://192.168.43.84/analogRead?pin=A0");
  18.        
  19.         $str = json_decode($a);
  20.         $str1 = json_decode($b);
  21.        
  22.         $fp = fopen("1.csv","a");  
  23.  
  24.         foreach ($str as $key)
  25.         {
  26.             fwrite($fp, $key);
  27.             fwrite($fp, ",");  
  28.         }
  29.         i = i+150;
  30.         fclose($fp);
  31.  
  32.         $fp = fopen("2.csv","a");  
  33.         foreach ($str1 as $key1)
  34.         {
  35.             fwrite($fp, $key1);
  36.             fwrite($fp, ",");  
  37.         }
  38.         fclose($fp);
  39.        
  40.         sleep(150);
  41.         if(i==43100)
  42.         {
  43.             break;
  44.         }
  45.     }
  46.   }
  47.  
  48.   read_data();
  49.   // This is for authenticating the database
  50.   $connect = mysql_connect('localhost','root','password');
  51.  
  52.   if(!$connect)
  53.   {
  54.     echo "No database found";
  55.   }
  56.  
  57.   // your database name
  58.   $cid = mysql_select_db(riversData,$connect);
  59.  
  60.   define('CSV_PATH','./');
  61.  
  62.   $csv_file = CSV_PATH."1.csv";
  63.   $csv_file1 = CSV_PATH."2.csv";
  64.  
  65.   if(($handle = $fopen($csv_file,"r"))!==FALSE)
  66.   {
  67.     fgetcsv($handle);
  68.     while(($data = fgetcsv($handle,1000,","))!==FALSE)
  69.     {
  70.         $num = count($data);
  71.         for($c=0;$c<$num;$c++)
  72.         {
  73.             $col[$c]=$data[$c];
  74.         }
  75.         $col1 = $col[0];
  76.         $col2 = $col[1];
  77.  
  78.     // now upload your database using insert querries or bulk load
  79.         $query = "INSERT INTO river1node1(pressure,moisture,atTime,nodeId) VALUES(,'".$col2"',,,1)"
  80.         $s = mysql_query($query,$connect);
  81.  
  82.     }
  83.     fclose(handle);
  84.   }
  85.  
  86.   if(($handle = $fopen($csv_file1,"r"))!==FALSE)
  87.   {
  88.     fgetcsv($handle);
  89.     while(($data = fgetcsv($handle,1000,","))!==FALSE)
  90.     {
  91.         $num = count($data);
  92.         for($c=0;$c<$num;$c++)
  93.         {
  94.             $col[$c]=$data[$c];
  95.         }
  96.         $col1 = $col[0];
  97.         $col2 = $col[1];
  98.  
  99.     // now upload your database using insert querries or bulk load
  100.         $query = "INSERT INTO river1node1(pressure,moisture,atTime,nodeId) VALUES(,,'".$col2."',,1)"
  101.  
  102.         $s = mysql_query($query,$connect);
  103.  
  104.     }
  105.     fclose(handle);
  106.   }
  107.  
  108.   mysql_close($connect);
  109. ?>
  110.  
  111. </body>
  112. </html>
Add Comment
Please, Sign In to add comment