Use df robot sensor instead
This commit is contained in:
@@ -9,6 +9,11 @@
|
|||||||
#include <XPowersLib.h>
|
#include <XPowersLib.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
|
#include "DFRobot_mmWave_Radar.h"
|
||||||
|
#include <HardwareSerial.h>
|
||||||
|
|
||||||
|
#include "Thread.h"
|
||||||
|
#include "ThreadController.h"
|
||||||
|
|
||||||
// Serial
|
// Serial
|
||||||
#define BAUD 115200
|
#define BAUD 115200
|
||||||
@@ -18,25 +23,20 @@
|
|||||||
#define TX2_PIN TOUCH_IICSDA
|
#define TX2_PIN TOUCH_IICSDA
|
||||||
|
|
||||||
// Sensor configuration
|
// Sensor configuration
|
||||||
#define COMMAND "FDFCFBFA0800120000000200000004030203" //Normal mode command, see documentation waveshare sensor
|
#define PRESENCE_DISTANCE 2 // in m
|
||||||
#define PRESENCE_DISTANCE 200 // in cm
|
|
||||||
#define DELAY_STATUS_CHECK 1000 // delay (in ms) between two checks of the presence
|
#define DELAY_STATUS_CHECK 1000 // delay (in ms) between two checks of the presence
|
||||||
#define SLEEP_DELAY 30000 // delay (in ms) until sleep mode
|
#define SLEEP_DELAY 30000 // delay (in ms) until sleep mode
|
||||||
#define POWER_DELAY 100000 // delay (in ms) until turning off the screen
|
#define POWER_DELAY 100000 // delay (in ms) until turning off the screen
|
||||||
|
|
||||||
byte configCommand[] = {
|
HardwareSerial mySerial(1);
|
||||||
0xFD, 0xFC, 0xFB, 0xFA, 0x08, 0x00, 0x12, 0x00,
|
DFRobot_mmWave_Radar sensor(&mySerial);
|
||||||
0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x04, 0x03,
|
|
||||||
0x02, 0x03
|
|
||||||
};
|
|
||||||
|
|
||||||
SemaphoreHandle_t xSemaphore = NULL;
|
SemaphoreHandle_t xSemaphore = NULL;
|
||||||
PowersSY6970 PMU;
|
PowersSY6970 PMU;
|
||||||
|
|
||||||
// Sensor variables
|
// Sensor variables
|
||||||
uint16_t state = 1; // 0 = presence detected, 1 = no presence detected for the past SLEEP_DELAY ms, 2 = no presence detected for the past POWER_DELAY ms
|
uint16_t state = 1; // 0 = presence detected, 1 = no presence detected for the past SLEEP_DELAY ms, 2 = no presence detected for the past POWER_DELAY ms
|
||||||
unsigned long lastMotionCheckTime = millis();
|
unsigned long lastMotionDetected = 0; // time when the last motion was detected
|
||||||
unsigned long lastMotionDetected = 0;
|
|
||||||
|
|
||||||
// Time variables
|
// Time variables
|
||||||
static uint32_t last_tick;
|
static uint32_t last_tick;
|
||||||
@@ -52,6 +52,10 @@ static lv_color_t *buf1;
|
|||||||
static String BASE_URL_HA = "http://192.168.1.2:8123/api/services";
|
static String BASE_URL_HA = "http://192.168.1.2:8123/api/services";
|
||||||
static String HA_TOKEN = "abc";
|
static String HA_TOKEN = "abc";
|
||||||
|
|
||||||
|
ThreadController controller;
|
||||||
|
Thread thread1 = Thread();
|
||||||
|
Thread thread2 = Thread();
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
xSemaphore = xSemaphoreCreateBinary();
|
xSemaphore = xSemaphoreCreateBinary();
|
||||||
xSemaphoreGive(xSemaphore);
|
xSemaphoreGive(xSemaphore);
|
||||||
@@ -60,10 +64,12 @@ void setup() {
|
|||||||
initializeWifi();
|
initializeWifi();
|
||||||
initializeTime();
|
initializeTime();
|
||||||
initializeUI();
|
initializeUI();
|
||||||
|
initializeThreading();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
delay(1);
|
controller.run();
|
||||||
|
|
||||||
if (transfer_num <= 0 && lcd_PushColors_len <= 0)
|
if (transfer_num <= 0 && lcd_PushColors_len <= 0)
|
||||||
lv_timer_handler();
|
lv_timer_handler();
|
||||||
|
|
||||||
@@ -74,9 +80,6 @@ void loop() {
|
|||||||
static int flag_bl = 0;
|
static int flag_bl = 0;
|
||||||
static unsigned long cnt = 0;
|
static unsigned long cnt = 0;
|
||||||
|
|
||||||
printLocalTime();
|
|
||||||
readAndProcessSensorLines();
|
|
||||||
|
|
||||||
cnt++;
|
cnt++;
|
||||||
if (cnt >= 100) {
|
if (cnt >= 100) {
|
||||||
if (flag_bl == 0) {
|
if (flag_bl == 0) {
|
||||||
@@ -110,12 +113,12 @@ void initSerial() {
|
|||||||
digitalWrite(TOUCH_RES, HIGH);
|
digitalWrite(TOUCH_RES, HIGH);
|
||||||
delay(2);
|
delay(2);
|
||||||
|
|
||||||
// Start Serial2 for the HMMD Sensor
|
// Start HMMD Sensor
|
||||||
Serial2.begin(BAUD, SERIAL_8N1, RX2_PIN, TX2_PIN);
|
mySerial.begin(BAUD, SERIAL_8N1, RX2_PIN, TX2_PIN); //RX,TX
|
||||||
Serial.println("Serial2 Initialized on RX:" + String(RX2_PIN) + ", TX:" + String(TX2_PIN));
|
Serial.println("Sensor Initialized on RX:" + String(RX2_PIN) + ", TX:" + String(TX2_PIN));
|
||||||
Serial.println("Sending configuration to the sensor");
|
sensor.factoryReset(); //Restore to the factory settings
|
||||||
delay(2000);
|
sensor.DetRangeCfg(0, PRESENCE_DISTANCE); //The detection range is as far as 9m
|
||||||
Serial2.write(configCommand, sizeof(configCommand));
|
sensor.OutputLatency(0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void initializeWifi() {
|
void initializeWifi() {
|
||||||
@@ -188,6 +191,17 @@ void initializeUI() {
|
|||||||
Serial.println("UI initialized");
|
Serial.println("UI initialized");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void initializeThreading() {
|
||||||
|
thread1.onRun(printLocalTime);
|
||||||
|
thread1.setInterval(100);
|
||||||
|
|
||||||
|
thread2.onRun(readAndProcessSensorLines);
|
||||||
|
thread2.setInterval(1000);
|
||||||
|
|
||||||
|
controller.add(&thread1);
|
||||||
|
controller.add(&thread2);
|
||||||
|
}
|
||||||
|
|
||||||
void printLocalTime() {
|
void printLocalTime() {
|
||||||
if (millis() - last_tick > 100) {
|
if (millis() - last_tick > 100) {
|
||||||
struct tm timeInfo;
|
struct tm timeInfo;
|
||||||
@@ -203,51 +217,31 @@ void printLocalTime() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void readAndProcessSensorLines() {
|
void readAndProcessSensorLines() {
|
||||||
if (millis() - lastMotionCheckTime >= DELAY_STATUS_CHECK) {
|
int currentStatus = sensor.readPresenceDetection();
|
||||||
lastMotionCheckTime = millis();
|
|
||||||
// Check if data is available on Serial2
|
|
||||||
while (Serial2.available() > 0) {
|
|
||||||
// Read a line of text until a newline character (\n) is received
|
|
||||||
// The timeout helps prevent blocking forever if a line ending is missed
|
|
||||||
String line = Serial2.readStringUntil('\n');
|
|
||||||
|
|
||||||
// Clean up the line: remove potential carriage return (\r) and leading/trailing whitespace
|
if (currentStatus == 1) {
|
||||||
line.trim();
|
|
||||||
|
|
||||||
// Check if the line contains the "Range" information
|
|
||||||
if (line.startsWith("Range ")) {
|
|
||||||
// Extract the substring after "Range "
|
|
||||||
String distanceStr = line.substring(6);
|
|
||||||
int distance = distanceStr.toInt();
|
|
||||||
bool currentStatus = distance <= PRESENCE_DISTANCE;
|
|
||||||
|
|
||||||
if (currentStatus) {
|
|
||||||
lastMotionDetected = millis();
|
lastMotionDetected = millis();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Serial.print("Radar: ");
|
//Serial.printf("Sensor: %d\n", currentStatus);
|
||||||
//Serial.println(distance);
|
|
||||||
|
|
||||||
if (currentStatus && state != 0) {
|
if (currentStatus == 1 && state != 0) {
|
||||||
state = 0;
|
state = 0;
|
||||||
Serial.println("Motion detected, turning screen on.");
|
Serial.println("Motion detected, turning screen on.");
|
||||||
lv_msg_send(MSG_NEW_VOLT, "0");
|
lv_msg_send(MSG_NEW_VOLT, "0");
|
||||||
//restApiAction(0);
|
//restApiAction(0);
|
||||||
} else if (!currentStatus && state == 1 && (millis() - lastMotionDetected >= POWER_DELAY)) {
|
} else if (currentStatus == 0 && state == 1 && (millis() - lastMotionDetected >= POWER_DELAY)) {
|
||||||
state = 2;
|
state = 2;
|
||||||
Serial.printf("No motion detected for %d ms, turning screen off.\n", POWER_DELAY);
|
Serial.printf("No motion detected for %d ms, turning screen off.\n", POWER_DELAY);
|
||||||
lv_msg_send(MSG_NEW_VOLT, "2");
|
lv_msg_send(MSG_NEW_VOLT, "2");
|
||||||
//restApiAction(2);
|
//restApiAction(2);
|
||||||
} else if (!currentStatus && state == 0 && (millis() - lastMotionDetected >= SLEEP_DELAY)) {
|
} else if (currentStatus == 0 && state == 0 && (millis() - lastMotionDetected >= SLEEP_DELAY)) {
|
||||||
state = 1;
|
state = 1;
|
||||||
Serial.printf("No motion detected for %d ms, turning screen screensaver on.\n", SLEEP_DELAY);
|
Serial.printf("No motion detected for %d ms, turning screen screensaver on.\n", SLEEP_DELAY);
|
||||||
lv_msg_send(MSG_NEW_VOLT, "1");
|
lv_msg_send(MSG_NEW_VOLT, "1");
|
||||||
//restApiAction(1);
|
//restApiAction(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// action: 0 => Screen on, => 1 Screen off, => 2 Power off
|
// action: 0 => Screen on, => 1 Screen off, => 2 Power off
|
||||||
void restApiAction(int action) {
|
void restApiAction(int action) {
|
||||||
|
|||||||
Reference in New Issue
Block a user