Combining Projects

Hello all;

There are two projects that I have done, but neither project is my code.

In project 1, I use a HTML page to trigger the relay. No issues and everything works great

In project 2, I use IFTTT and if an alarm is received than I get an email and notice that the door is open.

Both of these work great alone but I am wondering if I get both of these into one device. This way it will save me 50% cost.

I use this on my family lodge for their front door. The reset works good for resetting the modem that locks up once in a while.

----- Project 1: -----

Boron Lte
Non Latching Relay Featherwing

----- Code -----

int PowerPin = D2;

void setup() {
  pinMode(PowerPin, OUTPUT);                  
  Particle.function("cycle",cyclePower);      
  digitalWrite(PowerPin, LOW);               
}

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

int cyclePower(String command) {  
  digitalWrite(PowerPin,HIGH);              
  delay(10000);
  digitalWrite(PowerPin,LOW);
  return 0;
}

----- Project 2: -----

Boron Lte
Magnetic Door switch

----- Code -----

int DAP = D9;
int DAR = D13;
int led = D7;

void setup() {
  Serial.begin(230400);
  pinMode(DAP,OUTPUT);
  pinMode(DAR,INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  while (Particle.connected()) {
    if(digitalRead(DAR)==HIGH) {
      Particle.publish("doorStatus","DOOR OPEN",30);
      digitalWrite(led, HIGH);
      delay(5000);
    }
    else {
      //  Particle.publish("doorStatus","DOOR CLOSED",30);
      digitalWrite(led,LOW);
      //    delay(5000);
    }
  }
}

Definitely!

Both these codes are reeealy simple and hence combining them is no problem at all - but if you want to get proficient in using IoT devices you should try to understand the code, how it works and why.
Armed with that knowledge it shouldn’t be much trouble for you to combine both codes.

However, some things should definitely be changed in both these codes:

  • avoid using delay()
  • don’t use while(Particle.connected()) as it will trap the code flow and consequently impair the ability to do other stuff along side it
  • depending on your DAR sensor, you may need a pull resistor (internal or external) to ensure a discrete level when the switch is open.
1 Like

Thank you,

These codes are simple, and I will take a look into and attempt to see if I can get these working together.

D

If you get stuck, don’t hesitate to ask specific questions :+1:

To avoid delay(10000) in the function callback of project 1 you could look at Software Timers (one-shot in particular) or have loop() check a global “timer flag” that you set in int cyclePower() and then reset your PowerPin when needed from there.

Once you got that you should be equipped to also replace delay(5000) in project 2 and when that’s done combining the two tasks to run concurrently will come naturally.