Brandon Poulton
Published © MIT

Mail

Detect activity in your physical mailbox from anywhere in the world.

BeginnerFull instructions provided1 hour1,188
Mail

Things used in this project

Hardware components

NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Pir Motion sensor
×1
USB Powerbank
×1

Software apps and online services

Cayenne
myDevices Cayenne
Arduino IDE
Arduino IDE

Story

Read more

Code

Mail

Arduino
#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space

#include <CayenneDefines.h>
#include <BlynkSimpleEsp8266.h>
#include <CayenneWifiClient.h>
#include <Arduino.h>
#include <SoftwareSerial.h>



// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "";
char ssid[] = "";
char password[] = "";

// Virtual Pin of the Digital Motion Sensor widget.
#define VIRTUAL_PIN V1

// Digital pin the motion sensor is connected to. Do not use digital pins 0 or 1 since those conflict with the use of Serial.
int motionSensorPin = 4;

void setup()
{
  Serial.begin(9600);
  Cayenne.begin(token, ssid, password);
 
}

void loop()
{
  Cayenne.run();
  checkSensor(); 
}


int previousState = -1;
int currentState = -1;
unsigned long previousMillis = 0;

void checkSensor()
{
  unsigned long currentMillis = millis();
  // Check sensor data every 250 milliseconds
  if (currentMillis - previousMillis >= 250) {
    // Check the sensor state and send data when it changes.
    currentState = digitalRead(motionSensorPin);
    if (currentState != previousState) {
      Cayenne.virtualWrite(VIRTUAL_PIN, currentState);
      previousState = currentState;
    }
        previousMillis = currentMillis;
  }
}

Credits

Brandon Poulton

Brandon Poulton

2 projects • 1 follower

Comments