Connect Spark core with Android App made in Appinventor

Hi, does anybody have any experience with controlling a Spark core pins with custom made Android App?
I am thinking of a Door Lock, as a remote controll based on a phone.
I click one single button, and the Spark core will set a HIGH output on D0 for 10seconds for example.
I know this can be done somehow with the standard Spark Core app, which is on Google play, but I am thinking of one single large button, with no settings, no extra clicks…a custom made Appinventor App.
I did some basic Appsin Appinventor 2 allready, and it is really easy, but I have no idea about the Spark thing : what to call to controll the Spark, what is the syntax , What code should have the spark to listen to the commands…etc.
Anybody could help?
Regards.
Jozef

Appinventor is awesome. I have used it to make a custom app to control spark core.
On the spark core side, you would be writing functions that get exposed through the spark cloud. These functions are called when a matching HTTP POST request is made throught the API. Try with a simple example first, like LEDs over the internet. Then, look up the documentation here:
http://docs.spark.io/firmware/
On the app inventor side, you will have to get a web object to execute HTTP POST request. You probably already know this but I am gonna anyway share here:

HI, thank you for help. Great you like the idea and AppInventor too. I did alot testing, I used different projects, and seems I can not establish a connection through the API…(Or I am doing something all the time wrong) Is there any way, how to check if API is working?..
…Or do I need to write certain code, to allow core talking through API,…
…or is there any standard syntax, which I can ping from normall Chrome window to check if API returnes some value?
Regarding your project in AI2 : would you mind posting a exported code, so I could check, if the problem is somewhere in AI2 code?..Many thanks anyway for help.

P.S. I am not really that good to browse through heap of code on spark firmware website and tell which one I need from all the variations with functions which are not from real world :slight_smile:

Hi .
I tried several different ways how to do this simple thing, but still no Luck.
I just want to create a single Button in Appinventor2 to ON/OFF. This state should be transfered(Written) to a D0 of my sparkcore.
Anybody can help please?
Unknown : what to call in AI2 (syntax, objects, code) and what code to put to my Sparkcore to make it listen to this Application (in Android)…I am lost…
Jozef

HI Jozef

Sorry to hear you are still having trouble. I am also sorry for the delay in response.
Ok, lets get you started with this. Try flashing this firmware on your spark

//Initialize
define ON 1
define OFF 0

int Pin0 = D0; //Define your pin

int OUT1State = 0; //This variable maintains the current state of the Pin0

void setup() {
pinMode(Pin0,OUTPUT);

Spark.function("pin0", OUT0); //This is the function that gets executed when there is a matching API request

// Initialize low
digitalWrite(Pin1, LOW);

}

void loop(){ //nothing ot do here
}

int OUT0() {
if(OUT0State == OFF) {
//Make high
digitalWrite(Pin0, HIGH);
OUT0State = ON; // Out 0 set high
return 200; // This SHOULD be checked in the app
}
else {
digitalWrite(Pin0, LOW);
OUT0State = OFF; // Out 0 set to low
return 200; // This SHOULD be checked in the app
}

}

Now, you can call the pin0 function (which maps to OUT0) over the internet by making a HTTP post request using cURL or something similar. If you have mac, you can open a terminal window and test it out directly. The HTTP post request looks something like this:

"https://api.spark.io/v1/devices/CORE_ID/pin0 -d access_token=ACCESS_TOKEN"

Put values for CORE_ID and ACCESS_TOKEN

You cannot test this url using chrome as that's not how a post request is made. If you have a iphone, you can use iCURL app to do this. I have detailed the process here:

Hope this helps.

Ok, I made an android app through app inventor to go along with the firmware. You can find it here:

Add the access token and core id in the Blocks pano.
Hope this works.

1 Like

HI Rahilj, thank you, it works great, thank you, now it is much clearer. Great work!

Thanks. I am glad you find it helpful. While I was working on this, I added the functionality to update the status of the pins in real time. This could be a good example of implementing HTTP GET requests to query spark variables in app inventor. You can find the aia here:

Don’t forget to declare corresponding spark variables in the setup() using:
//Spark Variables
Spark.variable(“pin1state”, &OUT1State, INT);
Spark.variable(“pin2state”, &OUT2State, INT);
Spark.variable(“pin3state”, &OUT3State, INT);

1 Like

Hey guys,

is ist possible to give an example of generating a new access token with MIT App Inventor?
i dont get it to work
this is the error:

it would be awesome if sb can help!
thanks

Hmm, do you mind sharing the .aia? I would like to test this functionality too.

hi,
no problem, here it is:

the button “sesam öffne dich” is just to toggling a pin on the spark core, to open/close my garage
Status button is used to read out the return value stored in a local variable on the spark

the email and assword boxes are not used at the moment, i m tired typing my creditials :smiley:

by the way: according to the MIT AI referenc: the block buildRequestData already encodes the text with x-www-form-urlencoded

Hey guys,
isn’t there anybody out there who can help me??

my problem is how to realize the “-u” command…

the “-d” command is realized this way and works!

curl https://api.spark.io/oauth/token -u spark:spark
-d grant_type=password -d username=joe@example.com -d password=SuperSecret

any help is appriciated!
thanks in advance

lala

Hi @lala

I don’t know anything about Appinventor unfortunately, but the -u spark:spark part of the call is a a Basic Auth username and password. These are required to be passed but are not checked and any values will work.

Maybe this page will help:

http://puravidaapps.com/filedownload.php

hi @bko

thank you very much, i got it to work with your helpful link!!! :slight_smile:
i solved my problem.

Here is my solution, the Buttons should be enumerated like in @rahilj 's example:

listing all tokens:

getting a new token:

reading out the respons:

I also will share my .aia for use in your projects.
in future i will add the feature of a normal login…

here is a screenshot of my app:

I forgot something intresting:

the base64 encoding doesnt work in appinventor “out of the box”,
but there is an extension app, to convert the textboxes in an base64 string

https://play.google.com/store/apps/details?id=com.namibsoft.aie&hl=de

i have not implementet it jet, but it should work easily.

3 Likes

@rahilj First, thank you very much for putting in the time to make a decent example. Being a total newb to spark core stuff, it was very helpful for me. This thread is where I kept winding up at in searches for examples.

The next challenge was sending data (arguments, parameters?, not sure exactly what to call them) along with the function. So I went back to the examples and found out that I didn’t (still don’t totally) understand the exact content of what was sent in the message to the device. When I looked at http://docs.spark.io/examples/ “CONTROL LEDS OVER THE 'NET” and I got to the point where you send terminal commands (curl) I hit a road block with the -d params=l1,HIGH line. Eventually I found Postman for Chrome and made the commands work. Here is an example of Postman setup and working for me with the LEDS example (changed the led to control D7 since that is the built in light on the spark).

That helped to do some troubleshooting, but app Inventor was still not working for me. Eventually, after some more trial, error, and searching screenshots of app inventor, I was able to make this send the parameters for me:

The extra list option and the way it is nested in the blocks is not totally obvious.

Here’s a glance at the screen in the design layout:
(edit: new users can only post 2 images :frowning: It only had 2 buttons and 3 labels giving feedback)

So, most of you are probably reading this and shrug it off like this is one big “well duh”, but this soaked up way too many hours of reading for me. Hopefully someone else will find it useful to know.

Glad I could be of some help. Good work there @mcgavinz26. Great that you are able to send parameters as part of the POST request. I agree it’s not totally obvious to make additional lists to attach parameters in the request but that’s the only way I have found it to work as well.

Would it be possible to add the code that was used on the Spark Core side in order to get the App Inventor project working (version 2)? I’m having a hard time figuring that out. Thanks!

Hi, I’ve been struggling to get my Photon to respond to App Inventor. Any chance you’d be willing to share the aia for the one you got working as well as the code on the Particle side? Thank you! I think once I see this functioning I can better grasp the subtleties.

How would i go about displaying a variable from a sensor connected to the spark core in my android app inventor application?