How to get POST to work on App Inventor communciating with Spark Core Wi-Fi module

506 views
Skip to first unread message

J M

unread,
Mar 5, 2014, 9:14:12 PM3/5/14
to mitappinv...@googlegroups.com

I am trying to create an Android app to interact with an embedded application running on a Spark Core Wi-Fi module.  The Spark Core is a Wi-Fi module that runs Arduino code on its included ARM processor (see https://www.spark.io).  

Communication to this module is done via the "Spark Cloud", a site that acts as an intermediary between your web-based app and the embedded program running on the ARM core.  It securely passes GETs and POSTs between your app and your specific Spark Core module using the ID of your specific Spark Core, and your private authentication token (both of which you get as part of connecting your core to Spark Cloud).

I have been able to use App Inventor's “call web.Url” and "when web.GotText" components to get data off of the Spark Core in combination with Spark's recommended GET:

https://api.spark.io/v1/devices/[your specific Core ID]/[Variable holding data to be returned]?access_token=[Your authentication ID]  

But I have not been able to get the POST to work.  The Spark Core POST includes a function name that will be called on the ARM, and a string to be passed to that function. A set of data is returned.  Their recommended POST syntax is:

https://api.spark.io/v1/devices/[your specific Core ID]/[function to be called] -d access_token=[Your authentication ID] -d "args=[string to be passed]"

If executed utilizing curl in a command window on windows, both GET and POST work fine.  So how to get this POST to work on App Inventor?

Hopefully this is not a dumb question - I have written many Arduino programs but am not that knowledgeable on web programming like GET and POST. I tried to use the "call web.PostText" component, putting the above POST into the text box.  But I get an "Error 1109 - the specific URL is invalid:".  

Any thoughts or pointers to helpful sites appreciated.  Found nothing on the Spark site.

phantomfoot

unread,
Mar 6, 2014, 6:16:52 AM3/6/14
to mitappinv...@googlegroups.com
Can't really help that much I'm afraid, but the -d flag is curl, and as the error says, the below is an invalid URL with those -d flags in it.

https://api.spark.io/v1/devices/[your specific Core ID]/[function to be called] -d access_token=[Your authentication ID] -d "args=[string to be passed]"

You could try putting the access_token and args in BuildRequestData.

text BuildRequestData(list list)
Converts a list of two-element sublists, representing name and value pairs, to a string formatted as application/x-www-form-urlencoded media type, suitable to pass to PostText.


Here's an example of a POST request I did in AI classic, if it helps:

Taifun

unread,
Mar 6, 2014, 9:37:12 AM3/6/14
to mitappinv...@googlegroups.com
my suggestion to try is:

web.url = web.UriEncode https://api.spark.io/v1/devices/[your specific Core ID]/[function to be called] -d access_token=[Your authentication ID] -d "args=[string to be passed]"

web.postText = <empty string>

if this does not work, you might want to provide a screenshot of your relevant blocks
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

Scott Ferguson

unread,
Mar 6, 2014, 9:50:12 AM3/6/14
to mitappinv...@googlegroups.com
My suggestion is to try using phantomfoot's blocks where the first embedded make a list block (name:value pair) block contains two items: 'args' as the first item  and the data to be passed as the second item
---
Message has been deleted

J M

unread,
Mar 6, 2014, 12:07:14 PM3/6/14
to mitappinv...@googlegroups.com

Thanks for ideas, especially pointing out that -d is just for curl. Obviously not going to work with that in there.

 

I attached the blocks in attachment as suggested (the Spark Core ID and API Token hidden in the red boxes).  I may be in over my head.  Will see if I can implement any of the suggestions. And will post similar question on the Spark Core forum - thought my app issue was with use of App Inventor but it may be more likely that it is how to make the proper POST to Spark Cloud.. Thanks again.

AI2_Blocks.JPG

J M

unread,
Mar 6, 2014, 1:50:38 PM3/6/14
to mitappinv...@googlegroups.com
For reference, after more searching, found this set of posts that addresses this question directly.
 
 

phantomfoot

unread,
Mar 6, 2014, 8:04:37 PM3/6/14
to mitappinv...@googlegroups.com
Perfect! So it looks like our guess was correct, you need to put access_token and args in BuildRequestData.
Reply all
Reply to author
Forward
0 new messages