完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛扫一扫,分享给好友
|
我正在研究 esp8266,rtc ds3231 模块,在此主要关注功耗,因为我需要将 esp 保持在深度睡眠状态一段时间,esp 将进入深度睡眠但在该时间段内不会重置。请务必检查我的代码并帮助我完成代码中需要完成的任何更改。
/* esp 每 2 小时保持深度睡眠模式 */ #include #include #include #include //const char* ssid = "学院”; // Light_WIFI const char* ssid = "Light_WIFI"; const char* password = "light@123"; 常量字节中断引脚 = 13; 易失布尔警报= 0; 字节 temp_Minutes = 0,temp_Hours = 0,temp_seconds = 0; //无符号字符数据[20] = "2020:5:27:13:55:25"; // 要写入的数据 char data[20] = "2020:5:27:13:55:25"; uint8_t 距离; char date_time_eep[20]; 无符号整型地址,i,j,cnt; /* 静态IP地址配置 */ //IPAddress staticIP(192,168,1,22); //ESP静态ip //IP地址网关(192,168,1,9); //您的 WiFi 路由器(网关)的 IP 地址 //IPAddress subnet(255,255,255,0); //子网掩码 //rtc对象 RtcDS3231 rtcObject(Wire); // RtcDateTime (currentTime); void i2c_eeprom_write_page(int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) { Wire.beginTransmission(设备地址); Wire.write((int)(eeaddresspage >> 8)); // MSB Wire.write((int)(eeaddresspage & 0xFF)); // LSB 字节 c; for ( c = 0; c < length; c++) Wire.write(data[c]); Wire.endTransmission(); } void i2c_eeprom_write_byte(int deviceaddress, unsigned int eeaddress, byte data) { int rdata = data; Wire.beginTransmission(设备地址); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.write(rdata); Wire.endTransmission(); } byte i2c_eeprom_read_byte(int deviceaddress, unsigned int eeaddress) { byte rdata = 0xFF; Wire.beginTransmission(设备地址); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(设备地址,1); 如果 (Wire.available()) rdata = Wire.read(); 返回数据; } void i2c_eeprom_read_buffer(int deviceaddress, unsigned int eeaddress, byte *buffer, int length) { Wire.beginTransmission(deviceaddress); Wire.write((int)(eeaddress >> 8)); // MSB Wire.write((int)(eeaddress & 0xFF)); // LSB Wire.endTransmission(); Wire.requestFrom(设备地址,长度); 整数 c = 0; 对于 ( c = 0; c < 长度; c++) 如果 (Wire.available()) buffer[c] = Wire.read(); } void connect_to_wifi() { // WiFi.begin(ssid, NULL); WiFi.begin(ssid, 密码); // WiFi.config(staticIP, gateway, subnet); Serial.println(WiFi.localIP()); while(WiFi.status() != WL_CONNECTED) { 延迟(50); 序列号.print("."); } 返回; } ICACHE_RAM_ATTR void handleInterrupt() { alarm = true; } void delay_ds3231() { for(i = 0 ; i <= 1000 ; i++) for(j = 0; j <= 2000 ; j++); ///// 手动延迟 // } void setup() { 连线开始();// 初始化连接 WiFi.mode(WIFI_OFF); //WiFi.forceSleepBegin(); 延迟(1); 序列号.begin(9600); Serial.println("我在设置中"); Serial.print("MAC:"); Serial.println(WiFi.macAddress()); pinMode(中断引脚,INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(interruptPin) , handleInterrupt , FALLING ); rtcObject.开始(); 延迟(50); /* * 从 ds3231 eeprom 读取 */ RtcDateTime now = rtcObject.GetDateTime(); RtcDateTime (currentTime) = rtcObject.GetDateTime(); //Serial.println(currentTime); // 如果(现在 <当前时间) // { // Serial.println("RTC 早于编译时间!(更新日期时间)"); // rtcObject.SetDateTime(currentTime); // } Serial.println("读取 eeprom 存储器"); i2c_eeprom_read_buffer(0x57,0,(byte *)date_time_eep,sizeof(date_time_eep)); Serial.print("来自 eep 的设置日期:"); Serial.println(date_time_eep); 延迟(50); if( date_time_eep[5] != '1') { //Serial.println (date_time_eep[5]); Serial.println("无"); } unsigned int y , M , h , m=0, s; 内部地址=0;//首地址 //从内存中访问首地址 i2c_eeprom_write_page(0x57, 0, (byte *)data, sizeof(data)); 字节 b = i2c_eeprom_read_byte(0x57, 1); 布尔冒号=假; 无符号整数 time_date_int[10] ={0} , i = 0; 序列号.println(); while (b != 0) { Serial.println("outer while 1"); Serial.print("b : "); 序列号.println(b); while((b != ':') && (b != 0)) { Serial.print("inner while :"); time_date_int[i] *= 10; time_date_int[i] += (b - 48); // Serial.print(time_date_int[i]); // Serial.print(" inner while index : "); // 序列号.println(i); //延迟(500); // 序列号.println(i); b = i2c_eeprom_read_byte(0x57,地址);//从内存中访问一个地址 addr++; } Serial.print("外部索引:"); //序列号.println(i); Serial.print(time_date_int[i]); 我++; 序列号.print(":"); b = i2c_eeprom_read_byte(0x57,地址); 地址++; 休息; // i2c_eeprom_read_buffer(0x57,0,(byte *)date_time_eep,sizeof(date_time_eep)); } Serial.println(); for(i=0 ; i < 6 ; i++) { Serial.print(time_date_int[i]); 序列号.print(" "); } Serial.println(); 如果(time_date_int[0] >= 2020) { // sprintf(数据, "%d:%d:%d:%d:%d:%d", 2020, 5, 27, 13, 55, 30); i2c_eeprom_write_page(0x57, 0, (byte *)data, sizeof(data)); 延迟(100); Serial.println("eeprom重写"); for(i = 0 ; i < 10 ; i++) time_date_int[i] = 0; b = i2c_eeprom_read_byte(0x57, 1); 我=0; 地址 = 0; 序列号.println(); while (b!=0) { Serial.println("outer while 2"); while((b != ':') && (b != 0)) { Serial.println("inner while"); time_date_int[i] *= 10; time_date_int[i] += (b - 48); // 序列号.println(i); b = i2c_eeprom_read_byte(0x57,地址);//从内存中访问一个地址 addr++; } Serial.print(i); Serial.print("索引"); Serial.print(time_date_int[i]); 我++; 序列号.print(":"); b = i2c_eeprom_read_byte(0x57,地址); 地址++; } } m=currentTime.Minute(); Serial.print("当前分钟:"); 序列号.println(m); if(time_date_int[0] != currentTime.Year()) { Serial.println(“日期和时间与 ds3231 eeprom 不匹配和更新”); RtcDateTime (currentTime) = RtcDateTime(time_date_int[0]-2000 , time_date_int[1] , time_date_int[2] , time_date_int[3] , time_date_int[4] , time_date_int[5]); rtcObject.SetDateTime(currentTime); } else if( m > time_date_int[4] ) { Serial.println("日期和时间已更新"); } byte temp_Minutes = 0, temp_Hours = 0, temp_seconds = 0; rtcObject.Enable32kHzPin(false); rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne); // rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeClock); // 设置引脚模式 // rtcObject.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz); // 设置频率 temp_seconds = currentTime.Second(); Serial.print("当前秒数:"); Serial.println(temp_seconds); 如果(temp_seconds == 0) { Serial.print(“处理秒数:”); Serial.println(temp_seconds); temp_seconds = 59; } // DS3231AlarmOne alarm1(0, 0, 0, temp_seconds-1, DS3231AlarmOneControl_SecondsMatch); /// 每 5 分钟中断一次 temp_Minutes = currentTime.Minute(); /// 5 分钟 Serial.print("Current Minutes: "); Serial.println(temp_Minutes); if((temp_Minutes+1) > 59) { Serial.print("处理时间:"); Serial.println(temp_Minutes); temp_Minutes = -1; } // DS3231AlarmOne alarm1(0, 0, temp_Minutes+1, 0, DS3231AlarmOneControl_MinutesSecondsMatch); /// 每 5 分钟中断一次 temp_Hours = currentTime.Hour(); Serial.println("当前时间:"); Serial.println(temp_Hours); 如果(temp_Hours == 0) { temp_Hours = 2; Serial.println("闹钟@2am"); } else if((temp_Hours >= 2) && (temp_Hours <= 3)) { temp_Hours = 4; Serial.println("闹钟@4am"); } else if((temp_Hours >= 3) && (temp_Hours <= 5)) { temp_Hours = 6; 序列号.println(" else if((temp_Hours >= 5) && (temp_Hours <= 7)) { temp_Hours = 8; Serial.println("闹钟@8am"); } else if((temp_Hours >= 7) && (temp_Hours <= 9)) { temp_Hours = 10; Serial.println("闹钟@10am"); } else if((temp_Hours >= 9) && (temp_Hours <= 11)) { temp_Hours = 12; Serial.println("闹钟@12pm"); } else if((temp_Hours >= 11) && (temp_Hours <= 13)) { temp_Hours = 14; Serial.println("警报@2pm"); else if((temp_Hours >= 13) && (temp_Hours <= 15)) { temp_Hours = 16; Serial.println("闹钟@4pm"); } else if((temp_Hours >= 15) && (temp_Hours <= 17)) { temp_Hours = 18; Serial.println("闹钟@6pm"); } else if((temp_Hours >= 17) && (temp_Hours <= 19)) { temp_Hours = 20; Serial.println("闹钟@8pm"); } else if((temp_Hours >= 19) && (temp_Hours <= 21)) { temp_Hours = 22; Serial.println("闹钟@10pm"); else if((temp_Hours >= 21) && (temp_Hours <= 23)) { temp_Hours = 0; Serial.println("闹钟@12am"); } DS3231AlarmOne alarm1(0, temp_Hours, 0, 0, DS3231AlarmOneControl_HoursMinutesSecondsMatch); /// 每 5 分钟中断一次 delay(50); rtcObject.SetAlarmOne(alarm1); rtcObject.LatchAlarmsTriggeredFlags(); 延迟(50); Serial.println("移动到设置"); } void loop() { 延迟(50); Serial.println("我在循环中"); //延迟(500); /* 传感器读数 */ /* while(data[i] != 0xFF) { data[i] = Serial.read(); } while(0xFF != Serial.read()) for(int i=0;i<4;i++) { data[i]=Serial.read(); } 距离 = 数据[1]*256+数据[2]; //串行打印(“检查=”);Serial.println(数据[3]); uint8_t 检查 = (data[0]+data[1]+data[2])&0xFF; //Serial.print("checksum = ");Serial.println(checks); if (checks == data[3]) //校验和等于数据[3] { Serial.println("distance = "+String(distance)); } else { Serial.println("数据损坏"); 延迟(100); } */ // 读取传感器读数 rtcObject.LatchAlarmsTriggeredFlags(); // 连接WIFI WiFi.forceSleepWake(); 延迟(1); //建立wifi连接 WiFi.mode(WIFI_STA); while (WiFi.status() != WL_CONNECTED) { connect_to_wifi(); 序列号.println(); Serial.print("已连接,IP地址:"); Serial.println(WiFi.localIP()); } // 更新最新的日期和时间 if(alarm == true) { Serial.println("in handler"); handleAlarm(); /// 连接到 I/O 引脚时中断 } //delay(1000); //delay_ds3231(); RtcDateTime (时间戳) = rtcObject.GetDateTime(); Serial.print("时间:"); 字符时间 1[20]; sprintf(time1, "%d:%d:%d:%d:%d:%d", timestamp.Year(), timestamp.Month(), timestamp.Day(), timestamp.Hour(), timestamp.Minute(), timestamp.Second() ); Serial.println(time1); i2c_eeprom_write_page(0x57, 0, (byte *)time1, sizeof(time1)); 时间 1[0] = 0; 延迟(20); i2c_eeprom_read_buffer(0x57,0,(byte *)time1,sizeof(time1)); Serial.print("ds3231 EEPROM:"); Serial.println(time1); Serial.println("要睡觉\n"); Serial.println(currentTime.Minute()); 延迟(100); ESP.deepSleep(0); // esp 永远沉睡。 延迟(10); }//环形 void handleAlarm() /// 它不会执行,因为 INT/SQW 引脚通过 100uf 电容连接到 ESP.RST 引脚(请将 10k 电阻串联到 VCC) { alarm = false; RtcDateTime (时间戳) = rtcObject.GetDateTime(); 00*1e6); // 1 小时 Serial.print("time interrupt at:"); 字符时间 1[20]; sprintf(time1, "%d:%d:%d:%d:%d:%d", timestamp.Year(), timestamp.Month(), timestamp.Day(), timestamp.Hour(), 时间戳。分钟(), 时间戳。秒() ); 碳纳米管 = 0; Serial.println(time1); rtcObject.LatchAlarmsTriggeredFlags(); } 提前致谢 ... |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
479 浏览 0 评论
1006 浏览 0 评论
1716 浏览 0 评论
请问一下我想用ESP8685直接替换ESP8266而不用写程序,可以让ESP8685直接通过之前ESP8266的外挂的flash运行程序吗
1375 浏览 1 评论
1258 浏览 1 评论
为blufi_device设置自定义名称,但是无法修改,为什么?
1295浏览 4评论
请问ESP32-S2-WROOM怎么获得ESP32-S2外接FLASH的唯一序列号?
945浏览 3评论
2396浏览 3评论
ESP-IDF的VScode插件的build按钮点击会报错的原因?
2585浏览 3评论
ESP-Jumpstart例程中第5个工程:5_cloud连接报错是哪里的问题?
1077浏览 2评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-18 00:35 , Processed in 0.612307 second(s), Total 72, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
674
