Dec.06

Push Notification from Arduino Yún with motion sensor (Part 2/2)

Supposedly, you’ve already filled your theoretical knowledge gaps and prepared hardware after reading the first part. Before we move on to the essence, I just want to mention that while I was writing this part, a few questions arised that most likely would’ve stayed unanswered without Ivan‘s input – thanks (officially 🙂 ) for being so helpful!

Ok, what are we doing again?

We are going to use Temboo’s client (preinstalled on every Yún) to communicate it with API of Parse.com – a mobile backend – which will then send a push notification to Android device. As it states on the official site “Temboo makes it easy for the Arduino Yún to connect to over 100 web-based resources and services (e.g. Facebook, Dropbox, US Census data) by standardizing how you interact with their Application Programming Interfaces (APIs)”. To be more specific, it makes it easy to call these external API’s with smart code snippets called Choreos (all of Choreos are listed here).


 

Image source: http://blog.parse.com/learn/engineering/playing-with-arduino-temboo-and-parse/

 

Note: You can also call Parse API without Temboo’s Choreos – using the official Arduino Yun Parse SDK.  I’ve decided to describe Temboo as it seems to be much more universal, but if your project is anything more than hobbyist experiment you need to know that Temboo gives you only 250 api calls per month for free (for me, it looks like they’re enforcing usage of their referral plan) so just keep in mind that Parse’s free plan seems much better.

Challenge accepted

Ok, firstly we need to obtain API keys. Register your app on Parse.com and open it’s Settings > Keys. You will need Application ID and REST API Key.

I've blurred out keys for example project, I must have missed Photoshop.

I’ve blurred out keys for example project, I must have missed Photoshop.

 

Take a look at getting started with Temboo or just follow these strictly goal-directed steps:

Zrzut ekranu 2015-12-02 02.22.06

Noone cares about blurring, right.

{
  "data": {
    "channel": "yun",
    "alert": "Quiet! I think something is moving there...",
    "sound": "",
    "badge": "Increment"
  },
  "where": {
    "deviceType": "android"
  }
}

You can also view your sent pushes in Parse (in Push section).

 

Note: Don’t miss the fact that the channel is defined to “yun” which means that if any device wants to receive this push it needs to be subscribed to this channel.

Very important little differences

What you’ve copied from Temboo is prepared to be executed as an *.ino file. As you might have noticed, I remain completely unconvinced about using Arduino IDE, it’s serial monitor or even *.ino files. Let me bring out the little differences between *.ino and *.cpp files.

 

Arduino Sketch File (*.ino) is ‘almost’ valid C code, except it:

  • Does not need functions prototypes
  • Does not need #include “Arduino.h” preprocessor directive

‘Almost’ valid is not valid enough for C compilator, so let’s repair what was generated (and keep it a *.cpp file).

#include <Arduino.h>
#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // Contains Temboo account information

// The number of times to trigger the action if the condition is met
// We limit this so you won't use all of your Temboo calls while testing
int maxCalls = 10;

// The number of times this Choreo has been run so far in this sketch
int calls = 0;

int inputPin = 7;

void runSendNotification(int sensorValue);

void setup() {
    Serial.begin(9600);

    // For debugging, wait until the serial console is connected
    delay(4000);
    while(!Serial);
    Bridge.begin();

    // Initialize pins
    pinMode(inputPin, INPUT);

    Serial.println("Setup complete.\n");
}

void loop() {
    int sensorValue = digitalRead(inputPin);
    Serial.println("Sensor: " + String(sensorValue));

    if (sensorValue == HIGH) {
        if (calls < maxCalls) {
            Serial.println("\nTriggered! Calling SendNotification Choreo...");
            runSendNotification(sensorValue);
            calls++;
        } else {
            Serial.println("\nTriggered! Skipping to save Temboo calls. Adjust maxCalls as required.");
        }
    }
    delay(250);
}

void runSendNotification(int sensorValue) {
    TembooChoreo SendNotificationChoreo;

    // Invoke the Temboo client
    SendNotificationChoreo.begin();

    // Set Temboo account credentials
    SendNotificationChoreo.setAccountName(TEMBOO_ACCOUNT);
    SendNotificationChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    SendNotificationChoreo.setAppKey(TEMBOO_APP_KEY);

    // Set Choreo inputs
    SendNotificationChoreo.addInput("RESTAPIKey", "o9SchesGIcheskuechesK9jchesEKsqchesrM8zT");
    SendNotificationChoreo.addInput("ApplicationID", "zchesus6Lchesaq39ches1HchesuVOchessltTB6FS");
    SendNotificationChoreo.addInput("Notification", "{\n  \"data\": {\n    \"channel\": \"yun\",\n    \"alert\": \"Quiet! I think something is moving there...\",\n    \"sound\": \"\",\n    \"badge\": \"Increment\"\n  },\n  \"where\": {\n    \"deviceType\": \"android\"\n  }\n}");

    // Identify the Choreo to run
    SendNotificationChoreo.setChoreo("/Library/Parse/PushNotifications/SendNotification");

    // Run the Choreo
    unsigned int returnCode = SendNotificationChoreo.run();

    // Read and print the error message
    while (SendNotificationChoreo.available()) {
        char c = SendNotificationChoreo.read();
        Serial.print(c);
    }
    Serial.println();
    SendNotificationChoreo.close();
}

Note: To be perfectly honest ‘Arduino.h’ is already included in both Bridge and Temboo, but we need to work on good habits, right?

 

Note: If you would decide to go with *.ino – PlatformIO is also able to handle *.ino files and automatically adds what’s mentioned above at build time before compilation phase!

One more thing…

We also need to include directories with added libraries (Bridge, Temboo) to build path (CMakeLists.txt). PlatformIO handles this too, all we need to do in this case is reiniting the project:

platformio init --ide=clion

Reload the project in CLion and notice newly generated lines:

...
include_directories("$ENV{HOME}/.platformio/packages/framework-arduinoavr/cores/arduino")
include_directories("$ENV{HOME}/.platformio/packages/framework-arduinoavr/libraries/Bridge/src")
include_directories("$ENV{HOME}/.platformio/packages/framework-arduinoavr/libraries/Temboo/src")
include_directories("$ENV{HOME}/.platformio/packages/framework-arduinoavr/libraries/Temboo/src/utility")
include_directories("$ENV{HOME}/.platformio/packages/toolchain-atmelavr/avr/include")
...

Now is the time to build & upload, connect serial port monitor and test if pushes are successfully sent through Parse. Then, once you’ve got it all set up..

Android time

 
Create a new project in Android Studio and edit your applications build file (/app/build.gradle) to add Parse’s SDK.

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.parse.bolts:bolts-android:1.+'
    compile 'com.parse:parse-android:1.+'
}

Modify AndroidManifest.xml file in reference to mentioned Parse’s guide (especially remembering about changing the package name!).

 

Open MainActivity.java and add these lines at the end of an onCreate() method (with your api keys):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    Parse.initialize(this, "o9Sg9PcheseLHygK9jKLIPEKsq4GFyrM8zT", "XIxCJLpclTfqQ3Pwwcp5OKchesecMwybnhsnIEM");
    ParseInstallation.getCurrentInstallation().saveInBackground();
    ParsePush.subscribeInBackground("yun", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if(e==null)
                Toast.makeText(MainActivity.this, "Succesfully subbed to yun channel!", Toast.LENGTH_LONG).show();
            else
                Toast.makeText(MainActivity.this, "Couldn't sub - check internet connection, api keys and AndroidManifest.xml!", Toast.LENGTH_LONG).show();
        }
    });
}

Run it on your device (with enabled developer options) and check on Parse (Core > Data > Installation) if it subscribed successfully. If so, you should receive push notifications from your Yún!

You don't need to write a simple line of code to view this on watch, #justsayin.

Android Wear device (like my G Watch R) is able to view notifications, no additional code was needed.

Arduino,Programming,Android
Share this Story:
  • facebook
  • twitter
  • gplus

Leave a comment

Comment