LaMetric Firmware 1.6.0 and local push

UPDATE (08.08.2016): Apparently you can get your api key at https://developer.lametric.com/user/devices now.

With the LaMetric Time Firmware update 1.6.0 the API for local push notifications changed and my tutorial for the 1.5.0 firmware does’t work anymore. The new firmware offers the possibility to show notifications without installing an extra app.

For this to work you have to create an “notifications app” in the developer backend. This is similar to creating the old apps, but you don’t have to add it to your LaMetric Time. Go to the developer backend and create a new notifications app. Choose a name, enter a short description and set the redirect url to a dummy domain – e.g. http://dfgdgddfdfg.de (in this case, it doesn’t really matter, because we use this only for ourself). Then click “Save” and your app is ready. Get the “Client ID” from the info page of the app.

To communicate with the LaMetric Time in a local network we need the api key. With the 1.6.0 firmware this has to be obtained by getting an access token via OAuth2. With your client id at hand open the following URL in a browser after replacing the CLIENT-ID with your client id.

https://developer.lametric.com/api/v2/oauth2/authorize/?client_id=CLIENT-ID&response_type=token&scope=basic+devices_read+devices_write&state=STATE

This should open a website, asking you if you want to authorize your new app to access your LaMetric account. Click “ALLOW” and you should be redirected to the redirect url you entered before. If you used facebook.com, you probably see an error messsage, 404 site not found, but that’s ok. We’re only interested in the current URL – this should look like

https://developer.lametric.com/api/v2/oauth2/authorize/www.facebook.com#access_token=xyz&expires_in=7200&token_type=Bearer&scope=basic+devices_read+devices_write&state=STATE

Copy the access token part from the url – in the example above it’s “xyz”. Using this access token we can get the api key from our LaMetric Time with the following php script – again, replace the CLIENT-ID with your client-idaccess token from before. Make sure you run this script shortly after getting the access token, since it expires after few minutes.

<?php
$url = "https://developer.lametric.com/api/v2/users/me/devices";

$curl = curl_init();

$headers = array(
    "Accept: application/json",
    "Authorization: Bearer ACCESS TOKEN", 
    "Cache-Control: no-cache", 
);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 

$response = curl_exec($curl);

echo $response;
curl_close($curl);

?>

The result should look like

[
   {
      "updated_at" : "---",
      "api_key" : "123123123",
      "serial_number" : "---",
      "name" : "LaMetric",
      "id" : ---,
      "created_at" : "---",
      "state" : "configured",
      "mac" : "---",
      "ipv4_internal" : "123.123.123.123",
      "wifi_ssid" : "---"
   }
]

The two important parts are the api_key and the ipv4_internal. With those two things we can send notifications directly to the LaMetric Time. To use the api key, it has to be prefixed by “dev:” and the whole string needs to be base64 encoded – if you don’t know what this means, don’t worry, it’s easy to do:

1. api key from above: 123123123
2. new api key with prefix: dev:123123123
3. Copy the whole string and paste it on https://www.base64encode.org and click “ENCODE”: ZGV2OjEyMzEyMzEyMw==

This is your api key you need to use whenever you interact with the LaMetric Time. Using the following script you can see all available api endpoints on your device – meaning all the things you can interact with.

<?php
$url = "https://123.123.123.123:4343/api/v2";

$curl = curl_init();

$headers = array(
    "Accept: application/json",
    "Authorization: Basic ZGV2OjEyMzEyMzEyMw==", 
    "Cache-Control: no-cache", 
);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 


$response = curl_exec($curl);

echo $response;
curl_close($curl);

?>

The output of this script looks like

{
   "endpoints" : {
      "audio_url" : "https://123.123.123.123:4343/api/v2/device/audio",
      "concrete_notification_url" : "https://123.123.123.123:4343/api/v2/device/notifications{/:id}",
      "device_url" : "https://123.123.123.123:4343/api/v2/device",
      "widget_update_url" : "https://123.123.123.123:4343/api/v2/widget/update{/:id}",
      "notifications_url" : "https://123.123.123.123:4343/api/v2/device/notifications",
      "current_notification_url" : "https://123.123.123.123:4343/api/v2/device/notifications/current",
      "bluetooth_url" : "https://123.123.123.123:4343/api/v2/device/bluetooth",
      "wifi_url" : "https://123.123.123.123:4343/api/v2/device/wifi",
      "display_url" : "https://123.123.123.123:4343/api/v2/device/display"
   },
   "api_version" : "2.0.0"
}

To sent a notification use the following script as an example:

<?php
$url = "https://123.123.123.123:4343/api/v2/device/notifications";

$frames = array(
    "priority" => "info",
    'icon_type' => 'none',
    "model" => array(
        "cycles" => 0,
    	"frames" => array(
    		array(
                "icon" => "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAYAAADED76LAAAAUklEQVQYlWNUVFBgYGBgYBC98uE/AxJ4rSPAyMDAwMCETRJZjAnGgOlAZote+fCfCV0nOmA0+yKAYTwygJuAzQoGBgYGRkUFBQZ0dyDzGQl5EwCTESNpFb6zEwAAAABJRU5ErkJggg==",
    			"text" => "Hello"
    		)	
    	)
    )
);

$curl = curl_init();

$headers = array(
    "Accept: application/json",
    "Content-Type: application/json",
    "Authorization: Basic ZGV2OjEyMzEyMzEyMw==", 
    "Cache-Control: no-cache", 
);

echo json_encode($frames);

echo "\n";

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $url); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl, CURLOPT_POST, 1); 
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($frames)); 


$response = curl_exec($curl);

echo $response;

curl_close($curl);

?>

When you run this script, you should see a “Hello” notification that stays until you hit the Action button. To get more information about notifications (e.g. setting sound) have a look at the new documentation for firmware 1.6.0.

7
Leave a comment

avatar
4 Comment threads
3 Thread replies
1 Followers
 
Most reacted comment
Hottest comment thread
4 Comment authors
MirkoMarkus SteinerAlexuser1306 Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
trackback

[…] UPDATE: This doesn’t work with firmware 1.6.0 – see updated article here: LaMetric Firmware 1.6.0 and local push […]

user1306
Guest
user1306

first thank you very much for this tutorial much better than anything on La Metric website or forums.
could you please post an example of a notification with sound, please. i cant seem to figure it out
thanks in advance

Markus Steiner
Guest
Markus Steiner

Hey Alex, thanks a lot for this post, I’ve received my LaMetric device last night and it took me some time to make it work but it helped me a lot – I had some strange issues with php on my windows pc, so I switched to my Mac and that worked just fine. One thing I’ve noted is in your paragraph: Copy the access token part from the url – in the example above it’s „xyz“. Using this access token we can get the api key from our LaMetric Time with the following php script – again, replace the… Read more »

Mirko
Guest
Mirko

Hello Together,
great work, this blog and the posts are very helpfull. Huge thanks to all! I have recieved my LaMetric Time two days ago and updated the device to firmware 1.7.2. To send notifications successfully to the device it was necessarry add “curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);” to the cURL header.