Giter Site home page Giter Site logo

php-linkyapi's Introduction

php-LinkyAPI


This API being essentially to collect our french electricity consumption, this page is in ... French!


API php pour récupérer vos données de consommations Linky

Voici une API simple d'utilisation pour récupérer vos données de consommations du compteur Linky, sous forme json, lisible !

J'utilise personnellement cette API avec une tâche planifiée (cron) toutes les 8h pour enregistrer l'ensemble des données dans un fichier json. Ce qui me permet de conserver mes données, et de les afficher avec Plotly par exemple, pour faire des corrélations avec le chauffage (Qivivo), les relevés Netatmo, etc.

Voici le script php qui log les données Linky dans un fichier json : linkyCron.php

Pré-requis

  • Un compteur Linky !
  • Un compte Enedis. Vous pouvez le créer ici. Vous devez attendre quelques semaines après l'installation du Linky pour voir vos données sur le site Enedis. Une fois ces données disponible, vous pouvez utiliser cette API.
  • Un serveur php avec accès à internet (mutualisé sur hébergement, NAS Synology, etc.)

Utilisation

  • Téléchargez le fichier phpLinkyAPI.php sur votre serveur..
  • Créez un script php avec vos identifiants/mot de passe Enedis et un include de l'API.

Connection

require("phpLinkyAPI.php"); //Linky custom API

$_Linky = new Linky($enedis_user, $enedis_pass, false);
if (isset($_Linky->error)) echo '__ERROR__: ', $_Linky->error, "<br>";

La connexion au site Enedis est assez lente, de l'ordre de 5sec...
Les conditions générales d'Enedis peuvent changer, dans ce cas il faut se connecter à son compte pour les accepter, sinon le script tombe dessus et ne fonctionne pas


Une fois connecté, ajoutez les fonctions désirées dans votre script. A noter que les données du Linky ne sont disponibles que le lendemain. La date d'hier servira donc de date de fin dans la plupart des cas.

OPERATIONS

//Si nous sommes le 25 Février 2018:

//Consommation par demi-heure:
$data = $_Linky->getData_perhour('24/02/2018');
echo "<pre>getData_perhour:<br>".json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)."</pre><br>";

//Vous pouvez aussi le faire automatiquement:
$today = new DateTime('NOW', new DateTimeZone('Europe/Paris'));
$yesterday = $today->sub(new DateInterval('P1D'));
$data = $_Linky->getData_perhour($yesterday->format('d/m/Y'));
echo "<pre>getData_perhour:<br>".json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)."</pre><br>";

//Consommation par jour:
//Utilisez toujours des dates d'un mois glissant, sinon les données renvoyées peuvent être décalées, surtout pour le mois courant.
$data = $_Linky->getData_perday('01/01/2018', '31/01/2018');
echo "<pre>getData_perday:<br>".json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)."</pre><br>";

//Consommation par mois:
//Même si les données n'existent pas, il faut donner une année glissante:
$data = $_Linky->getData_permonth('01/02/2017', '24/02/2018');
echo "<pre>getData_permonth:<br>".json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)."</pre><br>";

//Consommation par année:
$data = $_Linky->getData_peryear();
echo "<pre>getData_peryear:<br>".json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)."</pre><br>";

//Vous pouvez aussi directement appeller cette fonction pour récupérer l'ensemble des données jusqu'à hier:
$_Linky->getAll();
echo "<pre>getAll:<br>".json_encode($_Linky->_data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT)."</pre><br>";

VISUALISATION

Pour visualiser les données récoltées dans le fichier json (voir script au dessus en intro), voici un fichier html/javascript. Dans le screen ci-dessus, je n'avais évidemment pas le Linky en 2015 / 2016 / 2017. J'ai simplement récupéré les infos annuelles depuis mon compte EDF, pour les ajouter à la main dans le json 😏

Téléchargez le fichier LinkyLog.html, mettez le sur votre serveur, changez éventuellement la ligne var linkyLogPath = "cache/linkyLog.json" pour pointer sur votre fichier json.

Ensuite, ouvrez le simplement dans un navigateur !

Vous pouvez bien sûr personnaliser le fichier, pour lire un autre json avec d'autres données et dessiner des corrélations.

Version history

v0.1 (2018-02-25)

  • Première version !

License

The MIT License (MIT)

Copyright (c) 2018 KiboOst

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

php-linkyapi's People

Contributors

kiboost avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.