Teleinfo iinst jpgraph.php

De MicElectroLinGenMet.

Page : Démodulateur téléinformation_EDF

<?php
// Génére un graphe en image PNG en focntion des donnée téléinfo IINST de la base MySql.
// Intensité instantannée en Ampères sur les 3 phases (triphasé).
// Par Domos.
 
//Librairies JpGraph
include ("/var/www/jpgraph/src/jpgraph.php");
include ("/var/www/jpgraph/src/jpgraph_line.php");
 
// Base de donnée Téléinfo:
/*
Format de la table:
timestamp 	rec_date 	rec_time 	adco 		optarif isousc 	hchp 		hchc 		ptec 	inst1 	inst2 	inst3 	imax1 	imax2 	imax3 	pmax 	papp 	hhphc 	motdetat 	ppot 	adir1 	adir2 	adir3
1234998004 	2009-02-19 	00:00:04 	700609361116 	HC.. 	20 	11008467 	10490214 	HP 	1 	0 	1 	18 	23 	22 	8780 	400 	E 	000000 		00 	0 	0 	0
1234998065 	2009-02-19 	00:01:05 	700609361116 	HC.. 	20 	11008473 	10490214 	HP 	1 	0 	1 	18 	23 	22 	8780 	400 	E 	000000 		00 	0 	0 	0
1234998124 	2009-02-19 	00:02:04 	700609361116 	HC.. 	20 	11008479 	10490214 	HP 	1 	0 	1 	18 	23 	22 	8780 	390 	E 	000000 		00 	0 	0 	0
1234998185 	2009-02-19 	00:03:05 	700609361116 	HC.. 	20 	11008484 	10490214 	HP 	1 	0 	0 	18 	23 	22 	8780 	330 	E 	000000 		00 	0 	0 	0
1234998244 	2009-02-19 	00:04:04 	700609361116 	HC.. 	20 	11008489 	10490214 	HP 	1 	0 	0 	18 	23 	22 	8780 	330 	E 	000000 		00 	0 	0 	0
1234998304 	2009-02-19 	00:05:04 	700609361116 	HC.. 	20 	11008493 	10490214 	HP 	1 	0 	0 	18 	23 	22 	8780 	330 	E 	000000 		00 	0 	0 	0
1234998365 	2009-02-19 	00:06:05 	700609361116 	HC.. 	20 	11008498 	10490214 	HP 	1 	0 	0 	18 	23 	22 	8780 	320 	E 	000000 		00 	0 	0 	0
*/
 
// Formatage Date.
function TimeCallback($aVal) 
{
    return Date('H:i', $aVal);
}
 
$periodesecondes = 24*3600 ;							// 24h.
$heurecourante = date('H') ;							// Heure courante.
$timestampheure = mktime($heurecourante,0,0,date("m"),date("d"),date("Y"));	// Timestamp courant à heure fixe (mn et s à 0).
$timestampdebut = $timestampheure - $periodesecondes ;				// Recule de 24h.
//printf("datedebut : (%s) %s<br \>", $timestampdebut, date("YmdHis", $timestampdebut)) ;
 
// Connexion MySql et requète.
$serveur="localhost"; 
$login="root";
$base="maison";
$fd = fopen ("./mysql.txt", "r");
$pass = rtrim(fgets($fd, 4096)) ;	// $pass contient password compte root MySql.
fclose ($fd);
mysql_connect($serveur, $login, $pass) or die("Erreur de connexion au serveur MySql");
mysql_select_db($base) or die("Erreur de connexion a la base de donnees $base");
 
$table="teleinfo"; 
$query="SELECT timestamp, rec_date, rec_time, inst1, inst2, inst3 
	FROM `$table` 
	WHERE timestamp >= $timestampdebut 
	ORDER BY timestamp DESC" ; 
$result=mysql_query($query) or die ("<b>Erreur</b> dans la requète <b>" . $query . "</b> : "  . mysql_error() . " !<br>"); 
$num_rows = mysql_num_rows($result) ;
$num_rows-- ;
while ($row = mysql_fetch_array($result)) 
{
	//printf("Timestamp_unix: %s, Date: %s, Heure: %s, inst1: %s, inst2: %s, inst3: %s <br \>", $row[0], $row["rec_date"], $row["rec_time"], $row["inst1"], $row["inst2"], $row["inst3"]);
	$timestp[$num_rows] = $row["timestamp"] ;
	$iinst1[$num_rows] = $row["inst1"] ;
	$iinst2[$num_rows] = $row["inst2"] + 10 ;	// Décalage de 10A pour lisibilité graphe.
	$iinst3[$num_rows] = $row["inst3"] + 20 ;	// Décalage de 20A pour lisibilité graphe.
	$num_rows-- ;
}
mysql_free_result($result) ;
mysql_close() ;
 
//exit(0) ;
 
$date =	Date('d/m/Y, H:i', $timestp[count($timestp) -1]) ;
 
// Get start time
$start = $timestp[0] ;
$end = $timestp[count($timestp) -1] ;
 
// Génération du graphe.	
$graph = new Graph(800,450);
$graph->SetMarginColor('black');
$graph->SetColor('gray1');
$graph->SetMargin(60,40,40,80);
//$graph->SetShadow() ;
 
// Now specify the X-scale explicit but let the Y-scale be auto-scaled
$graph->SetScale("intlin",0,35,$start,$end);
$graph->title->Set("Intensité instantanée le $date");
$graph->title->SetColor("gray7");
 
$graph->yaxis->title->Set("Amperes"); 
$graph->yaxis->title->SetMargin(20); 
$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->yaxis->title->SetColor("gray7");
$graph->xaxis->title->Set("24h");
$graph->xaxis->title->SetMargin(30); 
$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
$graph->xaxis->title->SetColor("gray7");
 
$graph->yaxis->SetColor("gray5","gray7");
$graph->xaxis->SetColor("gray5","gray7");
 
$graph->ygrid->SetColor("gray5");
$graph->xgrid->Show(true) ;
$graph->xgrid->SetColor("gray5");
 
// Setup the callback and adjust the angle of the labels
$graph->xaxis->SetLabelFormatCallback('TimeCallback');
 
// Set the labels and minor ticks.
$graph->xaxis->scale->ticks->Set(7200,1800);
$graph->yaxis->scale->ticks->Set(2,1);
 
// Adjust the legend position
$graph->legend->SetLayout(LEGEND_HOR);
$graph->legend->Pos(0.05,0.97,"left","bottom");
 
$linei1 = new LinePlot($iinst1,$timestp);
$linei1->SetWeight(2);
$linei1->SetColor('green');
$linei1->SetLegend("IINST1");
 
$linei2 = new LinePlot($iinst2,$timestp);
$linei2->SetWeight(2);
$linei2->SetColor('blue');
$linei2->SetLegend("IINST2");
 
$linei3 = new LinePlot($iinst3,$timestp);
$linei3->SetWeight(2);
$linei3->SetColor('red');
$linei3->SetLegend("IINST3");
 
$graph->Add($linei1);
$graph->Add($linei2);
$graph->Add($linei3);
 
$iinst1_actuelle=$iinst1[count($iinst1) - 1] + 0 ;
$iinst2_actuelle=$iinst2[count($iinst2) - 1] - 10 ;
$iinst3_actuelle=$iinst3[count($iinst3) - 1] - 20 ;
$txt=new Text("IINST1 actuelle: $iinst1_actuelle A --- IINST2 actuelle: $iinst2_actuelle A --- IINST3 actuelle: $iinst3_actuelle A ", 280, 420);
$txt->SetColor("gray7");
$graph->AddText($txt);
 
$graph->Stroke();
?>