Morgan CurtisPatrick Sturm
Published

Wake-Up Call!

A proper awaking is vital to a productive day. Wake-Up Call will help you achieve your goals.

IntermediateFull instructions provided2 hours786
Wake-Up Call!

Things used in this project

Hardware components

Photon
Particle Photon
×2
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Temperature Sensor
Temperature Sensor
×1
SparkFun Vibration Motor
×1
Cairondin Mini Smart Plug
×1
Resistor 1k ohm
Resistor 1k ohm
×1
Resistor 4.75k ohm
Resistor 4.75k ohm
×1
1N4001 Diode
×1
0.1µF ceramic capacitor
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1

Software apps and online services

Google Sheets
Google Sheets
Maker service
IFTTT Maker service
Particle IDE

Story

Read more

Schematics

Temperature Diagram

The temperature sensor set-up is fairly simple with only the sensor, one resistor and the photon.

Buzzer Diagram

The "buzzer" or vibration motor circuit is slightly more complicated, consisting of a transistor, diode and capacitor in addition to a resistor, motor and photon.

Code

Buzzer Code

Arduino
int motorPin = D3; //set pin D3 to be the positive connection for the motor
    
void setup()
{ 
  pinMode(motorPin, OUTPUT);//the motorPin is an output
  Spark.function("BuzzBuzz", BuzzBuzz);//create a function. which will be called by IFTTT
}
int BuzzBuzz(String command)
{
 digitalWrite(motorPin, HIGH); //Start vibration for half a second
 delay(500);
 digitalWrite(motorPin, LOW);
 delay(2000);                   //wait 2 seconds
 digitalWrite(motorPin, HIGH);  //Start vibration for half a second
 delay(500);                    
 digitalWrite(motorPin, LOW);
 delay(2000);                   //wait 2 seconds
 digitalWrite(motorPin, HIGH);  //Start vibration for half a second
 delay(500);                
 digitalWrite(motorPin, LOW);
 delay(2000);                   //wait 2 seconds
 digitalWrite(motorPin, HIGH);  //Start vibration for half a second
 delay(500);
 digitalWrite(motorPin, LOW);
 delay(2000);                   //wait 2 seconds
 Particle.publish("welcome_your_daily_mini_heart_attack", "WAKE");  //publish event with the variable WAKE, to be read by buddy photon
 return 1;
}
void loop()
{
}

Temperature Sensor

Arduino
// This #include statement was automatically added by the Particle IDE.
#include <OneWire.h>
//The majority of this code, outside of noted sections is attributed to Particle, "Tutorial #4: Temperature Logger"

OneWire ds = OneWire(D2);  // 1-wire signal on pin D2 

unsigned long lastUpdate = 0;

float lastTemp;

//Part of original code, create a variable "LightState" with a default value of -1
int LightState = -1;
//

void setup() {
  Serial.begin(9600);
  Spark.variable("LightState", &LightState, INT);   //Create a variale that IFTTT will monitor and react according to the value
  Particle.subscribe("welcome_your_daily_mini_heart_attack", LightControl);
}

void loop(void) {
  byte i;
  byte present = 0;
  byte type_s;
  byte data[12];
  byte addr[8];
  float celsius, fahrenheit;

  if ( !ds.search(addr)) {
    Serial.println("No more addresses.");
    Serial.println();
    ds.reset_search();
    delay(250);
    return;
  }

  // The order is changed a bit in this example
  // first the returned address is printed

  Serial.print("ROM =");
  for( i = 0; i < 8; i++) {
    Serial.write(' ');
    Serial.print(addr[i], HEX);
  }

  // second the CRC is checked, on fail,
  // print error and just return to try again

  if (OneWire::crc8(addr, 7) != addr[7]) {
      Serial.println("CRC is not valid!");
      return;
  }
  Serial.println();

  // we have a good address at this point
  // what kind of chip do we have?
  // we will set a type_s value for known types or just return

  // the first ROM byte indicates which chip
  switch (addr[0]) {
    case 0x10:
      Serial.println("  Chip = DS1820/DS18S20");
      type_s = 1;
      break;
    case 0x28:
      Serial.println("  Chip = DS18B20");
      type_s = 0;
      break;
    case 0x22:
      Serial.println("  Chip = DS1822");
      type_s = 0;
      break;
    case 0x26:
      Serial.println("  Chip = DS2438");
      type_s = 2;
      break;
    default:
      Serial.println("Unknown device type.");
      return;
  }

  // this device has temp so let's read it

  ds.reset();               // first clear the 1-wire bus
  ds.select(addr);          // now select the device we just found
  // ds.write(0x44, 1);     // tell it to start a conversion, with parasite power on at the end
  ds.write(0x44, 0);        // or start conversion in powered mode (bus finishes low)

  // just wait a second while the conversion takes place
  // different chips have different conversion times, check the specs, 1 sec is worse case + 250ms
  // you could also communicate with other devices if you like but you would need
  // to already know their address to select them.

  delay(1000);     // maybe 750ms is enough, maybe not, wait 1 sec for conversion

  // we might do a ds.depower() (parasite) here, but the reset will take care of it.

  // first make sure current values are in the scratch pad

  present = ds.reset();
  ds.select(addr);
  ds.write(0xB8,0);         // Recall Memory 0
  ds.write(0x00,0);         // Recall Memory 0

  // now read the scratch pad

  present = ds.reset();
  ds.select(addr);
  ds.write(0xBE,0);         // Read Scratchpad
  if (type_s == 2) {
    ds.write(0x00,0);       // The DS2438 needs a page# to read
  }

  // transfer and print the values

  Serial.print("  Data = ");
  Serial.print(present, HEX);
  Serial.print(" ");
  for ( i = 0; i < 9; i++) {           // we need 9 bytes
    data[i] = ds.read();
    Serial.print(data[i], HEX);
    Serial.print(" ");
  }
  Serial.print(" CRC=");
  Serial.print(OneWire::crc8(data, 8), HEX);
  Serial.println();

  // Convert the data to actual temperature
  // because the result is a 16 bit signed integer, it should
  // be stored to an "int16_t" type, which is always 16 bits
  // even when compiled on a 32 bit processor.
  int16_t raw = (data[1] << 8) | data[0];
  if (type_s == 2) raw = (data[2] << 8) | data[1];
  byte cfg = (data[4] & 0x60);

  switch (type_s) {
    case 1:
      raw = raw << 3; // 9 bit resolution default
      if (data[7] == 0x10) {
        // "count remain" gives full 12 bit resolution
        raw = (raw & 0xFFF0) + 12 - data[6];
      }
      celsius = (float)raw * 0.0625;
      break;
    case 0:
      // at lower res, the low bits are undefined, so let's zero them
      if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
      if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
      if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
      // default is 12 bit resolution, 750 ms conversion time
      celsius = (float)raw * 0.0625;
      break;

    case 2:
      data[1] = (data[1] >> 3) & 0x1f;
      if (data[2] > 127) {
        celsius = (float)data[2] - ((float)data[1] * .03125);
      }else{
        celsius = (float)data[2] + ((float)data[1] * .03125);
      }
  }

  // remove random errors
  if((((celsius <= 0 && celsius > -1) && lastTemp > 5)) || celsius > 49) {
      celsius = lastTemp;
  }

  fahrenheit = celsius * 1.8 + 32.0;
  lastTemp = celsius;
  Serial.print("  Temperature = ");
  Serial.print(celsius);
  Serial.print(" Celsius, ");
  Serial.print(fahrenheit);
  Serial.println(" Fahrenheit");

  // now that we have the readings, we can publish them to the cloud
  String temperature = String(fahrenheit); // store temp in "temperature" string
  Particle.publish("temperature", temperature, PRIVATE); // publish to cloud
  delay(60000); // 60 second delay
}

//This part of the original code, it is a function that is called after the event from the buddy photon is published. The goal is to change a varriable "LightState" to different values that IFTTT monitors, then turns off the light accordingly
void LightControl(const char *event, const char *data)
{
  if (strcmp(data,"WAKE")==0) 
  {
    delay(300000);
    // if buddy photon says the sensor has been activated wait 5 mins
    LightState = 1;
    // turn light on
    delay(2700000);
    // Keep on for 45 mins
    LightState = 0;
    // then turn off to conserve power
    delay(60000);
    // Keep varriable at 0 to let IFTTT to toggle light
    LightState = -1;
  }
  else 
  {
  }
}
    // if the data is something else, don't do anything.
//The majority of this code, outside of noted sections is attributed to Particle, "Tutorial #4: Temperature Logger"

Credits

Morgan Curtis
1 project • 2 followers
Patrick Sturm
1 project • 1 follower
Thanks to Particle.

Comments