Call function through URL?

I know this question looks like the other questions, however I just wanted to ask if it is possible to call a function through a normal URL only?

For example I have a function in my particle photon that switches the LED 7 on then off. Just by clicking that URL it would do that.

Reason being I want other people to be able to click that URL too, and not only the user (aka make it public).

Hi yes,

That should be straght forward this is taken from an example:- ‘Controlling LEDs over the Internet’

 <!-- Replace your-device-ID-goes-here with your actual device ID
 and replace your-access-token-goes-here with your actual access token-->
 <!DOCTYPE>
 <html>
   <body>
   <center>
   <br>
<br>
   <br>
   <form action="https://api.particle.io/v1/devices/**your-device-ID-goes-here**/led1?access_token=**your-access-token-goes-here**" method="POST">
     Tell your device what to do!<br>
    <br>
     <input type="radio" name="arg" value="on">Turn the LED on.
     <br>
     <input type="radio" name="arg" value="off">Turn the LED off.
     <br>
     <br>
     <input type="submit" value="Do it!">
    </form>
    </center>
    </body>
    </html>

And that should work with the following code on the device:-1:

// -----------------------------------
// Controlling LEDs over the Internet
// -----------------------------------

/* First, let's create our "shorthand" for the pins
Same as in the Blink an LED example:
led1 is D0, led2 is D7 */

int led1 = D7;


// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Particle function

void setup()
{

   // Here's the pin configuration, same as last time
   pinMode(led1, OUTPUT);


   // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
   Particle.function("led1",ledToggle1);
  
   // This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.

   // For good measure, let's also make sure both LEDs are off when we start:
   digitalWrite(led1, LOW);

}


/* Last time, we wanted to continously blink the LED on and off
Since we're waiting for input through the cloud this time,
we don't actually need to put anything in the loop */

void loop()
{
   // Nothing to do here
}

// We're going to have a super cool function now that gets called when a matching API request is sent
// This is the ledToggle function we registered to the "led" Particle.function earlier.

int ledToggle1(String command) {
    /* Particle.functions always take a string as an argument and return an integer.
    Since we can pass a string, it means that we can give the program commands on how the function should be used.
    In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
    Then, the function returns a value to us to let us know what happened.
    In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
    and -1 if we received a totally bogus command that didn't do anything to the LEDs.
    */

    if (command=="on") {
        digitalWrite(led1,HIGH);
        digitalWrite(led2,HIGH);
        return 1;
    }
    else if (command=="off") {
        digitalWrite(led1,LOW);
        digitalWrite(led2,LOW);
         return 0;
    }
    else {
        return -1;
    }
}

} 

Give that a go.

Liam

1 Like

you want to GET/POST to a url, directly on the Photon, or using a server?

either is possible.

the Photon can also serve up a little webpage itself.

There are many examples of the Photon being the server, here is one example that turns on/off a led using a graphical approach.

1 Like

Is hiding your access token one of your goals?

1 Like

Yes. And if possible make that link expire at a certain time period.

I guess you can change your access token periodically.