Pepe Ruiz
Published © CC0

My Guardian for the Workshop

Device will send emails if the door or window has been opened, and will monitor the temperature and humidity of the place.

IntermediateFull instructions provided3 hours587
My Guardian for the Workshop

Things used in this project

Hardware components

Photon
Particle Photon
×1
Resistor 10k ohm
Resistor 10k ohm
×1
General Purpose Transistor NPN
General Purpose Transistor NPN
×1
Relay (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Workshop Guardian

Workshop guardian qj1p07cg1q

Materials

Wp 20170815 09 53 54 rich1 vfqc2zh7jc

Google Docs Chart

Googlechart gg1nxafn6f

Code

Workshop guardian code

C/C++
This code reads two magnetic door sensors and publish the variable in particle cloud. then I use IFTTT to send me an email when the door has been open. Also I added a DHT22 sensor to measure the temperature and humidity of the workshop. this data goes to public variables and I build charts in Google docs with this info.
// This #include statement was automatically added by the Particle IDE.
#include <Adafruit_DHT.h>

#define DOOR_SENSOR_PIN     D0
#define WINDOW_SENSOR_PIN   D1
#define ON_BOARD_LED        D7

#define CLOSED  0
#define OPEN    1 




/************************************************************/
/* DHT 22 Sensor section */
#define DHTPIN D2     // what pin we're connected to

#define DHTTYPE DHT22		// DHT 22 (AM2302)

// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);



/* Global Variables */
char resultstr[64];


double humidity=0;    
double temperature=0; 
int DoorState = CLOSED;
int WindowState = CLOSED;
int Prev_DoorState = 0;
int Prev_WindowState = 0;




/*****************************************************************************************************/
void setup() 
{
    /* Peripheral Configuration */
    Serial.begin(9600);                         // Serial Port Configuration
    pinMode(DOOR_SENSOR_PIN,INPUT);             // Door Sensor
    pinMode(WINDOW_SENSOR_PIN,INPUT_PULLDOWN);  // Window Sensor
    pinMode(ON_BOARD_LED,OUTPUT);               // System Armed Indicator

    /* Particle Variables to publish */
    Particle.variable("temperature",temperature); 
    Particle.variable("humidity",humidity); 
    Particle.variable("result", resultstr, STRING); 
   
    /* DHT Sensot setup */
	Serial.println("DHTxx test!");
	dht.begin();
	delay(2000);
}

 


/*****************************************************************************************************/
void loop() 
{
    
    
    /* Reading Temperature and Humidity Sensors */
	humidity = dht.getHumidity();       // Read Humidity
	temperature = dht.getTempCelcius(); // Read temperature
	
	Serial.print("Humid: "); 
	Serial.print(humidity);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(temperature);
	Serial.println("*C ");

    /* Build a Json with the sensor information to make it available for G Docs */
    sprintf(resultstr, "{\"temperature\":%2f,\"humidity\":%2f}", temperature, humidity); 

	
    /* Checking Door and Window Sensors */
    if(digitalRead(DOOR_SENSOR_PIN))
    {
        DoorState=1;
    }
    else
    {
        DoorState=0;
    }

    if(!digitalRead(WINDOW_SENSOR_PIN))
    {
        WindowState=1;
    }
    else
    {
        WindowState=0;
    }


    /* Checking last door status  */
    if(DoorState!=Prev_DoorState)
    {
        Prev_DoorState = DoorState;
        if(DoorState == CLOSED)
        {
            Serial.println("Door Closed");
            #ifndef PUBLISH_DISABLED 
            Particle.publish("Door","Closed");
            #endif
        }
        else
        {
            Serial.println("Door Open");
            #ifndef PUBLISH_DISABLED 
            Particle.publish("Door","Open");
            #endif
        }
    }

    /* Checking last Window status  */
    if(WindowState!=Prev_WindowState)
    {
        Prev_WindowState = WindowState;    
        if(WindowState == CLOSED)
        {
            Serial.println("Window Closed");
            #ifndef PUBLISH_DISABLED 
            Particle.publish("Window","Closed");
            #endif
        }
        else
        {
            Serial.println("Window Open");
            #ifndef PUBLISH_DISABLED 
            Particle.publish("Window","Open");
            #endif
        }
    }

    delay(2000);
}

Google spreadsheet Code

JavaScript
Google Spreadsheet script to get info from Particle IO
/*
Create a spreadhsheet in google docs
go to Tools -> Script Editor
Paste the code below.. Don´t forget to add your own particle credentials
click on "current project triggers" (the small clock left to the play button) and select how often you wanto to read from particle cloud.
* Remember that if you read very often you will fullfill your doc very fast and will crash so I recomend to update every 15 or 30 minutes

*/


function collectData() {
  var  sheet = SpreadsheetApp.getActiveSheet();

  var response = UrlFetchApp.fetch("https://api.particle.io/v1/devices/3e0028001447353236343033/result?access_token=6eda6b98a7dc48a03445fd194c1e973fc2cfbd18");

  try {
    var response = JSON.parse(response.getContentText()); // parse the JSON the Core API created
    var result = unescape(response.result); // you'll need to unescape before your parse as JSON
  
    try {
      var p = JSON.parse(result); // parse the JSON you created
      var d = new Date(); // time stamps are always good when taking readings
      sheet.appendRow([d, p.temperature, p.humidity]); // append the date, data1, data2 to the sheet
    } catch(e)
    {
      Logger.log("Unable to do second parse");
    }
  } catch(e)
  {
    Logger.log("Unable to returned JSON");
  }
}

Credits

Pepe Ruiz

Pepe Ruiz

0 projects • 9 followers
Mexican Embedded Engineer
Contact

Comments