Welcome to Hackster!
Hackster is a community dedicated to learning hardware, from beginner to pro. Join us, it's free!
David K
Published © GPL3+

Fridge door alarm, ESP8266, Alexa notification

A magnetic sensor detects that the fridge door is open, then sends notification to Alexa, which declares that "Fridge door is open".

BeginnerFull instructions provided2 hours960
Fridge door alarm, ESP8266, Alexa notification

Things used in this project

Hardware components

Adafruit Feather HUZZAH with ESP8266 WiFi
Adafruit Feather HUZZAH with ESP8266 WiFi
×1
Reed Switch, SPST-NO
Reed Switch, SPST-NO
×2
Jumper wires (generic)
Jumper wires (generic)
×2

Software apps and online services

Arduino IDE
Arduino IDE
Sinric
Alexa Voice Service
Amazon Alexa Alexa Voice Service

Story

Read more

Schematics

Circuit Schematic

the sensor with two reed switches in series is mounted to the fridge and two magnets are attached to the doors.

Code

Arduino code

Arduino
/* Magnetic switch for fridge door with Sinric by David K
 * In this example the close door even is sent only when it changes, 
 * The open door event is sent when the fridge is open for longer then AlertTimeInSec
 */

#include <ESP8266WiFi.h>
#include <Arduino.h>

#include <SinricPro.h>
#include <SinricProContactsensor.h>

#define WIFI_SSID         "*******"           // Your wifi network ID
#define WIFI_PASS         "*******"          // Your wifi Password
#define APP_KEY           "********************************"                                        // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
#define APP_SECRET        ***********************************************************************"   // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
#define CONTACT_ID        "******************************"                                                    // Should look like "5dc1564130xxxxxxxxxxxxxx"
#define BAUD_RATE         9600                // Change baudrate to your need

#define MAGPIN       12                       // PIN where contactsensor is connected to
                                              // HIGH  = contact is open
                                              // LOW = contact is closed
#define AlertTimeInSec    60   // time after which to alert 
#define CycleTimeInSec    10  // how often it measures the door

bool myPowerState = 1;                        // assume device is turned on
bool contactState = 0;                        // Status of the switch defined as LOW or 0 or door closed in the beginning
bool lastContactState = 0;
unsigned long lastCheck = 0;

void handleContactsensor() {
  if (!myPowerState) return;                  // if device switched off...do nothing

  contactState = digitalRead(MAGPIN);   // read actual state of contactsensor
  unsigned long actualMillis = millis();
  if (actualMillis - lastCheck < CycleTimeInSec*1000) return; // check status every second

  if (contactState == 0) {
    if (lastContactState == 1) {
      Serial.printf("Fridge door is closed\n");
      SinricProContactsensor &myContact = SinricPro[CONTACT_ID]; // get contact sensor device
      myContact.sendContactEvent(!contactState);            // send event with actual state
    }
    lastCheck = actualMillis;
    lastContactState = contactState;
    return;
  }
  
  if ((actualMillis - lastCheck > AlertTimeInSec*1000) && (contactState == 1)){        // not processing if less than 1min
    Serial.printf("Fridge door is open\n");
    SinricProContactsensor &myContact = SinricPro[CONTACT_ID]; // get contact sensor device
    myContact.sendContactEvent(!contactState);            // send event with actual state
    lastCheck = actualMillis;
    lastContactState = contactState;
  }
  
}

bool onPowerState(const String &deviceId, bool &state) {
  Serial.printf("Device %s turned %s (via SinricPro) \r\n", deviceId.c_str(), state?"on":"off");
  myPowerState = state;
  return true; // request handled properly
}


// setup function for WiFi connection
void setupWiFi() {
  Serial.printf("\r\n[Wifi]: Connecting");
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.printf(".");
    delay(250);
  }
  IPAddress localIP = WiFi.localIP();
  Serial.printf("connected!\r\n[WiFi]: IP-Address is %d.%d.%d.%d\r\n", localIP[0], localIP[1], localIP[2], localIP[3]);
}

// setup function for SinricPro
void setupSinricPro() {
  // add device to SinricPro
  SinricProContactsensor& myContact = SinricPro[CONTACT_ID];

  // set callback function to device
  myContact.onPowerState(onPowerState);

  // setup SinricPro
  SinricPro.onConnected([](){ Serial.printf("Connected to SinricPro\r\n"); });
  SinricPro.onDisconnected([](){ Serial.printf("Disconnected from SinricPro\r\n"); });
  SinricPro.begin(APP_KEY, APP_SECRET);
}

// main setup function
void setup() {
  Serial.begin(BAUD_RATE); Serial.printf("\r\n\r\n");

  pinMode(MAGPIN, INPUT_PULLUP);

  setupWiFi();
  setupSinricPro();
  
  contactState = digitalRead(MAGPIN);
  SinricProContactsensor &myContact = SinricPro[CONTACT_ID]; // get contact sensor device
  myContact.sendContactEvent(!contactState);            // send event with actual state
  lastContactState = contactState;
}

void loop() {
  handleContactsensor();
  SinricPro.handle();
}

Credits

David K
2 projects • 3 followers
I was born in 2007, I was in middle school when I posted my first project.
Contact

Comments

Please log in or sign up to comment.