数码之家

 找回密码
 立即注册
搜索
查看: 38746|回复: 11

[综合] ESP8266时钟加入DS3231

[复制链接]
发表于 2021-3-19 09:10:54 | 显示全部楼层 |阅读模式

爱科技、爱创意、爱折腾、爱极致,我们都是技术控

您需要 登录 才可以下载或查看,没有账号?立即注册

x
小白一个,跪求大佬指点。
8266+TM1637的时钟,想加入一个DS3231,让时钟掉电或断网继续走时,还请大佬不吝赐教,谢谢!

下面是时钟代码,代码来自度娘。


#include <ESP8266WiFi.h>
#include <time.h>
#include <TM1637Display.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager


// NTP Config
int timezone = 8 * 3600;
int dst = 0;

// Module connection pins (Digital Pins)
#define CLK D5
#define DIO D7
#define TEST_DELAY   2000

const uint8_t SEG_CONN[] = {
  SEG_A | SEG_D | SEG_E | SEG_F,                   // C
  SEG_C | SEG_D | SEG_E | SEG_G,                   // o
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_C | SEG_E | SEG_G                            // n
};

const uint8_t SEG_PASS[] = {
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,           // P
  SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
  SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,           // S
  SEG_A | SEG_C | SEG_D | SEG_F | SEG_G            // S
};

const uint8_t SEG_FAIL[] = {
  SEG_A | SEG_E | SEG_F | SEG_G,                   // F
  SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
  SEG_F | SEG_G,                                   // I
  SEG_D | SEG_F | SEG_G                            // L
};

const uint8_t SEG_DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
};

TM1637Display display(CLK, DIO);

String lastTimeStr;
unsigned long lastShowColon = 0;
bool showColon = false;
bool hourcheck = false;

void setup()
{
  display.setBrightness(0x01);// 0x01~0x0e
  WiFiManager wifiManager;
  wifiManager.autoConnect("ClockAP");

  display.setSegments(SEG_CONN);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
  }
  display.setSegments(SEG_PASS);
  delay(1000);

  // Get time from NTP server
  configTime(timezone, dst, "ntp1.aliyun.com");
  while (!time(nullptr))
  {
    delay(500);
  }
  display.setSegments(SEG_DONE);
  delay(1000);
}

void loop()
{
  uint8_t hour;
  bool hourcheck;
  time_t now = time(nullptr);
  struct tm* p_tm = localtime(&now);

  hour = p_tm->tm_hour;

  if (hour < 1)
  {
    hourcheck = true;
  }
  else
  {
    hourcheck = false;
  }
  if ((p_tm->tm_sec % 2) == 0)
  {
    display.showNumberDecEx(hour * 100 + p_tm->tm_min, 0x40 , hourcheck);
  }
  else
  {
    display.showNumberDecEx(hour * 100 + p_tm->tm_min, 0x00, hourcheck );
  }
}

发表于 2021-3-19 10:51:55 | 显示全部楼层
不太会玩8266,但是我觉得应该可以用三个全局变量表示时分秒,本机时钟累加走时,用网络时间校准即可(每天或每小时都可以,因为本地时钟也不会差的太离谱,可以认为是简单的NTP)。
回复 支持 反对

使用道具 举报

发表于 2021-3-19 11:07:18 来自手机浏览器 | 显示全部楼层
楼上说的对,首先是时间变量
int ds_hour, ds_min, ds_sec , sec;
int minute1, minute2, hour1, hour2;
然后是向ds3231写时间
if (WiFi.status() == WL_CONNECTED) {
    Serial.println("已联网,准备更新时间!!!");
    timeClient.begin();
    timeClient.update();
    //秒,分,时的获取及写入;
    int Hour_ex = timeClient.getHours();                   //hh
    if (Hour_ex != 8) {
      Wire.beginTransmission(0x68);
      Wire.write(0x00);
      int Sec_ex = timeClient.getSeconds();                   //ss
      int dd = (Sec_ex / 10 * 16) + (Sec_ex % 10);
      Wire.write(dd);
      Wire.endTransmission();
      Wire.beginTransmission(0x68);
      Wire.write(0x01);
      int Minu_ex = timeClient.getMinutes();                   //mm
      int ddd = (Minu_ex / 10 * 16) + (Minu_ex % 10);
      Wire.write(ddd);
      Wire.endTransmission();
      Wire.beginTransmission(0x68);
      Wire.write(0x02);
      int Hour_ex = timeClient.getHours();                   //hh
      int d = (Hour_ex / 10 * 16) + (Hour_ex % 10);
      Wire.write(d);
      Wire.endTransmission();
      Serial.println("时间更新完成!!!");
    }
    else {
      Serial.println("更新时间出错???");
    }
接下来是读取时间
void read_time() {
  Wire.beginTransmission(0x68);
  Wire.write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(0x68, 3);
  ds_sec = Wire.read();
  ds_min = Wire.read();
  ds_hour = Wire.read();
  hour1 = (ds_hour / 16);
  hour2 = (ds_hour % 16 );
  minute1 = (ds_min / 16 );
  minute2 = (ds_min % 16);
  sec = ((ds_sec / 16) * 10 + ds_sec % 16);
}

打赏

参与人数 1家元 +10 收起 理由
家睦 + 10

查看全部打赏

回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-19 11:53:40 | 显示全部楼层
wachenng 发表于 2021-3-19 11:07
楼上说的对,首先是时间变量
int ds_hour, ds_min, ds_sec , sec;
int minute1, minute2, hour1, hour2;

真是的不懂,能不能帮忙整理一下,万分感谢!!!
回复 支持 反对

使用道具 举报

发表于 2021-3-19 16:06:09 来自手机浏览器 | 显示全部楼层
围观一下,不懂arduino我的1637模块没有网络的时候就是error了…
回复 支持 反对

使用道具 举报

发表于 2021-3-19 16:06:44 来自手机浏览器 | 显示全部楼层
还是加上这个功能比较好,掉网了也可以用
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-19 16:36:30 | 显示全部楼层
触景情伤 发表于 2021-3-19 16:06
还是加上这个功能比较好,掉网了也可以用

是的,我现在的就是没网不能用了
所以想求大佬帮忙搞个能断网使用的
回复 支持 反对

使用道具 举报

发表于 2021-3-19 18:12:16 | 显示全部楼层
回复 支持 反对

使用道具 举报

发表于 2021-3-19 23:29:11 来自手机浏览器 | 显示全部楼层
找模块化的编程软件自己拼代码模块就好了,
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-22 08:19:59 | 显示全部楼层
刚刚认识arduino,目前还是伸手党,试着拼了,但是都失败了,所以来求大佬
回复 支持 反对

使用道具 举报

发表于 2021-3-22 13:44:41 来自手机浏览器 | 显示全部楼层
longjunling 发表于 2021-3-22 08:19
刚刚认识arduino,目前还是伸手党,试着拼了,但是都失败了,所以来求大佬

不知道你手里面还有没有其他的单片机,要是有的话可以试着改成其他的单片机,arduino我不怎么会用,手里这个1637模块也纯属折腾性质,啥时候玩够了我就拆了换成单片机+3231的…
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-3-27 14:46:38 | 显示全部楼层
没有大佬愿意帮忙吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

APP|手机版|小黑屋|关于我们|联系我们|法律条款|技术知识分享平台

闽公网安备35020502000485号

闽ICP备2021002735号-2

GMT+8, 2024-4-20 08:55 , Processed in 0.218400 second(s), 10 queries , Redis On.

Powered by Discuz!

© 2006-2023 smzj.net

快速回复 返回顶部 返回列表