1. /*
  2. DS3231_test.pde
  3. Eric Ayars
  4. 4/11
  5.  
  6. Test/demo of read routines for a DS3231 RTC.
  7.  
  8. Turn on the serial monitor after loading this to check if things are
  9. working as they should.
  10.  
  11. */
  12.  
  13. #include <DS3231.h>
  14. #include <Wire.h>
  15. #include <EEPROM.h>
  16.  
  17. #define powerPin 7
  18. DS3231 Clock;
  19.  
  20. String ReceivedCache="";
  21. String BTime="2010-07-24 11:15:00";
  22. String ETime="2010-07-24 11:15:00";
  23. boolean isFire=false;
  24.  
  25. void setup() {
  26. // Start the I2C interface
  27. Wire.begin();
  28. Clock.setClockMode(false);
  29. Serial.begin();
  30. pinMode(powerPin,OUTPUT);
  31. digitalWrite(powerPin,LOW);
  32. Clock.turnOnAlarm();
  33. RetrieveFireSet();
  34. }
  35.  
  36. void loop() {
  37.  
  38. handleCmd();
  39. checkFire();
  40. }
  41.  
  42. void checkFire(){
  43. String dateTime=GetTime();
  44. if(dateTime>=BTime && dateTime<=ETime){
  45. digitalWrite(powerPin,HIGH);
  46. isFire=true;
  47. }else{
  48. digitalWrite(powerPin,LOW);
  49. isFire=false;
  50. }
  51. }
  52.  
  53. String formatNum(int a){
  54. if(a<)return "" +(String)a;
  55. return (String)a;
  56. }
  57.  
  58. String GetTime(){
  59. bool Century=false;
  60. bool h12=false;
  61. bool PM=false;
  62. int second,minute,hour,date,month,year,temperature;
  63. second=Clock.getSecond();
  64. minute=Clock.getMinute();
  65. hour=Clock.getHour(h12, PM);
  66. date=Clock.getDate();
  67. month=Clock.getMonth(Century);
  68. year=Clock.getYear();
  69.  
  70. String dateTime="" +formatNum(year) +"-"
  71. +formatNum(month) +"-"
  72. +formatNum(date) +" "
  73. +formatNum(hour) +":"
  74. +formatNum(minute)+":"
  75. +formatNum(second);
  76. return dateTime;
  77. }
  78.  
  79. void handleGetTime(){
  80.  
  81. String dateTime=GetTime();
  82. Serial.println("OK:"+dateTime);
  83.  
  84. }
  85. void handleSetTime(){
  86.  
  87. int second,minute,hour,date,month,year,dayOfWeek;
  88. String dateTime=ReceivedCache.substring(,);
  89.  
  90. year =dateTime.substring(,).toInt();
  91. month =dateTime.substring(,).toInt();
  92. date=dateTime.substring(,).toInt();
  93.  
  94. hour=dateTime.substring(,).toInt();
  95. minute=dateTime.substring(,).toInt();
  96. second=dateTime.substring(,).toInt();
  97. dayOfWeek=dateTime.substring(,).toInt();
  98. Clock.setSecond(second);//Set the second
  99. Clock.setMinute(minute);//Set the minute
  100. Clock.setHour(hour); //Set the hour
  101. Clock.setDoW(dayOfWeek); //Set the day of the week
  102. Clock.setDate(date); //Set the date of the month
  103. Clock.setMonth(month); //Set the month of the year
  104. Clock.setYear(year); //Set the year (Last two digits of the year)
  105.  
  106. Serial.println("OK:");
  107. }
  108.  
  109. void handleGetFire(){
  110. String tmp=_ReadFireSet();
  111. if(tmp==""){
  112. Serial.println("EE:fire time not set!");
  113. }else{
  114. Serial.println("OK:" + tmp);
  115. }
  116. }
  117.  
  118. void handleSetFire(){
  119.  
  120. for(int address=;address<;address++){
  121. EEPROM.write(address,(byte)ReceivedCache[address]);
  122. //Serial.print((char)EEPROM.read(address));
  123. }
  124. //Serial.println("");
  125. String bTime=ReceivedCache.substring(,);
  126. String eTime=ReceivedCache.substring(,);
  127. bool flag=RetrieveFireSet();
  128. // Serial.println("flag:" + (String)flag);
  129. if(flag && (bTime==BTime && eTime==ETime)){
  130. Serial.println("OK:");
  131. }else{
  132. Serial.println("EE:Set Fail");
  133. }
  134. }
  135.  
  136. String _ReadFireSet(){
  137. int address=;
  138. String tmp="";
  139. char readChar=' ';
  140. for(int address=;address<;address++){
  141. readChar=(char)EEPROM.read(address);
  142. tmp +=readChar;
  143. }
  144.  
  145. if(tmp!="SetF:"){
  146. return "";
  147. }
  148.  
  149. tmp="";
  150. for(int address=;address<;address++){
  151. readChar=(char)EEPROM.read(address);
  152. tmp +=readChar;
  153. }
  154. //Serial.println(tmp);
  155. return tmp;
  156. }
  157.  
  158. bool RetrieveFireSet(){
  159. String tmp=_ReadFireSet();
  160. if(tmp==""){
  161. return false;
  162. }else{
  163. BTime=tmp.substring(,);
  164. ETime=tmp.substring(,);
  165. return true;
  166. }
  167. }
  168.  
  169. //read Serial data and hand command
  170. //
  171. void handleCmd(){
  172. char readChar=' ';
  173.  
  174. while(Serial.available()>){
  175. readChar=(char)Serial.read();
  176. ReceivedCache =ReceivedCache+ (String)readChar;
  177. //delayMicroseconds(10);
  178. }
  179. //Serial.println("ABC");
  180. // Serial.println(ReceivedCache);
  181. if(ReceivedCache.startsWith("GetT:")){
  182. handleGetTime();
  183. ReceivedCache=ReceivedCache.substring();
  184.  
  185. }else if(ReceivedCache.startsWith("SetT:")){
  186. //like->SetT:2015-07-24 16:54:23,7
  187. if(ReceivedCache.length()>=){
  188. handleSetTime();
  189. ReceivedCache=ReceivedCache.substring();
  190. }
  191. }else if(ReceivedCache.startsWith("GetS:")){
  192. Serial.println("OK:"+(String)isFire);
  193. ReceivedCache=ReceivedCache.substring();
  194. }else if(ReceivedCache.startsWith("GetF:")){
  195. handleGetFire();
  196. ReceivedCache=ReceivedCache.substring();
  197. }else if(ReceivedCache.startsWith("SetF:")){
  198. if(ReceivedCache.length()>=){
  199. handleSetFire();
  200. ReceivedCache=ReceivedCache.substring();
  201. }
  202. }else if(ReceivedCache.startsWith("GetC:")){
  203. int temperature=Clock.getTemperature();
  204. Serial.println("OK:" +(String)temperature);
  205. ReceivedCache=ReceivedCache.substring();
  206. }
  207. else{
  208. if(ReceivedCache.length()>=){
  209. ReceivedCache="";
  210. }
  211. }
  212.  
  213. if(readChar=='\n')ReceivedCache="";
  214. }

arduino 串口命令解析的更多相关文章

  1. 邵国际: C 语言对象化设计实例 —— 命令解析器

    本文系转载,著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 邵国际 来源: 微信公众号linux阅码场(id: linuxdev) 内容简介 单片机工程师常常疑惑为什么 ...

  2. win10上使用php与python实现与arduino串口通信

    注意: php 需要php7,安装及开启php_dio.dll com口按照实际的进行设置,如果不知道可以打开arduino编辑器进行查看 可以与用户实现命令行交互,但是效率过慢,不清楚如何优化,使用 ...

  3. linux mknod命令解析

    linux mknod命令解析 http://www.cnblogs.com/cobbliu/archive/2011/07/05/2389014.html mknod:make node  生成设备 ...

  4. C#基础:命令解析

    1.普通格式命令的解析 例如: RENA<SP>E:\\A.txt<SP>C:\\B.txt<CRLF> (SP -> 空格,CRLF -> 回车加换行 ...

  5. Appium Android Bootstrap源码分析之命令解析执行

    通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...

  6. virsh的详细命令解析(一)

    virsh的详细命令解析 virsh 有命令模式和交互模式如果直接在vrish后面添加参数是命令模式,如果直接写virsh,就会进入交互模式 virsh list 列出所有的虚拟机,虚拟机的状态有(8 ...

  7. IIC读写AT24C02代码2——串口命令控制多页读写

    通过串口输入 R .W 进行控制程序读写IIC设备.波特率9600bps,晶振115200HZ. main.c /*------------------------------------------ ...

  8. (转)Linux命令:使用dig,nslookup命令解析域名

    Linux命令:使用dig命令解析域名 Linux下解析域名除了使用nslookup之外,开可以使用dig命令来解析域名,dig命令可以得到更多的域名信息. dig的全称是 (domain infor ...

  9. angular-cli.json配置参数解析,常用命令解析

    1.angular-cli.json配置参数解析 { "project": { "name": "ng-admin", //项目名称 &qu ...

随机推荐

  1. php用正则判断是否为数字

    验证数字:^[0-9]*$验证n位的数字:^\d{n}$验证至少n位数字:^\d{n,}$验证m-n位的数字:^\d{m,n}$验证零和非零开头的数字:^(0|[1-9][0-9]*)$验证有两位小数 ...

  2. 前端-CSS-9-文本和字体-背景颜色

    字体属性 div{ width: 300px; height: 100px; /*background-color: red;*/ border: 1px solid red; /*设置字体大小 px ...

  3. javascript时间日期操作

    Js获取当前日期时间及其它操作 var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();   ...

  4. 程序员教程-10章-C++程序设计

    本来这章是不打算写了的,毕竟自己学的是JAVA,C++就不想看了,但毕竟还有一点时间,就把C++也看一下吧 目录结构 10.1 C++程序基础 10.1.1 数据类型 1 基本数据类型 2 常量和变量 ...

  5. 【转】Phong和Blinn-Phong光照模型

    来自:http://www.cnblogs.com/bluebean/p/5299358.html Phong和Blinn-Phong是计算镜面反射光的两种光照模型,两者仅仅有很小的不同之处. 1.P ...

  6. How to Pronounce OPPORTUNITY

    How to Pronounce OPPORTUNITY Share Tweet Share Take the opportunity to learn this word!  Learn how t ...

  7. 调整Mic音量

    uses  MMSystem; function GetLineInHandle(AudioType : integer) : integer;var  i : integer;  AudioCaps ...

  8. Hibernate迫切左外连接和迫切内连接

    •迫切左外连接: •LEFT JOIN FETCH 关键字表示迫切左外连接检索策略. –list() 方法返回的集合中存放实体对象的引用, 每个 Department 对象关联的 Employee  ...

  9. ingress 密码验证

    traefik ingress 上面的方式需要引入haprox或者nginx,多引入了一个代理转发层,其实ingress本身就提供了basic auth的支持,在ingress规则中添加额外的认证an ...

  10. Java对称与非对称加密解密,AES与RSA

    加密技术可以分为对称与非对称两种. 对称加密,解密,即加密与解密用的是同一把秘钥,常用的对称加密技术有DES,AES等 而非对称技术,加密与解密用的是不同的秘钥,常用的非对称加密技术有RSA等 为什么 ...