Silicon
Published

ESP8266 - PIR Motion sensor detection

In this project, we are looking to detect motion (change) not average IR levels.

IntermediateFull instructions provided2,283
ESP8266 - PIR Motion sensor detection

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
PIR Motion Sensor (generic)
PIR Motion Sensor (generic)
×1
LED (generic)
LED (generic)
×1

Software apps and online services

Cayenne
myDevices Cayenne

Story

Read more

Code

ESP8266 - PIR Motion sensor detection

Arduino
//#define CAYENNE_DEBUG         // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  //Comment this out to disable prints and save space
#include <CayenneWiFi.h>

// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "AuthenticationToken";
// Your network name and password.
char ssid[] = "NetworkSSID";
char password[] = "NetworkPassword";

// 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.
const 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

Silicon

Silicon

0 projects • 3 followers

Comments