#include <Arduino.h>
#include <WiFi.h>
#include <esp_sleep.h>
#include <WiFiUdp.h>
#include <LittleFS.h>
#define INTERVAL 600000 // 10 minutes in milliseconds
RTC_DATA_ATTR unsigned long wakeUpCount = 0;
RTC_DATA_ATTR unsigned long lastInterval = 0;
RTC_DATA_ATTR unsigned long activityDurations[4] = {0, 0, 0, 0}; // Resting, Walking, Running, Playing
enum ActivityType { RESTING, WALKING, RUNNING, PLAYING };
struct IMUdata {
float x, y, z;
};
void initLittleFS() {
if (!LittleFS.begin()) {
Serial.println("Failed to mount LittleFS");
return;
}
Serial.println("LittleFS mounted successfully");
}
void storeActivityData(unsigned long timestamp) {
File file = LittleFS.open("/activity_data.txt", FILE_APPEND);
if (!file) {
Serial.println("Failed to open file for appending");
return;
}
file.print("Timestamp: ");
file.print(timestamp);
file.print(", Resting: ");
file.print(activityDurations[RESTING]);
file.print("s, Walking: ");
file.print(activityDurations[WALKING]);
file.print("s, Running: ");
file.print(activityDurations[RUNNING]);
file.print("s, Playing: ");
file.print(activityDurations[PLAYING]);
file.println("s");
file.close();
Serial.println("Data stored successfully.");
}
ActivityType classifyActivity() {
return static_cast<ActivityType>(random(0, 4));
}
class SimulatedQMI8658 {
private:
bool motionDetected;
void (*wakeupCallback)();
public:
SimulatedQMI8658() : motionDetected(false), wakeupCallback(nullptr) {}
bool begin() {
Serial.println("Simulated QMI8658 initialized.");
return true;
}
bool readFromFifo(IMUdata* acc, int accCount, IMUdata* gyr, int gyrCount) {
for (int i = 0; i < accCount; i++) {
acc[i].x = random(-32768, 32767) / 1000.0;
acc[i].y = random(-32768, 32767) / 1000.0;
acc[i].z = random(-32768, 32767) / 1000.0;
}
for (int i = 0; i < gyrCount; i++) {
gyr[i].x = random(-32768, 32767) / 100.0;
gyr[i].y = random(-32768, 32767) / 100.0;
gyr[i].z = random(-32768, 32767) / 100.0;
}
return true;
}
void configWakeOnMotion() {
Serial.println("Simulated wake-on-motion configured.");
}
void setWakeupMotionEventCallBack(void (*callback)()) {
wakeupCallback = callback;
}
void simulateMotion() {
if (random(100) < 5 && !motionDetected) {
motionDetected = true;
Serial.println("Motion detected! Triggering callback.");
if (wakeupCallback) {
wakeupCallback();
}
} else {
motionDetected = false;
}
}
};
SimulatedQMI8658 sensor;
void onMotionDetected() {
Serial.println("Motion detected callback executed!");
}
class TimeManager {
public:
void begin() {
Serial.println("Time Manager initialized");
}
void displayTime() {
Serial.print("Wake-up time (seconds): ");
Serial.println(millis() / 1000);
}
void enterDeepSleep() {
wakeUpCount++;
Serial.println("Entering deep sleep...");
Serial.flush();
esp_sleep_enable_timer_wakeup(2 * 60 * 1000000);
esp_deep_sleep_start();
}
};
TimeManager timeManager;
void setup() {
Serial.begin(115200);
initLittleFS();
sensor.begin();
sensor.setWakeupMotionEventCallBack(onMotionDetected);
sensor.configWakeOnMotion();
Serial.println("Wake-up count: " + String(wakeUpCount));
timeManager.displayTime();
randomSeed(analogRead(0));
unsigned long currentTime = millis();
if (currentTime - lastInterval >= INTERVAL) {
storeActivityData(currentTime);
lastInterval = currentTime;
memset(activityDurations, 0, sizeof(activityDurations));
}
sensor.simulateMotion();
timeManager.enterDeepSleep();
}
void loop() {
IMUdata accelData[10];
IMUdata gyroData[10];
sensor.readFromFifo(accelData, 10, gyroData, 10);
sensor.simulateMotion();
ActivityType activity = classifyActivity();
activityDurations[activity] += 1;
delay(1000);
}