0 效果
话不多说,先来看看最后实现的效果(从左到右分别为时、分、秒,当时设计的时候忘了设计中间的冒号了
-_-):
画了PCB,请朋友帮忙画了solidworks打印出来框架,然后买螺丝进行拼装。
1 简介
某天逛某宝的时候突然发现了拟辉光钟,当时就觉得挺好看的,但一看价格就直接劝退了(这里放了两张价格)
于是我产生了自己动手做的想法。现在网上搜了一下拟辉光钟的工作原理:
拟辉光钟是利用光线在亚克力板上的划痕处产生光线折射,使得划痕处的光线较亮,配置以随时间而变的灯光,达到类似于辉光钟的效果。
在搜索了一些资料后,我把我的工作分成了两个部分,第一个部分就是时间的获取,第二个部分是灯光的控制。下面主要就这两个大的模块介绍下具体怎么实现的。
#mermaid-svg-SV6m6fmWAxVjU1jh .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .label text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .node rect,#mermaid-svg-SV6m6fmWAxVjU1jh .node circle,#mermaid-svg-SV6m6fmWAxVjU1jh .node ellipse,#mermaid-svg-SV6m6fmWAxVjU1jh .node polygon,#mermaid-svg-SV6m6fmWAxVjU1jh .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .node .label{text-align:center;fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .node.clickable{cursor:pointer}#mermaid-svg-SV6m6fmWAxVjU1jh .arrowheadPath{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-SV6m6fmWAxVjU1jh .flowchart-link{stroke:#333;fill:none}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel rect{opacity:0.9}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel span{color:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .cluster text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-SV6m6fmWAxVjU1jh .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-SV6m6fmWAxVjU1jh text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .actor-line{stroke:grey}#mermaid-svg-SV6m6fmWAxVjU1jh .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sequenceNumber{fill:#fff}#mermaid-svg-SV6m6fmWAxVjU1jh #sequencenumber{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh #crosshead path{fill:#333;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .messageText{fill:#333;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-SV6m6fmWAxVjU1jh .labelText,#mermaid-svg-SV6m6fmWAxVjU1jh .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .loopText,#mermaid-svg-SV6m6fmWAxVjU1jh .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-SV6m6fmWAxVjU1jh .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-SV6m6fmWAxVjU1jh .noteText,#mermaid-svg-SV6m6fmWAxVjU1jh .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-SV6m6fmWAxVjU1jh .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-SV6m6fmWAxVjU1jh .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-SV6m6fmWAxVjU1jh .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .section{stroke:none;opacity:0.2}#mermaid-svg-SV6m6fmWAxVjU1jh .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-SV6m6fmWAxVjU1jh .section2{fill:#fff400}#mermaid-svg-SV6m6fmWAxVjU1jh .section1,#mermaid-svg-SV6m6fmWAxVjU1jh .section3{fill:#fff;opacity:0.2}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle0{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle1{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle2{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle3{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-SV6m6fmWAxVjU1jh .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .grid path{stroke-width:0}#mermaid-svg-SV6m6fmWAxVjU1jh .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-SV6m6fmWAxVjU1jh .task{stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText:not([font-size]){font-size:11px}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-SV6m6fmWAxVjU1jh .task.clickable{cursor:pointer}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText0,#mermaid-svg-SV6m6fmWAxVjU1jh .taskText1,#mermaid-svg-SV6m6fmWAxVjU1jh .taskText2,#mermaid-svg-SV6m6fmWAxVjU1jh .taskText3{fill:#fff}#mermaid-svg-SV6m6fmWAxVjU1jh .task0,#mermaid-svg-SV6m6fmWAxVjU1jh .task1,#mermaid-svg-SV6m6fmWAxVjU1jh .task2,#mermaid-svg-SV6m6fmWAxVjU1jh .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside0,#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside2{fill:#000}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside1,#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside3{fill:#000}#mermaid-svg-SV6m6fmWAxVjU1jh .active0,#mermaid-svg-SV6m6fmWAxVjU1jh .active1,#mermaid-svg-SV6m6fmWAxVjU1jh .active2,#mermaid-svg-SV6m6fmWAxVjU1jh .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-SV6m6fmWAxVjU1jh .activeText0,#mermaid-svg-SV6m6fmWAxVjU1jh .activeText1,#mermaid-svg-SV6m6fmWAxVjU1jh .activeText2,#mermaid-svg-SV6m6fmWAxVjU1jh .activeText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .done0,#mermaid-svg-SV6m6fmWAxVjU1jh .done1,#mermaid-svg-SV6m6fmWAxVjU1jh .done2,#mermaid-svg-SV6m6fmWAxVjU1jh .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .doneText0,#mermaid-svg-SV6m6fmWAxVjU1jh .doneText1,#mermaid-svg-SV6m6fmWAxVjU1jh .doneText2,#mermaid-svg-SV6m6fmWAxVjU1jh .doneText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .crit0,#mermaid-svg-SV6m6fmWAxVjU1jh .crit1,#mermaid-svg-SV6m6fmWAxVjU1jh .crit2,#mermaid-svg-SV6m6fmWAxVjU1jh .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit0,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit1,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit2,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit0,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit1,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit2,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-SV6m6fmWAxVjU1jh .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-SV6m6fmWAxVjU1jh .milestoneText{font-style:italic}#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText0,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText1,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText2,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText0,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText1,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText2,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup text .title{font-weight:bolder}#mermaid-svg-SV6m6fmWAxVjU1jh g.clickable{cursor:pointer}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-SV6m6fmWAxVjU1jh .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-SV6m6fmWAxVjU1jh .dashed-line{stroke-dasharray:3}#mermaid-svg-SV6m6fmWAxVjU1jh #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh .commit-id,#mermaid-svg-SV6m6fmWAxVjU1jh .commit-msg,#mermaid-svg-SV6m6fmWAxVjU1jh .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-SV6m6fmWAxVjU1jh .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-SV6m6fmWAxVjU1jh .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .node circle.state-start{fill:black;stroke:black}#mermaid-svg-SV6m6fmWAxVjU1jh .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-SV6m6fmWAxVjU1jh #statediagram-barbEnd{fill:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state .divider{stroke:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-SV6m6fmWAxVjU1jh .note-edge{stroke-dasharray:5}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-SV6m6fmWAxVjU1jh .error-icon{fill:#522}#mermaid-svg-SV6m6fmWAxVjU1jh .error-text{fill:#522;stroke:#522}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-thickness-normal{stroke-width:2px}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-SV6m6fmWAxVjU1jh .marker{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-SV6m6fmWAxVjU1jh {
color: rgba(0, 0, 0, 0.75);
font: ;
}
2 STM32F4+ESP8266获取网络时间
我之前的项目中一直在用STM32,比较熟悉,所以这次依然使用了STM32作为主控板。获取时间的大体思路是利用ESP8266WIFI模块能够连网的功能,在上电后能够通过MCU控制ESP8266连接到网络并发送相应的API请求,获得当前时间,之后把当前时间赋值到RTC时钟,实现自走的功能。
2.1 ESP8266的联网和API请求
首先先简要介绍一下ESP8266模块,在安可信官网上有此模块的详细介绍,这里放一个链接:
安可信,在它用户文档里对该模块的介绍是这样的:
ESP8266是一款超低功耗的UART-WiFi 透传模块,拥有业内极富竞争力的封装尺寸和超低能耗技术,专为移动设备和物联网应用设计,可将用户的物理设备连接到Wi-Fi 无线网络上,进行互联网或局域网通信,实现联网功能。
此模块拥有独立控制能力,可以作为主控芯片使用,也可以作为其他控制器的联网芯片,使主控制器拥有联网功能。
模块联网的方式是通过其RXD与TXD分别与串口的TX端和RX端相连,通过串口通信发送AT指令来控制ESP8266实现联网。有关模块的STA、AP、STA+AP模式还有AT指令集可以参考以下链接:
ESP8266 AT指令集:https://blog.csdn.net/qq_45104817/article/details/105834987
模块教程:https://blog.csdn.net/mbs520/article/details/109572070
在本项目通过使用STM32上的USART6给8266发送以下的AT指令实现了联网功能:
1.设置工作模式:
AT+CWMODE=1 //设置模式,1为STA模式(从机)
2.重启: 重启可以保证模式能够转换成功
AT+RST
3.联网:(在这里遇到了一个小问题,就是我在学校连校园网的话需要通过用户认证,但单纯的用这个指令不能联网成功。网上说可以通过串口发送响应的完成认证的请求头,但后来我通过笔记本连网后再开热点解决了):
AT+CWJAP="name","password" //name为你想连的WIFI名称,password为你的WIFI密码
4.设置连接方式 只有将连接方式设置为单路连接才能启用TCP/IP
AT+CIPMUX=0
TCP/IP是一种网络通信协议,可以理解为设备连接到目标网络后,通过这个特有的双方都理解的语言才可以实现数据通信,详细的介绍可以参考:https://zhuanlan.zhihu.com/p/33889997
5.连接到目标站点 第4步设置了单路连接,现在就可以启用TCP连接到API站点获取数据了!
AT+CIPSTART="TCP","quan.suning.com",80 //协议形式;站点IP或网址;端口号(指定一个没有被占用的即可)
这里quan.suning.com即为我们要用的API,连接到该网络后,可以通过发送标准形式的请求头来获取当前时间。
6.开启透传 开启透传模式,为传送报头做准备
AT+CIPMODE=1
7.发送报头,接收数据
res=esp8266_send_data("GET http://quan.suning.com/getSysTime.do HTTP/1.1\r\nHost: quan.suning.com\r\n\r\n",50);
返回来的数据如下:
receive:HTTP/1.1 200 OK
Date: Sat, 16 Oct 2021 10:31:07 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 62
Connection: keep-alive
Server: styx
Set-Cookie: tradeLdc=NJGX_YG;Expires=Sat, 16-Oct-21 22:31:07 GMT
Strict-Transport-Security: max-age=300
Cache-Control: no-cache,no-store,max-age=0,s-maxage=0
Access-Control-Allow-Credentials: true
X-Ser: BC98_dx-lt-yd-jiangsu-zhenjiang-3-cache-16, BC204_lt-shanxi-taiyuan-6-cache-4
X-Cache: MISS from BC204_lt-shanxi-taiyuan-6-cache-4(baishan)
{"sysTime2":"2021-10-16 18:31:07","sysTime1":"20211016183107"}
我们的主要目的是获取当前时间,所以返回来的数据中大部分是没有用的,通过观察发现我们只需要提取出“sysTime2”后面跟的时间即可,所以我们需要一些算法来把时间提取出来。我的思路就是先通过字符串查找函数strstr找到"sysTime2"所在位置的指针,再根据时、分、秒相对于指针的偏移量依次把数据提取出来,这里我把我的代码放在下面,当然大家也可以设置更加高效的算法:
#define HOURS_ADD_DRES 22 //小时相对于data_pt指针的偏移量是22
char *data_pt=NULL;
while(!data_pt){
res=esp8266_send_data("GET http://quan.suning.com/getSysTime.do HTTP/1.1\r\nHost: quan.suning.com\r\n\r\n",50);
data_pt = strstr((const char *) res,(const char *)"sysTime2");//在返回数据中找sysTime2
}
printf("GET TIME SUCCESS!\r\n");//如果找到了,data_pt非空,退出循环,获取时间成功
int k=0;
char time[10];
char *hour_string;//存放时间的字符串
int hour,second,minute=0;
hour_string = data_pt + HOURS_ADD_DRES; //hour_string指针指向时-分-秒时间的小时位第一位,精准锁定
for(i=0;i<=7;i++)//把各位的数据提取出来
{
time[k]=*hour_string;
hour_string++;k++;
}
time[8]='\0';
hour=(time[0]-48)*10+(time[1]-48);
minute=(time[3]-48)*10+(time[4]-48);
second=(time[6]-48)*10+(time[7]-48);//ASCII码转换成数字,方便后续
printf("time:%s",time);//把当前获取到的时间打印到串口
可以看到串口输出了正确的值,获取时间成功了!
2.2 RTC时钟的设置
在这之前我们已经通过ESP8266联网实现了当前时间的获取,但如此这样一直获取时间显然是不现实的,一方面每次发送请求有一定的失败概率,如果获取时间失败那么时间的显示就会不连续。另一方面网站面临多次重复的请求会对你的IP地址进行屏蔽来防止恶意攻击。此外如此频繁的请求也会消耗太多的资源放在请求上,导致程序的效率不高。通过了解我发现可以通过芯片内部的RTC时钟来实现时间的自走功能。
有关RTC时钟的具体实现与详解可以参考这篇:RCT详解
通过配置RTC,将ESP8266模块获取的时间写入RTC,之后可以利用RTC的秒中断或者根据RTC的计数器变动来刷新当前时间值,在灯光上进行不同的显示。
RTC_Set_Time(hour,minute,second,1);
此代码实现了随着时间的变化刷新显示,同时,也可以采用秒中断的方法(后者更加精确)
RTC_TimeTypeDef RTC_TimeTypeInitStructure;
RTC_GetTime(RTC_Format_BIN,&RTC_TimeTypeInitStructure);
flags0=RTC_TimeTypeInitStructure.RTC_Seconds%10;
flags1=RTC_TimeTypeInitStructure.RTC_Seconds/10;
flagm0=RTC_TimeTypeInitStructure.RTC_Minutes%10;
flagm1=RTC_TimeTypeInitStructure.RTC_Minutes/10;
flagh0=RTC_TimeTypeInitStructure.RTC_Hours%10;
flagh1=RTC_TimeTypeInitStructure.RTC_Hours/10;
if(flags0!=befores0){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Seconds%10,5,1);
befores0=flags0;}
if(flags1!=befores1){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Seconds/10,4,0);
befores1=flags1;}
if(flagm0!=beforem0){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Minutes%10,3,1);
beforem0=flagm0;}
if(flagm1!=beforem1){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Minutes/10,2,0);
beforem1=flagm1;}
if(flagh0!=beforeh0){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Hours%10,1,1);
beforeh0=flagh0;}
if(flagh1!=beforeh1){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Hours/10,0,0);
beforeh1=flagh1;}
在下一篇文章中会具体介绍灯光控制模块!谢谢大家的支持!
版权声明:本文为CSDN博主「eric_accept」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/eric_accept/article/details/119839343
0 效果
话不多说,先来看看最后实现的效果(从左到右分别为时、分、秒,当时设计的时候忘了设计中间的冒号了
-_-):
画了PCB,请朋友帮忙画了solidworks打印出来框架,然后买螺丝进行拼装。
1 简介
某天逛某宝的时候突然发现了拟辉光钟,当时就觉得挺好看的,但一看价格就直接劝退了(这里放了两张价格)
于是我产生了自己动手做的想法。现在网上搜了一下拟辉光钟的工作原理:
拟辉光钟是利用光线在亚克力板上的划痕处产生光线折射,使得划痕处的光线较亮,配置以随时间而变的灯光,达到类似于辉光钟的效果。
在搜索了一些资料后,我把我的工作分成了两个部分,第一个部分就是时间的获取,第二个部分是灯光的控制。下面主要就这两个大的模块介绍下具体怎么实现的。
#mermaid-svg-SV6m6fmWAxVjU1jh .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .label text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .node rect,#mermaid-svg-SV6m6fmWAxVjU1jh .node circle,#mermaid-svg-SV6m6fmWAxVjU1jh .node ellipse,#mermaid-svg-SV6m6fmWAxVjU1jh .node polygon,#mermaid-svg-SV6m6fmWAxVjU1jh .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .node .label{text-align:center;fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .node.clickable{cursor:pointer}#mermaid-svg-SV6m6fmWAxVjU1jh .arrowheadPath{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-SV6m6fmWAxVjU1jh .flowchart-link{stroke:#333;fill:none}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel rect{opacity:0.9}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel span{color:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .cluster text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-SV6m6fmWAxVjU1jh .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-SV6m6fmWAxVjU1jh text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .actor-line{stroke:grey}#mermaid-svg-SV6m6fmWAxVjU1jh .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sequenceNumber{fill:#fff}#mermaid-svg-SV6m6fmWAxVjU1jh #sequencenumber{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh #crosshead path{fill:#333;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .messageText{fill:#333;stroke:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-SV6m6fmWAxVjU1jh .labelText,#mermaid-svg-SV6m6fmWAxVjU1jh .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .loopText,#mermaid-svg-SV6m6fmWAxVjU1jh .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-SV6m6fmWAxVjU1jh .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-SV6m6fmWAxVjU1jh .noteText,#mermaid-svg-SV6m6fmWAxVjU1jh .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-SV6m6fmWAxVjU1jh .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-SV6m6fmWAxVjU1jh .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-SV6m6fmWAxVjU1jh .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-SV6m6fmWAxVjU1jh .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .section{stroke:none;opacity:0.2}#mermaid-svg-SV6m6fmWAxVjU1jh .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-SV6m6fmWAxVjU1jh .section2{fill:#fff400}#mermaid-svg-SV6m6fmWAxVjU1jh .section1,#mermaid-svg-SV6m6fmWAxVjU1jh .section3{fill:#fff;opacity:0.2}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle0{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle1{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle2{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle3{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-SV6m6fmWAxVjU1jh .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .grid path{stroke-width:0}#mermaid-svg-SV6m6fmWAxVjU1jh .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-SV6m6fmWAxVjU1jh .task{stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText:not([font-size]){font-size:11px}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-SV6m6fmWAxVjU1jh .task.clickable{cursor:pointer}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-SV6m6fmWAxVjU1jh .taskText0,#mermaid-svg-SV6m6fmWAxVjU1jh .taskText1,#mermaid-svg-SV6m6fmWAxVjU1jh .taskText2,#mermaid-svg-SV6m6fmWAxVjU1jh .taskText3{fill:#fff}#mermaid-svg-SV6m6fmWAxVjU1jh .task0,#mermaid-svg-SV6m6fmWAxVjU1jh .task1,#mermaid-svg-SV6m6fmWAxVjU1jh .task2,#mermaid-svg-SV6m6fmWAxVjU1jh .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside0,#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside2{fill:#000}#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside1,#mermaid-svg-SV6m6fmWAxVjU1jh .taskTextOutside3{fill:#000}#mermaid-svg-SV6m6fmWAxVjU1jh .active0,#mermaid-svg-SV6m6fmWAxVjU1jh .active1,#mermaid-svg-SV6m6fmWAxVjU1jh .active2,#mermaid-svg-SV6m6fmWAxVjU1jh .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-SV6m6fmWAxVjU1jh .activeText0,#mermaid-svg-SV6m6fmWAxVjU1jh .activeText1,#mermaid-svg-SV6m6fmWAxVjU1jh .activeText2,#mermaid-svg-SV6m6fmWAxVjU1jh .activeText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .done0,#mermaid-svg-SV6m6fmWAxVjU1jh .done1,#mermaid-svg-SV6m6fmWAxVjU1jh .done2,#mermaid-svg-SV6m6fmWAxVjU1jh .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .doneText0,#mermaid-svg-SV6m6fmWAxVjU1jh .doneText1,#mermaid-svg-SV6m6fmWAxVjU1jh .doneText2,#mermaid-svg-SV6m6fmWAxVjU1jh .doneText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .crit0,#mermaid-svg-SV6m6fmWAxVjU1jh .crit1,#mermaid-svg-SV6m6fmWAxVjU1jh .crit2,#mermaid-svg-SV6m6fmWAxVjU1jh .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit0,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit1,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit2,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit0,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit1,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit2,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-SV6m6fmWAxVjU1jh .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-SV6m6fmWAxVjU1jh .milestoneText{font-style:italic}#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText0,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText1,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText2,#mermaid-svg-SV6m6fmWAxVjU1jh .doneCritText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText0,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText1,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText2,#mermaid-svg-SV6m6fmWAxVjU1jh .activeCritText3{fill:#000 !important}#mermaid-svg-SV6m6fmWAxVjU1jh .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup text .title{font-weight:bolder}#mermaid-svg-SV6m6fmWAxVjU1jh g.clickable{cursor:pointer}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-SV6m6fmWAxVjU1jh .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-SV6m6fmWAxVjU1jh .dashed-line{stroke-dasharray:3}#mermaid-svg-SV6m6fmWAxVjU1jh #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh .commit-id,#mermaid-svg-SV6m6fmWAxVjU1jh .commit-msg,#mermaid-svg-SV6m6fmWAxVjU1jh .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-SV6m6fmWAxVjU1jh .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-SV6m6fmWAxVjU1jh .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-SV6m6fmWAxVjU1jh .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-SV6m6fmWAxVjU1jh .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-SV6m6fmWAxVjU1jh .edgeLabel text{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-SV6m6fmWAxVjU1jh .node circle.state-start{fill:black;stroke:black}#mermaid-svg-SV6m6fmWAxVjU1jh .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-SV6m6fmWAxVjU1jh #statediagram-barbEnd{fill:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state .divider{stroke:#9370db}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-SV6m6fmWAxVjU1jh .note-edge{stroke-dasharray:5}#mermaid-svg-SV6m6fmWAxVjU1jh .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-SV6m6fmWAxVjU1jh .error-icon{fill:#522}#mermaid-svg-SV6m6fmWAxVjU1jh .error-text{fill:#522;stroke:#522}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-thickness-normal{stroke-width:2px}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-SV6m6fmWAxVjU1jh .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-SV6m6fmWAxVjU1jh .marker{fill:#333}#mermaid-svg-SV6m6fmWAxVjU1jh .marker.cross{stroke:#333}
:root { --mermaid-font-family: "trebuchet ms", verdana, arial;}
#mermaid-svg-SV6m6fmWAxVjU1jh {
color: rgba(0, 0, 0, 0.75);
font: ;
}
2 STM32F4+ESP8266获取网络时间
我之前的项目中一直在用STM32,比较熟悉,所以这次依然使用了STM32作为主控板。获取时间的大体思路是利用ESP8266WIFI模块能够连网的功能,在上电后能够通过MCU控制ESP8266连接到网络并发送相应的API请求,获得当前时间,之后把当前时间赋值到RTC时钟,实现自走的功能。
2.1 ESP8266的联网和API请求
首先先简要介绍一下ESP8266模块,在安可信官网上有此模块的详细介绍,这里放一个链接:
安可信,在它用户文档里对该模块的介绍是这样的:
ESP8266是一款超低功耗的UART-WiFi 透传模块,拥有业内极富竞争力的封装尺寸和超低能耗技术,专为移动设备和物联网应用设计,可将用户的物理设备连接到Wi-Fi 无线网络上,进行互联网或局域网通信,实现联网功能。
此模块拥有独立控制能力,可以作为主控芯片使用,也可以作为其他控制器的联网芯片,使主控制器拥有联网功能。
模块联网的方式是通过其RXD与TXD分别与串口的TX端和RX端相连,通过串口通信发送AT指令来控制ESP8266实现联网。有关模块的STA、AP、STA+AP模式还有AT指令集可以参考以下链接:
ESP8266 AT指令集:https://blog.csdn.net/qq_45104817/article/details/105834987
模块教程:https://blog.csdn.net/mbs520/article/details/109572070
在本项目通过使用STM32上的USART6给8266发送以下的AT指令实现了联网功能:
1.设置工作模式:
AT+CWMODE=1 //设置模式,1为STA模式(从机)
2.重启: 重启可以保证模式能够转换成功
AT+RST
3.联网:(在这里遇到了一个小问题,就是我在学校连校园网的话需要通过用户认证,但单纯的用这个指令不能联网成功。网上说可以通过串口发送响应的完成认证的请求头,但后来我通过笔记本连网后再开热点解决了):
AT+CWJAP="name","password" //name为你想连的WIFI名称,password为你的WIFI密码
4.设置连接方式 只有将连接方式设置为单路连接才能启用TCP/IP
AT+CIPMUX=0
TCP/IP是一种网络通信协议,可以理解为设备连接到目标网络后,通过这个特有的双方都理解的语言才可以实现数据通信,详细的介绍可以参考:https://zhuanlan.zhihu.com/p/33889997
5.连接到目标站点 第4步设置了单路连接,现在就可以启用TCP连接到API站点获取数据了!
AT+CIPSTART="TCP","quan.suning.com",80 //协议形式;站点IP或网址;端口号(指定一个没有被占用的即可)
这里quan.suning.com即为我们要用的API,连接到该网络后,可以通过发送标准形式的请求头来获取当前时间。
6.开启透传 开启透传模式,为传送报头做准备
AT+CIPMODE=1
7.发送报头,接收数据
res=esp8266_send_data("GET http://quan.suning.com/getSysTime.do HTTP/1.1\r\nHost: quan.suning.com\r\n\r\n",50);
返回来的数据如下:
receive:HTTP/1.1 200 OK
Date: Sat, 16 Oct 2021 10:31:07 GMT
Content-Type: text/html;charset=UTF-8
Content-Length: 62
Connection: keep-alive
Server: styx
Set-Cookie: tradeLdc=NJGX_YG;Expires=Sat, 16-Oct-21 22:31:07 GMT
Strict-Transport-Security: max-age=300
Cache-Control: no-cache,no-store,max-age=0,s-maxage=0
Access-Control-Allow-Credentials: true
X-Ser: BC98_dx-lt-yd-jiangsu-zhenjiang-3-cache-16, BC204_lt-shanxi-taiyuan-6-cache-4
X-Cache: MISS from BC204_lt-shanxi-taiyuan-6-cache-4(baishan)
{"sysTime2":"2021-10-16 18:31:07","sysTime1":"20211016183107"}
我们的主要目的是获取当前时间,所以返回来的数据中大部分是没有用的,通过观察发现我们只需要提取出“sysTime2”后面跟的时间即可,所以我们需要一些算法来把时间提取出来。我的思路就是先通过字符串查找函数strstr找到"sysTime2"所在位置的指针,再根据时、分、秒相对于指针的偏移量依次把数据提取出来,这里我把我的代码放在下面,当然大家也可以设置更加高效的算法:
#define HOURS_ADD_DRES 22 //小时相对于data_pt指针的偏移量是22
char *data_pt=NULL;
while(!data_pt){
res=esp8266_send_data("GET http://quan.suning.com/getSysTime.do HTTP/1.1\r\nHost: quan.suning.com\r\n\r\n",50);
data_pt = strstr((const char *) res,(const char *)"sysTime2");//在返回数据中找sysTime2
}
printf("GET TIME SUCCESS!\r\n");//如果找到了,data_pt非空,退出循环,获取时间成功
int k=0;
char time[10];
char *hour_string;//存放时间的字符串
int hour,second,minute=0;
hour_string = data_pt + HOURS_ADD_DRES; //hour_string指针指向时-分-秒时间的小时位第一位,精准锁定
for(i=0;i<=7;i++)//把各位的数据提取出来
{
time[k]=*hour_string;
hour_string++;k++;
}
time[8]='\0';
hour=(time[0]-48)*10+(time[1]-48);
minute=(time[3]-48)*10+(time[4]-48);
second=(time[6]-48)*10+(time[7]-48);//ASCII码转换成数字,方便后续
printf("time:%s",time);//把当前获取到的时间打印到串口
可以看到串口输出了正确的值,获取时间成功了!
2.2 RTC时钟的设置
在这之前我们已经通过ESP8266联网实现了当前时间的获取,但如此这样一直获取时间显然是不现实的,一方面每次发送请求有一定的失败概率,如果获取时间失败那么时间的显示就会不连续。另一方面网站面临多次重复的请求会对你的IP地址进行屏蔽来防止恶意攻击。此外如此频繁的请求也会消耗太多的资源放在请求上,导致程序的效率不高。通过了解我发现可以通过芯片内部的RTC时钟来实现时间的自走功能。
有关RTC时钟的具体实现与详解可以参考这篇:RCT详解
通过配置RTC,将ESP8266模块获取的时间写入RTC,之后可以利用RTC的秒中断或者根据RTC的计数器变动来刷新当前时间值,在灯光上进行不同的显示。
RTC_Set_Time(hour,minute,second,1);
此代码实现了随着时间的变化刷新显示,同时,也可以采用秒中断的方法(后者更加精确)
RTC_TimeTypeDef RTC_TimeTypeInitStructure;
RTC_GetTime(RTC_Format_BIN,&RTC_TimeTypeInitStructure);
flags0=RTC_TimeTypeInitStructure.RTC_Seconds%10;
flags1=RTC_TimeTypeInitStructure.RTC_Seconds/10;
flagm0=RTC_TimeTypeInitStructure.RTC_Minutes%10;
flagm1=RTC_TimeTypeInitStructure.RTC_Minutes/10;
flagh0=RTC_TimeTypeInitStructure.RTC_Hours%10;
flagh1=RTC_TimeTypeInitStructure.RTC_Hours/10;
if(flags0!=befores0){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Seconds%10,5,1);
befores0=flags0;}
if(flags1!=befores1){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Seconds/10,4,0);
befores1=flags1;}
if(flagm0!=beforem0){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Minutes%10,3,1);
beforem0=flagm0;}
if(flagm1!=beforem1){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Minutes/10,2,0);
beforem1=flagm1;}
if(flagh0!=beforeh0){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Hours%10,1,1);
beforeh0=flagh0;}
if(flagh1!=beforeh1){
ws281x_showNum(RTC_TimeTypeInitStructure.RTC_Hours/10,0,0);
beforeh1=flagh1;}
在下一篇文章中会具体介绍灯光控制模块!谢谢大家的支持!
版权声明:本文为CSDN博主「eric_accept」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/eric_accept/article/details/119839343
暂无评论