找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5855|回復(fù): 5
打印 上一主題 下一主題
收起左側(cè)

STM32+Air800模塊GPS定位和GPRS發(fā)送到服務(wù)器STM32源程序下載

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:308658 發(fā)表于 2018-12-2 16:53 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
基于STM32的Air800模塊GPS衛(wèi)星定位和GPRS通訊發(fā)送到服務(wù)器代碼

單片機(jī)源程序如下:
  1. #include "delay.h"
  2. #include "sys.h"
  3. #include "usart.h"        
  4. #include "bluetooth.h"
  5. #include "Air800.h"
  6. #include "timer.h"
  7. #include "GPIO.h"

  8. /***我的疑問*******/
  9. /*
  10.   查詢GPS信息,發(fā)AT指令通過主串口還是GPS串口發(fā)送
  11. */

  12. int main(void)
  13. {
  14.         delay_init();                     //延時函數(shù)初始化         
  15.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //設(shè)置NVIC中斷分組0:0位搶占優(yōu)先級,4位響應(yīng)優(yōu)先級
  16.         GPIO_init();
  17.         nvic_init();
  18.         TIM3_Int_Init(1999,71);//20ms
  19.         uart_init(9600);         //串口初始化為9600
  20.         air800_init();
  21.         bluetooth_init();
  22.         while(1)
  23.         {
  24.                 bluetooth_link(blue_MAC);//藍(lán)牙連接
  25.                 GPS_deal();
  26.                 GPRS_send();
  27.                 LED_Status();
  28.         }
  29. }
復(fù)制代碼
  1. #include "Air800.h"
  2. #include "delay.h"
  3. #include "usart.h"
  4. #include "bluetooth.h"
  5. #include "timer.h"
  6. #include "math.h"
  7. GPS gps;

  8. char IPaddress[IPaddress_LEN];
  9. u8 GPRS_TX_BUF[GPRS_Buf_len];
  10. u8 flag_gprs_send=0,flag_set=0;

  11. void air800_init()
  12. {
  13.         //開機(jī)
  14.         PWRKEY=0;
  15.         delay_ms(1500);
  16.         PWRKEY=1;        
  17.         delay_ms(2500);

  18.         flag_set=1;//進(jìn)入配置模式
  19. /******以下是GPRS初始化配置********/
  20.         Air_send_instructions("\r\nTX\r\n");
  21.   //設(shè)置波特率9600
  22.         Air_send_instructions("\r\nAT+IPR=9600:&W\r\n");
  23.         //使模塊附著GPRS網(wǎng)絡(luò)
  24.         Air_send_instructions("\r\nAT+CGATT=1\r\n");
  25.         //設(shè)置透傳模式
  26.         Air_send_instructions("\r\nAT+CIPMODE=1\r\n");
  27.         //設(shè)置APN
  28.         Air_send_instructions("\r\nAT+CSTT=\"CMNET\"\r\n");
  29.         //激活移動場景,建立無線連接
  30.         Air_send_instructions("\r\nAT+CIICR\r\n");
  31.         //查詢分配的IP地址
  32.         Air_send_instructions("\r\nAT+CIFSR\r\n");
  33.         //設(shè)置要連接的遠(yuǎn)端服務(wù)器類型
  34.         Air_send_instructions("\r\nAT+CIPSTART=\"TCP\",\"60.166.18.9\",7500\r\n");
  35.         
  36. /******以上是GPRS初始化配置********/
  37.         
  38.         
  39.         
  40.         
  41. /******以下是GPS初始化配置********/
  42.         
  43.         //打開GPS
  44.         Air_send_instructions("\r\nAT+CGNSPWR=1\r\n");
  45.         //設(shè)置NMEA語句類型
  46.         Air_send_instructions("\r\nAT+CGNSSEQ=\"RMC\" \r\n");
  47.         
  48. /******以上是GPS初始化配置********/
  49.         flag_set=0;//結(jié)束配置模式
  50. }
  51. /*********************************************************
  52. 函數(shù)名稱:Air_send_instructions
  53. 功    能:配置Air800模塊的指令函數(shù)
  54. 參    數(shù):
  55. *********************************************************/
  56. void Air_send_instructions(char array[])
  57. {
  58.         u8 i=0,len;
  59.         len=strlen(array);
  60.         strcpy(USART_TX_BUF,array);
  61.   for(i=0;i<len;i++)
  62.         {
  63.                 USART_SendData(USART1,USART_TX_BUF[i]);
  64.         }
  65.         while(flag_RX_ok==0);//判斷反饋是否成功
  66.         flag_RX_ok=0;
  67. }
  68. /*********************************************************
  69. 函數(shù)名稱:parse_gpsBuffer
  70. 功    能:解析GPS信息
  71. 參    數(shù):
  72. *********************************************************/
  73. void parse_gpsBuffer()
  74. {
  75.         u8 i=0;
  76.         char *string;
  77.         char *string_next;
  78.         
  79.         if(gps.RX_success==1)
  80.         {
  81.                 gps.fix_status=gps.Buffer[1]-'0';//獲取GPS是否定位
  82.                 for(i=0;i<=3;i++)
  83.                 {
  84.                         if(i==0) string=strstr(gps.Buffer, ",");
  85.                         string++;
  86.                         if((string_next = strstr(string, ",")) != NULL)//檢驗GNSS運(yùn)行狀態(tài)
  87.                         {
  88.                                 switch(i)
  89.                                 {
  90.                                         case 1:memcpy(gps.UTCTime,string,string_next-string);break;//UTC時間
  91.                                         case 2:memcpy(gps.latitudebuffer,string,string_next-string);break;//緯度
  92.                                         case 3:memcpy(gps.longitudebuffer,string,string_next-string);break;//經(jīng)度
  93.                                   default:break;
  94.                                 }
  95.                                 string=string_next;
  96.                         }
  97.                         else
  98.                         {
  99.                                 gps.RX_success=0;
  100.                                 memset(gps.Buffer,0,GPS_Buffer_Length);//清空無效數(shù)據(jù)
  101.                                 break;//跳出for
  102.                         }
  103.                 }
  104.                 parse_gpstime(gps.UTCTime);//提取時間日期
  105.                 parse_gps_location(gps.latitudebuffer,gps.longitudebuffer);//提取經(jīng)緯度
  106.                 memset(gps.Buffer,0,GPS_Buffer_Length);//清空
  107.                 memset(gps.UTCTime,0,UTCTime_Length);//清空
  108.                 memset(gps.latitudebuffer,0,latitude_Length);//清空
  109.                 memset(gps.longitudebuffer,0,longitude_Length);//清空
  110.         }
  111. }
  112.         
  113. /*********************************************************
  114. 函數(shù)名稱:parse_gpstime
  115. 功    能:提取GPS轉(zhuǎn)換北京時間日期
  116. 參    數(shù):
  117. *********************************************************/

  118. void parse_gpstime(char array[])
  119. {
  120.         gps.year=(array[0]-'0')*1000+(array[1]-'0')*100+(array[2]-'0')*10+(array[3]-'0');
  121.         gps.month=(array[4]-'0')*10+(array[5]-'0');
  122.         gps.day=(array[6]-'0')*10+(array[7]-'0');
  123.         gps.hour=(array[8]-'0')*10+(array[9]-'0');
  124.   gps.minute=(array[10]-'0')*10+(array[11]-'0');
  125.         gps.second=(array[11]-'0')*10+(array[12]-'0');
  126.         //北京時間=UTC+8;
  127.         gps.hour+=8;
  128.         if(gps.hour>23)
  129.         {
  130.                 gps.hour=gps.hour-24;
  131.                 gps.day++;
  132.                 if(gps.day>28)
  133.                 {
  134.                         if(gps.month==2)
  135.                         {
  136.                                 if(gps.year%400==0||(gps.year%100!=0&&gps.year%4==0))//閏年
  137.                                 {
  138.                                         if(gps.day>29)
  139.                                         {
  140.                                                 gps.month=3;
  141.                                           gps.day=1;
  142.                                         }
  143.                                 }
  144.                                 else//平年
  145.                                 {
  146.                                         gps.month=3;
  147.                                         gps.day=1;
  148.                                 }
  149.                         }
  150.                         else if(gps.month==4||gps.month==6||gps.month==9||gps.month==11)//day為30天
  151.                         {
  152.                                 if(gps.day>30)
  153.                                 {
  154.                                         gps.day=1;
  155.                                         gps.month++;
  156.                                 }
  157.                         }
  158.                         else if(gps.month==1||gps.month==3||gps.month==5||gps.month==7||gps.month==8||gps.month==10||gps.month==12)//day為31天
  159.                         {
  160.                                 if(gps.day>31)
  161.                                 {
  162.                                         if(gps.month==12)
  163.                                         {
  164.                                                 gps.day=1;
  165.                                                 gps.month=1;
  166.                                                 gps.year++;
  167.                                         }
  168.                                         else
  169.                                         {
  170.                                                 gps.day=1;
  171.                                                 gps.month++;
  172.                                         }
  173.                                 }
  174.                         }
  175.                 }
  176.         }
  177. }

  178. /*********************************************************
  179. 函數(shù)名稱:parse_gps_location
  180. 功    能:提取GPS的經(jīng)緯度并放大10^6
  181. 參    數(shù):
  182. *********************************************************/
  183. void parse_gps_location(char lat[],char lon[])
  184. {
  185.         u8 i=0;
  186.         char *string;
  187.         
  188.         //獲得放大10^6的經(jīng)度值
  189.         if(lat[0]=='-')
  190.         {
  191.                 gps.EW=1;//西經(jīng)為1
  192.                 string=strstr(lat, ",");
  193.                 for(i=1;i<(string-lat);i++)
  194.                 {
  195.                         gps.latitude+=(lat[i]-'0')*pow(10,string-lat-i-1+6);//放大10^6倍
  196.                 }
  197.                 for(i=0;i<6;i++)
  198.                 {
  199.                         ++string;
  200.                         gps.latitude+=((*string)-'0')*pow(10,5-i);
  201.                 }
  202.         }
  203.         else
  204.         {
  205.                 gps.EW=0;
  206.                 string=strstr(lat, ",");
  207.                 for(i=0;i<(string-lat);i++)
  208.                 {
  209.                         gps.latitude+=(lat[i]-'0')*pow(10,string-lat-i-1+6);//放大10^6倍
  210.                 }
  211.                 for(i=0;i<6;i++)
  212.                 {
  213.                         ++string;
  214.                         gps.latitude+=((*string)-'0')*pow(10,5-i);
  215.                 }
  216.         }
  217.         
  218.         //獲得放大10^6的緯度值
  219.         if(lon[0]=='-')
  220.         {
  221.                 gps.NS=1;//南緯為1
  222.                 string=strstr(lon, ",");
  223.                 for(i=1;i<(string-lon);i++)
  224.                 {
  225.                         gps.longitude+=(lon[i]-'0')*pow(10,string-lon-i-1+6);//放大10^6倍
  226.                 }
  227.                 for(i=0;i<6;i++)
  228.                 {
  229.                         ++string;
  230.                         gps.longitude+=((*string)-'0')*pow(10,5-i);
  231.                 }
  232.         }
  233.         else
  234.         {
  235.                 gps.NS=0;
  236.                 string=strstr(lon, ",");
  237.                 for(i=0;i<(string-lon);i++)
  238.                 {
  239.                         gps.longitude+=(lon[i]-'0')*pow(10,string-lon-i-1+6);//放大10^6倍
  240.                 }
  241.                 for(i=0;i<6;i++)
  242.                 {
  243.                         ++string;
  244.                         gps.longitude+=((*string)-'0')*pow(10,5-i);
  245.                 }
  246.         }
  247. }
  248. void GPS_deal()
  249. {
  250.         if(gps.RX_success==0) Air_send_instructions("\r\nAT+CGNSINF\r\n");//查詢GPS的GNSS信息
  251.         
  252.         parse_gpsBuffer();//解析GPS信息
  253. }

  254. /*********************************************************
  255. 函數(shù)名稱:GPRS_send
  256. 功    能:GPRS發(fā)送函數(shù)
  257. 參    數(shù):
  258. *********************************************************/
  259. void GPRS_send()
  260. {
  261.         u8 i=0;
  262.         u32 *ptr;
  263.         
  264.         if(flag_bluetooth_ok&&gps.RX_success&&time_count==100)
  265.         {
  266.                 flag_gprs_send=1;//GPRS發(fā)送標(biāo)志
  267.                 strcpy(USART_TX_BUF,"\r\nAT+CIPSEN\r\n");
  268.                 for(i=0;i<strlen("\r\nAT+CIPSEN\r\n");i++)
  269.                 {
  270.                         USART_SendData(USART1,USART_TX_BUF[i]);
  271.                 }
  272.                 memset(USART_TX_BUF,0,USART_LEN);//清空
  273.         
  274.                 GPRS_TX_BUF[0]='\r';
  275.                 GPRS_TX_BUF[1]='\n';
  276.                 GPRS_TX_BUF[2]='>';
  277.                 GPRS_TX_BUF[3]=0x7e;//標(biāo)識位
  278.         
  279.                 //消息頭
  280.                 GPRS_TX_BUF[4]=0x02;
  281.                 GPRS_TX_BUF[5]=0x00;
  282.                 GPRS_TX_BUF[6]=0x00;
  283.                 GPRS_TX_BUF[7]=0x1c;//消息體長度28
  284.                 GPRS_TX_BUF[8]=0x00;
  285.                 GPRS_TX_BUF[9]=0x00;
  286.                 GPRS_TX_BUF[10]=0x00;
  287.                 GPRS_TX_BUF[11]=0x00;
  288.                 GPRS_TX_BUF[12]=0x00;
  289.                 GPRS_TX_BUF[13]=0x00;
  290.                 GPRS_TX_BUF[14]=0x00;
  291.                 GPRS_TX_BUF[15]=0x00;
  292.         
  293.                 //消息體
  294.                 GPRS_TX_BUF[16]=0x00;
  295.                 GPRS_TX_BUF[17]=0x00;
  296.                 GPRS_TX_BUF[18]=0x00;
  297.                 GPRS_TX_BUF[19]=0x00;
  298.         
  299.                 GPRS_TX_BUF[20]=0x00;
  300.                 GPRS_TX_BUF[21]=0x04;//狀態(tài)為GPS定位
  301.                 GPRS_TX_BUF[22]=0x00;
  302.                 GPRS_TX_BUF[23]=gps.fix_status*2+gps.NS*4+gps.EW*8;//GPS的定位,經(jīng)緯狀態(tài)

  303.                 ptr=(u32*)(&(GPRS_TX_BUF[24]));
  304.                 *ptr=gps.longitude;//緯度值
  305.                 ++ptr;
  306.                 *ptr=gps.latitude;//經(jīng)度值
  307.         
  308.                 GPRS_TX_BUF[32]=0x00;
  309.                 GPRS_TX_BUF[33]=0x00;

  310.                 GPRS_TX_BUF[34]=heart_rate;//心率
  311.                 GPRS_TX_BUF[35]=blood_oxygen;//血氧
  312.         
  313.                 GPRS_TX_BUF[36]=0x00;
  314.                 GPRS_TX_BUF[37]=0x00;
  315.         
  316.                 GPRS_TX_BUF[38]=gps.year;
  317.                 GPRS_TX_BUF[39]=gps.month;
  318.                 GPRS_TX_BUF[40]=gps.day;
  319.                 GPRS_TX_BUF[41]=gps.hour;
  320.                 GPRS_TX_BUF[42]=gps.minute;
  321.                 GPRS_TX_BUF[43]=gps.second;
  322.   
  323.                 //檢驗碼 從消息頭開始到結(jié)尾字節(jié)異或
  324.                 GPRS_TX_BUF[44]=0;
  325.                 for(i=4;i<=43;i++)
  326.                 {
  327.                         GPRS_TX_BUF[44]^=GPRS_TX_BUF[i];
  328.                 }

  329.                 GPRS_TX_BUF[45]=0x7e;//標(biāo)識位
  330.                 GPRS_TX_BUF[46]=0x1A;
  331.                 GPRS_TX_BUF[47]='\r';
  332.                 GPRS_TX_BUF[48]='\n';
  333.         
  334.                 for(i=0;i<=48;i++)
  335.                 {
  336.                         USART_SendData(USART1,GPRS_TX_BUF[i]);        
  337.                 }
  338.                 while(flag_RX_ok==0);//判斷反饋是否成功
  339.                 flag_RX_ok=0;
  340.         
  341.         }
  342. }
復(fù)制代碼


所有資料51hei提供下載:
STM32.20181028.rar (293.72 KB, 下載次數(shù): 120)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏2 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

沙發(fā)
ID:402609 發(fā)表于 2019-5-6 20:15 | 只看該作者
你好,請問有視頻教學(xué)么?這個能連到阿里云么?
回復(fù)

使用道具 舉報

板凳
ID:523363 發(fā)表于 2019-5-7 09:31 | 只看該作者
目前正在開發(fā)一個基于HT32+air802T的GPS/GPRS的產(chǎn)品,類似于LZ,感謝!
回復(fù)

使用道具 舉報

地板
ID:523829 發(fā)表于 2019-8-1 13:32 | 只看該作者
可以  只是代碼有點(diǎn)亂
回復(fù)

使用道具 舉報

5#
ID:523829 發(fā)表于 2019-8-6 15:29 | 只看該作者
調(diào)試了 串口沒輸出 是啥原因
回復(fù)

使用道具 舉報

6#
ID:882275 發(fā)表于 2022-1-25 12:51 | 只看該作者
代碼能跑起來嗎?有沒有帶硬件的電路圖
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表