#!/usr/bin/php
<?php 
/**
* Example of PARTNER Weather station API
* If you need more details, please take a glance at https://dev.netatmo.com/doc
*/
define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__ . '/src/Netatmo/autoload.php';
require_once 'Utils.php';
require_once 'Config.php';
//App client configuration
$config = array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password);
$client = new Netatmo\Clients\NAWSApiClient($config);
//Authentication with Netatmo server (OAuth2)
try {
    $tokens = $client->getAccessToken();
} catch (Netatmo\Exceptions\NAClientException $ex) {
    handleError("An error happened while trying to retrieve your tokens: " . $ex->getMessage() . "\n", TRUE);
}
try {
    $deviceList = $client->getPartnerDevices();
} catch (Netatmo\Exceptions\NAClientException $ex) {
    handleError("An error occured while retrieving device list: " . $ex->getMessage() . "\n", TRUE);
}
if (!isset($deviceList['weather_stations']) || empty($deviceList['weather_stations'])) {
    echo "No Weather stations affiliated to partner app";
} else {
    try {
        $data = $client->getData($deviceList['weather_stations'][0]);
        printWSBasicInfo($data['devices'][0]);
    } catch (Netatmo\Exceptions\NAClientException $ex) {
Esempio n. 2
0
<?php

//require php5-curl
require_once "src/Netatmo/autoload.php";
$config = array();
$config['client_id'] = "client-id-from-dev.netatmo.com";
$config['client_secret'] = "client-secret-from-dev.netatmo.com";
$config['scope'] = 'read_station read_thermostat write_thermostat';
$client = new Netatmo\Clients\NAWSApiClient($config);
#token
$username = "******";
$pwd = "your-netatmo-password";
$client->setVariable('username', $username);
$client->setVariable('password', $pwd);
try {
    $tokens = $client->getAccessToken();
    $refresh_token = $tokens["refresh_token"];
    $access_token = $tokens["access_token"];
} catch (Netatmo\Exceptions\NAClientException $ex) {
    echo "An error occcured while trying to retrieve your tokens \n";
}
$data = $client->getData(NULL, TRUE);
foreach ($data['devices'] as $device) {
    //    print_r($device['dashboard_data']);
    //    echo "Device name: " . $device['station_name'] . "\n";
    if ($argv[1] == "discovery") {
        echo "Device name: " . $device['station_name'] . "\n";
    } elseif ($argv[1] == "data") {
        echo "- netatmo." . $device['station_name'] . ".main.pressure " . $device['dashboard_data']['Pressure'] . "\n";
        echo "- netatmo." . $device['station_name'] . ".main.noise " . $device['dashboard_data']['Noise'] . "\n";
        echo "- netatmo." . $device['station_name'] . ".main.temp " . $device['dashboard_data']['Temperature'] . "\n";
<?php

/*
* Authentication to Netatmo Servers using Authorization grant.
* This script has to be hosted on a webserver in order to make it work
* For more details about Netatmo API, please take a look at https://dev.netatmo.com/doc
*/
define('__ROOT__', dirname(dirname(__FILE__)));
require_once __ROOT__ . '/src/Netatmo/autoload.php';
require_once 'Config.php';
require_once 'Utils.php';
//API client configuration
$config = array("client_id" => $client_id, "client_secret" => $client_secret, "scope" => Netatmo\Common\NAScopes::SCOPE_READ_STATION);
$client = new Netatmo\Clients\NAWSApiClient($config);
//if code is provided in get param, it means user has accepted your app and been redirected here
if (isset($_GET["code"])) {
    //get the tokens, you can store $tokens['refresh_token'] in order to quickly retrieve a new access_token next time
    try {
        $tokens = $client->getAccessToken();
    } catch (Netatmo\Exceptions\NAClientException $ex) {
        echo "An error occured while trying to retrieve your tokens \n";
        echo "Reason: " . $ex->getMessage() . "\n";
        die;
    }
    //retrieve user's weather station data
    try {
        $data = $client->getData();
    } catch (Netatmo\Exceptions\NAClientException $ex) {
        echo "An error occured while retrieving data: " . $ex->getMessage() . "\n";
        die;
    }
 */
function printDevices($devices, $title = NULL)
{
    if (!is_null($devices) && is_array($devices) && !empty($devices)) {
        if (!is_null($title)) {
            printMessageWithBorder($title);
        }
        foreach ($devices as $device) {
            printWSBasicInfo($device);
        }
    }
}
//App client configuration
$scope = Netatmo\Common\NAScopes::SCOPE_READ_STATION;
$config = array("client_id" => $client_id, "client_secret" => $client_secret, "username" => $test_username, "password" => $test_password);
$client = new Netatmo\Clients\NAWSApiClient($config);
//Authentication with Netatmo server (OAuth2)
try {
    $tokens = $client->getAccessToken();
} catch (Netatmo\Exceptions\NAClientException $ex) {
    handleError("An error happened while trying to retrieve your tokens: " . $ex->getMessage() . "\n", TRUE);
}
//Retrieve user's Weather Stations Information
try {
    //retrieve all stations belonging to the user, and also his favorite ones
    $data = $client->getData(NULL, TRUE);
    printMessageWithBorder("Weather Stations Basic Information");
} catch (Netatmo\Exceptions\NAClientException $ex) {
    handleError("An error occured while retrieving data: " . $ex->getMessage() . "\n", TRUE);
}
if (empty($data['devices'])) {