#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
//Add real time clock library (RTCLib here)
//#include "RTClib.h"
#include "time.h"
//宣告LCD獎数
//L1 quidcrysta11cd(12,11,5,4,3,2);/设
LiquidCrystal_I2C lcd(0x27,16,2);//设置LCD
//定義RTC特問疑散
struct tm timeinfo;
//宣告MPU6050爱敦
Adafruit_MPU6050 mpu;
//宣告角度等楚数
float angle=0;
float last_angle=0;//上-次的角度
float step_length=0.65;
int steps=0;
//宣台角度等漫数
int stepCount=8;//計步器計数器
//初始
void setup(){
//初始化I2C和更示屏
Wire.begin();
Serial.begin(115200);
Serial.println("Hello,ESP 32");
lcd.init();
lcd.backlight();
WiFi.begin("Wokwi-GUEST","");
while (WiFi.status() !=WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println("Connected");
Serial.println(WiFi.localIP());
configTime(8*3600,0,"pool.ntp.org");
//等待時間同步完成
while (!time(nullptr)){
delay(1000);
}
//初始化MPU6058
if (!mpu.begin()){
Serial.println("Failed to find MPU6050 chip");
while (1){
delay(10);
}
}
}
void loop(){
//发取RTC符間
if (!getLocalTime(&timeinfo)) {
lcd.setCursor(0,0);
lcd.print("Failed to get time");
return;
}
//1照示动同
lcd.clear();
lcd.setCursor(0,0);//first line
lcd.print("Time:");
lcd.print(String(timeinfo.tm_hour)+":"+String(timeinfo.tm_min)+":"+String(timeinfo.tm_sec));
//進入煙眠模式,每0.1分雄映醒一次以更新特同
esp_sleep_enable_timer_wakeup(6000000);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH,ESP_PD_OPTION_ON);
esp_deep_sleep_start();
//or read MPU6050 (outside ESP32)
sensors_event_t event,event2,event3;
mpu.getEvent(&event,&event2,&event3);
//1针草向发
float raw_angle = atan2(event.acceleration.y,event.acceleration.z);
angle = raw_angle*0.1/ PI;
//棕查是否联生了一步
if (angle >0 && last_angle<0){
steps++;
lcd.setCursor(0,1);//second line
lcd.print("Steps:");
lcd.print(steps);
}
//更新上一灾的角度
last_angle=angle;
//延迟一段時間,避免通於频聚的针草
delay(100);
}