Back to Parent

/**********************
*
* Application Setup
*
**********************/
//if temperature sensor's data is big then this, regard stove as on
int stoveOnTemp = 21;
//if has been on longer then this (milliseconds), send a alert
int alertAfterMs = 10000;



/**********************
*
* Main code
*
**********************/

//pins
int sensorIndicateLED = D0;
int stoveIndicateLED = D1;
int tempSensor = A0;
int presSensor = A1;


int counter = 0;
bool sensorOn;

int stoveOn;
unsigned long stoveOnTime;
unsigned long stoveOnMs;

bool alertSent = false;

int tempReading;
double temperature;

void setup(){
  //set pins
  pinMode(sensorIndicateLED, OUTPUT);
  pinMode(stoveIndicateLED, OUTPUT);
  pinMode(tempSensor, INPUT);
  pinMode(presSensor, INPUT);

  //expose variables
  Spark.variable("stoveOn", &stoveOn, INT);
  Spark.variable("stoveOnMs", &stoveOnMs, INT);
  Spark.variable("temperature", &temperature, DOUBLE);

  initValues();
}

/**********************
*
* Main Loop
*
**********************/
void loop(){

  //if switch the sensor on/off through force sensor
  if(analogRead(presSensor) > 1000){
    //change sensor on/off
    sensorOn = !sensorOn;

    //if deactivate sensor, init all values
    if(!sensorOn){
      initValues();

    //if activate sensor, flash LED once as feed back
    }else{
      digitalWrite(sensorIndicateLED, HIGH);
      delay(100);
      digitalWrite(sensorIndicateLED, LOW);
    }

    //delay to prevent iterating switch on/off sensor
    delay(1000);
  }


  if(sensorOn){
    //update temperature variable
    tempReading = getTemp(tempSensor);

    //flash LED to indicate data reading
    digitalWrite(sensorIndicateLED, HIGH);
    delay(100);
    digitalWrite(sensorIndicateLED, LOW);

    //if the stove on
    if(temperature > stoveOnTemp){
      digitalWrite(stoveIndicateLED, HIGH);

      //if stove is just turned on
      if(stoveOn == 0){
        //record turn on time
        stoveOnTime = millis();
        //set stove status variable
        stoveOn = 1;

      //if stove is already on
      }else{

        //calculate how long has the stove been turned on
        stoveOnMs = millis() - stoveOnTime;

        //if the stove is turned on for too long
        if(stoveOnMs > alertAfterMs){

          //start to flash alert LED
          digitalWrite(stoveIndicateLED, counter%2 ? HIGH : LOW);

          //if hasn't sent alert, send a alert SMS using IFTTT
          if(!alertSent){
            Spark.publish("StoveOnTooLongAlert", "alert");
            alertSent = true;
          }
        }
      }

    //if the stove off
    }else{
      initValues();
      digitalWrite(stoveIndicateLED, LOW);
    }

    delay(1000);
    counter++;
  }
}

/**********************
*
* function declarations
*
**********************/
void initValues(){
  alertSent = false;
  stoveOn = 0;
  stoveOnTime = 0;
  stoveOnMs = 0;
}

double getTemp(int tempSensorPin){
  return ((analogRead(tempSensorPin) * 3.3) / 4095.0  - 0.5) * 100;
}
Click to Expand

Content Rating

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

0