-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathESP32-Sensor.cpp
More file actions
103 lines (85 loc) · 2.77 KB
/
Copy pathESP32-Sensor.cpp
File metadata and controls
103 lines (85 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Fixed Sketch for Arduino IoT Cloud Thing "security_KerD"
https://create.arduino.cc/cloud/things/9e615aa7-4e71-4b28-8ffd-43d3f9e1a5d9
*/
#include "thingProperties.h"
// Pin definitions
int microwavePin = 3; // Presence/microwave sensor on digital pin 3
int LedPin = 10; // LED on digital pin 10
// Activity counter variables
// int activityCount = 0;
unsigned long lastResetTime = 0;
const unsigned long RESET_INTERVAL = 3600000; // 1 hour in milliseconds
bool lastPresenceState = false;
void setup() {
// Initialize serial
Serial.begin(9600);
delay(1500);
// Initialize Cloud properties
initProperties();
// Set pin modes
pinMode(microwavePin, INPUT);
pinMode(LedPin, OUTPUT);
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
// Debug info
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Initialize timer
lastResetTime = millis();
}
void loop() {
ArduinoCloud.update();
// Read presence sensor from pin
bool currentPresence = digitalRead(microwavePin);
presence = currentPresence; // Update cloud variable
presenceInt = currentPresence;
// Detect rising edge (presence sensor turns ON)
if (currentPresence && !lastPresenceState) {
activityCount++;
Serial.print("Activity detected! Total count: ");
Serial.println(activityCount);
}
// Update last state
lastPresenceState = currentPresence;
// Check if one hour has passed to reset the counter
if (millis() - lastResetTime >= RESET_INTERVAL) {
activityCount = 0;
lastResetTime = millis();
Serial.println("Activity counter reset after 1 hour");
}
// Control LED from IoT Cloud variable
digitalWrite(LedPin, lED ? HIGH : LOW);
// Debug info every few seconds (optional - remove if too verbose)
static unsigned long lastPrint = 0;
if (millis() - lastPrint >= 5000) {
Serial.print("Activities in last hour: ");
Serial.println(activityCount);
lastPrint = millis();
}
}
/*
Cloud variable callback functions
*/
void onLEDChange() {
// Reflect LED control immediately when changed from dashboard
digitalWrite(LedPin, lED ? HIGH : LOW);
}
void onPresenceChange() {
// This callback is triggered when presence changes from the cloud dashboard
// The main detection logic is in loop() when reading from the physical pin
}
/*
Since ActivityCount is READ_WRITE variable, onActivityCountChange() is
executed every time a new value is received from IoT Cloud.
*/
void onActivityCountChange() {
// Add your code here to act upon ActivityCount change
}
/*
Since PresenceInt is READ_WRITE variable, onPresenceIntChange() is
executed every time a new value is received from IoT Cloud.
*/
void onPresenceIntChange() {
// Add your code here to act upon PresenceInt change
}