极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27184|回复: 7

C3-470 使用经验一例

[复制链接]
发表于 2014-11-4 19:22:08 | 显示全部楼层 |阅读模式
GGA 报文输出0表示未定位
RMC 报文输出A表示定位有效,经纬度输出0
额,目测这个有效说的是时间有效
回复

使用道具 举报

 楼主| 发表于 2014-11-11 22:05:56 | 显示全部楼层
本帖最后由 hi55234 于 2014-11-11 22:19 编辑


朴素的极致



  1. #include <MemoryFree.h>

  2. #include <LiquidCrystal.h>
  3. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);




  4. #include <SoftwareSerial.h>
  5. SoftwareSerial gps(8, 9);
  6. char tempx[17];
  7. char GPRMC[80];
  8. char GPGGA[80];
  9. int gpschangertest;
  10. boolean gpschanger;
  11. int weiduA;
  12. unsigned long weiduB,weiduC;
  13. int jingduA;
  14. unsigned long jingduB,jingduC;


  15. boolean GPGGAget=0;
  16. boolean GPRMCget=0;
  17. unsigned long gpstimes;
  18. boolean konghangpanduan;
  19. boolean gpsshuchuqiehuanB;
  20. int jsq1=0;
  21. int jsq2=0;
  22. int jsq3=0;
  23. int jsq4=0;
  24. int jsq5=0;
  25. int jsq6=0;
  26. int jsq7=0;
  27. int GPGGAlong=0;
  28. int GPRMClong=0;
  29. boolean jiaoyanjieguoA,jiaoyanjieguoC;
  30. float sudu;

  31. String jianyan="";
  32. int yihuoyunsuan;



  33. boolean dingweiok=0;

  34. boolean gpsxianguangA=0;
  35. boolean fangzhilianji=1;

  36. int lcdxianshi=0;

  37. /*---------------------GPS end---------------------------------*/
  38. int gpsshuchuqiehuanA;


  39.   
  40. #include<EEPROM.h>
  41. int EEPROMvalue;





  42. const int chipSelect = 10;






  43. char TrackB0[8]="-0000.0";

  44. char TrackB1[11]="00.0000000";
  45. char TrackB2[12]="000.0000000";

  46. char TrackB4[20]="2000-00-00T00:00:00";

  47. char TrackB5[8]="0000.00";
  48. char TrackB6[7]="000.00";


  49. boolean weiduxuweitiao=0;
  50. boolean jingduxuweitiao=0;
  51. int weiduzhanwei,jingduzhanwei;


  52. int utc8s,utc8f,utc8m,utc8n,utc8y,utc8r;

  53. int xiaoyue[5]={4,6,9,11};

  54. boolean rijinwei,yuejinwei,nianjinwei,xiaoyueok;

  55. unsigned long fentodu;





  56. #include <OneWire.h>
  57. #include <DallasTemperature.h>
  58. OneWire oneWire(16);
  59. DallasTemperature sensors(&oneWire);



  60. #include <dht11.h>
  61. dht11 DHT11;
  62. #define DHT11PIN 17




  63. #include <Wire.h>
  64. #include <RTClib.h>
  65. void printDateTime(DateTime dateTime);
  66. RTC_DS1307 RTC;
  67. int nian,yue,ri,shi,fen,miao;
  68. boolean shoushiqidong;




  69. void setup() {

  70.   
  71.   Wire.begin();
  72.   RTC.begin();

  73. pinMode(14,INPUT);
  74. pinMode(15,INPUT);

  75. lcd.begin(16, 2);


  76. Serial.begin(9600);
  77. gps.begin(9600);

  78.   
  79. EEPROMvalue= EEPROM.read(0);
  80. if(EEPROMvalue==0)gpsshuchuqiehuanB=0;
  81. else gpsshuchuqiehuanB=1;

  82.   
  83. }


  84. void loop() {



  85. if(lcdxianshi<3)ruanchuankou();
  86. else{


  87.    
  88.    sensors.requestTemperatures();
  89.    
  90.    jsq1=sensors.getTempCByIndex(0);
  91.    
  92.       DHT11.read(DHT11PIN);
  93.    
  94.    jsq2=DHT11.humidity;

  95.    
  96.     for(int col=0;col<16;col++)tempx[col]=0;
  97.    
  98. DS1307tiquxiaodui();   

  99.   

  100.   
  101.    
  102.     jsq3=jsq2*0.1;
  103.   tempx[0] = jsq3+48;
  104.   jsq3=jsq2%10;
  105.   tempx[1] = jsq3+48;

  106. tempx[2]='%';
  107. tempx[3]=32;


  108.   jsq3=jsq1*0.1;
  109.   tempx[4] = jsq3+48;
  110.   jsq3=jsq1%10;
  111.   tempx[5] = jsq3+48;
  112.   tempx[6]='C';
  113.   tempx[7]=32;
  114.   
  115. }



  116. if(millis()-gpstimes>10 && !konghangpanduan){
  117. konghangpanduan=1;
  118. gpsxianguangA=1;



  119. }



  120. if(gpsxianguangA && lcdxianshi<3){

  121. gpsxianguangA=0;


  122. gpsdechuli();


  123. if(!gpsshuchuqiehuanB){
  124. Serial.println(GPGGA);
  125. Serial.println(GPRMC);
  126. Serial.print("freeMemory=");
  127. Serial.println(freeMemory());
  128. Serial.println();
  129. }

  130. if(jiaoyanjieguoA)jiexiGPGGA();

  131. if(jiaoyanjieguoC){
  132. jiexiGPRMC();

  133. if(dingweiok && gpschanger) {
  134. gpschanger=0;


  135. }

  136. }


  137.   now1602out();



  138. for(int col=0;col<80;col++)GPRMC[col]=0;
  139. for(int col=0;col<80;col++)GPGGA[col]=0;
  140. for(int col=0;col<16;col++)tempx[col]=0;
  141. jsq1=0;
  142. GPGGAget=0;
  143. GPRMCget=0;

  144. gpstimes=millis();
  145. }









  146.   gpsshuchuqiehuanA =digitalRead(14);
  147. if (gpsshuchuqiehuanA==HIGH && fangzhilianji){

  148. fangzhilianji=0;
  149. gpstimes=millis();
  150. gpsshuchuqiehuanB=!gpsshuchuqiehuanB;
  151. EEPROM.write(0,gpsshuchuqiehuanB);

  152. }else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();


  153.   gpsshuchuqiehuanA =digitalRead(15);
  154. if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
  155.   
  156.   fangzhilianji=0;
  157. gpstimes=millis();
  158. Serial.print("lcdxianshi=");
  159. Serial.println(lcdxianshi);

  160. if(!dingweiok){

  161. if(lcdxianshi<3)lcdxianshi=3;
  162. else lcdxianshi=0;

  163. }else lcdxianshi++;

  164. lcdxianshi=lcdxianshi%4;

  165. }else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();

  166. if(millis()-gpstimes>450 && !fangzhilianji){
  167. gpstimes=millis();
  168. fangzhilianji=1;
  169. }




  170. if(lcdxianshi==3) now1602out();








  171. }



  172. void ruanchuankou()
  173. {


  174. while (gps.available() > 0) {
  175. gpstimes=millis();
  176. konghangpanduan=0;


  177. if(GPRMCget){
  178. GPRMC[jsq1]=gps.read();
  179. if(gpsshuchuqiehuanB)Serial.print(GPRMC[jsq1]);
  180. if(GPRMC[jsq1-3]=='*'){
  181. GPRMClong=jsq1;
  182. GPRMCget=0;
  183. jsq1=0;
  184. }else if(jsq1<79)jsq1++;



  185. }else if(GPGGAget){

  186. GPGGA[jsq1]=gps.read();
  187. if(gpsshuchuqiehuanB)Serial.print(GPGGA[jsq1]);
  188. if(GPGGA[jsq1-3]=='*'){
  189. GPGGAlong=jsq1;
  190. GPGGAget=0;
  191. jsq1=0;
  192. }else if(jsq1<79)jsq1++;



  193. }else{
  194. tempx[jsq1]=gps.read();
  195. if(gpsshuchuqiehuanB)Serial.print(tempx[jsq1]);
  196. if(jsq1<6)jsq1++;

  197. }



  198. if(jsq1==6){


  199. if(tempx[4]=='M' && tempx[5]=='C'){

  200. GPRMCget=1;
  201. GPGGAget=0;
  202. for(int col=0;col<6;col++)GPRMC[col]=tempx[col];
  203. for(int col=0;col<6;col++)tempx[col]=0;

  204. }else if(tempx[jsq1-2]=='G' && tempx[jsq1-1]=='A'){

  205. GPGGAget=1;
  206. GPRMCget=0;
  207. for(int col=0;col<6;col++)GPGGA[col]=tempx[col];
  208. for(int col=0;col<6;col++)tempx[col]=0;

  209. }else{

  210. for(int col=0;col<5;col++)tempx[col]=tempx[col+1];
  211. jsq1=5;
  212. }

  213. }


  214. }



  215. }


  216. void gpsdechuli()
  217. {

  218. for(int col=1;col<GPRMClong-3;col++){
  219. if(col==1)yihuoyunsuan=GPRMC[col];
  220. else yihuoyunsuan=yihuoyunsuan ^ GPRMC[col];
  221. }

  222. if(yihuoyunsuan==0){
  223. jianyan="00";
  224. }else if(yihuoyunsuan>15){
  225. jianyan = String(yihuoyunsuan,HEX);
  226. }else{
  227. jianyan = "0";
  228. jianyan += String(yihuoyunsuan,HEX);
  229. }
  230. jianyan.toUpperCase();
  231. if(jianyan[0]==GPRMC[GPRMClong-2] && jianyan[1]==GPRMC[GPRMClong-1] ){
  232. jiaoyanjieguoC=1;
  233. }else{
  234. jiaoyanjieguoC=0;
  235. }

  236. jianyan="";




  237. for(int col=1;col<GPGGAlong-3;col++){
  238. if(col==1)yihuoyunsuan=GPGGA[col];
  239. else yihuoyunsuan=yihuoyunsuan ^ GPGGA[col];
  240. }

  241. if(yihuoyunsuan==0){
  242. jianyan="00";
  243. }else if(yihuoyunsuan>15){
  244. jianyan = String(yihuoyunsuan,HEX);
  245. }else{
  246. jianyan = "0";
  247. jianyan += String(yihuoyunsuan,HEX);
  248. }
  249. jianyan.toUpperCase();
  250. if(jianyan[0]==GPGGA[GPGGAlong-2] && jianyan[1]==GPGGA[GPGGAlong-1] ){
  251. jiaoyanjieguoA=1;
  252. }else{
  253. jiaoyanjieguoA=0;
  254. }


  255. jianyan="";


  256. }


  257. void jiexiGPGGA()
  258. {

  259.    jsq2=0;
  260.    jsq3=0;
  261.    jsq4=0;
  262.    jsq5=0;


  263.     for(int col=1;col<GPGGAlong-3;col++){
  264.      if(GPGGA[col]==',')jsq2++;
  265.    if(jsq2==10){
  266.    jsq3=col;
  267.    col=GPGGAlong-3;
  268.    }
  269.    
  270.    
  271.    if(jsq2<9)jsq4=col;
  272.    
  273.    if(jsq2<7)jsq5=col;
  274.    
  275.    }
  276.    
  277.    
  278.    if(GPGGA[jsq5]==49)dingweiok=1;
  279.    else dingweiok=0;
  280.    
  281.    
  282. for(int col=0;col<8;col++)TrackB0[col]=0;
  283. memcpy(TrackB0,GPGGA+jsq4+2, jsq3-jsq4-2);

  284. }


  285. void jiexiGPRMC()
  286. {

  287.    jsq2=0;
  288.    jsq3=0;
  289.    jsq4=0;
  290.    jsq5=0;
  291.    jsq6=0;
  292.    jsq7=0;
  293.    
  294.     for(int col=1;col<GPRMClong-3;col++){
  295.      if(GPRMC[col]==',')jsq2++;
  296.    if(jsq2==9){
  297.    jsq3=col;
  298.    col=GPRMClong-3;
  299.    }
  300.    
  301.    if(jsq2<8)jsq4=col;
  302.    if(jsq2<6)jsq5=col;
  303.    if(jsq2<4)jsq6=col;
  304.    if(jsq2<2)jsq7=col;
  305.    }


  306.   if(GPRMC[jsq7+2]=='A')chinatime();


  307. if(dingweiok){

  308. jingweiduchuli();
  309. hangxiangyusudu();

  310. }else {
  311.   weiduA=0;
  312.   weiduB=0;
  313.   weiduC=0;
  314.   jingduA=0;
  315.   jingduB=0;
  316.   jingduC=0;
  317. }


  318. }


  319.   void chinatime()
  320. {


  321. jianyan = String(GPRMC[7]);
  322. jianyan += GPRMC[8];
  323. utc8s=jianyan.toInt()+8;


  324. if(utc8s>23)rijinwei=1;
  325. utc8s=utc8s%24;

  326. jianyan = String(GPRMC[9]);
  327. jianyan += GPRMC[10];
  328. utc8f=jianyan.toInt();



  329. jianyan = String(GPRMC[11]);
  330. jianyan += GPRMC[12];
  331. utc8m=jianyan.toInt();







  332. jianyan = String(GPRMC[jsq3+1]);
  333. jianyan += GPRMC[jsq3+2];
  334. utc8r=jianyan.toInt();




  335. jianyan = String(GPRMC[jsq3+3]);
  336. jianyan += GPRMC[jsq3+4];
  337. utc8y=jianyan.toInt();



  338. jianyan = String(GPRMC[jsq3+5]);
  339. jianyan += GPRMC[jsq3+6];
  340. utc8n=jianyan.toInt();





  341. if(rijinwei){

  342. if(utc8y==2 && utc8r==28){
  343. if(utc8n % 400 == 0 || utc8n % 100 != 0 && utc8n % 4 == 0)utc8r=29;
  344. else {
  345. utc8r=1;
  346. yuejinwei=1;
  347. }
  348. }else{

  349. for(int col=0;col<4;col++){
  350. if(xiaoyue[col]==utc8y)xiaoyueok=1;
  351. }
  352. if(xiaoyueok && utc8r==30){
  353. utc8r=1;
  354. yuejinwei=1;
  355. }else if(!xiaoyueok && utc8r==31){
  356. utc8r=1;
  357. yuejinwei=1;
  358. }else{
  359. utc8r++;
  360. }
  361. }
  362. }
  363. if(yuejinwei && utc8y==12){
  364. utc8y=1;
  365. nianjinwei=1;
  366. }else if(yuejinwei){
  367. utc8y++;
  368. }
  369. if(nianjinwei)utc8n++;



  370. for(int col=0;col<16;col++)tempx[col]=0;
  371. if(utc8n>9){
  372. sprintf(tempx, "%d", utc8n);
  373. memcpy(TrackB4+2,tempx, 2);
  374. }else{
  375.   TrackB4[2]=48;
  376.   TrackB4[3]=utc8n+48;

  377. }

  378.   for(int col=0;col<16;col++)tempx[col]=0;
  379.   if(utc8y>9){
  380.   sprintf(tempx, "%d", utc8y);
  381. memcpy(TrackB4+5,tempx, 2);
  382. } else{
  383.   TrackB4[5]=48;
  384.    TrackB4[6]=utc8y+48;

  385. }
  386.    for(int col=0;col<16;col++)tempx[col]=0;
  387. if(utc8r>9){
  388. sprintf(tempx, "%d", utc8r);
  389. memcpy(TrackB4+8,tempx, 2);
  390. }else{
  391.   TrackB4[8]=48;
  392. TrackB4[9]=utc8r+48;

  393. }

  394.     for(int col=0;col<16;col++)tempx[col]=0;
  395. if(utc8s>9){
  396. sprintf(tempx, "%d", utc8s);
  397. memcpy(TrackB4+11,tempx, 2);
  398. }else{
  399. TrackB4[11]=48;
  400. TrackB4[12]=utc8s+48;

  401. }
  402.     for(int col=0;col<16;col++)tempx[col]=0;
  403. if(utc8f>9){
  404. sprintf(tempx, "%d", utc8f);
  405. memcpy(TrackB4+14,tempx, 2);
  406. }else{
  407. TrackB4[14]=48;
  408.   TrackB4[15]=utc8f+48;
  409. }

  410.     for(int col=0;col<16;col++)tempx[col]=0;
  411.   if(utc8m>9){
  412.   sprintf(tempx, "%d", utc8m);
  413. memcpy(TrackB4+17,tempx, 2);
  414. }else{
  415. TrackB4[17]=48;
  416.   TrackB4[18]=utc8m+48;
  417. }


  418. }


  419. void jingweiduchuli()
  420. {

  421.   
  422. for(int col=0;col<16;col++)tempx[col]=0;
  423. memcpy(tempx,GPRMC+jsq7+4, 2);
  424. jianyan =tempx;
  425. gpschangertest=jianyan.toInt();
  426. if(weiduA!=gpschangertest){
  427. weiduA=gpschangertest;
  428. gpschanger=1;
  429. }



  430. if(GPRMC[jsq6+2]==83)weiduxuweitiao=1;
  431. if(GPRMC[jsq5+2]==87)jingduxuweitiao=1;


  432. memcpy(TrackB1,GPRMC+jsq7+4, 2);

  433. if(gpschangertest<10) {
  434. TrackB1[0]=32;
  435. weiduzhanwei=1;
  436. }else weiduzhanwei=2;


  437. for(int col=0;col<16;col++)tempx[col]=0;

  438. memcpy(tempx,GPRMC+jsq7+6, 2);
  439. jianyan =tempx;
  440. gpschangertest=jianyan.toInt();
  441. if(weiduB!=gpschangertest){
  442. weiduB=gpschangertest;
  443. gpschanger=1;
  444. }



  445.   for(int col=0;col<16;col++)tempx[col]=0;

  446. memcpy(tempx,GPRMC+jsq7+9, 4);
  447. if(jsq6-jsq7==12) memcpy(tempx,GPRMC+jsq7+9, 4);
  448. else if(jsq6-jsq7==13)memcpy(tempx,GPRMC+jsq7+9, 5);


  449. jianyan =tempx;
  450. gpschangertest=jianyan.toInt();
  451. if(weiduC!=gpschangertest){
  452. weiduC=gpschangertest;
  453. gpschanger=1;
  454. }


  455. fentodu=weiduB*100000+weiduC*10;
  456. fentodu=fentodu/6;

  457.   for(int col=0;col<16;col++)tempx[col]=0;
  458. dtostrf(fentodu,6,0,tempx);





  459. if(fentodu>99999){
  460. memcpy(TrackB1+3,tempx, 6);
  461. }else if(fentodu>9999){
  462. TrackB1[3]=48;
  463.   memcpy(TrackB1+4,tempx, 5);
  464. }else if(fentodu>999){
  465. TrackB1[3]=48;
  466. TrackB1[4]=48;
  467.   memcpy(TrackB1+5,tempx, 4);

  468. }else if(fentodu>99){
  469. TrackB1[3]=48;
  470. TrackB1[4]=48;
  471. TrackB1[5]=48;
  472.   memcpy(TrackB1+6,tempx, 3);

  473. }else if(fentodu>9){
  474. TrackB1[3]=48;
  475. TrackB1[4]=48;
  476. TrackB1[5]=48;
  477. TrackB1[6]=48;
  478.   memcpy(TrackB1+7,tempx, 2);
  479. }else{
  480. TrackB1[3]=48;
  481. TrackB1[4]=48;
  482. TrackB1[5]=48;
  483. TrackB1[6]=48;
  484. TrackB1[7]=48;
  485.   memcpy(TrackB1+8,tempx, 1);

  486. }





  487. if(weiduxuweitiao){
  488. weiduxuweitiao=0;

  489. if(weiduzhanwei==2){


  490.    for(int col=0;col<16;col++)tempx[col]=0;
  491. tempx[0]=45;
  492. memcpy(tempx+1,TrackB1, 9);
  493. memcpy(TrackB1,tempx, 10);

  494. }else{

  495.   TrackB1[0]=45;

  496. }

  497. }





  498. for(int col=0;col<16;col++)tempx[col]=0;

  499.    memcpy(tempx,GPRMC+jsq6+4, 3);
  500. jianyan =tempx;
  501. gpschangertest=jianyan.toInt();
  502. if(jingduA!=gpschangertest){
  503. jingduA=gpschangertest;
  504. gpschanger=1;
  505. }



  506.   

  507.   memcpy(TrackB2,GPRMC+jsq6+4, 3);
  508. jingduzhanwei=3;

  509. if(gpschangertest<100) {
  510. TrackB2[0]=32;
  511. jingduzhanwei=2;
  512. }

  513. if(gpschangertest<10) {
  514. TrackB2[1]=32;
  515. jingduzhanwei=1;
  516. }

  517. for(int col=0;col<16;col++)tempx[col]=0;

  518. memcpy(tempx,GPRMC+jsq6+7, 3);
  519. jianyan =tempx;
  520. gpschangertest=jianyan.toInt();
  521. if(jingduB!=gpschangertest){
  522. jingduB=gpschangertest;
  523. gpschanger=1;
  524. }

  525.   for(int col=0;col<16;col++)tempx[col]=0;

  526. if(jsq5-jsq6==13)memcpy(tempx,GPRMC+jsq6+10, 4);
  527. else if(jsq5-jsq6==14)memcpy(tempx,GPRMC+jsq6+10, 5);

  528. jianyan =tempx;
  529. gpschangertest=jianyan.toInt();
  530. if(jingduC!=gpschangertest){
  531. jingduC=gpschangertest;
  532. gpschanger=1;
  533. }

  534. jianyan="";


  535.   fentodu=jingduB*100000+jingduC*10;
  536. fentodu=fentodu/6;

  537.   for(int col=0;col<16;col++)tempx[col]=0;
  538. dtostrf(fentodu,6,0,tempx);





  539. if(fentodu>99999){
  540. memcpy(TrackB2+4,tempx, 6);
  541. }else if(fentodu>9999){
  542. TrackB2[4]=48;
  543.   memcpy(TrackB2+5,tempx, 5);
  544. }else if(fentodu>999){
  545. TrackB2[4]=48;
  546. TrackB2[5]=48;
  547.   memcpy(TrackB2+6,tempx, 4);

  548. }else if(fentodu>99){
  549. TrackB2[4]=48;
  550. TrackB2[5]=48;
  551. TrackB2[6]=48;
  552.   memcpy(TrackB2+7,tempx, 3);

  553. }else if(fentodu>9){
  554. TrackB2[4]=48;
  555. TrackB2[5]=48;
  556. TrackB2[6]=48;
  557. TrackB2[7]=48;
  558.   memcpy(TrackB2+8,tempx, 2);
  559. }else{
  560. TrackB2[4]=48;
  561. TrackB2[5]=48;
  562. TrackB2[6]=48;
  563. TrackB2[7]=48;
  564. TrackB2[8]=48;
  565.   memcpy(TrackB2+9,tempx, 1);

  566. }





  567. if(jingduxuweitiao){
  568. jingduxuweitiao=0;

  569. if(jingduzhanwei==3){


  570.    for(int col=0;col<16;col++)tempx[col]=0;
  571. tempx[0]=45;
  572. memcpy(tempx+1,TrackB2, 10);
  573. memcpy(TrackB2,tempx, 11);

  574. }else if(jingduzhanwei==2){

  575.   TrackB2[0]=45;

  576. }else{

  577. TrackB2[1]=45;
  578. }

  579. }


  580. }


  581. void hangxiangyusudu()
  582. {

  583. for(int col=0;col<16;col++)tempx[col]=0;
  584. memcpy(tempx,GPRMC+jsq5+4,jsq4-jsq5-3);


  585. for(int col=0;col<jsq4-jsq5-3;col++){

  586. if(tempx[col]==46){




  587. if(col==1)fentodu=(tempx[0]-48)*1000;
  588. else if(col==2)fentodu=(tempx[0]-48)*10000+(tempx[1]-48)*1000;
  589. else if(col==3)fentodu=(tempx[0]-48)*100000+(tempx[1]-48)*10000+(tempx[2]-48)*1000;





  590. if(jsq4-jsq5-col==5)fentodu=fentodu+(tempx[col+1]-48)*100;
  591. else if(jsq4-jsq5-col==6)fentodu=fentodu+(tempx[col+1]-48)*100+(tempx[col+2]-48)*10;
  592. else if(jsq4-jsq5-col==7)fentodu=fentodu+(tempx[col+1]-48)*100+(tempx[col+2]-48)*10+tempx[col+3]-48;





  593. fentodu=fentodu*1852*0.0001;
  594. col=jsq4-jsq5-3;

  595. }

  596. }


  597. sudu=fentodu*0.01;
  598. for(int col=0;col<8;col++)TrackB5[col]=0;
  599. dtostrf(sudu,1,2,TrackB5);


  600. for(int col=0;col<7;col++)TrackB6[col]=0;
  601. memcpy(TrackB6,GPRMC+jsq4+2, jsq3-jsq4-2);


  602. }


  603. void printDateTime(DateTime dateTime) {
  604.    
  605. nian=dateTime.year(), DEC;

  606.    
  607.     yue=dateTime.month(), DEC;
  608.    
  609. ri=dateTime.day(), DEC;
  610.    
  611. shi=dateTime.hour(), DEC;
  612.    
  613. fen=dateTime.minute(), DEC;
  614.    
  615. miao=dateTime.second(), DEC;


  616. }


  617. void DS1307tiquxiaodui()
  618. {
  619.   


  620.   
  621.     DateTime now = RTC.now();
  622.       
  623.       printDateTime(now);


  624. if(dingweiok){
  625. if(yue != utc8y) shoushiqidong=1;
  626. if(ri != utc8r) shoushiqidong=1;
  627. if(shi != utc8s) shoushiqidong=1;
  628. if(fen != utc8f) shoushiqidong=1;
  629. if(miao-utc8m>2 || utc8m-miao>2) shoushiqidong=1;
  630. if(shoushiqidong){
  631. shoushiqidong=0;

  632.    RTC.set(RTC_YEAR, utc8n);
  633.    
  634.    RTC.set(RTC_MONTH, utc8y);
  635.    
  636.    RTC.set(RTC_DAY, utc8r);
  637.    
  638.    RTC.set(RTC_HOUR, utc8s);
  639.    
  640. RTC.set(RTC_MINUTE, utc8f);
  641.    
  642.    RTC.set(RTC_SECOND, utc8m);
  643. }
  644. }
  645. /*
  646. jsq3=nian*0.1;
  647.   jsq3=jsq3%10;
  648.   temp2[7] = jsq3+48, DEC;
  649.   jsq3=nian%10;
  650.   temp2[8] = jsq3+48, DEC;

  651.    
  652.   jsq3=yue*0.1;
  653.   temp2[10] = jsq3+48, DEC;
  654.   jsq3=yue%10;
  655.   temp2[11] = jsq3+48, DEC;


  656.     jsq3=ri*0.1;
  657.   temp2[13] = jsq3+48, DEC;
  658.   jsq3=ri%10;
  659.   temp2[14] = jsq3+48, DEC;
  660.    
  661.    */
  662.    
  663.   jsq3=shi*0.1;
  664.   tempx[8] = jsq3+48, DEC;
  665.   jsq3=shi%10;
  666.   tempx[9] = jsq3+48, DEC;
  667.   tempx[10]=':';

  668.   jsq3=fen*0.1;
  669.   tempx[11] = jsq3+48, DEC;
  670.   jsq3=fen%10;
  671.   tempx[12] = jsq3+48, DEC;
  672.   tempx[13]=':';


  673.   jsq3=miao*0.1;
  674.   tempx[14]= jsq3+48, DEC;
  675.   jsq3=miao%10;
  676.   tempx[15] = jsq3+48, DEC;
  677. }


  678. void now1602out()
  679. {


  680. /*GPS模式下的显示情况说明

  681. char TrackB0[8]="-0000.0";
  682. char TrackB1[11]="00.0000000";
  683. char TrackB2[12]="000.0000000";
  684. char TrackB4[20]="2000-00-00T00:00:00";
  685. char TrackB5[8]="0000.00";
  686. char TrackB6[7]="000.00";


  687. lcdxianshi=0显示内容如下

  688. LAT.  00.0000000
  689. 0123456789012345
  690. LON. 000.0000000

  691. lcdxianshi=1显示内容如下

  692. ASL(G): -0000.0M
  693. 0123456789012345
  694. YAW(GPS): 000.00


  695. lcdxianshi=2显示内容如下

  696.   00-00T00:00:00
  697. 0123456789012345
  698. speed:000.00km/h


  699. I'm so sorry but
  700. 0123456789012345
  701. now the GPS lost

  702. */

  703. if(!dingweiok && lcdxianshi<3){

  704. lcd.setCursor(0, 0);
  705. lcd.print("I'm so sorry but");
  706. lcd.setCursor(0, 1);
  707. lcd.print("now the GPS lost");

  708. }else{

  709. if(lcdxianshi==0){

  710. lcd.setCursor(0, 0);
  711. lcd.print("LAT.  ");
  712. lcd.print(TrackB1);

  713. lcd.setCursor(0, 1);
  714. lcd.print("LON. ");
  715. lcd.print(TrackB2);


  716. }else if(lcdxianshi==1){

  717. lcd.setCursor(0, 0);
  718. lcd.print("ASL(G): ");
  719. lcd.print(TrackB0);
  720. lcd.print("M    ");
  721. lcd.setCursor(0, 1);
  722. lcd.print("YAW(GPS): ");
  723. lcd.print(TrackB6);
  724. lcd.print("        ");


  725. }else if(lcdxianshi==2){

  726. for(int col=0;col<16;col++)tempx[col]=0;
  727. for(int col=0;col<14;col++)tempx[col]=TrackB4[col+5];
  728. lcd.setCursor(0, 0);
  729. lcd.print("  ");
  730. lcd.print(tempx);
  731. lcd.setCursor(0, 1);
  732. lcd.print("speed:");
  733. lcd.print(TrackB5);
  734. lcd.print("km/h    ");

  735. }else if(lcdxianshi==3){

  736. lcd.setCursor(0, 0);
  737. lcd.print("now lcdxianshi=3");
  738.   lcd.setCursor(0, 1);
  739.   


  740. lcd.print(tempx);


  741. }

  742. }


  743. }









复制代码

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-11-12 22:16:24 | 显示全部楼层


带记忆、能校时,gps不接也行


  1. #include <MemoryFree.h>

  2. #include <LiquidCrystal.h>
  3. LiquidCrystal lcd(2, 3, 4, 5, 6, 7);



  4. #include <SoftwareSerial.h>
  5. SoftwareSerial gps(8, 9);
  6. char tempx[17];
  7. char GPRMC[80];
  8. char GPGGA[80];
  9. int gpschangertest;
  10. boolean gpschanger;
  11. int weiduA;
  12. unsigned long weiduB,weiduC;
  13. int jingduA;
  14. unsigned long jingduB,jingduC;


  15. boolean GPGGAget=0;
  16. boolean GPRMCget=0;
  17. unsigned long gpstimes;
  18. boolean konghangpanduan;
  19. boolean gpsshuchuqiehuanB;
  20. int jsq1=0;
  21. int jsq2=0;
  22. int jsq3=0;
  23. int jsq4=0;
  24. int jsq5=0;
  25. int jsq6=0;
  26. int jsq7=0;
  27. int GPGGAlong=0;
  28. int GPRMClong=0;
  29. boolean jiaoyanjieguoA,jiaoyanjieguoC;
  30. float sudu;

  31. String jianyan="";
  32. int yihuoyunsuan;



  33. boolean dingweiok=0;

  34. boolean gpsxianguangA=0;
  35. boolean fangzhilianji=1;

  36. int lcdxianshi=0;

  37. /*---------------------GPS end---------------------------------*/
  38. int gpsshuchuqiehuanA;


  39.   
  40. #include<EEPROM.h>
  41. int EEPROMvalue;





  42. const int chipSelect = 10;






  43. char TrackB0[8]="-0000.0";

  44. char TrackB1[11]="00.0000000";
  45. char TrackB2[12]="000.0000000";

  46. char TrackB4[20]="2000-00-00T00:00:00";

  47. char TrackB5[8]="0000.00";
  48. char TrackB6[7]="000.00";


  49. boolean weiduxuweitiao=0;
  50. boolean jingduxuweitiao=0;
  51. int weiduzhanwei,jingduzhanwei;


  52. int utc8s,utc8f,utc8m,utc8n,utc8y,utc8r;

  53. int xiaoyue[5]={4,6,9,11};

  54. boolean rijinwei,yuejinwei,nianjinwei,xiaoyueok;

  55. unsigned long fentodu;





  56. #include <OneWire.h>
  57. #include <DallasTemperature.h>
  58. OneWire oneWire(10);
  59. DallasTemperature sensors(&oneWire);



  60. #include <dht11.h>
  61. dht11 DHT11;
  62. #define DHT11PIN 11




  63. #include <Wire.h>
  64. #include <RTClib.h>
  65. void printDateTime(DateTime dateTime);
  66. RTC_DS1307 RTC;
  67. int nian,yue,ri,shi,fen,miao;
  68. boolean shoushiqidong;





  69. int gp2y1010dust=2;
  70. int gp2y1010led=15;
  71. int dustVal=0;
  72. int delayTime=280;
  73. int delayTime2=40;
  74. float offTime=9680;

  75. int dusti=0;

  76. float dustValPM25,averagedustVal;
  77. int AQI;

  78. int mySensVals [10];

  79. unsigned long time1=millis();



  80. void setup() {


  81. pinMode(gp2y1010led,OUTPUT);





  82. lcd.begin(16, 2);

  83.   
  84.   Wire.begin();
  85.   RTC.begin();

  86. pinMode(12,INPUT);
  87. pinMode(14,INPUT);



  88. Serial.begin(9600);
  89. gps.begin(9600);

  90.   
  91. EEPROMvalue= EEPROM.read(0);
  92. if(EEPROMvalue==0)gpsshuchuqiehuanB=0;
  93. else gpsshuchuqiehuanB=1;

  94. EEPROMvalue= EEPROM.read(10);
  95. if(EEPROMvalue==0)lcdxianshi=0;
  96. else if(EEPROMvalue==1)lcdxianshi=1;
  97. else if(EEPROMvalue==2)lcdxianshi=2;
  98. else if(EEPROMvalue==3)lcdxianshi=3;
  99. else {
  100.   EEPROM.write(10,0);
  101.   lcdxianshi=0;
  102. }

  103.   
  104. }


  105. void loop() {



  106. if(lcdxianshi<3)ruanchuankou();
  107. else{


  108. fenchentou();


  109.    
  110.    sensors.requestTemperatures();
  111.    
  112.    jsq1=sensors.getTempCByIndex(0);
  113.    
  114.       DHT11.read(DHT11PIN);
  115.    
  116.    jsq2=DHT11.humidity;

  117.    
  118.     for(int col=0;col<16;col++)tempx[col]=0;
  119.    
  120. DS1307tiquxiaodui();   

  121.   

  122.   
  123.    
  124.     jsq3=jsq2*0.1;
  125.   tempx[0] = jsq3+48;
  126.   jsq3=jsq2%10;
  127.   tempx[1] = jsq3+48;

  128. tempx[2]='%';
  129. tempx[3]=32;


  130.   jsq3=jsq1*0.1;
  131.   tempx[4] = jsq3+48;
  132.   jsq3=jsq1%10;
  133.   tempx[5] = jsq3+48;
  134.   tempx[6]='C';
  135.   tempx[7]=32;
  136.   
  137. }



  138. if(millis()-gpstimes>10 && !konghangpanduan){
  139. konghangpanduan=1;
  140. gpsxianguangA=1;



  141. }



  142. if(gpsxianguangA && lcdxianshi<3){

  143. gpsxianguangA=0;


  144. gpsdechuli();


  145. if(!gpsshuchuqiehuanB){
  146. Serial.println(GPGGA);
  147. Serial.println(GPRMC);
  148. Serial.print("freeMemory=");
  149. Serial.println(freeMemory());
  150. Serial.println();
  151. }

  152. if(jiaoyanjieguoA)jiexiGPGGA();

  153. if(jiaoyanjieguoC){
  154. jiexiGPRMC();

  155. if(dingweiok && gpschanger) {
  156. gpschanger=0;


  157. }

  158. }


  159.   now1602out();



  160. for(int col=0;col<80;col++)GPRMC[col]=0;
  161. for(int col=0;col<80;col++)GPGGA[col]=0;
  162. for(int col=0;col<16;col++)tempx[col]=0;
  163. jsq1=0;
  164. GPGGAget=0;
  165. GPRMCget=0;

  166. gpstimes=millis();
  167. }









  168.   gpsshuchuqiehuanA =digitalRead(12);
  169. if (gpsshuchuqiehuanA==HIGH && fangzhilianji){

  170. fangzhilianji=0;
  171. gpstimes=millis();
  172. gpsshuchuqiehuanB=!gpsshuchuqiehuanB;
  173. EEPROM.write(0,gpsshuchuqiehuanB);

  174. }else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();


  175.   gpsshuchuqiehuanA =digitalRead(14);
  176. if (gpsshuchuqiehuanA==HIGH && fangzhilianji){
  177.   
  178.   fangzhilianji=0;
  179. gpstimes=millis();


  180. if(!dingweiok){

  181. if(lcdxianshi<3)lcdxianshi=3;
  182. else lcdxianshi=0;

  183. }else lcdxianshi++;

  184. lcdxianshi=lcdxianshi%4;
  185. EEPROM.write(10,lcdxianshi);


  186. }else if(gpsshuchuqiehuanA==HIGH)gpstimes=millis();

  187. if(millis()-gpstimes>450 && !fangzhilianji){
  188. gpstimes=millis();
  189. fangzhilianji=1;
  190. }




  191. if(lcdxianshi==3) now1602out();








  192. }





  193. void fenchentou()
  194. {


  195. if (millis() - time1 > 1000){
  196. time1=millis();

  197. /*粉尘头控制代码*/
  198. digitalWrite(gp2y1010led,LOW);
  199. delayMicroseconds(delayTime);
  200. dustVal=analogRead(gp2y1010dust);
  201. delayMicroseconds(delayTime2);
  202. digitalWrite(gp2y1010led,HIGH);

  203. /*邻采样10次平均averagedustVal的计算*/
  204. mySensVals [dusti]=dustVal;
  205. dusti++;
  206. dusti=dusti%10;

  207. averagedustVal=0;
  208. for(int col=0;col<10;col++)averagedustVal=averagedustVal+mySensVals[col];
  209. averagedustVal=averagedustVal*0.1;



  210. /*计算pm2.5的数值 电压阀值以上用:(v*0.172-0.0999)*1000,阀值以下用线性,参考国外帖子,认为输出最低17,对应粉尘5000*/






  211. if(averagedustVal>184.5)dustValPM25=0.83984375*averagedustVal-99.9;
  212. else dustValPM25=averagedustVal*0.298373984;


  213. /*AQI的计算,美国标准*/

  214. if(dustValPM25<15.4) AQI=(50-0)/(15.4-0)*(dustValPM25-0)+0;
  215. else if(dustValPM25<40.4) AQI=(100-51)/(40.4-15.5)*(dustValPM25-15.5)+51;
  216. else if(dustValPM25<65.4) AQI=(150-101)/(65.4-40.5)*(dustValPM25-40.5)+101;
  217. else if(dustValPM25<150.4) AQI=(200-151)/(150.4-65.5)*(dustValPM25-65.5)+151;
  218. else if(dustValPM25<250.4) AQI=(300-201)/(250.4-150.5)*(dustValPM25-150.5)+201;
  219. else if(dustValPM25<350.4) AQI=(400-301)/(350.4-250.5)*(dustValPM25-250.5)+301;
  220. else AQI=(500-401)/(500.4-350.5)*(dustValPM25-350.5)+401;


  221. lcd.setCursor(0, 0);
  222. lcd.print(dustValPM25);
  223. lcd.print("ug AQI:");
  224. lcd.print(AQI);
  225. lcd.print("     ");

  226. }




  227. if (millis() < time1){
  228. time1=millis();
  229. }


  230. }



  231. void ruanchuankou()
  232. {


  233. while (gps.available() > 0) {
  234. gpstimes=millis();
  235. konghangpanduan=0;


  236. if(GPRMCget){
  237. GPRMC[jsq1]=gps.read();
  238. if(gpsshuchuqiehuanB)Serial.print(GPRMC[jsq1]);
  239. if(GPRMC[jsq1-3]=='*'){
  240. GPRMClong=jsq1;
  241. GPRMCget=0;
  242. jsq1=0;
  243. }else if(jsq1<79)jsq1++;



  244. }else if(GPGGAget){

  245. GPGGA[jsq1]=gps.read();
  246. if(gpsshuchuqiehuanB)Serial.print(GPGGA[jsq1]);
  247. if(GPGGA[jsq1-3]=='*'){
  248. GPGGAlong=jsq1;
  249. GPGGAget=0;
  250. jsq1=0;
  251. }else if(jsq1<79)jsq1++;



  252. }else{
  253. tempx[jsq1]=gps.read();
  254. if(gpsshuchuqiehuanB)Serial.print(tempx[jsq1]);
  255. if(jsq1<6)jsq1++;

  256. }



  257. if(jsq1==6){


  258. if(tempx[4]=='M' && tempx[5]=='C'){

  259. GPRMCget=1;
  260. GPGGAget=0;
  261. for(int col=0;col<6;col++)GPRMC[col]=tempx[col];
  262. for(int col=0;col<6;col++)tempx[col]=0;

  263. }else if(tempx[jsq1-2]=='G' && tempx[jsq1-1]=='A'){

  264. GPGGAget=1;
  265. GPRMCget=0;
  266. for(int col=0;col<6;col++)GPGGA[col]=tempx[col];
  267. for(int col=0;col<6;col++)tempx[col]=0;

  268. }else{

  269. for(int col=0;col<5;col++)tempx[col]=tempx[col+1];
  270. jsq1=5;
  271. }

  272. }


  273. }



  274. }


  275. void gpsdechuli()
  276. {

  277. for(int col=1;col<GPRMClong-3;col++){
  278. if(col==1)yihuoyunsuan=GPRMC[col];
  279. else yihuoyunsuan=yihuoyunsuan ^ GPRMC[col];
  280. }

  281. if(yihuoyunsuan==0){
  282. jianyan="00";
  283. }else if(yihuoyunsuan>15){
  284. jianyan = String(yihuoyunsuan,HEX);
  285. }else{
  286. jianyan = "0";
  287. jianyan += String(yihuoyunsuan,HEX);
  288. }
  289. jianyan.toUpperCase();
  290. if(jianyan[0]==GPRMC[GPRMClong-2] && jianyan[1]==GPRMC[GPRMClong-1] ){
  291. jiaoyanjieguoC=1;
  292. }else{
  293. jiaoyanjieguoC=0;
  294. }

  295. jianyan="";




  296. for(int col=1;col<GPGGAlong-3;col++){
  297. if(col==1)yihuoyunsuan=GPGGA[col];
  298. else yihuoyunsuan=yihuoyunsuan ^ GPGGA[col];
  299. }

  300. if(yihuoyunsuan==0){
  301. jianyan="00";
  302. }else if(yihuoyunsuan>15){
  303. jianyan = String(yihuoyunsuan,HEX);
  304. }else{
  305. jianyan = "0";
  306. jianyan += String(yihuoyunsuan,HEX);
  307. }
  308. jianyan.toUpperCase();
  309. if(jianyan[0]==GPGGA[GPGGAlong-2] && jianyan[1]==GPGGA[GPGGAlong-1] ){
  310. jiaoyanjieguoA=1;
  311. }else{
  312. jiaoyanjieguoA=0;
  313. }


  314. jianyan="";


  315. }


  316. void jiexiGPGGA()
  317. {

  318.    jsq2=0;
  319.    jsq3=0;
  320.    jsq4=0;
  321.    jsq5=0;


  322.     for(int col=1;col<GPGGAlong-3;col++){
  323.      if(GPGGA[col]==',')jsq2++;
  324.    if(jsq2==10){
  325.    jsq3=col;
  326.    col=GPGGAlong-3;
  327.    }
  328.    
  329.    
  330.    if(jsq2<9)jsq4=col;
  331.    
  332.    if(jsq2<7)jsq5=col;
  333.    
  334.    }
  335.    
  336.    
  337.    if(GPGGA[jsq5]==49)dingweiok=1;
  338.    else dingweiok=0;
  339.    
  340.    
  341. for(int col=0;col<8;col++)TrackB0[col]=0;
  342. memcpy(TrackB0,GPGGA+jsq4+2, jsq3-jsq4-2);

  343. }


  344. void jiexiGPRMC()
  345. {

  346.    jsq2=0;
  347.    jsq3=0;
  348.    jsq4=0;
  349.    jsq5=0;
  350.    jsq6=0;
  351.    jsq7=0;
  352.    
  353.     for(int col=1;col<GPRMClong-3;col++){
  354.      if(GPRMC[col]==',')jsq2++;
  355.    if(jsq2==9){
  356.    jsq3=col;
  357.    col=GPRMClong-3;
  358.    }
  359.    
  360.    if(jsq2<8)jsq4=col;
  361.    if(jsq2<6)jsq5=col;
  362.    if(jsq2<4)jsq6=col;
  363.    if(jsq2<2)jsq7=col;
  364.    }


  365.   if(GPRMC[jsq7+2]=='A')chinatime();


  366. if(dingweiok){

  367. jingweiduchuli();
  368. hangxiangyusudu();

  369. }else {
  370.   weiduA=0;
  371.   weiduB=0;
  372.   weiduC=0;
  373.   jingduA=0;
  374.   jingduB=0;
  375.   jingduC=0;
  376. }


  377. }


  378.   void chinatime()
  379. {


  380. jianyan = String(GPRMC[7]);
  381. jianyan += GPRMC[8];
  382. utc8s=jianyan.toInt()+8;


  383. if(utc8s>23)rijinwei=1;
  384. utc8s=utc8s%24;

  385. jianyan = String(GPRMC[9]);
  386. jianyan += GPRMC[10];
  387. utc8f=jianyan.toInt();



  388. jianyan = String(GPRMC[11]);
  389. jianyan += GPRMC[12];
  390. utc8m=jianyan.toInt();







  391. jianyan = String(GPRMC[jsq3+1]);
  392. jianyan += GPRMC[jsq3+2];
  393. utc8r=jianyan.toInt();




  394. jianyan = String(GPRMC[jsq3+3]);
  395. jianyan += GPRMC[jsq3+4];
  396. utc8y=jianyan.toInt();



  397. jianyan = String(GPRMC[jsq3+5]);
  398. jianyan += GPRMC[jsq3+6];
  399. utc8n=jianyan.toInt();





  400. if(rijinwei){

  401. if(utc8y==2 && utc8r==28){
  402. if(utc8n % 400 == 0 || utc8n % 100 != 0 && utc8n % 4 == 0)utc8r=29;
  403. else {
  404. utc8r=1;
  405. yuejinwei=1;
  406. }
  407. }else{

  408. for(int col=0;col<4;col++){
  409. if(xiaoyue[col]==utc8y)xiaoyueok=1;
  410. }
  411. if(xiaoyueok && utc8r==30){
  412. utc8r=1;
  413. yuejinwei=1;
  414. }else if(!xiaoyueok && utc8r==31){
  415. utc8r=1;
  416. yuejinwei=1;
  417. }else{
  418. utc8r++;
  419. }
  420. }
  421. }
  422. if(yuejinwei && utc8y==12){
  423. utc8y=1;
  424. nianjinwei=1;
  425. }else if(yuejinwei){
  426. utc8y++;
  427. }
  428. if(nianjinwei)utc8n++;



  429. for(int col=0;col<16;col++)tempx[col]=0;
  430. if(utc8n>9){
  431. sprintf(tempx, "%d", utc8n);
  432. memcpy(TrackB4+2,tempx, 2);
  433. }else{
  434.   TrackB4[2]=48;
  435.   TrackB4[3]=utc8n+48;

  436. }

  437.   for(int col=0;col<16;col++)tempx[col]=0;
  438.   if(utc8y>9){
  439.   sprintf(tempx, "%d", utc8y);
  440. memcpy(TrackB4+5,tempx, 2);
  441. } else{
  442.   TrackB4[5]=48;
  443.    TrackB4[6]=utc8y+48;

  444. }
  445.    for(int col=0;col<16;col++)tempx[col]=0;
  446. if(utc8r>9){
  447. sprintf(tempx, "%d", utc8r);
  448. memcpy(TrackB4+8,tempx, 2);
  449. }else{
  450.   TrackB4[8]=48;
  451. TrackB4[9]=utc8r+48;

  452. }

  453.     for(int col=0;col<16;col++)tempx[col]=0;
  454. if(utc8s>9){
  455. sprintf(tempx, "%d", utc8s);
  456. memcpy(TrackB4+11,tempx, 2);
  457. }else{
  458. TrackB4[11]=48;
  459. TrackB4[12]=utc8s+48;

  460. }
  461.     for(int col=0;col<16;col++)tempx[col]=0;
  462. if(utc8f>9){
  463. sprintf(tempx, "%d", utc8f);
  464. memcpy(TrackB4+14,tempx, 2);
  465. }else{
  466. TrackB4[14]=48;
  467.   TrackB4[15]=utc8f+48;
  468. }

  469.     for(int col=0;col<16;col++)tempx[col]=0;
  470.   if(utc8m>9){
  471.   sprintf(tempx, "%d", utc8m);
  472. memcpy(TrackB4+17,tempx, 2);
  473. }else{
  474. TrackB4[17]=48;
  475.   TrackB4[18]=utc8m+48;
  476. }


  477. }


  478. void jingweiduchuli()
  479. {

  480.   
  481. for(int col=0;col<16;col++)tempx[col]=0;
  482. memcpy(tempx,GPRMC+jsq7+4, 2);
  483. jianyan =tempx;
  484. gpschangertest=jianyan.toInt();
  485. if(weiduA!=gpschangertest){
  486. weiduA=gpschangertest;
  487. gpschanger=1;
  488. }



  489. if(GPRMC[jsq6+2]==83)weiduxuweitiao=1;
  490. if(GPRMC[jsq5+2]==87)jingduxuweitiao=1;


  491. memcpy(TrackB1,GPRMC+jsq7+4, 2);

  492. if(gpschangertest<10) {
  493. TrackB1[0]=32;
  494. weiduzhanwei=1;
  495. }else weiduzhanwei=2;


  496. for(int col=0;col<16;col++)tempx[col]=0;

  497. memcpy(tempx,GPRMC+jsq7+6, 2);
  498. jianyan =tempx;
  499. gpschangertest=jianyan.toInt();
  500. if(weiduB!=gpschangertest){
  501. weiduB=gpschangertest;
  502. gpschanger=1;
  503. }



  504.   for(int col=0;col<16;col++)tempx[col]=0;

  505. memcpy(tempx,GPRMC+jsq7+9, 4);
  506. if(jsq6-jsq7==12) memcpy(tempx,GPRMC+jsq7+9, 4);
  507. else if(jsq6-jsq7==13)memcpy(tempx,GPRMC+jsq7+9, 5);


  508. jianyan =tempx;
  509. gpschangertest=jianyan.toInt();
  510. if(weiduC!=gpschangertest){
  511. weiduC=gpschangertest;
  512. gpschanger=1;
  513. }


  514. fentodu=weiduB*100000+weiduC*10;
  515. fentodu=fentodu/6;

  516.   for(int col=0;col<16;col++)tempx[col]=0;
  517. dtostrf(fentodu,6,0,tempx);





  518. if(fentodu>99999){
  519. memcpy(TrackB1+3,tempx, 6);
  520. }else if(fentodu>9999){
  521. TrackB1[3]=48;
  522.   memcpy(TrackB1+4,tempx, 5);
  523. }else if(fentodu>999){
  524. TrackB1[3]=48;
  525. TrackB1[4]=48;
  526.   memcpy(TrackB1+5,tempx, 4);

  527. }else if(fentodu>99){
  528. TrackB1[3]=48;
  529. TrackB1[4]=48;
  530. TrackB1[5]=48;
  531.   memcpy(TrackB1+6,tempx, 3);

  532. }else if(fentodu>9){
  533. TrackB1[3]=48;
  534. TrackB1[4]=48;
  535. TrackB1[5]=48;
  536. TrackB1[6]=48;
  537.   memcpy(TrackB1+7,tempx, 2);
  538. }else{
  539. TrackB1[3]=48;
  540. TrackB1[4]=48;
  541. TrackB1[5]=48;
  542. TrackB1[6]=48;
  543. TrackB1[7]=48;
  544.   memcpy(TrackB1+8,tempx, 1);

  545. }





  546. if(weiduxuweitiao){
  547. weiduxuweitiao=0;

  548. if(weiduzhanwei==2){


  549.    for(int col=0;col<16;col++)tempx[col]=0;
  550. tempx[0]=45;
  551. memcpy(tempx+1,TrackB1, 9);
  552. memcpy(TrackB1,tempx, 10);

  553. }else{

  554.   TrackB1[0]=45;

  555. }

  556. }





  557. for(int col=0;col<16;col++)tempx[col]=0;

  558.    memcpy(tempx,GPRMC+jsq6+4, 3);
  559. jianyan =tempx;
  560. gpschangertest=jianyan.toInt();
  561. if(jingduA!=gpschangertest){
  562. jingduA=gpschangertest;
  563. gpschanger=1;
  564. }



  565.   

  566.   memcpy(TrackB2,GPRMC+jsq6+4, 3);
  567. jingduzhanwei=3;

  568. if(gpschangertest<100) {
  569. TrackB2[0]=32;
  570. jingduzhanwei=2;
  571. }

  572. if(gpschangertest<10) {
  573. TrackB2[1]=32;
  574. jingduzhanwei=1;
  575. }

  576. for(int col=0;col<16;col++)tempx[col]=0;

  577. memcpy(tempx,GPRMC+jsq6+7, 3);
  578. jianyan =tempx;
  579. gpschangertest=jianyan.toInt();
  580. if(jingduB!=gpschangertest){
  581. jingduB=gpschangertest;
  582. gpschanger=1;
  583. }

  584.   for(int col=0;col<16;col++)tempx[col]=0;

  585. if(jsq5-jsq6==13)memcpy(tempx,GPRMC+jsq6+10, 4);
  586. else if(jsq5-jsq6==14)memcpy(tempx,GPRMC+jsq6+10, 5);

  587. jianyan =tempx;
  588. gpschangertest=jianyan.toInt();
  589. if(jingduC!=gpschangertest){
  590. jingduC=gpschangertest;
  591. gpschanger=1;
  592. }

  593. jianyan="";


  594.   fentodu=jingduB*100000+jingduC*10;
  595. fentodu=fentodu/6;

  596.   for(int col=0;col<16;col++)tempx[col]=0;
  597. dtostrf(fentodu,6,0,tempx);





  598. if(fentodu>99999){
  599. memcpy(TrackB2+4,tempx, 6);
  600. }else if(fentodu>9999){
  601. TrackB2[4]=48;
  602.   memcpy(TrackB2+5,tempx, 5);
  603. }else if(fentodu>999){
  604. TrackB2[4]=48;
  605. TrackB2[5]=48;
  606.   memcpy(TrackB2+6,tempx, 4);

  607. }else if(fentodu>99){
  608. TrackB2[4]=48;
  609. TrackB2[5]=48;
  610. TrackB2[6]=48;
  611.   memcpy(TrackB2+7,tempx, 3);

  612. }else if(fentodu>9){
  613. TrackB2[4]=48;
  614. TrackB2[5]=48;
  615. TrackB2[6]=48;
  616. TrackB2[7]=48;
  617.   memcpy(TrackB2+8,tempx, 2);
  618. }else{
  619. TrackB2[4]=48;
  620. TrackB2[5]=48;
  621. TrackB2[6]=48;
  622. TrackB2[7]=48;
  623. TrackB2[8]=48;
  624.   memcpy(TrackB2+9,tempx, 1);

  625. }





  626. if(jingduxuweitiao){
  627. jingduxuweitiao=0;

  628. if(jingduzhanwei==3){


  629.    for(int col=0;col<16;col++)tempx[col]=0;
  630. tempx[0]=45;
  631. memcpy(tempx+1,TrackB2, 10);
  632. memcpy(TrackB2,tempx, 11);

  633. }else if(jingduzhanwei==2){

  634.   TrackB2[0]=45;

  635. }else{

  636. TrackB2[1]=45;
  637. }

  638. }


  639. }


  640. void hangxiangyusudu()
  641. {

  642. for(int col=0;col<16;col++)tempx[col]=0;
  643. memcpy(tempx,GPRMC+jsq5+4,jsq4-jsq5-3);


  644. for(int col=0;col<jsq4-jsq5-3;col++){

  645. if(tempx[col]==46){




  646. if(col==1)fentodu=(tempx[0]-48)*1000;
  647. else if(col==2)fentodu=(tempx[0]-48)*10000+(tempx[1]-48)*1000;
  648. else if(col==3)fentodu=(tempx[0]-48)*100000+(tempx[1]-48)*10000+(tempx[2]-48)*1000;





  649. if(jsq4-jsq5-col==5)fentodu=fentodu+(tempx[col+1]-48)*100;
  650. else if(jsq4-jsq5-col==6)fentodu=fentodu+(tempx[col+1]-48)*100+(tempx[col+2]-48)*10;
  651. else if(jsq4-jsq5-col==7)fentodu=fentodu+(tempx[col+1]-48)*100+(tempx[col+2]-48)*10+tempx[col+3]-48;





  652. fentodu=fentodu*1852*0.0001;
  653. col=jsq4-jsq5-3;

  654. }

  655. }


  656. sudu=fentodu*0.01;
  657. for(int col=0;col<8;col++)TrackB5[col]=0;
  658. dtostrf(sudu,1,2,TrackB5);


  659. for(int col=0;col<7;col++)TrackB6[col]=0;
  660. memcpy(TrackB6,GPRMC+jsq4+2, jsq3-jsq4-2);


  661. }


  662. void printDateTime(DateTime dateTime) {
  663.    
  664. nian=dateTime.year(), DEC;

  665.    
  666.     yue=dateTime.month(), DEC;
  667.    
  668. ri=dateTime.day(), DEC;
  669.    
  670. shi=dateTime.hour(), DEC;
  671.    
  672. fen=dateTime.minute(), DEC;
  673.    
  674. miao=dateTime.second(), DEC;


  675. }


  676. void DS1307tiquxiaodui()
  677. {
  678.   


  679.   
  680.     DateTime now = RTC.now();
  681.       
  682.       printDateTime(now);


  683. if(dingweiok){
  684. if(yue != utc8y) shoushiqidong=1;
  685. if(ri != utc8r) shoushiqidong=1;
  686. if(shi != utc8s) shoushiqidong=1;
  687. if(fen != utc8f) shoushiqidong=1;
  688. if(miao-utc8m>2 || utc8m-miao>2) shoushiqidong=1;
  689. if(shoushiqidong){
  690. shoushiqidong=0;

  691.    RTC.set(RTC_YEAR, utc8n);
  692.    
  693.    RTC.set(RTC_MONTH, utc8y);
  694.    
  695.    RTC.set(RTC_DAY, utc8r);
  696.    
  697.    RTC.set(RTC_HOUR, utc8s);
  698.    
  699. RTC.set(RTC_MINUTE, utc8f);
  700.    
  701.    RTC.set(RTC_SECOND, utc8m);
  702. }
  703. }
  704. /*
  705. jsq3=nian*0.1;
  706.   jsq3=jsq3%10;
  707.   temp2[7] = jsq3+48, DEC;
  708.   jsq3=nian%10;
  709.   temp2[8] = jsq3+48, DEC;

  710.    
  711.   jsq3=yue*0.1;
  712.   temp2[10] = jsq3+48, DEC;
  713.   jsq3=yue%10;
  714.   temp2[11] = jsq3+48, DEC;


  715.     jsq3=ri*0.1;
  716.   temp2[13] = jsq3+48, DEC;
  717.   jsq3=ri%10;
  718.   temp2[14] = jsq3+48, DEC;
  719.    
  720.    */
  721.    
  722.   jsq3=shi*0.1;
  723.   tempx[8] = jsq3+48, DEC;
  724.   jsq3=shi%10;
  725.   tempx[9] = jsq3+48, DEC;
  726.   tempx[10]=':';

  727.   jsq3=fen*0.1;
  728.   tempx[11] = jsq3+48, DEC;
  729.   jsq3=fen%10;
  730.   tempx[12] = jsq3+48, DEC;
  731.   tempx[13]=':';


  732.   jsq3=miao*0.1;
  733.   tempx[14]= jsq3+48, DEC;
  734.   jsq3=miao%10;
  735.   tempx[15] = jsq3+48, DEC;
  736. }


  737. void now1602out()
  738. {


  739. /*GPS模式下的显示情况说明

  740. char TrackB0[8]="-0000.0";
  741. char TrackB1[11]="00.0000000";
  742. char TrackB2[12]="000.0000000";
  743. char TrackB4[20]="2000-00-00T00:00:00";
  744. char TrackB5[8]="0000.00";
  745. char TrackB6[7]="000.00";


  746. lcdxianshi=0显示内容如下

  747. LAT.  00.0000000
  748. 0123456789012345
  749. LON. 000.0000000

  750. lcdxianshi=1显示内容如下

  751. ASL(G): -0000.0M
  752. 0123456789012345
  753. YAW(GPS): 000.00


  754. lcdxianshi=2显示内容如下

  755.   00-00T00:00:00
  756. 0123456789012345
  757. speed:000.00km/h


  758. I'm so sorry but
  759. 0123456789012345
  760. now the GPS lost

  761. */

  762. if(!dingweiok && lcdxianshi<3){

  763. lcd.setCursor(0, 0);
  764. lcd.print("I'm so sorry but");
  765. lcd.setCursor(0, 1);
  766. lcd.print("now the GPS lost");

  767. }else{

  768. if(lcdxianshi==0){

  769. lcd.setCursor(0, 0);
  770. lcd.print("LAT.  ");
  771. lcd.print(TrackB1);

  772. lcd.setCursor(0, 1);
  773. lcd.print("LON. ");
  774. lcd.print(TrackB2);


  775. }else if(lcdxianshi==1){

  776. lcd.setCursor(0, 0);
  777. lcd.print("ASL(G): ");
  778. lcd.print(TrackB0);
  779. lcd.print("M    ");
  780. lcd.setCursor(0, 1);
  781. lcd.print("YAW(GPS): ");
  782. lcd.print(TrackB6);
  783. lcd.print("        ");


  784. }else if(lcdxianshi==2){

  785. for(int col=0;col<16;col++)tempx[col]=0;
  786. for(int col=0;col<14;col++)tempx[col]=TrackB4[col+5];
  787. lcd.setCursor(0, 0);
  788. lcd.print("  ");
  789. lcd.print(tempx);
  790. lcd.setCursor(0, 1);
  791. lcd.print("speed:");
  792. lcd.print(TrackB5);
  793. lcd.print("km/h    ");

  794. }else if(lcdxianshi==3){



  795.   lcd.setCursor(0, 1);
  796.   


  797. lcd.print(tempx);


  798. }

  799. }


  800. }









复制代码

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-9 12:26:49 | 显示全部楼层
额,不绑定QQ不能发帖了,那就回帖吧:
gps 取时间,太久没玩了
  1. #include <stdlib.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <SoftwareSerial.h>
  4. String returnStr = "";
  5. SoftwareSerial mySerial(10, 11); // RX, TX
  6. LiquidCrystal_I2C lcd(0x27,16,2);//i2c地址

  7. void setup() {
  8.   Serial.begin(19200);
  9.   mySerial.begin(19200);
  10.   lcd.init();   // lcd初始化
  11.   lcd.backlight();//开背光
  12. }

  13. void loop() { // run over and over
  14.   
  15.         while(mySerial.available() > 0){ //gps的 $GPRMC 读取为字符串returnStr PS:gps设定为只输出$GPRMC
  16.                 returnStr += char(mySerial.read());
  17.                 delay(2);
  18.         }
  19.    
  20.         //处理$GPRMC,长度变量是速度、方向,长度不固定,但是时间、定位标记、经纬度、以及最后的日期长度是固定的具体看GPS文档
  21.         if (returnStr.length() > 0){
  22.                 Serial.print(returnStr);//可有可无,调试才需要
  23.                 String str0 = returnStr.substring (18,19);//截取gps定位标记
  24.   
  25.                 if(str0 == "A"){ //是否已定位
  26.                         int str_len = returnStr.length();//因为速度、方向长度不固定,所以取总长,倒扣取日期【太久没玩、C++菜鸟,不会分割】
  27.                         String str_ymd = returnStr.substring (str_len-13,str_len-7);  //取日期
  28.                         int GMT_8_hour =  (returnStr.substring (7,9).toInt() + 8 )% 24 ; // hour + 8 模运算,不过日期没改,还是UTC,喜欢折腾可以日进位,但平白增加运算量,感觉不值
  29.                         String str1 = ":"+ returnStr.substring (9,11) +":"+ returnStr.substring (11,13)+ " " + str_ymd.substring (5,6) +"-"+ str_ymd.substring (2,4) +"-"+str_ymd.substring (0,2); //:minute:second year-month-day
  30.                         //LCD 16*2 位置少 年只取个位
  31.   
  32.                         lcd.setCursor(0,0); // LCD0 @hour:minute:second month-day @11:11:11 7-05-09
  33.                         lcd.print(GMT_8_hour);
  34.                         lcd.print(str1);
  35.                         lcd.setCursor(0,1);
  36.                         lcd.print("                          ");//清屏是非常慢的,还是直接覆盖靠谱

  37.                 }
  38.                 else{
  39.                         lcd.setCursor(0,0);
  40.                         lcd.print("GPS Lost         ");
  41.                         lcd.setCursor(0,1);
  42.                         lcd.print("                          ");
  43.                 }
  44.   
  45.         returnStr = "";
  46.         }

  47. }
复制代码


本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

发表于 2017-5-9 13:03:05 | 显示全部楼层
........代码这么长
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-9 23:33:46 | 显示全部楼层
弘毅 发表于 2017-5-9 13:03
........代码这么长

原来放了太多功能,也太追求细节,现在马虎一下一样过



  1. #include <stdlib.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <SoftwareSerial.h>
  4. String returnStr = "";
  5. SoftwareSerial mySerial(10, 11); // RX, TX
  6. LiquidCrystal_I2C lcd(0x26,16,2);//i2c地址

  7. int liuliangji=12;
  8. int diancifa=13;

  9. unsigned long time1=millis();
  10. boolean jiaohua = 0;
  11. boolean chongfupanduan=0;
  12. boolean test1=1;

  13. int jsqLLx=0;
  14. float shishiliuliang=0;
  15. float leijiliuliang=0;
  16. ////////////////////////////////////////

  17. void setup() {
  18.   Serial.begin(19200);
  19.   mySerial.begin(19200);
  20.   lcd.init();   // lcd初始化
  21.   lcd.backlight();//开背光
  22.   
  23.   pinMode(liuliangji,INPUT); //流量计接12
  24.   pinMode(diancifa, OUTPUT); //电磁阀接13
  25.   digitalWrite(diancifa, HIGH);//关闭电磁阀
  26. }

  27. void loop() { // run over and over
  28.   
  29.         while(mySerial.available() > 0){ //gps的 $GPRMC 读取为字符串returnStr PS:gps设定为只输出$GPRMC
  30.                 returnStr += char(mySerial.read());
  31.                 delay(2);
  32.         }
  33.    
  34.         //处理$GPRMC,长度变量是速度、方向,长度不固定,但是时间、定位标记、经纬度、以及最后的日期长度是固定的具体看GPS文档
  35.         if (returnStr.length() > 0){
  36.                 Serial.print(returnStr);//可有可无,调试才需要
  37.                 String str0 = returnStr.substring (18,19);//截取gps定位标记
  38.   
  39.                 if(str0 == "A"){ //是否已定位
  40.                         int str_len = returnStr.length();//因为速度、方向长度不固定,所以取总长,倒扣取日期【太久没玩、C++菜鸟,不会分割】
  41.                         String str_ymd = returnStr.substring (str_len-13,str_len-7);  //取日期
  42.                         int GMT_8_hour =  (returnStr.substring (7,9).toInt() + 8 )% 24 ; // hour + 8 模运算,不过日期没改,还是UTC,喜欢折腾可以日进位,但平白增加运算量,感觉不值
  43.                         int GMT_8_minute = returnStr.substring (9,11).toInt();
  44.                        
  45.                         String str1 = ":"+ returnStr.substring (9,11) +":"+ returnStr.substring (11,13)+ " " + str_ymd.substring (5,6) +"-"+ str_ymd.substring (2,4) +"-"+str_ymd.substring (0,2); //:minute:second year-month-day
  46.                         //LCD 16*2 位置少 年只取个位
  47.                        
  48.                         if (GMT_8_hour == 18 && GMT_8_minute == 55){
  49.                                 jiaohua = 1;
  50.                         }
  51.                         if (GMT_8_minute % 2 == 0){
  52.                                 jiaohua = 1;
  53.                         }
  54.   
  55.                         lcd.setCursor(0,0); // LCD0 @hour:minute:second month-day @11:11:11 7-05-09
  56.                         lcd.print(GMT_8_hour);
  57.                         lcd.print(str1);
  58.                         lcd.setCursor(0,1);
  59.                         lcd.print("                          ");//清屏是非常慢的,还是直接覆盖靠谱

  60.                 }
  61.                 else{
  62.                         lcd.setCursor(0,0);
  63.                         lcd.print("GPS Lost         ");
  64.                         lcd.setCursor(0,1);
  65.                         lcd.print("                          ");
  66.                 }
  67.   
  68.         returnStr = "";
  69.         }
  70.        
  71.        
  72.         if(jiaohua){

  73.                 lcd.setCursor(9,0);//LCD 处理
  74.                 lcd.print("Endless");
  75.                 lcd.setCursor(0,1);
  76.                 lcd.print("                          ");
  77.                
  78.                 unsigned long time0=millis();
  79.                 unsigned long time1=millis(); //内部循环数秒
  80.                 digitalWrite(diancifa, LOW); //开(电磁阀)闸放水
  81.                
  82.                
  83.                 while(jiaohua){ //一旦激活浇花,就是死循环,除非满足条件,置0跳出
  84.                        
  85.                         if(millis()-time0>200000){ //超时跳出[200s]
  86.                                 jiaohua = 0;
  87.                                 digitalWrite(diancifa, HIGH);//关闭电磁阀
  88.                                 leijiliuliang = 0;
  89.                         }
  90.                        
  91.                         if(leijiliuliang>10){ //流量超出[10L]
  92.                                 jiaohua = 0;
  93.                                 digitalWrite(diancifa, HIGH);//关闭电磁阀
  94.                                 leijiliuliang = 0;
  95.                         }
  96.                        
  97.                         if(millis()-time1>1000) {

  98.                                 time1=millis();//每一秒还原一次
  99.                                 shishiliuliang=jsqLLx/7.5;//计算实时流量(公式:频率=7.5*流量(L/min))
  100.                                 leijiliuliang=leijiliuliang+(shishiliuliang/60);//(每秒积分)计数累计流量
  101.                                
  102.                                 lcd.setCursor(0,1);
  103.                                 lcd.print(shishiliuliang);
  104.                                 lcd.print("L/m ");
  105.                                 lcd.print(leijiliuliang);
  106.                                 lcd.print("L        ");
  107.                                
  108.                                 jsqLLx=0;//清空每秒流量
  109.                          
  110.                          }else{
  111.                                 int liuliangjidianping =digitalRead(liuliangji);//读取流量计的电平

  112.                                 if(liuliangjidianping == HIGH && !chongfupanduan){ //高电平 且 本次没计数,就计数
  113.                                         jsqLLx++;
  114.                                         chongfupanduan=!chongfupanduan;//本次已计数,则标记已计数
  115.                                 }

  116.                                 if(liuliangjidianping == LOW && chongfupanduan)chongfupanduan=!chongfupanduan;//低电平,且 计数标记为1,则重置计数标记
  117.                          }

  118.                
  119.                 }

  120.                
  121.         }

  122. }
复制代码

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-12 18:28:20 | 显示全部楼层
12v 外接供电,目前只控制一个电磁阀,多控制几个,就能定量浇灌了
PS:墙角信号差,GPS难以定位,算了,直接取时间就行

  1. #include <stdlib.h>
  2. #include <LiquidCrystal_I2C.h>
  3. #include <SoftwareSerial.h>
  4. String returnStr = "";
  5. SoftwareSerial mySerial(10, 11); // RX, TX
  6. LiquidCrystal_I2C lcd(0x23,16,2);//i2c地址

  7. int liuliangji=12;
  8. int diancifa=13;
  9. int Manual_switch=9;

  10. boolean jiaohua = 0;
  11. boolean chongfupanduan=0;


  12. int jsqLLx=0;
  13. float shishiliuliang=0;
  14. float leijiliuliang=0;
  15. ////////////////////////////////////////

  16. void setup() {
  17.   Serial.begin(19200);
  18.   mySerial.begin(19200);
  19.   lcd.init();   // lcd初始化
  20.   lcd.backlight();//开背光
  21.   
  22.   pinMode(Manual_switch,INPUT); //手动开启9
  23.   pinMode(liuliangji,INPUT); //流量计接12
  24.   pinMode(diancifa, OUTPUT); //电磁阀接13
  25.   digitalWrite(diancifa, HIGH);//关闭电磁阀
  26. }

  27. void loop() { // run over and over
  28.   
  29.         while(mySerial.available() > 0){ //gps的 $GPRMC 读取为字符串returnStr PS:gps设定为只输出$GPRMC
  30.                 returnStr += char(mySerial.read());
  31.                 delay(2);
  32.         }
  33.    
  34.         //处理$GPRMC,长度变量是速度、方向,长度不固定,但是时间、定位标记、经纬度、以及最后的日期长度是固定的具体看GPS文档
  35.         if (returnStr.length() > 0){

  36.                 int str_len = returnStr.length();//因为速度、方向长度不固定,所以取总长,倒扣取日期【太久没玩、C++菜鸟,不会分割】
  37.                 String str_ymd = returnStr.substring (str_len-13,str_len-7);  //取日期
  38.                 int GMT_8_hour =  (returnStr.substring (7,9).toInt() + 8 )% 24 ; // hour + 8 模运算,不过日期没改,还是UTC,喜欢折腾可以日进位,但平白增加运算量,感觉不值
  39.                 int GMT_8_minute = returnStr.substring (9,11).toInt();
  40.                
  41.                 String str1 = ":"+ returnStr.substring (9,11) +":"+ returnStr.substring (11,13)+ " " + returnStr.substring (18,19) + " "+ str_ymd.substring (2,4) +"-"+str_ymd.substring (0,2) + " "; //:minute:second A month-day
  42.                

  43.                 lcd.setCursor(0,0); // LCD0 @hour:minute:second month-day @11:11:11 A 05-09
  44.                 lcd.print(GMT_8_hour);
  45.                 lcd.print(str1);
  46.                 //lcd.setCursor(0,1);
  47.                 //lcd.print("                ");//清屏是非常慢的,还是直接覆盖靠谱

  48.                 if (GMT_8_hour == 19 && GMT_8_minute == 55){
  49.                         jiaohua = 1;
  50.                 }

  51.                        
  52.         returnStr = "";
  53.         }
  54.         ///////////////////////////////////////////////////////////////////////////////////////////////////////
  55.         //手动开启程序
  56.         int Manual_switch_OK =digitalRead(Manual_switch);
  57.         if(Manual_switch_OK == HIGH )jiaohua = 1;
  58.         ///////////////////////////////////////////////////////////////////////////////////////////////////////
  59.         if(jiaohua){

  60.                 lcd.setCursor(9,0);//LCD 处理
  61.                 lcd.print("Endless");
  62.                 //lcd.setCursor(0,1);
  63.                 //lcd.print("                          ");
  64.                
  65.                 unsigned long time0=millis();
  66.                 unsigned long time1=millis(); //内部循环数秒
  67.                 digitalWrite(diancifa, LOW); //开(电磁阀)闸放水
  68.                
  69.                
  70.                 while(jiaohua){ //一旦激活浇花,就是死循环,除非满足条件,置0跳出
  71.                        
  72.                         if(millis()-time0>3600000){ //超时跳出[3600s]
  73.                                 jiaohua = 0;
  74.                                 digitalWrite(diancifa, HIGH);//关闭电磁阀
  75.                                 leijiliuliang = 0;
  76.                         }
  77.                        
  78.                         if(leijiliuliang>50){ //流量超出[50L]
  79.                                 jiaohua = 0;
  80.                                 digitalWrite(diancifa, HIGH);//关闭电磁阀
  81.                                 leijiliuliang = 0;
  82.                         }
  83.                        
  84.                         if(millis()-time1>1000) {

  85.                                 time1=millis();//每一秒还原一次
  86.                                 shishiliuliang=jsqLLx/7.5;//计算实时流量(公式:频率=7.5*流量(L/min))
  87.                                 leijiliuliang=leijiliuliang+(shishiliuliang/60);//(每秒积分)计数累计流量
  88.                                
  89.                                 lcd.setCursor(0,1);
  90.                                 lcd.print(shishiliuliang);
  91.                                 lcd.print("L/m ");
  92.                                 lcd.print(leijiliuliang);
  93.                                 lcd.print("L        ");
  94.                                
  95.                                 jsqLLx=0;//清空每秒流量
  96.                          
  97.                          }else{
  98.                                 int liuliangjidianping =digitalRead(liuliangji);//读取流量计的电平

  99.                                 if(liuliangjidianping == HIGH && !chongfupanduan){ //高电平 且 本次没计数,就计数
  100.                                         jsqLLx++;
  101.                                         chongfupanduan=!chongfupanduan;//本次已计数,则标记已计数
  102.                                 }

  103.                                 if(liuliangjidianping == LOW && chongfupanduan)chongfupanduan=!chongfupanduan;//低电平,且 计数标记为1,则重置计数标记
  104.                          }

  105.                
  106.                 }

  107.                
  108.         }

  109.         ///////////////////////////////////////////////////////////////////////////////////////////////////////
  110. }
复制代码

本帖子中包含更多资源

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

x
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-5-22 09:33:59 | 显示全部楼层
python 字符串异或校验,修改gps输出项用的

  1. str_x = 'PSRF109,NMEA19200,NULL38400,GGA1,GLL1,GSA1,GSV1,RMC1,VTG1,USER1'

  2. xx=''
  3. for i in range(0,len(str_x),1):
  4.     if i==0:
  5.         xx = ord(str_x[i])
  6.     else:
  7.         xx = xx ^ ord(str_x[i])
  8. print(hex(xx))
复制代码
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 02:03 , Processed in 0.040564 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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