Zachary DarnellPaul Edwards
Published © GPL3+

Smart Grocery List

Every get to the store and then wonder if you have any milk? With the Smart Grocery List, this wont be a problem any longer.

BeginnerShowcase (no instructions)8 hours1,392
Smart Grocery List

Things used in this project

Hardware components

Photon
Particle Photon
×2
Photo resistor
Photo resistor
×2
Resistor 330 ohm
Resistor 330 ohm
×4
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×2
extension cord (generic)
×2
Refrigerator (generic)
Recommend using one already available instead of purchasing
×1
LED (generic)
LED (generic)
×2

Software apps and online services

Maker service
IFTTT Maker service
ThingSpeak API
ThingSpeak API
Google Sheets
Google Sheets

Story

Read more

Schematics

Photoresistor Circuit

Code

Fridge Door Code

C/C++
This code is for the particle placed in the refrigerator, publishes the door open and door closed variables used by IFTTT. It also integrates the photoresistor reading to Thingspeak
// This app will read the brightness reading off of the photoresistor. Then, if the read exceeds the threshold level, it will publish the variable stating that the frigde is open

int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.
int boardLed = D7; // This is the LED that is already on your device.


int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).

int power = A5;

int lightlevel;
int Fridge;
// The following values are the read values of the photoresistor 
int Closedvalue=5; // This is the average value that the photoresistor reads while in the fridge.
int Openvalue=260; // This is the average value that the photoresistor reads when exposed to average lighting.
int Cutoff=((Closedvalue+Openvalue)/2);

bool Dooropen = false;







void setup() {
 pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
  pinMode(boardLed,OUTPUT); // Our on-board LED is output as well
  pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
  pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)

digitalWrite(power,HIGH);



 Particle.variable("analogvalue", &lightlevel, INT);



}



void loop() {
/* In this loop function, it checks to see if the light level means the door is open
  When the status of the door changes, we'll send a Particle.publish() 


 It will also turn the LEDs on when the Photoresistor detects a light
  */

lightlevel=analogRead(photoresistor);
  
 // Get some data
  String data = String(10);
  // Trigger the integration
  //Particle.publish("Fridge", data, PRIVATE);
  // Wait 60 seconds
  //updateThingspeak();
  delay(2000);
  
 


  if (analogRead(photoresistor)<Cutoff){
      
      
    if (Dooropen==true) {
        // If the door was already open, then we assume you have closed it
        // We will send a publish to the cloud and blink LED on.

        // Send a publish to your devices...
        Particle.publish("doorStatus","Closed",60);
        // And flash the on-board LED on and off.
        digitalWrite(boardLed,HIGH);
        digitalWrite(led,HIGH);
        delay(3000);
        digitalWrite(boardLed,LOW);
        digitalWrite(led,LOW);
        // And change the status of the door
        Dooropen=false;
    }
    else {
        // Otherwise, this isn't a new status, and we don't have to do anything.
        // This way, the device will not continue to send door open and door closed notifications if it is held open
    }
    }
  
  else 
      // If you are above the threshold, assume the door is open
      if (Dooropen==false) {  // If the door is already closed, we must have opened it

        // Send a publish...
        Particle.publish("doorStatus","Open",60);
        // And flash the  LED on and off.
        digitalWrite(boardLed,HIGH);
        digitalWrite(led,HIGH);
        delay(3000);
        digitalWrite(boardLed,LOW);
        digitalWrite(led,LOW);

        // and change the status of the door
        Dooropen=true;
      }
      else {
          // Otherwise, this isn't a new status, and we don't have to do anything.
      }
   
    
}

bool updateThingspeak() {
      
      bool success = Particle.publish("Fridge",+  "{ \"1\": \"" + String(lightlevel) + "\"}",60,PRIVATE);
      return success;
  }

MilkSensor

C/C++
This code was used to operate and read a photoresistor to tell if a gallon of milk was in close proximity to it.
int led = D0; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.
int boardLed = D7;
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).

int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
// The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
// That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.

int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
int present;
int critical_value = 30;
int dooropencutoff = 90;
int door;

bool dooropen = false;
bool milkpresent = true;

String data;  //string global variable for publishing

// Next we go into the setup function.

void setup() {

    // First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
    pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
    pinMode(photoresistor,INPUT);  // Our photoresistor pin is input (reading the photoresistor)
    pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)
    pinMode(boardLed,OUTPUT);
    // Next, write the power of the photoresistor to be the maximum possible, so that we can use this for power.
    digitalWrite(power,HIGH);

    // We are going to declare a Paritcle.variable() here so that we can access the value of the photoresistor from the cloud.
    Particle.variable("analogvalue", &analogvalue, INT);
    // This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.
    Particle.variable("door", &door, INT);
    // We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
   Particle.subscribe("doorStatus",myHandler);
}

 
void myHandler(const char *event,const char *data)
{  if (dooropen==true) {
    if (strcmp(data,"Closed")==0) {
    // if your buddy's beam is intact, then turn your board LED off
    door=0;
   dooropen=false;
   delay(12000);
   digitalWrite(boardLed,HIGH);
   delay(1000);
    digitalWrite(led,HIGH);
    delay(3000);
   analogvalue = analogRead(photoresistor);
   if (milkpresent==true){
   if  (analogvalue<critical_value) {
    Particle.publish("Milk","No");
    milkpresent=false;
    present=0;
    updateThingspeak();
    }}
    else{
    if (analogvalue>dooropencutoff){
    }
    else if(analogvalue>critical_value) {Particle.publish("Milk","Yes");
    milkpresent=true;
    present=1;
    updateThingspeak();
    }
    else{}
    }
    digitalWrite(boardLed,LOW);
    digitalWrite(led,LOW);
    }
  else {
  //Particle.publish("Door","Open");
    // if the data is something else, don't do anything.
    // Really the data shouldn't be anything but those two listed above.
  }}
  else {
        if (strcmp(data,"Open")==0){
      door=1;
      dooropen=true;
    }
        else{
    }
  }
}

bool updateThingspeak() 
{
     
        bool success = Particle.publish("Thingspeak", +
     "{ \"1\": \"" + String(present) + "\" }", 60, PRIVATE);
    return success; //if sent, then turn of the send flag, otherwise let it try again.
    
}

Credits

Zachary Darnell

Zachary Darnell

1 project • 2 followers
Paul Edwards

Paul Edwards

1 project • 1 follower

Comments