Web connected led question

Hello! :slight_smile:

This is my first time using the particle photon and I decided to make a remote garage door opener. I used the web connected led example code but every time I click “do it” it gives me that page with the information is there any way I can skip that info so when I click “do it” it submits but never takes me to another page?

-Thanks

Hey there and welcome to the community!

It seems as though you’re not the first one with that question, and as such I’d like to redirect you to some of the previous topics:



Have a look at those and let us know if you need further assistance!

Hi thanks for the reply but I’m a newbie and I can’t quite wrap my head around the threads you gave me. Is there an easier way to do it?

The other were newbies too, you’ve got to start somewhere. Not sure if it’s going to get much easier than a step by step tutorial:

Yes, it’s possible.

Here is the code I came up with: (Make sure to replace ((DEVICE ID)) and ((ACCESS TOKEN)) with your device ID and access token respectively

<!DOCTYPE html>
<html>
   <body>
      <center>
         <input class="messageCheckbox" type="checkbox" name="arg"> Enable LED<br>
         <input type="submit" onclick="toggleLED()" value="Send">
         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
         <script type="text/javascript">
            function toggleLED(objButton) {
            requestURL = "https://api.particle.io/v1/devices/((DEVICE ID)/led?access_token=((ACCESS TOKEN))";
            value = $('.messageCheckbox:checked').val();
			
            var xhr = new XMLHttpRequest();
            xhr.open("POST", requestURL, true);
            xhr.setRequestHeader('Content-Type', 'application/json');
            xhr.send(JSON.stringify({
            value: value
            }));
            }
         </script>
      </center>
   </body>
</html>

And the code that runs on your particle device is slightly different:

// -----------------------------------
// 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 = D0;
int led2 = 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);
   pinMode(led2, 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("led",ledToggle);
   // 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);
   digitalWrite(led2, 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 ledToggle(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==NULL) {
        digitalWrite(led1,LOW);
        digitalWrite(led2,LOW);
        return 0;
    }
    else {
        return -1;
    }
}