I’ve currently got a webpage that has a few buttons on it to register button presses and light up LEDs and such. I have some example code on the page that maps the function result to the console.log, but I also want to be able to parse the result onto the webpage for user feedback after a button is pressed (to know that the Photon actually did something).
Hah, yeah. Your original code worked out for the console.log part no problem.
This works, but the data doesn’t actually publish any response. I’m not sure if I need to specify a response in my firmware code (I’m assuming that I do), but the console.log replys with something similar to:
Alright, made a small change to the code above, give it another shot.
The problem was that an object was being returned, which you can’t display without doing something with it. Either select the key you’d like to show from the JSON, or make the object a string, and print that.
On the device, you should always have a return value, whatever it may be. It’s useful for passing through different responses
That should be fine. The whole “make the object a string” is on the receiving (javascript) end, since the cloud actually gives you an object with the response in it. This thing:
You can ‘select’ the contents of that by doing data.return_value, where data is the object name, and return_value is one of the keys contained in the object.
The code I posted above should just print out the whole object. Depending on what you need, you can choose either tactic.
Ah, I got it working. Was cleaning up the HTML to post and stripped out some css and a few other js calls.
Something else is preventing it from showing in my code, but I’ll narrow it down. Thanks!
While Jordy focuses on the web side I just want to throw in this firmware hint
int buttonAction(String command) {
if (command=="buttonpress") {
Spark.publish("Button_Push","Web"); // you might rather set a flag inside here
// and do the publishing in loop()
// and use Particle.publish() as Spark. might get deprecated
digitalWrite(led1, HIGH);
return 1;
}
return 0; // your fn should ALWAYS return an int, not only on certain conditions
}