How can MIT app inventor receive voltage value from Particle Photon?

310 views
Skip to first unread message

zekrom fast

unread,
Mar 11, 2018, 11:11:18 AM3/11/18
to MIT App Inventor Forum
Hi everyone.

I'm here to ask anyone about how MIT app inventor can receive 'voltage' value from Particle Photon.
I have done the apps but it shows an error like below.




Below is my apps block code:



Is there any missing block?


When I'm creating the same function using HTML code, it is successful.




Below is my code in HTML.


var url = "https://api.spark.io/v1/devices/" + deviceID + "/volts";

function callback(data, status){
if (status == "success") {
volts = parseFloat(data.result);
volts = volts.toFixed(2);
g.refresh(volts);
setTimeout(getReading, 1000);
}
else {
alert("There was a problem");
}
}

function getReading(){
    $.get(url, {access_token: accessToken}, callback);
}
</script>

Below is my code in Particle Photon.
int reading = 0;
double volts = 0.0;
int analogPin = A0;

void setup() {
    Spark.variable("analog", &reading, INT);
    Spark.variable("volts", &volts, DOUBLE);
}

void loop() {
  reading = analogRead(analogPin);
  volts = reading * 3.3 / 4096.0;
}


So,
Does anyone know what I'm doing wrong in my MIT app inventor block code?
Please share with me.

Here, I give the link the links that I used in order to create this app.

Those links mostly show hoe to post from MITapp to Particle Photon.

Please, hope anyone can help me to discuss this.
Thanks.










1

Taifun

unread,
Mar 11, 2018, 12:17:33 PM3/11/18
to mitappinv...@googlegroups.com
set the authorization in the header, see screenshot and make sure, there is a space after "Bearer"
see also the screenshot about how to use the PostText block...
however you probably have to use GET and not POST, see the API documentaion...

Taifun


zekrom fast

unread,
Mar 11, 2018, 8:17:20 PM3/11/18
to MIT App Inventor Forum
I've tried your suggestion.

Please have a look at my new block code.












and here is the result,

















It says [error invalid request, error description:"The access token was not found"]

Don't you think I have put the access token?



Ghica

unread,
Mar 12, 2018, 9:41:42 AM3/12/18
to MIT App Inventor Forum
In getDeviceInfo there is no wwwWeb.Get, so you will never use the URI that you build there.
In wwwWeb.GotText you have: if get responseCode What is that supposed to mean?
I think you want to compare it to a value that is an OK response (maybe the value 200, maybe something else), and if you are calling getDeviceInfo, you get a kind of recursive web call. 
Try to find out what you really want to do.

A good way to debug is to use DoIt. When Connected to the companion, put your mouse on something that you want to know the value of, clcik with the right mouse button and choose DoIt.
In this case another good way to debug is to use a normal browser, like Firefox or Chrome. Type the URI in your browser and see if what you get returned is OK. If it is, use it to build your request in the app,
otherwise, correct it. 
But, you wrote: When I'm creating the same function using HTML code, it is successful. What HTML code did you create and where did you run it?
Cheers, Ghica.

Taifun

unread,
Mar 12, 2018, 10:03:54 AM3/12/18
to MIT App Inventor Forum
Don't you think I have put the access token?

see again my screenshot how to set the access token, see also the API documentation
also remove the volts from the header, the volts you will receive as response content in the Web.GotText event

Taifun

Bob Glicksman

unread,
Mar 17, 2018, 5:14:50 PM3/17/18
to MIT App Inventor Forum
It may be helpful to look at the examples of calling Particle functions and getting particle variable data at:  https://github.com/TeamPracticalProjects/MIT-App-Inventor-Particle-Photon-test.  You can reduce the Particle cloud response from a complex JSON structure to just the variable data by using "?format=raw" in the url, as in the attached image.  Make sure that you convert the voltage to either an integer or a String in your Particle firmware, as these are the only data types that are supported in Particle.variable()


Get.jpg
Reply all
Reply to author
Forward
0 new messages