Back to Parent

/*
 * Project Thermal-Compare
 * Description:
 * Author: Nicholas Stone
 * Date: 1/29/2017
 * Version: 3
 */

#include "OneWire.h"
#include "spark-dallas-temperature.h"

//instances
OneWire oneWire(D0);
DallasTemperature dallas(&oneWire);


//vars
// double tempC = 0.0;
int tempF = 0;
int MasterTempF = 80;
int MaxTempDiff = 1;
//Sample Rate est (Loop Rate)
int sampleRate = 1000;
//Burst Rate of published messages (15 minutes)
int burstRate = 900000;
int timeSincePublish= 900000;

//motion read
int motionPin = D1;
int motionValue = 0;
int motionState = 0;
int motionRate = 900000;
int timeSinceMotion = 0;

//LED
int ledPin = D2;

//Fan / Servo
int servoPin = D3;
Servo fanServo;
int servoPos = 0;




void setup() {
  //Register
  // Particle.variable("tempC", &tempC, DOUBLE);
  Particle.variable("tempF", &tempF, INT);
  //Particle.variable("motion",&motionValue,INT);
  //Particle.variable("tSincePublish",&timeSincePublish,INT);
  Particle.variable("tSinceMotion",&timeSinceMotion,INT);
  //Particle.variable("motionStatus",&motionState,INT);

  pinMode(motionPin, INPUT);
  pinMode(ledPin, OUTPUT);

  fanServo.attach(D3);


  //start library
  dallas.begin();

}

void loop() {
  //check motion
  motionValue = digitalRead(motionPin);
  if(motionValue == 1) {
    motionState = 1;
    timeSinceMotion = 0;
    digitalWrite(ledPin, HIGH);
  } else{if(timeSinceMotion>=motionRate) {
    motionState = 0;
    digitalWrite(ledPin, LOW);
    } else {
      timeSinceMotion = timeSinceMotion + sampleRate;
      digitalWrite(ledPin, LOW);
    }
  }



  //request conversion
  dallas.requestTemperatures();

  //get temperature in Celsius and convert to Fahrenheit
  tempF = (int)DallasTemperature::toFahrenheit(dallas.getTempCByIndex(0));

//check if difference exceeds configuration and enough time has passed since the last publication
if(abs(tempF - MasterTempF)>MaxTempDiff && timeSincePublish>=burstRate
  //&& motionState == 0
)
  {
    Particle.publish("TempDiffExceeded", "true");
    // reset burst rate time counter
    timeSincePublish = 0;

    //run fan
    fanServo.write(90);
    fanServo.write(0);
    fanServo.write(90);
    fanServo.write(0);
    }

  timeSincePublish = timeSincePublish + sampleRate;
  delay(sampleRate);


}

/*int fanControl(INT inAngle)
{
  servoPos = constrain(inAngle,0,180);
  fanServo.write(servoPos);

  return 1;
}*/
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0