Laura HopperRyan Odessa
Published

FANtastic! Remote Fan Setup

A quick and easy way to control a fan with the touch of a button.

IntermediateFull instructions provided4 hours984
FANtastic! Remote Fan Setup

Things used in this project

Story

Read more

Schematics

Button (publish) Photon Circuit

the button is pressed, and the photon publishes the command.

Servo (subscribe) Circuit

This setup receives the signal (subscribe) and tells the server to turn.

Code

Subscribing Code

C/C++
This is the portion of code for the subscribing photon that controls the servo to turn on the fan.
Servo myservo;  // create servo object to control a servo
                // a maximum of eight servo objects can be created

int led2 = D7;
int led1 = D1;

void setup() {

  // We are going to tell our device that D1 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.
  myservo.attach(D0);  // attaches the servo on the D0 pin to the servo object
  // Only supported on pins that have PWM
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  Particle.subscribe("LED_HobbitCowboy", LED_Trigger);

}


void LED_Trigger(const char *event, const char *data)
{
  /* Particle.subscribe handlers are void functions, which means they don't return anything.
  They take two variables-- the name of your event, and any data that goes along with your event. */
  
   if (strcmp(data,"off")) {
    digitalRead(D7);
    if (digitalRead(D7) == LOW) {
    digitalWrite(led1, HIGH);
    digitalWrite(led2, HIGH);
    myservo.write(150); 
    Particle.publish("FANtasticIFTTT", "on");  //Publishes an event to IFTTT to update Google Sheet with fan status
  
    }
    else if (digitalRead(D7) == HIGH) {
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    myservo.write(20); 
    Particle.publish("FANtasticIFTTT", "off");

    }


    else {
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }
   }
}

Publishing Code

C/C++
This is the portion of code for the publishing photon that sends signals when the button is pressed. Note the second timer! This is important to make sure signals get sampled and sent appropriately.
int pushB = D6; // var for button definition
int led = D7; //declare on-board led
int bState = 0;
int sec_interval = 2;
int prev_second = 0;


void setup() {
    
    pinMode(pushB, INPUT);
    pinMode(led, OUTPUT);

}

void loop() {
    
    button();
    secondTimer();

}

void button() {
  if (digitalRead(pushB)==1){
  digitalWrite(led, HIGH);   // Turn ON the LED pins
  bState = 1;
  } else {
      digitalWrite(led, LOW);  // Turn OFF the LED pins
      bState = 0;
  }
}
//the following prevents oversaturation of signals
void secondTimer(){
  int curr_second = (millis() / 1000);
  if ((curr_second % sec_interval == 0) && (curr_second != prev_second)) {
    if (bState == 0){
      Particle.publish("LED_HobbitCowboy", "on");
      Serial.println(bState);  // print values to the serial monitor
    }else{
      Particle.publish("LED_HobbitCowboy", "off");
      Serial.println(bState);  // print values to the serial monitor
    }
  prev_second = curr_second;
  }
}

Credits

Laura Hopper

Laura Hopper

1 project • 0 followers
Ryan Odessa

Ryan Odessa

1 project • 1 follower

Comments