Arduino_Scuola
Published © GPL3+

WiFi Meteo Station

This project allows you to monitor the temperature and humidity on the web using Arduino Yun.

IntermediateFull instructions provided1.33 hours4,365
WiFi Meteo Station

Things used in this project

Hardware components

Arduino Yun
Arduino Yun
×1
5 KΩ resistor
×1
Breadboard and Wire Kit
×1
sensor RHT03
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Code snippet #1

Arduino
#include "DHT.h"                 //librarie used by the RHT03 sensor
#include <Bridge.h>             //function used to communicate with Arduino
#include <YunServer.h>       //We call the functions for creating a server 
#include <YunClient.h>        //to display the data collected.      

#define DHTPIN 2                  //We define the number of pin (2 in my case)  
#define DHTTYPE DHT22       //the type of sensor RHT03

YunServer server;                      //Initialize the global variables that are used for the server and the sensor:                                             
                                                  //The variable "h" will serve to store the humidity reading and "t" for the temperature
DHT dht(DHTPIN, DHTTYPE);
int h, t;

void setup() {
  Bridge.begin();
  server.listenOnLocalhost();
  server.begin();
  dht.begin();
}

void loop() {
  h = dht.readHumidity();
  t = dht.readTemperature();

  YunClient client = server.accept();
  if (client) {
    process(client);
    client.stop();
  }
  
  delay(50);
}

void process(YunClient client) {
  
  String command = client.readStringUntil('\r');

  if (command == "termo") {
    temp(client);
  }

  if (command == "idro") {
    idro(client);
  }
}

void temp(YunClient client) {
  if (isnan(t)) {
    client.print(F("Error"));
  } else {
    client.print(t);
  }
}

void idro(YunClient client) {
    if (isnan(t)) {
      client.print(F("Error"));
    } else {
      client.print(h);
    }
}

Code snippet #2

XML
<?PHP
$umido = file_get_contents ("http://192.168.1.200/arduino/idro");
$tempe = file_get_contents ("http://192.168.1.200/arduino/termo");
date_default_timezone_set('Europe/Rome');
$save= date ("H:i:s d.m.Y")."|".$umido."|".$tempe."\n";
$write_file = fopen("datilog.txt","a");
fwrite($write_file,$save);
fclose($write_file);
?> 

Code snippet #3

XML
<?PHP
if (isset($_GET['nrv'])) {
	$val = intval($_GET['nrv']);
	if ($val < 1) $val = 10;
	if ($val > 50) $val = 50;
} else {
	$val = 10;
}
if (isset($_GET['pag'])) {
	$pag = intval($_GET['pag'])*$val;
	if ($pag == 0) $pag = 10;
} else {
	$pag = 10;
}
$linee = file("datilog.txt");
foreach($linee as $line)
{
   $nr++;
   if ($nr <= $pag && $nr > $pag-$val-1) $numero++;
   list($dat[$numero],$umi[$numero],$tem[$numero]) = explode("|", $line);
 }
if ($numero > $val) $numero = $val;
$pp = $nr/$val;
if ($pp-intval($pp) != 0) {
	$pp = intval($pp)+1;
} else {
	$pp = intval($pp);
}
if (($pag/$val) > $pp) {
	$pag = $pp*$val;
	echo '<meta http-equiv="refresh" content="0; url=?pag='.($pag/$val).'&nrv='.$val.'">';
}
?>

Code snippet #4

XML
<!DOCTYPE html>
include ("data.php");
$umido = file_get_contents ("http://localhost/arduino/idro");
$tempe = file_get_contents ("http://localhost/arduino/termo");
?>
<HTML>
<HEAD>
<TITLE> Arduino YUN Pagina <?PHP echo $pag/$val; ?></TITLE>

    
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="css/print.css" type="text/css" media="print" />
   
 
<script type="text/javascript" src="//www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1');
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        var wrapper = new google.visualization.ChartWrapper({
          chartType: 'ColumnChart',
          dataTable: [['', <?PHP for($a = 1; $a < $numero; $a++) { echo "'".$dat[$a]."',"; } echo "'".$dat[$numero]."'"; ?> ],
                      ['', <?PHP for($a = 1; $a < $numero; $a++) { echo $tem[$a].","; } echo $tem[$numero]; ?> ]],
          options: {'temperatura': 'data'},
          containerId: 'temperatura'
        });
        wrapper.draw();
      }
      
      

      google.setOnLoadCallback(drawVisualization);
    </script>
    <script type="text/javascript">
      google.load('visualization', '1');
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        var wrapper = new google.visualization.ChartWrapper({
          chartType: 'ColumnChart',
          dataTable: [['', <?PHP for($a = 1; $a < $numero; $a++) { echo "'".$dat[$a]."',"; } echo "'".$dat[$numero]."'"; ?> ],
                      ['', <?PHP for($a = 1; $a < $numero; $a++) { echo $umi[$a].","; } echo $umi[$numero]; ?> ]],
          options: {'umidita': 'data'},
          containerId: 'umidita'
        });
        wrapper.draw();
      }
      
      

      google.setOnLoadCallback(drawVisualization);
    </script>
<script type='text/javascript'>
      google.load('visualization', '1', {packages:['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['C', <?PHP echo $tempe; ?>],
        ]);

        var options = {
          width: 400, height: 120,
          redFrom: 40, redTo: 60,
          yellowFrom:30, yellowTo: 40,
          minorTicks: 5,
          max: 60
        };

        var chart = new google.visualization.Gauge(document.getElementById('tempe'));
        chart.draw(data, options);
      }
    </script>
<script type='text/javascript'>
      google.load('visualization', '1', {packages:['gauge']});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Label', 'Value'],
          ['%', <?PHP echo $umido; ?>]
        ]);

        var options = {
          width: 400, height: 120,
          redFrom: 90, redTo: 100,
          yellowFrom:75, yellowTo: 90,
          minorTicks: 5
        };

        var chart = new google.visualization.Gauge(document.getElementById('umido'));
        chart.draw(data, options);
      }
    </script>
    
</HEAD>


<BODY>
<div align="center">
	<table border="0">

<tr><th align="center" colspan="2"><h1>Grafico Temperatura</h1></th></tr>

<tr><td align="right" width="55%"><h2>Temperatura attuale:</h2></td><td><div id='tempe'></div></td></tr>
<tr><th align="center" colspan="2"><div id="temperatura" style="width: 900px; height: 500px;"></div></th></tr>
<tr><th align="center" colspan="2"><h1>Grafico Umidita'</h1></th></tr>
<tr><td align="right" width="55%"><h2>Umidita' attuale:</h2></td><td><div id='umido'></div></td></tr>
<tr><th align="center" colspan="2"><div id="umidita" style="width: 900px; height: 500px;"></div></th></tr>
<tr><form id= "mioForm" action="index.php" method="get">
	<th align ="center">
Pagina: <select name="pag" onchange='this.form.submit()'> 
	<?PHP 
	for($a = 1; $a <= $pp ; $a++) {
		echo '<option value="'.$a.'"';
		if ($a == $pag/$val) echo ' selected';
		echo '>'.$a.'</option>';
	}
	?>  
</br>
</select>
   Numero Valori: <select name="nrv" onchange='this.form.submit()'>
	<?PHP
	for($a =1; $a <= 50; $a++)  {
		echo '<option value="'.$a.'"';
		if ($a == $val) echo ' selected';
		echo '>'.$a.'</option>';
	}
	?>
</select>
   <noscript><input type="submit" value="Submit"></noscript></th>
</form>
	</table>
</div>
</BODY>
</HTML>

Credits

FlorinAngelo

Posted by Arduino_Scuola

Comments