I’ve looked at the Spark documentation and as usual it’s written for people who already know how to do things.
I’m trying to call a function on my spark that has three integer arguments. I’ve written the function and have a Spark.function(); in the void loop() but what I need to do is sent it the arguments somehow and I’m stumped on that.
It’s something to do with curl no doubt but I can’t seem to get the formatting right. I’ve searched the forums but found nothing of use to me.
It there a tutorial anywhere that explains things for complete beginners to all this stuff?
@Owen The spark API only allows you to pass one argument into your function. (A String). Inside your function you’ll need to cut up the string unto multiple parts. From the docs, here is a sample function that does such a thing. The parameter string would look like “l1,HIGH”
int ledControl(String command)
{
int state = 0;
//find out the pin number and convert the ascii to integer
int pinNumber = (command.charAt(1) - '0') - 1;
//Sanity check to see if the pin numbers are within limits
if (pinNumber < 0 || pinNumber > 1) return -1;
// find out the state of the led
if(command.substring(3,7) == "HIGH") state = 1;
else if(command.substring(3,6) == "LOW") state = 0;
else return -1;
// write to the appropriate pin
digitalWrite(pinNumber, state);
return 1;
}
Ok that helps a bit but I have two further questions;
How would I go about converting three numbers from a string into integers for the function?
Say I sent the numbers “12,600,6” in the command String how would I get those into a format I can put into my function?
And secondly how do I actually send those to the spark via a webpage?
I tried that webpage and it just tells me that the function is working. I doesn’t say how to call it myself from my own webpage without going through that website.
If you want to write some code that hits the API, the magic words you want are “http POST request”. Whichever programming language your site is using, you should be able to find an example, what language do you want to use? (Ruby, Python, C#, PHP, Javascript, Java, etc, etc)
But how do you actually format the above to put into the address box on a web browser? (other than change the device ID, access token and function name).
int brewCoffee(String command);
void setup()
{
//register the Spark function
Spark.function("brew", brewCoffee);
}
void loop()
{
//this loops forever
}
//this function automagically gets called upon a matching POST request
int brewCoffee(String command)
{
//look for the matching argument "coffee" <-- max of 64 characters long
if(command == "coffee")
{
//do something here
return 1;
}
else return -1;
}
Hi kenneth, again thanks for helping on my similar issue as you saved me a lot of time and frustration. As long as Owen is using the SPARK API Cloud then it is not possible to directly communicate with the spark core using a simple browser http request. However, it is possible to do this from a webpage on a local net using a TCP server and client browser and without the token. The code you supplied me did allow me to change the state of one Spark Core Digital pin from a wepage running on my Tiny Webserver. I only needed the IP of the Spark Core and the IP of my Tiny Webserver which I obtained from my router DHCP table… I put the Client IP (Webserver) in the Spark Core IDE and locally compiled and flashed the core.
I will do further testing this weekend and yes this would be unsecure over the Internet but I think Old School will be okay and that can be addressed in the future, as you suggested, setting up a local cloud on my local network and use the API.
Not everyone needs the cloud for over the internet access as all I need right now is local control using a webserver. I will post my code next week when I can do more than just turn D4 High / Low
I am hoping that by assigning the Spark Core a static IP and opening a port on my router to the Core Static IP that I can control my Core from a hosted Apache password protected Webpage. If I am successful I will call it the poor mans cloud
I’m using a Photon and controlling it with Particle.function() and Particle.variable() working great from an HTML+Javascript webpage. But I haven’t been able to modify something without the page taking me to a result page that looks like this:
Thank you very much for the quick and accurate response! Now my Photon is working exactly the way it is supposed to be.
I used the Tutorial: Spark Variable and Function on One Web Page I found on your first link, and I just want to make a clarification. In this tutorial, at the end of the requestURL, after the setFunc it adds a “/” sign, but what worked for me was a "?" sign instead. I hope this helps someone else.