ESP32结合TFT,INA219,MPU6050

当没有按下按钮时任意移动会触发蜂鸣器,当按下按钮后可移动不触发蜂鸣器,开启INA219计费,屏幕显示状态。

#include <TFT_eSPI.h>  // 引入TFT_eSPI库
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_INA219.h>

TFT_eSPI tft = TFT_eSPI();  // 创建TFT_eSPI对象
Adafruit_MPU6050 mpu;  // 创建MPU6050对象
Adafruit_INA219 ina219;  // 创建INA219对象

const int buzzerPin = 16;  // 定义蜂鸣器引脚
const int buttonPin = 5;   // 定义按钮引脚
int mode = 0;  // 0: 关闭, 1: 开启

sensors_event_t initialAccel, initialGyro, temp;  // 创建传感器事件变量
bool isButtonPressed = false;

void setup() {
    Serial.begin(115200);
    tft.init();  // 初始化TFT屏幕
  tft.setRotation(1);  // 设置屏幕旋转方向
  tft.fillScreen(TFT_BLACK);  // 填充屏幕为黑色
  tft.setCursor(0, 0);  // 设置光标位置
  tft.setTextSize(1);  // 设置文本大小
  tft.setTextColor(TFT_WHITE);  // 设置文本颜色
  tft.println("你好,世界!");  // 在屏幕上打印文本
  delay(1000);

  if (!mpu.begin()) {  // 初始化MPU6050
    Serial.println("初始化MPU6050失败!");
    while (1);
  }
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);  // 设置加速度计量程
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);  // 设置陀螺仪量程
  mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);  // 设置滤波带宽



  pinMode(buzzerPin, OUTPUT);  // 设置蜂鸣器引脚为输出模式
  pinMode(buttonPin, INPUT_PULLUP);  // 设置按钮引脚为输入模式,带内部上拉电阻

  mpu.getEvent(&initialAccel, &initialGyro, &temp);  // 获取初始传感器数据

  if (!ina219.begin()) {  // 初始化INA219
    Serial.println("Failed to find INA219 chip");
    while (1) { delay(10); }
  }
}

void loop() {
  isButtonPressed = digitalRead(buttonPin) == LOW;  // 检测按钮是否被按下
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);  // 获取传感器数据

  if (isButtonPressed) {
    mode = 1 - mode;  // 切换模式
    delay(200);  // 延时去抖动
  }

  if (mode == 0 && hasMoved(initialAccel, a)) {  // 如果处于报警模式且检测到运动
    triggerAlarm();  // 触发报警
  }

  if (mode == 1) {  // 如果模式为开启
    float shuntvoltage = ina219.getShuntVoltage_mV();
    float busvoltage = ina219.getBusVoltage_V();
    float current_mA = ina219.getCurrent_mA();
    float power_mW = ina219.getPower_mW();
    float loadvoltage = busvoltage + (shuntvoltage / 1000);

    tft.fillScreen(TFT_BLACK);  // 清空屏幕
    tft.setCursor(0, 0);  // 设置光标位置
    tft.printf("Bus Voltage: %0.2f V\n", busvoltage);
    tft.printf("Shunt Voltage: %0.2f mV\n", shuntvoltage);
    tft.printf("Load Voltage: %0.2f V\n", loadvoltage);
    tft.printf("Current: %0.2f mA\n", current_mA);
    tft.printf("Power: %0.2f mW\n", power_mW);
  } else {
    tft.fillScreen(TFT_BLACK);  // 清空屏幕
    tft.setCursor(0, 30);  // 设置光标位置
    tft.println("正常模式");  // 在屏幕上打印文本
  }
}

bool hasMoved(sensors_event_t initialAccel, sensors_event_t currentAccel) {
  float deltaX = abs(currentAccel.acceleration.x - initialAccel.acceleration.x);
  float deltaY = abs(currentAccel.acceleration.y - initialAccel.acceleration.y);
  float deltaZ = abs(currentAccel.acceleration.z - initialAccel.acceleration.z);
  if (deltaX > 1 || deltaY > 1 || deltaZ > 1) {
    return true;
  }
  return false;
}

void triggerAlarm() {
  digitalWrite(buzzerPin, HIGH);
  delay(1000);
  digitalWrite(buzzerPin, LOW);
}