element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Enchanted Objects
  • Challenges & Projects
  • Design Challenges
  • Enchanted Objects
  • More
  • Cancel
Enchanted Objects
Blog Smart Key Hooks Client
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: crjeder
  • Date Created: 20 Apr 2015 1:13 PM Date Created
  • Views 1008 views
  • Likes 3 likes
  • Comments 9 comments
  • smart_key_hooks
  • enchanted_objects
Related
Recommended

Smart Key Hooks Client

crjeder
crjeder
20 Apr 2015

I started my "career" in electronics not long ago when the first Raspberry PI became available. But looking on the specs I decided that the BeagleBone Black suits my needs better and bought one. What I liked most about it after playing with it for a while was the c9 IDE and node.js. Therefore I decided to do the programming for this challenge in node.js, too.


The Cloud


As the cloud service I have chosen Pushbullet because it has:

  1. an easy, restfull API
  2. clients for all OSes
  3. integration in Tasker

Downside is: It does not offer end to end encryption. Therefore I had to Implement it. Since I do not have a lot of clients pre-shared key works well and is used in the implementation. The Pushbullet API is abstracted in a node.js module for even more convenience.

Provisional Client

The client consists of two parts, Tasker and a javascript code. Tasker is "Total Automation for Android" (Tasker website) a tool I use to automate many task on my Android devices. It is much like IFTTT but on the device not in the cloud. For security and privacy reasons "on device" is the preferred solution. What can Tasker do? Again citing from the website:

Tasker is an application for Android which performs tasks (sets of actions) based on contexts(application, time, date, location, event, gesture) in user-defined profiles or in clickable or timer home screen widgets.

With the Pushbullet integration every message received by the Pushbullet client a Tasker event is triggered. This can be used to start a task like launch a specific app or play a ring tone. Unfortunately the message content can not be accessed from the event. This would be necessary to start different tasks on different status changes of keys. To the rescue one of the available tasks is "code" which can run javascript programs. Let us step through the relevant portions of the javascript (nodejs):


var aes = require('crypto-js/aes');
var PushBullet = require('pushbullet');
var pusher = new PushBullet('xxxx'); //replace this with your token


this code snippet imports aes encryption from crypto-js and the nodejs libraray for Pushbullet. The last line instances and initializes the Pushbullet API. The returned object ('pusher') can be used to talk to the cloud. To initialize you need an application token which you can find in your account settings. This is a very weak authentication Pushbullet supports OAuth, too but I haven't tried it yet.


var options = {limit: 100};

 

For the Pushbullet API we must set a limit to the maximal returned pushes to prevent memory overruns etc.

Then call the search for the last message using the history function:

 

pusher.history(options, function(error, response)
{
    if(error) throw error;
    if(response.pushes.length <= 0) throw 'Error: no pushes';

    var i = 0;
    do
    {
        if (response.pushes[i].active && (response.pushes[i].sender_name == 'IoT-test' || response.pushes[i].receiver_email == '@ljkawsfowe') && response.pushes[i].type == 'note')
        {
            //var message = 'U2FsdGVkX1+Zls89vOpjlkXCcUQhDQrBEzZoH+3iuhU=';
            var message = response.pushes[i].body;
            if (message)
            {
                var decrypted = aes.decrypt(message, "geheim");
                var mask = 1;
                // works for Max_KEYS < 16
                for (var i = 0; i < MAX_KEYS; i++)
                {
                   setGlobal("Key" + i, ((decrypted.words[0] & mask) != 0)?"true":"false");
                   mask = mask << 1;
                }
          
                pusher.deletePush(response.pushes[i].iden);


            }
            break;
        }
        else
            i++;
    } while (i < options.limit);

    // ToDo: Error: no push found
});

 

Line 3 & 4 are for error handling. Then setup the loop for searching the response for through the past messages for one from the controller. I have set up a 'channel' for this purpose. In pushbullet channels are private by default. That's good! Depending on the interface used to send the push the sender_name or receiver_email are set. I was not able to figure out if this is a bug or if there are other differences I am not able to see. In response.pushes[i].body there should be a base 64 encoded AES encrypted message of one block. In line 15 the 'decrypted' object contains the block in 16 bit integers. The for loop in lines 18 - 22 converts the one bit status into individual Boolean variables. In line 20 a tasker API function is used to set android environment variables. The (decrypted.words[0] & mask) != 0)?"true":"false" makes use of the 'conditional operator' which is defined as:

 

variablename = (condition) ? value1:value2 
variablename = value1 // if condition = true
variablename = value2 // if condition = false


Therefore the statement in the client code could have been written as:


bitvalue = decrypted.words[0] & mask;
if (bitvalue > 0)
     bit_bool = 'true';
else
     bit_bool = 'false';


Which is more readable, by far, but obiously less compact.

So if the decrypted message starting whith 00000101b the code inside the for loop (line 18 - 22) sets the following global variables:


Key1 = 'true';
Key2 = 'false';
Key3 = 'true';
Key4 = 'false';
Key5 = 'false';
Key6 = 'false';
Key7 = 'false';
Key8 = 'false';


This values can then be used by other Tasker actions.


Line 24 deletes the message as it is processed now.


The provisional Client lacks some functions I've proposed and testing. But I'd like to share it because the development takes me to long to and I do also value your comments which have been always very useful in the past.

  • Sign in to reply

Top Comments

  • crjeder
    crjeder over 10 years ago in reply to Jan Cumps +2
    I am cheating here: I use BeagleBone Black while testing. When it all works I'll port to SAMA5D4.
  • crjeder
    crjeder over 10 years ago in reply to Workshopshed +1
    Workshopshed I added a paragraph in the description for this statement as it is not self explanatory. Sorry, I forgot to do this in the first place.
  • clem57
    clem57 over 10 years ago +1
    Python is already loaded on the SAM Yocto release. node.js is not. They do have a lightweight HTTP webserver tho. Clem
Parents
  • Workshopshed
    Workshopshed over 10 years ago

    The principle looks good, clever use of tasker

     

    setGlobal("Key" + i, ((decrypted.words[0] & mask) != 0)?"tue":"false");


    should that be "true" and "false" ?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • Workshopshed
    Workshopshed over 10 years ago

    The principle looks good, clever use of tasker

     

    setGlobal("Key" + i, ((decrypted.words[0] & mask) != 0)?"tue":"false");


    should that be "true" and "false" ?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • crjeder
    crjeder over 10 years ago in reply to Workshopshed

    Workshopshed

    I added a paragraph in the description for this statement as it is not self explanatory. Sorry, I forgot to do this in the first place.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube