#define PART 1 //第几个从机 5震动开关 6按键 18 LED
#include <Adafruit_NeoPixel.h>
#include <esp_now.h>
#include <WiFi.h>
//#include "XLOT_MAX301xx_PulseOximeter.h"
#include <Wire.h>
#define SAMPLE_PERIOD_MS 200 // 设置为0.2秒采集一次数据
#define REPORTING_PERIOD_MS 4000 // 设置为4秒更新一次数据
#define MOVING_AVERAGE_PERIODS 10 // 移动平均滤波的周期数
#define LED_PIN 18 // WS2812连接的引脚
#define NUM_LEDS 24 // WS2812的数量
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, LED_PIN, NEO_GRB + NEO_KHZ800);
//PulseOximeter pox;
bool crack = false;
int light = 0;
int plus = 0;
int mod = 0;
float beat_readings[MOVING_AVERAGE_PERIODS] = {0}; // 存储心率读数的数组
int readIndex = 0;
float total_beat = 0;
float average_beat = 0;
uint32_t tsLastSample = 0;
uint32_t tsLastReport = 0;
uint32_t last_beat = 0;
bool initialized = false;
float last_valid_beat = 0; // 存储上一次有效的心率读数
float current_beat = 0; // 存储当前心率读数
unsigned long LEDTIME = 0;
unsigned long sendtime = 0;
// 接收端的MAC地址
uint8_t broadcastAddress[] = {0xec, 0x64, 0xc9, 0x91, 0x29, 0x38};
// 发送结构体类型
typedef struct struct_message {
int a;
int b;
bool c;
} struct_message;
// 创建一个结构体变量
struct_message myData;
// 回调函数,函数将在发送消息时执行。此函数告诉我们信息是否成功发送;
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
//Serial.print("\r\nLast Packet Send Status:\t");
//0Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
}
/*
// Callback (registered below) fired when a pulse is detected
void onBeatDetected()
{
last_beat = millis();
}
void display_values()
{
Serial.print(" ");
Serial.println(current_beat);
}
void initial_display()
{
if (!initialized) {
Serial.println("Place finger on the sensor");
initialized = true;
}
}
void add_reading(float beat)
{
// 检查新的读数是否在合理范围内
if (beat > 30 && beat < 220) {
// 如果上一次的心率读数大于60,检查本次读数与上次读数的差值是否大于正负30
if (last_valid_beat >= 60 && (beat - last_valid_beat > 30 || beat - last_valid_beat < -30)) {
beat = last_valid_beat; // 如果差值超过正负30且上一次读数大于60,则使用上次的有效读数
} else {
last_valid_beat = beat; // 更新上次的有效读数
}
// 更新当前心率读数
current_beat = beat;
// 移除最旧的读数
total_beat -= beat_readings[readIndex];
// 添加新的读数
beat_readings[readIndex] = beat;
total_beat += beat;
// 前进到数组的下一个位置
readIndex = (readIndex + 1) % MOVING_AVERAGE_PERIODS;
// 计算平均值
average_beat = total_beat / MOVING_AVERAGE_PERIODS;
} else if (last_valid_beat < 60) {
current_beat = beat; // 更新当前心率读数为新的读数
}
}
*/
void setup() {
// 初始化串口波特率
Serial.begin(115200);
Wire.begin(21, 22);//(8,9)
while (!Serial)
{
delay(10);
}
Serial.println("111");
/*
if (!pox.begin()) {
Serial.println("FAILED TO INITIALIZE PULSE OXIMETER");
for (;;);
}
pox.setOnBeatDetectedCallback(onBeatDetected);
initial_display();
*/
pinMode(5, INPUT);//震动开关
pinMode(4, INPUT_PULLUP); //按键
strip.begin();
strip.show(); // 初始化所有像素为'off'
// 设置WIFI模式为STA模式,即无线终端
WiFi.mode(WIFI_STA);
// 初始化ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
//注册回调函数
esp_now_register_send_cb(OnDataSent);
// 注册通信频道
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0; //通道
peerInfo.encrypt = false;//是否加密为False
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println("Failed to add peer");
return;
}
}
void loop() {
current_beat=10;
Serial.println(myData.b);
//设置要发送的值
if (digitalRead(5)) //检测到撞击
{
crack = true;
}
myData.a = PART;
if (crack == true)
myData.b = current_beat * 100;
else
myData.b = current_beat;
myData.c = crack;
if (digitalRead(4) == LOW) {
mod++;
light = 0;
plus = 0;
setColor(0, 0, 0);//全关闭
if (mod == 3)
{
mod = 0;
}
while (digitalRead(4) == LOW)
{
delay(10);
}
}
LED_Run();//WS2812动作
//Get_BPM();//心率采集
SENDDATA();//信息传输
if (millis() - sendtime > 4000) {
crack = false;
sendtime = millis();
}
}
void NOW_INIT() {
}
void SENDDATA()
{
//发送信息到指定ESP32上
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
//判断是否发送成功
if (result == ESP_OK) {
Serial.println("Sent success");
}
else {
Serial.println("Error sending");
}
delay(5);
}
/*
void Get_BPM() {
// Make sure to call update as fast as possible
pox.update();
if (millis() - tsLastSample > SAMPLE_PERIOD_MS) {
float beat = pox.getHeartRate();
add_reading(beat);
tsLastSample = millis();
}
if (millis() - tsLastReport > REPORTING_PERIOD_MS) {
// Display the current heart rate every 4 seconds
display_values();
tsLastReport = millis();
}
if (millis() - last_beat > 1000) {
current_beat = 0;
average_beat = 0;
total_beat = 0;
for (int i = 0; i < MOVING_AVERAGE_PERIODS; i++) {
beat_readings[i] = 0;
}
readIndex = 0;
last_valid_beat = 0; // Reset the last valid reading
initial_display();
}
}*/
void LED_Run() {
if (mod == 0) {
setColor(0, 0, 0);//全关闭
}
else if (mod == 1) {
setColor(255, 255, 0);//全开
}
else if (mod == 2) {
if (millis() - LEDTIME > 50) {
LEDTIME = millis();
if (!plus) { //1熄灭0亮起
light += 5;
if (light == 255)
plus = 1; //满256减程
}
else
{
light -= 5;
if (light == 0)
plus = 0; //至0升程
}
setColor(light, light, 0);
}
}
}
void setColor(uint8_t r, uint8_t g, uint8_t b) {
for (int i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, strip.Color(r, g, b)); // 设置每个像素的颜色
}
strip.show(); // 显示更新后的颜色
}