Blink Cameras and Presense detection

Tags: #<Tag:0x00007f5c8c0247d0> #<Tag:0x00007f5c8c024668> #<Tag:0x00007f5c8c024528>

In my house me and my wife have phones, OH already checks them thru WiFi and Bluetooth and with this detects if we are at home. Recently I bought a set of Blink Cameras but i couldn’t find any add-on for OH that gives full control on this Cameras.

The only thing I needed was that OH detected me or my wife are at home and disarm (disable) the cameras, or, arm them if both of us are out.

Searched online for a solution and found this REPO: https://github.com/fronzbot/blinkpy
With a bit of POSTMan and some REST POSTs and GETs later I finally found out the final CURLs that needed to be executed.

First you need to use this CURL to get the SECRET_TOKEN, replace YOUR_SECRET_PASSWORD and EMAIL_THAT_YOU_USE_IN_THE_BLINK_APP with you own information:

curl -X POST \
  https://rest.prod.immedia-semi.com/login \
  -H 'Content-Type: application/x-www-form-urlencoded' \
  -H 'Host: prod.immedia-semi.com' \
  -H 'cache-control: no-cache' \
  -H 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
  -F 'password=[YOUR_SECRET_PASSWORD]' \
  -F 'client_specifier=iPhone 9.2 | 2.2 | 222' \
  -F email=[EMAIL_THAT_YOU_USE_IN_THE_BLINK_APP]

Then you will need to get the NETWORK_ID and CAMERA_ID, to do that use this CURL:

curl -X GET \
  https://rest-e001.immedia-semi.com/api/v3/accounts/[ACCOUNT_ID]/homescreen \
  -H 'Host: prod.immedia-semi.com' \
  -H 'TOKEN_AUTH: SECRET_TOKEN' \
  -H 'cache-control: no-cache'

Now use that info in this CURL to Enable the camera:

curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/enable -H 'Host: prod.immedia-semi.com' -H 'TOKEN_AUTH: [SECRET_TOKEN]' -H 'cache-control: no-cache'> /dev/null 2>&1

and this one to Disable the camenra:

curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/disable -H 'Host: prod.immedia-semi.com' -H 'TOKEN_AUTH: [SECRET_TOKEN]' -H 'cache-control: no-cache'> /dev/null 2>&1

Now you just need to create one script for enable and other for disable in the folder Scripts, make them executable. Also, if you have more then one camera, put 5s sleep in the middle of the CURL calls, this is to avoid API to refuse your connections.

== My own Script ==

#!/bin/bash
curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/disable -H 'Host: prod.immedia-semi.com' -H 'TOKEN_AUTH: SECRET_TOKEN' -H 'cache-control: no-cache'> /dev/null 2>&1
sleep 5s
curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/disable -H 'Host: prod.immedia-semi.com' -H 'TOKEN_AUTH: SECRET_TOKEN' -H 'cache-control: no-cache'> /dev/null 2>&1
echo "Blink - Cameras Disabled"

And the last bit is to add the execution of this scripts in wherever place you need, just by doing something like this:

val results = executeCommandLine ("/srv/openhab2-conf/scripts/disable_blink.sh",10000)
val results = executeCommandLine ("/srv/openhab2-conf/scripts/enable_blink.sh",10000)

And to finalize, here’s a little demo video showing this working: https://www.dropbox.com/s/1zdpgzhuihp6a4e/blink.mp4?dl=0

Enjoy and hope this helps someone.

1 Like

It looks like after a while the token dies. Let’s update the bash script and with a new PHP file.

I’m not a master Jedi at bashing, I’m more a PHP guy, that’s why the PHP is in here, but if you know how to do it in bash, share with me :slight_smile: and make this solution a bit more simple.

This is assuming that you have a PHP service running on your Pi.

== My new Script ==

#!/bin/bash
touch token.txt
tokenValue=$(cat token.txt)
code=$(curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/disable -H 'Host: prod.immedia-semi.com' -H "TOKEN_AUTH: ${tokenValue}")

if [[ $code == *"Unauthorized"* ]]; then
    sleep 5s
    cd /srv/openhab2-conf/scripts/; /usr/bin/php gettoken.php
    tokenValue=$(cat token.txt)
    curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/disable -H 'Host: prod.immedia-semi.com' -H "TOKEN_AUTH: ${tokenValue}"> /dev/null 2>&1
fi
sleep 5s
curl -X POST https://rest-e001.immedia-semi.com/network/[NETWORK_ID]/camera/[CAMERA_ID]/disable -H 'Host: prod.immedia-semi.com' -H "TOKEN_AUTH: ${tokenValue}" -H 'cache-control: no-cache'> /dev/null 2>&1

echo "Blink - Cameras Disabled"

== My PHP file to get the new token ==

$email=[EMAIL_THAT_YOU_USE_IN_THE_BLINK_APP]
$password=[YOUR_SECRET_PASSWORD];
$client="iPhone 9.2 | 2.2 | 222";

$headers = array(
    "Content-Type: application/x-www-form-urlencoded",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    "Host: prod.immedia-semi.com"
);
$data = "password=".$password."&email=".$email."&client_specifier=".$client;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://rest.prod.immedia-semi.com/login");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
$response = json_decode($result, true);
file_put_contents("token.txt", $response['authtoken']['authtoken']);