Pushing events to mobile app from electricImp agent

I am trying to build an interactive mobile app “connected” to an electricImp device.
Events can flow in both directions (from app to device and viceversa).
I have read The interactive imp: how to manage communication between app, agent and device and I understand how to handle interactions started/pulled from the mobile app. However I have not seen any interactions started/pushed form the device (e.g. when the device is switched on and connects to its agent, an event is pushed to the mobile app to wake it up; the mobile app does not need to be running on the mobile device)
Is this scenario feasible with current agent implementation? Any hints?

Thanks!

As I understand it, this is a limitation of mobile devices, not the Imp. The Imp can respond to a request made from a mobile device, but there is no way to initiate communication from the device to the mobile phone. This communication will be blocked by the carrier.

You might want to look at pubnub.com
I’ve used it before on a mobile web app but they have SDK for iOS & Android.
Revolv home automation platform uses it for real-time comms.

REST API https://www.mashape.com/pubnub/pubnub-network#!documentation
Ignore their electric imp SDK it’s in C not Squirrel

As jwehr notes, generally phones do not have public IP addresses - they are behind carrier NAT - and hence they can only make outbound connections.

This is why push services are provided by both Apple and Google (and presumably, Microsoft and RIM too). You can use these directly, but there are services - like pubnub as controlcloud suggested - which make it a lot easier to deal with.

Thanks a lot Hugo, controlCloud, jwehr!

I know the restrictions/features of mobile IP connections and the push services of the different platforms. I was wondering whether electricImp had integrated them in its cloud server/API. I will check how pubnub fits my needs (right now I am only looking for iOS and Android support)

@beardedinventor Matt any chance you could reach out to Pubnub and ask them them to create a proper Squirrel SDK or remove e.imp form their list “IoT Embedded” SDK as it’s a link to C lib.

Thanks.

@controlCloud - I’ll reach out… I know one or two people in the office have looked at PubSub before… we might even have some code kicking around (no promises, it may have just been a high level view).

Matt thanks on both fronts…

No code - but we’re going to reach out to them :slight_smile:

@controlCloud - Whipped up a basic PubNub library this afternoon :slight_smile:

https://github.com/electricimp/reference/tree/master/webservices/pubnub

If you’re interested, I’d love to chat about how you plan to use it (to make sure the required functionality is implemented). This is especially true if you’re looking at it for something commercial - let me know if you’re interested…

Hmmm… a Vanessa epaper display could get information from my phone and then display it…

@beardedinventor Matt a big thanks for this won’t have time till next week to try.

Why I want to use PubNub. Fragmented user experience & guaranteed message delivery

I’ve got my imps/agents talking to my own backend solution. I build mobile web Apps for each project and use Pushover/Twillo for notifications & alerts. So a user has to use two different Apps I never like this.

What I want to get built is a multi platform mobile App that combines web Apps & notifications. This would allow me/anyone to build custom solutions using HTML5 and still have real-time notifications in native container.

Also I want robust messaging delivery so I need some form of store and forward for when folk are offline. I don’t want to build this and with PubNub you have common API across all mobile devices and desktop. The only gotcha is iOS where you need to APN when you App is in the background etc. Also I want to integrate imps/RPI/automation devices in multiple locations.

Other contenders Firebase but that’s $600 a year vs $180 for Pubnub when you want to scale. And there is Skynet.im thanks for that one but it’s early days with those guys.

Lawrence

I have put some time to read through the documentation of GCM, Google Cloud Messaging API/server (http://developer.android.com/google/gcm/index.html).

GCM allows a bidirectional communication between an Android device/app and a server. I am using it to push data “directly” from an electricImp Agent to an Android app installed in my phone.

Basically you need to register a new project at google API server (you will get a project number and an API key) and obtain a registration_id from your mobile app (to get an id, you need your device to have a google account).
Once you have the API-key and the registrationId, you can start pushing messages to the device/app through an http POST API. More info can be found in this tutorial: http://hmkcode.com/android-google-cloud-messaging-tutorial/

An example http request (without key/id values):

`POST https://android.googleapis.com/gcm/send HTTP/1.1
Authorization: key=XXXXXXXXXXXXXX
Content-Type: application/json
Host: android.googleapis.com
Content-Length: 272

{“data”:{“Intensity”:100,“Color”:{“Blue”:0,“Green”:0,“Red”:255}},“registration_ids”:[“YYYYYYYYY”]}`

My squirrel code to generate this request from an electricImp agent:

`const GCM_URL = “https://android.googleapis.com/gcm/send”;
const jsonStringA= “{“data”:{“Intensity”:100,“Color”:{“Blue”:”;
const jsonStringB= “,“Green”:”;
const jsonStringC= “,“Red”:”;
const jsonStringD="}},“registration_ids”:[“YYYYYYYYY”]}";

local previousValue=256;

function postReading(potString) {
server.log("New Reading: " + potString);
if (previousValue!=potString) {
previousValue=potString;
local red=0;
local green=0;
local blue=0;
if ((potString/128)<1) {
local tmp=potString%128;
red=tmp2;
green=255-red;
} else {
local tmp=potString-128;
blue= tmp
2
red= 255-blue;
}
local body = jsonStringA + blue + jsonStringB + green + jsonStringC + red + jsonStringD;
local request = http.post(GCM_URL, {“Authorization”:“key=XXXXXXXXXXXXXX”,“Content-Type”:“application/json”}, body);
local resp = request.sendsync();
server.log("HTTPResponse: " + resp.statuscode + " - " + resp.body);
} else {
server.log(“POT value unchanged. No push send to GCM server”);
}
}`

VERY EASY and works perfectly! But the app costs money.

For a personal project, I think I’d use Electric Imp’s Twitter library and Tweet notifications. The Twitter app presents the notifications on your mobile device, and it’d work especially well if the notification account is the only one you’re following! It’s free too.