Skip Navigation

Particle On and off with function

This is a short snippet for working with https://www.particle.io/ device and an LED.
int led1 = D0;

// the little blue LED on your board
int led2 = D7; 

// when the device starts up it will run the setup function or when the device is reset
// void meaning it is not going to return a value
void setup() {

  // We are going to tell our device that D0 and D7 (which we named led1 and led2 respectively) are going to be output
  // (That means that we will be sending voltage to them, rather than monitoring voltage that comes from them)

  // It's important you do this here, inside the setup() function rather than outside it or in the loop function.
    pinMode(led1, OUTPUT);
    pinMode(led2, OUTPUT);
    
     // register the cloud function
    Particle.function("light", lightIt);

}


// this function automagically gets called upon a matching POST request
int lightIt(String command) {
   
    if(command == "on"){
        digitalWrite(led1, HIGH);
        // some example functions you might have
        // activateWaterHeater();
        // activateWaterPump();
    }
  
    if(command == "off"){
        digitalWrite(led1, LOW);
    }
}


void loop() {
  // To blink the LED, first we'll turn it on...
//   digitalWrite(led1, HIGH);
//   digitalWrite(led2, HIGH);

//   // We'll leave it on for 1 second...
//   delay(6000);

//   // Then we'll turn it off...
//   digitalWrite(led1, LOW);
//   digitalWrite(led2, LOW);

//   // Wait 1 second...
//   delay(1000);

  // And repeat!
}

Related Snippets

See all