Mark Leasure
Published © GPL3+

Automatic Mailbox Notification Unit

This sensor uses a magnetic reed switch to send an email notification via IFTTT, a Thingspeak graph, and an SMS text when triggered.

IntermediateFull instructions provided11 hours2,160
Automatic Mailbox Notification Unit

Things used in this project

Hardware components

Photon
Particle Photon
×2
SparkFun Magnetic Door Switch
×1
Breadboard (generic)
Breadboard (generic)
×2
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
×2
Wireless Battery Pack
×1
Samsung Galaxy S6 Edge (or any smartphone)
×1

Software apps and online services

Ubidots
Ubidots
Maker service
IFTTT Maker service
Twilio
ThingSpeak API
ThingSpeak API

Story

Read more

Schematics

Mailbox_Sensor (First Particle)

Particle Photon Subsciber

Code

DOOR_STATUS

C/C++
This code goes into the first Particle breadboard setup. It records when the magnetic reed switch is engaged and disengaged and publishes its data.
////////////////////////////////////////Mark Leasure's Mailbox Notification Sensor////////////////////////////////////////////////

// The function of the mailbox notification sensor is as follows:
//      1. Use magnetic reed switch to trigger if/then loop in code
//      2. If/then loop publishes information to IFTTT apps, which send an email and an sms text to my smartphone.
//      3. If/then loop writes information to Thingspeak graph.
//      4. If/then loop sends code to Twilio APP, which then sends SMS text to my smartphonw.
//      5. Code sends voltage information to Ubidots.
//      6. Particle code is subscribed to by second Particle Photon.

// The Thingspeak Libray widget must be added for publishing to work.
#include "ThingSpeak/ThingSpeak.h"

TCPClient client;

// The HttpClient Library widget was added for making sure the reed switch was functioning
#include "HttpClient/HttpClient.h"

// ---------------------------------------------
// Check if the sensor is open. Send to Ubidots.
// Ubidots is a web-based sensor data collection
// service. 
// ---------------------------------------------
// thanks to susan.donovan@bcc.cuny.edu for the ubidots example code

//Load HTTP libray needed to connect to Ubidots.com
HttpClient http;

//These values come from Ubidots profile information.
#define VARIABLE_ID "583a03847625421a8a18cfd2"
#define VARIABLE_ID_analog "583a03ee7625421ea119dceb" 
#define TOKEN "PW64DEs3zfmqATQnCKmO3R4YtFwb9E"


// Headers currently need to be set at init, useful for API keys etc.
http_header_t headers[] = {

    { "Content-Type", "application/json" },
    { "X-Auth-Token" , TOKEN },
    { NULL, NULL } // NOTE: Always terminate headers with NULL
};

http_request_t request;
http_response_t response;

// List your variables for each scenario:
char resultstr[64];
char resultstrAnalog[64];
int unixTime = 0;
int doorStat = 0;
int doorStatOLD = 0;
int rawAnalogStrength = 0;
unsigned long myChannelNumber = 195557;
const char * myWriteAPIKey = "D1P5OY7GZ0S4NIUC";

// Create the setup for the Particle code.
void setup() {
//Create particle variables    
    Particle.variable("isClosed", &doorStat, INT);
    Particle.variable("whenToggle", &unixTime, INT);
    Particle.variable("analogSignal", &rawAnalogStrength, INT);
// Initialize D0 pin as output   
    pinMode(D0, OUTPUT);
// WKP pin is reed switch input.
    pinMode(WKP, INPUT);
    request.hostname = "things.ubidots.com";
    request.port = 80;
// Run the Thingspeak Application
    ThingSpeak.begin(client);
}

void loop() {
// The returned value from the Core is going to be in the range from 0 to 4095

        rawAnalogStrength = analogRead(WKP);
        if (rawAnalogStrength < 400) {
            doorStatOLD = doorStat; // Save current value for later
            doorStat = 1; //closed
            digitalWrite(D0, LOW);    // Turn OFF the LED pins
            delay(100); 
           
            float voltage = rawAnalogStrength * (3.3 / 4095.0);
            ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
            delay(20000); // ThingSpeak will only accept updates every 15 seconds.
            
        } else {
            doorStatOLD = doorStat; // Save current value for later
            doorStat = 0; //open
            digitalWrite(D0, HIGH);   // Turn ON the LED pins
            delay(100); 
            
            Particle.publish("doorStat","0"); // Publishes to IFTT
            
            float voltage = rawAnalogStrength * (3.3 / 4095.0);
            ThingSpeak.writeField(myChannelNumber, 1, voltage, myWriteAPIKey);
            delay(20000); // ThingSpeak will only accept updates every 15 seconds
        
            Particle.publish("mailbox_opened");
        }
//Thanks to Hans for the Thinspeak code base.  
if (doorStatOLD != doorStat) { //If the door has opened or closed since the last reading then
// Send to Ubidots

    request.path = "/api/v1.6/variables/"VARIABLE_ID_analog"/values"; //send sensor data
    sprintf(resultstrAnalog, "{\"value\":%i}",rawAnalogStrength); 
    request.body = resultstrAnalog;
    http.post(request, response, headers);
    Serial.println(response.body);
    
    

    request.path = "/api/v1.6/variables/"VARIABLE_ID"/values"; //send binary result
    sprintf(resultstr, "{\"value\":%i}",doorStat); 
    request.body = resultstr;
    http.post(request, response, headers);
    Serial.println(response.body);
    delay(100);
}
}

JARVIS_RECEIVE

C/C++
This Particle subscribes to the data published by the first Particle and sends the Twilio function.
///////////Second Particle////////////////

//This code subscribes to the event in the first particle uses the data to send SMS code to Twilio.
//The LED function was for the sake of ensuring data communication.
int boardLed = D7;


void setup() {
    pinMode(boardLed,OUTPUT);
    //  sets the LED as an output for the Photon
    digitalWrite(boardLed,LOW);
    //  sets the pin to low to start
    Particle.subscribe("doorstatus", doorstatus,"1be36e12f6fd2db308351368df4a8b6006c15572");
    //  this allows the Photon to subscribe to the event published by the first Photon
}

void doorstatus(const char *event, const char *data)    {
    
    digitalWrite(boardLed,HIGH);
    Particle.publish("mailbox_opened");
}

Twilio Webhook

C/C++
This webhook implements your Twilio credentials and allows you to publish your data from the Twilio app to you phone via SMS text.
///Webhook Twilio//
{
  "eventName": "mailbox_opened",
  "url": "https://www.twilio.com/console/sms/dashboard",
  "requestType": "POST",
  "auth": {
    "username": "AC4e30db17ff81969d322063bb0c350c00",
    "password": "2102fde8aa1e7fcff0e6c69f5bd03496"
  },
  "form": {
    "From" : "+19803540241",
    "To" : "+17049937630",
    "Body": "Your Mail Has Arrived!"
  },
  "mydevices": true
}

Credits

Mark Leasure

Mark Leasure

1 project • 2 followers
UNCC mechanical engineering student
Thanks to Eric Ely and futurebird.

Comments