Using the example PHP

The Netatmo API is a set of webservices allowing developers and third parties to access Netatmo device's data.
ap33
Posts: 7
Joined: 08 Nov 2012, 10:28

Re: Using the example PHP

Post by ap33 » 08 Nov 2012, 10:33

Hello,

1- Is it possbile to have a Netatmo client not using cURL ? Many providers don't activate this library. So we have to looking for another way to access datas.
2- Can we access data in a domestic network, useful when internet connection is down and to save data locally in case of backup ?

Thanks

PS : +1 for a french forum ;-)

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 08 Nov 2012, 10:45

Hello,

Well below is an exemple of how to access user data with a no cUrl php request.
We do not provide a full client without curl for now, but you could easily extend the exemple below.

<?php

// This is just an example to illustrate the documentation
// Prefer the PHP SDK

$app_id = 'YOUR_APP_ID';
$app_secret = 'YOUR_APP_SECRET';
$username = 'YOUR_USERNAME';
$password = 'YOUR_PASSWORD';
$token_url = "https://api.netatmo.net/oauth2/token";
$postdata = http_build_query(
array(
'grant_type' => "password",
'client_id' => $app_id,
'client_secret' => $app_secret,
'username' => $username,
'password' => $password
)
);

$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);

$context = stream_context_create($opts);

$response = file_get_contents($token_url, false, $context);
$params = null;
$params = json_decode($response, true);

$api_url = "https://api.netatmo.net/api/getuser?access_token="
. $params['access_token'];

$user = json_decode(file_get_contents($api_url));
echo("It worked. Hello <".$user->body->mail.">\n");

?>

I'm sorry but there is no way to access your data when your internet is down.
Your station is able to store 3 weeks of data if your connection is down, and as soon as your connection will be available your station will send all its stored data in the cloud.
Thomas Rosenblatt, Netatmo Team.

ap33
Posts: 7
Joined: 08 Nov 2012, 10:28

Re: Using the example PHP

Post by ap33 » 08 Nov 2012, 12:04

Thanks for you reply, i will try this type of code.

It isn't a good new for me to know "there is no way to access your data when your internet is down". I would like tu use all datas in my home automation system, which isn't dependant of internet connnection, but local network.
So even the station is able to store 3 weeks of data if your connection is down, i can't use datas in live mode (every 15 minutes for example).
In the same way, i read in another post that datas of meteogroup cant't be get by the api.

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 09 Nov 2012, 23:15

This is the error that is displayed.

An error happend while trying to retrieve your tokens : No access token stored User does not have any devices

And I've verified that my device has authorized the code. I've also verified that curl is enabled and functioning.

curl

cURL support enabled
cURL Information 7.26.0-DEV

I've also tried the test code given above and the output provided is:

It worked. Hello

So again the script is not working. Again, any assistance is appreciated.

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 10 Nov 2012, 18:59

Ok jtienor,

Please send me the current php code your are running.
Thomas Rosenblatt, Netatmo Team.

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 11 Nov 2012, 01:50

<?php

require_once("NAApiClient.php");

$config = array();
$config['client_id'] = '508bfdb01877593998000aba';
$config['client_secret'] = 'GxlcoqYwufwWne29Mr5cQPldoKk0O2MiJpv9a7';
$client = new NAApiClient($config);


$username = "";
$pwd = "";
$client->setVariable("", $username);
$client->setVariable("", $pwd);
try
{
$tokens = $client->getAccessToken();
$refresh_token = $tokens["refresh_token"];
$access_token = $tokens["access_token"];
}
catch(NAClientException $ex)
{
echo "An error happend while trying to retrieve your tokens : ".$ex->getMessage()."\n";
}


// First retrieve user device list

try
{
$deviceList = $client->api("devicelist");
if(isset($deviceList["devices"][0]))
{
$device_id = $deviceList["devices"][0]["_id"];
// Ok now retrieve last temperature and humidity
$params = array("scale" =>"max",
"type"=>"Temperature,Humidity",
"date_end"=>"last",
"device_id"=>$device_id);
$res = $client->api("getmeasure", $params);
if(isset($res[0]) && isset($res[0]["beg_time"]))
{
$time = $res[0]["beg_time"];
$t = $res[0]["value"][0][0];
$h = $res[0]["value"][0][1];
echo "Temperature is $t Celsius @".date('c', $time)."\n";
echo "Humidity is $h Celsius @".date('c', $time)."\n";
}
}
}
catch(NAClientException $ex)
{
echo "User does not have any devices\n";
}

?>

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 11 Nov 2012, 10:51

You forgot to set username/password item variable ...

$client->setVariable("username", $username);
$client->setVariable("password", $pwd);
Thomas Rosenblatt, Netatmo Team.

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 11 Nov 2012, 16:58

Tom,

I set those. I simply emptied them out so as not to share my credentials with everyone. If you want to verify them, please contact me directly.

trosenblatt
Posts: 233
Joined: 18 Sep 2012, 12:18

Re: Using the example PHP

Post by trosenblatt » 11 Nov 2012, 18:13

This is the variable name not your credentials !!!
Thomas Rosenblatt, Netatmo Team.

jtienor
Posts: 11
Joined: 27 Oct 2012, 03:14

Re: Using the example PHP

Post by jtienor » 11 Nov 2012, 18:17

They are set directly above those lines

Post Reply

Return to “Netatmo API”