David Miller Jr.Joshua Holler
Published © GPL3+

Wake Up and Smell the Coffee

Are you a big coffee drinker or a caffeine addict? This project will have hot coffee waiting for you in the morning!

BeginnerShowcase (no instructions)6 hours722
Wake Up and Smell the Coffee

Things used in this project

Hardware components

Photon
Particle Photon
×2
Servos (Tower Pro MG996R)
×2
Photo resistor
Photo resistor
×2
Male/Female Jumper Wires
Male/Female Jumper Wires
×6
Male/Male Jumper Wires
×6
Breadboard (generic)
Breadboard (generic)
×2

Story

Read more

Schematics

Schematic for 2 photons, servos

This is the connection diagram for the Particle that is connected to the two servos. This is directly connected to the coffee machine.

Photo Sensor

This is the connection diagram for the Particle connected to the photo resistor. This will be placed in your room or wherever you wish the light sensor to be.

Single Photon Option

This project is possible to do using only one Particle. We had to make this for a class in college and were required to get two Particles to communicate. We realize that not everyone is able or willing to spend the money to buy two Particles, therefore we have included the schematics and code that can be used with a single Particle.

Code

Photo Resistor Particle Code

C/C++
//Labels the pin one end of the photo resistor is plugged into
int photoresistor= A0;

//Labels the pin the other end of the photo resistor is plugged into
int power = A5;

//Labels the led on the particle for use
int boardLed = D7;

//average photo resistor reading with the lights on
int lighton;

//average photo resistor reading with the lights off
int lightoff;

//this will be the minimum level of light necessary for the time based functions to run
int startThreshold;

//setup function
void setup() 
{
    //This is used when the DO Button is pressed on your phone. An explanation for its purpose is given below.
    Particle.subscribe("wait", wait, "2d0045000347343339373536");
    
    //This declares what time zone you are in, when making your own code you will have to find this on particle.io
    Time.zone(-4);
    
    //declare the modes of the pins that are used. INPUT is means that readings will be taken, OUTPUT means the particle will provide power through these pins
    pinMode(photoresistor,INPUT);
    pinMode(power,OUTPUT);
    pinMode(boardLed,OUTPUT);
    
   //By turning the output for the power to HIGH, the photo resistor will now constantly have power running to it
    digitalWrite(power,HIGH);
 
   //The D7 led will turn on to tell you to cover the photo resistor
    digitalWrite(boardLed,HIGH);
    delay(5000);
    
    //The light will turn off and readings will be taken
    digitalWrite(boardLed,LOW);
    
    int off_1 = analogRead(photoresistor);
    delay(300);
    int off_2 = analogRead(photoresistor);
    delay(300);
    
    //The led will flash to let you know the readings were taken
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    
    //The led will turn on to let you know to take your finger off the photo resistor
    digitalWrite(boardLed,HIGH);
    delay(5000);
    
    //The light will turn off and readings will be taken
    digitalWrite(boardLed,LOW);
    
    //NOTE: make sure the lights in the room are turned on when these readings are taken
    int on_1 = analogRead(photoresistor);
    delay(300);
    int on_2 = analogRead(photoresistor);
    delay (300);
    
    //The led will flash to let you know the readings were taken
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    
    //These take the average values of the photo resistor when covered and uncovered
    lightoff = (off_1+off_2)/2;
    lighton = (on_1+on_2)/2;
    
    //This takes the average of the covered and uncovered values to determine a base light level for the time based functions to run
    startThreshold = (lightoff+lighton)/2;
}

//When the DO Button is pressed on your phone to start making coffee, the Particle connected to the servos will make the coffee, but it will also publish a message to this Particle telling it to run a delay for a certain period of time. This prevents the time based function from running and automatically brewing a second cup.
void wait(const char *event, const char *data)
{
    if(data)
    {
        for ((Serial.print(Time.hour())>=0); (Serial.print(Time.hour())<=10);)
        {
            digitalWrite(boardLed,HIGH);
            delay(5000);
            digitalWrite(boardLed,LOW);
            delay(1800000);
        }
    }
}

//This code will run constantly since it is in a void loop
void loop() 
{
 
    //If the current day is between Monday and Friday, this code will run and proceed to the next level
    //NOTE: It is possible to adjust the days of the week that you wish this code to run. Simply adjust
    //the numbers according to the days you wish to run the code.
    //Sunday - 1
    //Monday - 2
    //Tuesday - 3
    //Wednesday - 4
    //Thursday - 5
    //Friday - 6
    //Saturday - 7
    for ((Serial.print(Time.weekday()) >= 1); (Serial.print(Time.weekday()) <= 7);)
    {
        //If the day of the week is between Monday and Friday, this code will run
        if ((Serial.print(Time.hour())>=2) && (Serial.print(Time.hour())<=6))
        {
            //If the time is between 6 and 7 in the morning, then code will run and proceed to the next level
            //NOTE: The time range is adjustable based on the time you normally wake up. This function works using the integers 0-23 where 0 is midnight and 23 is 11:00 P.M.
            if ((Serial.print(Time.hour()) >= 6) && (Serial.print(Time.hour()) <= 7))
            {
                //This requires the light level to be greater than the baseline level that was extablished in the setup
                if (analogRead(photoresistor) > startThreshold)
                {
                    //Once all the conditions have been met, this Particle will publish a message to the
                    //Particle connected to the coffee machine telling it to start making coffee
                    Particle.publish("startcoffee");
                    //As long as the time is within the time period specified above and the light level is above the baseline reading, the Particle will continue to publish messages, therefore a long enough delay was included so that the code will not run again until after the time period has passed
                    delay(3600000);
                }
            }
        }
        //if the day of the week is a Saturday or sunday, this code will work
        else
        {
            if ((Serial.print(Time.hour())>=8) && (Serial.print(Time.hour())<=9))
            {
                if (analogRead(photoresistor) > startThreshold)
                {
                    Particle.publish("startcoffee");
                    delay(3600000);
                }
            }
        }
    }    
}

Servo Particle Code

C/C++
//Declare that onservo and brewservo are Servos
Servo onservo;

Servo brewservo;

void setup()
{
    //Subscribes this Particle to the message startcoffee which is published by the Particle connected to the photo resistor
    Particle.subscribe("startcoffee", makecoffee, "ID of Photo Resistor Particle");
    
    //This is the function that is called when the DO Button is pressed on your phone
    Particle.function("startnow", startnow);
    
    //This declares the pins that the two servos are attached to
    onservo.attach(D0);
    brewservo.attach(D1);

    //This sets the two servos to their starting positions.
    onservo.write(0);
    brewservo.write(0); 
}

//This is the program that will run when the photo resistor Particle publishes the message startcoffee
void makecoffee(const char *event, const char *data)
{ 
    if(data)
    {                            
        //This will turn the power servo so that it pushes the power button and turns on the coffee machine
        onservo.write(70);
        delay(750);
        onservo.write(0);
        
        //This allows for the coffee machine to heat the water before the coffee can be made. This value will change based upon the type of coffee machine you use.
        delay(145000);
        
        //Pushes the button to start brewing coffee
        brewservo.write(40);
        delay(750);
        brewservo.write(0);
        
        //Sends a message to your phone letting you know the coffee has started
        Particle.publish("coffeestarted");
        
        //This is a delay for the coffee to brew
        delay(45000);
        
        //Sends a message to your phone telling you the coffee is done
        Particle.publish("coffeedone");
        
        //Puts a slight delay before the next function part of the code is run
        delay(300000);
        
        //Turns off the coffee machine
        //NOTE: depending on the type of coffee machine you have, this might not be necessary, some coffee machines such as Keurig turn off by themselves after a period of time, however old-school coffee machines do not, therefore this is included as a fire safety feature
        onservo.write(70);
        delay(750);
        onservo.write(0);
        
        //Sends a message to your phone telling you the coffee machine was turned off
        Particle.publish("machineoff");
    }
}   

//This is the function that will be run when you press the DO Button on your phone
int startnow(String command)
{ 
    //Tells the command that the DO Button must send for this code to run
    if(command == "now")
    {                            
        //This will send a message to the other Particle that will cause it to delay the code
        Particle.publish("wait");
        
        //This part is exactly the same as the section of code from above
        onservo.write(70);
        delay(750);
        onservo.write(0);
        
        delay(145000);
        
        brewservo.write(40);
        delay(750);
        brewservo.write(0);
        
        Particle.publish("coffeestarted");
        
        delay(45000);
        
        Particle.publish("coffeedone");
        
        delay(300000);
        
        onservo.write(70);
        delay(750);
        onservo.write(0);
        Particle.publish("machineoff");
    }
}

Single Particle Code

C/C++
//Declare that onservo and brewservo are Servos
Servo onservo;

Servo brewservo;

//Labels the pins that all the components are connected to
int photoresistor = A0;
int power = A5;
int boardLed = D7;

//These will be used to determine a base light level
int lighton;
int lightoff;
int startThreshold;

void setup()
{
    //This is the function that will be called when the DO Button is pressed on your phone
    Particle.function("startnow", startnow);
    
    //Declares the pins the servos are attached to and sets them to an initial position
    onservo.attach(D0);
    brewservo.attach(D1);
    onservo.write(0);
    brewservo.write(0);
    
    //Tells the Particle what your time zone is
    Time.zone(-4);
    
    //Declares the mode of the pins that are used and powers the photo resistor
    pinMode(photoresistor,INPUT);
    pinMode(power,OUTPUT);
    pinMode(boardLed,OUTPUT);
    digitalWrite(power,HIGH);
 
    //The D7 led will turn on to tell you to cover the photo resistor
    digitalWrite(boardLed,HIGH);
    delay(5000);
    
    //The light will turn off and readings will be taken
    digitalWrite(boardLed,LOW);
    
    int off_1 = analogRead(photoresistor);
    delay(300);
    int off_2 = analogRead(photoresistor);
    delay(300);
    
    //The led will flash to let you know the readings were taken
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    
    //The led will turn on to let you know to take your finger off the photo resistor
    digitalWrite(boardLed,HIGH);
    delay(5000);
    
    //The light will turn off and readings will be taken
    digitalWrite(boardLed,LOW);
    
    //NOTE: make sure the lights in the room are turned on when these readings are taken
    int on_1 = analogRead(photoresistor);
    delay(300);
    int on_2 = analogRead(photoresistor);
    delay (300);
    
    //The led will flash to let you know the readings were taken
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    digitalWrite(boardLed,HIGH);
    delay(300);
    digitalWrite(boardLed,LOW);
    delay(300);
    
    //These take the average values of the photo resistor when covered and uncovered
    lightoff = (off_1+off_2)/2;
    lighton = (on_1+on_2)/2;
    
    //This takes the average of the covered and uncovered values to determine a base light level for the time based functions to run
    startThreshold = 3*(lightoff+lighton)/4;
}

//This is the program that will run when you press the DO Button on your phone
int startnow(String command)
{
    //Tells the command that the DO Button must send for this part of the code to run
    if (command == "now")
    {
        //Turns on the coffee machine
        onservo.write(70);
        delay(750);
        onservo.write(0);
        
        //Allows the coffee machine time to heat up
        delay(90000);
        
        //Pushes the brew button
        brewservo.write(40);
        delay(750);
        brewservo.write(0);
        
        //Sends a message to your phone telling you coffee has started
        Particle.publish("coffeestarted");
        
        //Gives time while the coffee is brewing
        delay(45000);
        
        //Sends a message to your phone telling you the coffee is done
        Particle.publish("coffeedone");
        
        //delay before the machine is turned off
        delay(300000);
        
        //turns the coffee machine off for fire safety
        onservo.write(70);
        delay(750);
        onservo.write(0);
        
        //Sends a message to your phone telling you the coffee is off
        Particle.publish("machineoff");
        
        //When the DO Button is pressed on your phone to start making coffee, this portion of code run. To ensure that the time and light based portion of the code does not run and make another cup of coffee, this 'for' loop will delay the code long enough until the period of time that the coffee can be made has passed
        for ((Serial.print(Time.hour())>=0); (Serial.print(Time.hour())<=10);)
        {
            digitalWrite(boardLed,HIGH);
            delay(5000);
            digitalWrite(boardLed,LOW);
            delay(1800000);
        }
    }
}

void loop() 
{
 
    //If the current day is between Monday and Friday, this code will run and proceed to the next level
    //NOTE: It is possible to adjust the days of the week that you wish this code to run. Simply adjust
    //the numbers according to the days you wish to run the code.
    //Sunday - 1
    //Monday - 2
    //Tuesday - 3
    //Wednesday - 4
    //Thursday - 5
    //Friday - 6
    //Saturday - 7
    for ((Serial.print(Time.weekday()) >= 1); (Serial.print(Time.weekday()) <= 7);)
    {
        //If the day of the week is between Monday and Friday, this code will run
        if ((Serial.print(Time.hour())>=2) && (Serial.print(Time.hour())<=6))
        {
            //If the time is between 6 and 7 in the morning, then code will run and proceed to the next level
            //NOTE: The time range is adjustable based on the time you normally wake up. This function works using the integers 0-23 where 0 is midnight and 23 is 11:00 P.M.
            if ((Serial.print(Time.hour()) >= 6) && (Serial.print(Time.hour()) <= 7))
            {
                //This requires the light level to be greater than the baseline level that was extablished in the setup
                if (analogRead(photoresistor) > startThreshold)
                {
                    //Turns on the coffee machine
                    onservo.write(70);
                    delay(750);
                    onservo.write(0);
                    
                    //Allows the coffee machine time to heat up
                    delay(90000);
                    
                    //Pushes the brew button
                    brewservo.write(40);
                    delay(750);
                    brewservo.write(0);
                    
                    //Sends a message to your phone telling you coffee has started
                    Particle.publish("coffeestarted");
                    
                    //Gives time while the coffee is brewing
                    delay(45000);
                    
                    //Sends a message to your phone telling you the coffee is done
                    Particle.publish("coffeedone");
                    
                    //delay before the machine is turned off
                    delay(300000);
                    
                    //turns the coffee machine off for fire safety
                    onservo.write(70);
                    delay(750);
                    onservo.write(0);
                    
                    //Sends a message to your phone telling you the coffee is off
                    Particle.publish("machineoff");
                    delay(3600000);
                }
            }
        }
        //if the day of the week is a Saturday or sunday, this code will work
        else
        {
            if ((Serial.print(Time.hour())>=8) && (Serial.print(Time.hour())<=9))
            {
                if (analogRead(photoresistor) > startThreshold)
                {
                    //Turns on the coffee machine
                    onservo.write(70);
                    delay(750);
                    onservo.write(0);
                    
                    //Allows the coffee machine time to heat up
                    delay(90000);
                    
                    //Pushes the brew button
                    brewservo.write(40);
                    delay(750);
                    brewservo.write(0);
                    
                    //Sends a message to your phone telling you coffee has started
                    Particle.publish("coffeestarted");
                    
                    //Gives time while the coffee is brewing
                    delay(45000);
                    
                    //Sends a message to your phone telling you the coffee is done
                    Particle.publish("coffeedone");
                    
                    //delay before the machine is turned off
                    delay(300000);
                    
                    //turns the coffee machine off for fire safety
                    onservo.write(70);
                    delay(750);
                    onservo.write(0);
                    
                    //Sends a message to your phone telling you the coffee is off
                    Particle.publish("machineoff");
                    delay(3600000);
                }
            }
        }
    }    
}

Credits

David Miller Jr.

David Miller Jr.

1 project • 0 followers
Joshua Holler

Joshua Holler

1 project • 0 followers

Comments