1 |
#include <Arduino.h>
|
2 |
#include <Wire.h>
|
3 |
#include <Adafruit_Sensor.h>
|
4 |
#include <Adafruit_LSM303_U.h>
|
5 |
#include <Adafruit_9DOF.h>
|
6 |
#include <esp_sleep.h>
|
7 |
#include <WiFi.h>
|
8 |
#include <LSM303.h>
|
9 |
|
10 |
/* Sensor-Initialisierung */
|
11 |
Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301);
|
12 |
Adafruit_LSM303_Mag_Unified mag = Adafruit_LSM303_Mag_Unified(30302);
|
13 |
LSM303 lsm; // 9DOF-Objekt
|
14 |
|
15 |
// Bewegungserkennungs-Parameter
|
16 |
float movementThreshold = 0.05; // Schwellenwert für Bewegungserkennung
|
17 |
sensors_event_t lastAccelEvent;
|
18 |
sensors_event_t lastMagEvent;
|
19 |
unsigned long noMovementDuration = 0;
|
20 |
const unsigned long noMovementThreshold = 2000; // 2 Sekunden ohne Bewegung
|
21 |
|
22 |
// Pin-Zuweisungen
|
23 |
const int sda = 21;
|
24 |
const int scl = 22;
|
25 |
const int intPin = 15; // Interrupt-Pin für LSM303
|
26 |
|
27 |
// Funktion zur Bewegungserkennung
|
28 |
bool detectMovement(sensors_event_t *currentEvent) {
|
29 |
float deltaY = abs(currentEvent->acceleration.y - lastAccelEvent.acceleration.y);
|
30 |
float deltaZ = abs(currentEvent->acceleration.z - lastAccelEvent.acceleration.z);
|
31 |
return (deltaY > movementThreshold || deltaZ > movementThreshold);
|
32 |
}
|
33 |
|
34 |
void setup() {
|
35 |
Serial.begin(115200);
|
36 |
Serial.println("Woke up");
|
37 |
|
38 |
// I2C-Initialisierung
|
39 |
Wire.begin(sda, scl);
|
40 |
delay(500);
|
41 |
|
42 |
// LSM303 Initialisierung
|
43 |
if (!lsm.init()) {
|
44 |
Serial.println("Failed to initialize LSM303. Check your wiring!");
|
45 |
while (1);
|
46 |
}
|
47 |
|
48 |
// Sensor initialisieren
|
49 |
if (!accel.begin()) {
|
50 |
Serial.println(F("Ooops, no LSM303 accel detected ... Check your wiring!"));
|
51 |
while (1);
|
52 |
}
|
53 |
if (!mag.begin()) {
|
54 |
Serial.println("Ooops, no LSM303 mag detected ... Check your wiring!");
|
55 |
while (1);
|
56 |
}
|
57 |
|
58 |
// Konfiguration des LSM303 Accelerometers
|
59 |
lsm.writeAccReg(LSM303::CTRL_REG1_A, 0x27); // Normal Mode, 50Hz, alle Achsen aktiv
|
60 |
lsm.writeAccReg(LSM303::CTRL_REG2_A, 0x00); // Kein High-Pass Filter
|
61 |
lsm.writeAccReg(LSM303::CTRL_REG3_A, 0b01000100); // Interrupt auf INT1 enabled
|
62 |
lsm.writeAccReg(LSM303::CTRL_REG4_A, 0x00); // +-2g Empfindlichkeit
|
63 |
lsm.writeAccReg(LSM303::INT1_CFG_A, 0b00101010); // ZHIE und ZLIE aktiv
|
64 |
lsm.writeAccReg(LSM303::INT1_THS_A, 80); // Bewegungsschwelle, ~16mg pro LSB
|
65 |
lsm.writeAccReg(LSM303::INT1_DURATION_A, 0x00); // Dauer für Interrupt, 0ms
|
66 |
|
67 |
// ESP32 Konfiguration für Deep Sleep
|
68 |
esp_sleep_enable_ext0_wakeup(GPIO_NUM_15, 1); // Wakeup bei INT1
|
69 |
WiFi.mode(WIFI_OFF); // Deaktiviert WiFi
|
70 |
btStop(); // Deaktiviert Bluetooth
|
71 |
setCpuFrequencyMhz(80); // CPU-Frequenz reduzieren
|
72 |
|
73 |
// Speichere initiale Sensorwerte
|
74 |
accel.getEvent(&lastAccelEvent);
|
75 |
mag.getEvent(&lastMagEvent);
|
76 |
}
|
77 |
|
78 |
void loop() {
|
79 |
// Neue Sensorwerte lesen
|
80 |
sensors_event_t magEvent;
|
81 |
mag.getEvent(&magEvent);
|
82 |
sensors_event_t accelEvent;
|
83 |
accel.getEvent(&accelEvent);
|
84 |
|
85 |
// Bewegungserkennung
|
86 |
if (detectMovement(&accelEvent)) {
|
87 |
// Bewegung erkannt: Daten ausgeben
|
88 |
Serial.println(F("Movement detected! Sensor data:"));
|
89 |
Serial.print(F("ACCEL X: ")); Serial.print(accelEvent.acceleration.x);
|
90 |
Serial.print(F(" Y: ")); Serial.print(accelEvent.acceleration.y);
|
91 |
Serial.print(F(" Z: ")); Serial.println(accelEvent.acceleration.z);
|
92 |
Serial.print(F("MAG X: ")); Serial.print(magEvent.magnetic.x);
|
93 |
Serial.print(F(" Y: ")); Serial.print(magEvent.magnetic.y);
|
94 |
Serial.print(F(" Z: ")); Serial.println(magEvent.magnetic.z);
|
95 |
Serial.println();
|
96 |
|
97 |
// Letzte Werte aktualisieren
|
98 |
lastAccelEvent = accelEvent;
|
99 |
lastMagEvent = magEvent;
|
100 |
|
101 |
// Bewegung erkannt, ESP bleibt wach
|
102 |
noMovementDuration = 0;
|
103 |
delay(1000); // Verzögerung für Ausgabe
|
104 |
} else {
|
105 |
// Keine Bewegung erkannt
|
106 |
noMovementDuration += 1000;
|
107 |
delay(1000);
|
108 |
|
109 |
// Übergang in Deep Sleep
|
110 |
if (noMovementDuration >= noMovementThreshold) {
|
111 |
Serial.println(F("No movement detected, entering deep sleep..."));
|
112 |
esp_deep_sleep_start();
|
113 |
}
|
114 |
}
|
115 |
}
|