Custom Android App

Hi folk,

Just got my core recently & started digging - looks like fun.

I’ve gone through a few of the basic ‘hello world’ tests & so far so good.

What I would really want to do is have a customised mobile app control a custom build.

I’m really not much of an Android dev (working on getting my Python on), so I’m looking at MIT App Inventor as a solution to build some PoC app.

As such, I’ve started digging at the API docs. What’s “odd” is that is seems that all call happen via the internet & not directly with the node, meaning that my core needs to be always connected to be useful, whereas I plan on using it ‘off-line’ - or rather on an isolated network inaccessible from the 'net (almost air-gapped). Could this be correct?? Will this be addressed?

OK - accepting the above (for now) - I’m having a spot of trouble sending control signals from my App Inventor App to the node.

I send the following URL: (or something along these lines)

curl "https://api.spark.io/v1/devices/$CORE_ID/digitalwrite&?access_token=$TOKEN&params=D7,HIGH"

& get the following in response:

{
  "cmd": "VarReturn",
  "name": "digitalwrite&",
  "TEMPORARY_allTypes": {
    "string": " \u0000P\u0000",
    "uint32": $REDACTED,
    "number": $REDACTED,
    "double": null,
    "float": 1.0868491504456741e-19,
    "raw": " \u0000P\u0000"
  },
  "result": " \u0000P\u0000",
  "coreInfo": {
    "last_app": "",
    "last_heard": "2014-01-06T11:20:59.119Z",
    "connected": true,
    "deviceID": "$deviceID"
  }
}

Looks like the connection goes through, but nothing happens.

I’m still fidling with App Inventor, so if anyone else has had luck controlling the device from there, use/tips would be appreciated, otherwise if anyone else could kindly point me in the right direction, that would be greatly appreciated.

Hi @kieppie - regarding your question about offline acess, we do intend to make it possible to work with the Spark Core without it being connected to the internet, but that feature is still in development.

As for your request, it looks like you are sending a GET request, where the request you’re trying to do (digitalwrite) is actually a POST request.

Expanding on zach’s reply:

Try:
curl "https://api.spark.io/v1/devices/$CORE_ID/digitalwrite -d access_token=$TOKEN -d params=D7,HIGH"

Thanks for the feedback, @zach & @BDub.

@zach - looking forward to the non-net-connected release - seems a little silly to run potentially critical/vulnerable systems 'net-facing, especially outside of persistent wifi-range.

@BDub - did find that info via API docco’s, worked OK from CLI, but was unable to build POST request as per suggestion of @zach.

But I have managed to construct my POST from App Inventor @ 3am (took some digging & loads of trial & error) - so now I am able to do some basic IO (not dissimilar to the Spark mobile app), and can start looking at building both “client & server” using Blockly-based IDE :smile:

I note that the Spark IDE is based on Ace, a variant of the Cloud9 IDE (loaded this on a RasPi for playing with Minecraft via Python), & have been looking at integrating Blockly somehow.

/me think a funky build could be something like a little RC car with a Spark ‘brain’ controllable from MineCraft & Blockly/c9 form mobile/tablet :smile:

@kieppie What did your POST statement end up looking like? I just got my CORE and can’t get the POST request right in AI.

Thanks

@jdmaharrey - I’m assuming you mean in the context of the MIT App inventor?

I had to jump a few hoops - getting it to POST rather than GET was the trickiest part.

The POST code/blocks looks looks something like this:

Overall it worked OK, but I had some weird disconnectivity issues whenever I raised a state that I guess could’ve cause an error or exception

2 Likes

kieppie
would you mind posting the MIT project for downloading so I can see how you got it to post?

@kieppie I can’t seem to reproduce your result with my POST request. I would appreciate you pointing to some of the resources you used to get your result.
Thanks

@chrisnet - I can’t see how to post/share App-inventor work. If you know how I’d be happy to.

@jdmaharrey - honestly can’t recall where I found the info. I think managed to piece it together from scraps I found, some of which were form various google groups & other discussion fora & a lot of late-night trial & error.

OK - here you go

MD5 checksum: 06ee75310879608ce866bb7031a05e1a

Be sure to populate the textboxes with your device’s Device ID & Access Token.

This is only my 1st attempt at this (I’m not an Android dev), so it’s a little rough around the edges - only supposed to be a PoC.

To pull it in, see: Can I share project code with other App Inventor users?

(feedback welcome)

@kieppie Thanks to you I have my app controlling my core very reliably (I haven’t tried analog pins yet). I could not get the same code to work in App Inventor 1, but it works quite well in App Inventor 2.

glad I could help :slight_smile:

Kieppie:

I tried your code on App Inventor 2 and it works as advertised. I am not a Java dude so the AI 2 looks like the way to go for me. Now to figure out how to read data from my core with AI 2… should be interesting.

thanks for sharing your hard work!

David

@sierrasmith71 You might have figured this out already, but here is a quick example of how I display a temp from my spark core. If anyone has a better way I’d love to see.

@kieppie – I used your App Inventor 2 POST blocks and got my application to successfully POST to my Spark Core – would not have figured out what you did so thank you for the discovery work. And being able to use App Inventor made it trivial to create an app, especially since I am not skilled with Java.

I am writing because while the blocks work (I am using them exactly as you did), they frequently generate an error as follows:

Error 1103: Unable to post or put the text “access_token=xx…my_API_token…xxx&params=128V” with the specified URL:https://api.spark.io/v1/devices/xx...my_Core_ID...xx/function_to_be_called

(Also have picture of it attached with API token and Core ID blocked out. The 128V is the string I am passing).

This might happen three times in a row, or just once intermittently. I have done 30 POSTs in a row with no issue and then I get this error (doing the same exact POST).

Did you (or anyone else) run into this error? I have tried 2 different Spark Cores, 2 different Android devices (Note 2 running Android 4.3 and Venue 8 running Android 4.3 ) and 3 different Wi-Fi networks. Get the same intermittent error. That points to either App Inventor 2 or the Spark Cloud I suppose.

Any help appreciated.

UPDATE: Earlier version of this post said communication between android app and spark core occurred sometimes even when this error occurred. After a lot of testing I could not repeat this so am assuming it is not the case. If there is an error dialog, there is no communication.

Answer was provided by phantomfoot on App Inventor forum - Error 1103 indicates internet is not available. And thus is easy to see why it is intermittent. His solution within App Inventor 2 works perfectly. From his post on App Inventor forum:

++++++++++++++++++++++++++
Error 1103 is no internet connection.
https://groups.google.com/d/msg/programming-with-app-inventor/mc3YBXbpQ1s/SklzSh17Qp0J

That would explain why it is intermittent.

So one way to program for this is:

Catch the error in Screen1.ErrorOccurred.

If errorNumber = 1103,
notifier1.ShowAlert(“No internet connection detected. Retrying…”)
then call POST_Call_Function again. (It will then keep repeating calling this function until it connects).

Hi, I tested your POST function in Android APP made in Appinventor according to Kieppie’s project posted here, but unfortunately my Spark core doesnt do anything. Is there any way how to check what value does receive the Spark API from the App…or ony other solution to my problem?
Recently I made my Spark work, working with a web application found here : https://github.com/technobly/Simple-Spark-Core-Controller
And it works just fine (so I guess the core can read the data sent from browser)
I think my problem is that the Application I made in appinventor according to the project , doesnt send out correct data, or…Who knows?(I have added my own Spark ID and API key to the code offcourse…)

Anybody could help?

Hi,
just in case you want an Android app from the Google Play Store: