基于STM32的Air800模塊GPS衛星定位和GPRS通訊發送到服務器代碼
單片機源程序如下:
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "bluetooth.h"
- #include "Air800.h"
- #include "timer.h"
- #include "GPIO.h"
- /***我的疑問*******/
- /*
- 查詢GPS信息,發AT指令通過主串口還是GPS串口發送
- */
- int main(void)
- {
- delay_init(); //延時函數初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //設置NVIC中斷分組0:0位搶占優先級,4位響應優先級
- GPIO_init();
- nvic_init();
- TIM3_Int_Init(1999,71);//20ms
- uart_init(9600); //串口初始化為9600
- air800_init();
- bluetooth_init();
- while(1)
- {
- bluetooth_link(blue_MAC);//藍牙連接
- GPS_deal();
- GPRS_send();
- LED_Status();
- }
- }
復制代碼- #include "Air800.h"
- #include "delay.h"
- #include "usart.h"
- #include "bluetooth.h"
- #include "timer.h"
- #include "math.h"
- GPS gps;
- char IPaddress[IPaddress_LEN];
- u8 GPRS_TX_BUF[GPRS_Buf_len];
- u8 flag_gprs_send=0,flag_set=0;
- void air800_init()
- {
- //開機
- PWRKEY=0;
- delay_ms(1500);
- PWRKEY=1;
- delay_ms(2500);
- flag_set=1;//進入配置模式
- /******以下是GPRS初始化配置********/
- Air_send_instructions("\r\nTX\r\n");
- //設置波特率9600
- Air_send_instructions("\r\nAT+IPR=9600:&W\r\n");
- //使模塊附著GPRS網絡
- Air_send_instructions("\r\nAT+CGATT=1\r\n");
- //設置透傳模式
- Air_send_instructions("\r\nAT+CIPMODE=1\r\n");
- //設置APN
- Air_send_instructions("\r\nAT+CSTT=\"CMNET\"\r\n");
- //激活移動場景,建立無線連接
- Air_send_instructions("\r\nAT+CIICR\r\n");
- //查詢分配的IP地址
- Air_send_instructions("\r\nAT+CIFSR\r\n");
- //設置要連接的遠端服務器類型
- Air_send_instructions("\r\nAT+CIPSTART=\"TCP\",\"60.166.18.9\",7500\r\n");
-
- /******以上是GPRS初始化配置********/
-
-
-
-
- /******以下是GPS初始化配置********/
-
- //打開GPS
- Air_send_instructions("\r\nAT+CGNSPWR=1\r\n");
- //設置NMEA語句類型
- Air_send_instructions("\r\nAT+CGNSSEQ=\"RMC\" \r\n");
-
- /******以上是GPS初始化配置********/
- flag_set=0;//結束配置模式
- }
- /*********************************************************
- 函數名稱:Air_send_instructions
- 功 能:配置Air800模塊的指令函數
- 參 數:
- *********************************************************/
- void Air_send_instructions(char array[])
- {
- u8 i=0,len;
- len=strlen(array);
- strcpy(USART_TX_BUF,array);
- for(i=0;i<len;i++)
- {
- USART_SendData(USART1,USART_TX_BUF[i]);
- }
- while(flag_RX_ok==0);//判斷反饋是否成功
- flag_RX_ok=0;
- }
- /*********************************************************
- 函數名稱:parse_gpsBuffer
- 功 能:解析GPS信息
- 參 數:
- *********************************************************/
- void parse_gpsBuffer()
- {
- u8 i=0;
- char *string;
- char *string_next;
-
- if(gps.RX_success==1)
- {
- gps.fix_status=gps.Buffer[1]-'0';//獲取GPS是否定位
- for(i=0;i<=3;i++)
- {
- if(i==0) string=strstr(gps.Buffer, ",");
- string++;
- if((string_next = strstr(string, ",")) != NULL)//檢驗GNSS運行狀態
- {
- switch(i)
- {
- case 1:memcpy(gps.UTCTime,string,string_next-string);break;//UTC時間
- case 2:memcpy(gps.latitudebuffer,string,string_next-string);break;//緯度
- case 3:memcpy(gps.longitudebuffer,string,string_next-string);break;//經度
- default:break;
- }
- string=string_next;
- }
- else
- {
- gps.RX_success=0;
- memset(gps.Buffer,0,GPS_Buffer_Length);//清空無效數據
- break;//跳出for
- }
- }
- parse_gpstime(gps.UTCTime);//提取時間日期
- parse_gps_location(gps.latitudebuffer,gps.longitudebuffer);//提取經緯度
- memset(gps.Buffer,0,GPS_Buffer_Length);//清空
- memset(gps.UTCTime,0,UTCTime_Length);//清空
- memset(gps.latitudebuffer,0,latitude_Length);//清空
- memset(gps.longitudebuffer,0,longitude_Length);//清空
- }
- }
-
- /*********************************************************
- 函數名稱:parse_gpstime
- 功 能:提取GPS轉換北京時間日期
- 參 數:
- *********************************************************/
- void parse_gpstime(char array[])
- {
- gps.year=(array[0]-'0')*1000+(array[1]-'0')*100+(array[2]-'0')*10+(array[3]-'0');
- gps.month=(array[4]-'0')*10+(array[5]-'0');
- gps.day=(array[6]-'0')*10+(array[7]-'0');
- gps.hour=(array[8]-'0')*10+(array[9]-'0');
- gps.minute=(array[10]-'0')*10+(array[11]-'0');
- gps.second=(array[11]-'0')*10+(array[12]-'0');
- //北京時間=UTC+8;
- gps.hour+=8;
- if(gps.hour>23)
- {
- gps.hour=gps.hour-24;
- gps.day++;
- if(gps.day>28)
- {
- if(gps.month==2)
- {
- if(gps.year%400==0||(gps.year%100!=0&&gps.year%4==0))//閏年
- {
- if(gps.day>29)
- {
- gps.month=3;
- gps.day=1;
- }
- }
- else//平年
- {
- gps.month=3;
- gps.day=1;
- }
- }
- else if(gps.month==4||gps.month==6||gps.month==9||gps.month==11)//day為30天
- {
- if(gps.day>30)
- {
- gps.day=1;
- gps.month++;
- }
- }
- 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天
- {
- if(gps.day>31)
- {
- if(gps.month==12)
- {
- gps.day=1;
- gps.month=1;
- gps.year++;
- }
- else
- {
- gps.day=1;
- gps.month++;
- }
- }
- }
- }
- }
- }
- /*********************************************************
- 函數名稱:parse_gps_location
- 功 能:提取GPS的經緯度并放大10^6
- 參 數:
- *********************************************************/
- void parse_gps_location(char lat[],char lon[])
- {
- u8 i=0;
- char *string;
-
- //獲得放大10^6的經度值
- if(lat[0]=='-')
- {
- gps.EW=1;//西經為1
- string=strstr(lat, ",");
- for(i=1;i<(string-lat);i++)
- {
- gps.latitude+=(lat[i]-'0')*pow(10,string-lat-i-1+6);//放大10^6倍
- }
- for(i=0;i<6;i++)
- {
- ++string;
- gps.latitude+=((*string)-'0')*pow(10,5-i);
- }
- }
- else
- {
- gps.EW=0;
- string=strstr(lat, ",");
- for(i=0;i<(string-lat);i++)
- {
- gps.latitude+=(lat[i]-'0')*pow(10,string-lat-i-1+6);//放大10^6倍
- }
- for(i=0;i<6;i++)
- {
- ++string;
- gps.latitude+=((*string)-'0')*pow(10,5-i);
- }
- }
-
- //獲得放大10^6的緯度值
- if(lon[0]=='-')
- {
- gps.NS=1;//南緯為1
- string=strstr(lon, ",");
- for(i=1;i<(string-lon);i++)
- {
- gps.longitude+=(lon[i]-'0')*pow(10,string-lon-i-1+6);//放大10^6倍
- }
- for(i=0;i<6;i++)
- {
- ++string;
- gps.longitude+=((*string)-'0')*pow(10,5-i);
- }
- }
- else
- {
- gps.NS=0;
- string=strstr(lon, ",");
- for(i=0;i<(string-lon);i++)
- {
- gps.longitude+=(lon[i]-'0')*pow(10,string-lon-i-1+6);//放大10^6倍
- }
- for(i=0;i<6;i++)
- {
- ++string;
- gps.longitude+=((*string)-'0')*pow(10,5-i);
- }
- }
- }
- void GPS_deal()
- {
- if(gps.RX_success==0) Air_send_instructions("\r\nAT+CGNSINF\r\n");//查詢GPS的GNSS信息
-
- parse_gpsBuffer();//解析GPS信息
- }
- /*********************************************************
- 函數名稱:GPRS_send
- 功 能:GPRS發送函數
- 參 數:
- *********************************************************/
- void GPRS_send()
- {
- u8 i=0;
- u32 *ptr;
-
- if(flag_bluetooth_ok&&gps.RX_success&&time_count==100)
- {
- flag_gprs_send=1;//GPRS發送標志
- strcpy(USART_TX_BUF,"\r\nAT+CIPSEN\r\n");
- for(i=0;i<strlen("\r\nAT+CIPSEN\r\n");i++)
- {
- USART_SendData(USART1,USART_TX_BUF[i]);
- }
- memset(USART_TX_BUF,0,USART_LEN);//清空
-
- GPRS_TX_BUF[0]='\r';
- GPRS_TX_BUF[1]='\n';
- GPRS_TX_BUF[2]='>';
- GPRS_TX_BUF[3]=0x7e;//標識位
-
- //消息頭
- GPRS_TX_BUF[4]=0x02;
- GPRS_TX_BUF[5]=0x00;
- GPRS_TX_BUF[6]=0x00;
- GPRS_TX_BUF[7]=0x1c;//消息體長度28
- GPRS_TX_BUF[8]=0x00;
- GPRS_TX_BUF[9]=0x00;
- GPRS_TX_BUF[10]=0x00;
- GPRS_TX_BUF[11]=0x00;
- GPRS_TX_BUF[12]=0x00;
- GPRS_TX_BUF[13]=0x00;
- GPRS_TX_BUF[14]=0x00;
- GPRS_TX_BUF[15]=0x00;
-
- //消息體
- GPRS_TX_BUF[16]=0x00;
- GPRS_TX_BUF[17]=0x00;
- GPRS_TX_BUF[18]=0x00;
- GPRS_TX_BUF[19]=0x00;
-
- GPRS_TX_BUF[20]=0x00;
- GPRS_TX_BUF[21]=0x04;//狀態為GPS定位
- GPRS_TX_BUF[22]=0x00;
- GPRS_TX_BUF[23]=gps.fix_status*2+gps.NS*4+gps.EW*8;//GPS的定位,經緯狀態
- ptr=(u32*)(&(GPRS_TX_BUF[24]));
- *ptr=gps.longitude;//緯度值
- ++ptr;
- *ptr=gps.latitude;//經度值
-
- GPRS_TX_BUF[32]=0x00;
- GPRS_TX_BUF[33]=0x00;
- GPRS_TX_BUF[34]=heart_rate;//心率
- GPRS_TX_BUF[35]=blood_oxygen;//血氧
-
- GPRS_TX_BUF[36]=0x00;
- GPRS_TX_BUF[37]=0x00;
-
- GPRS_TX_BUF[38]=gps.year;
- GPRS_TX_BUF[39]=gps.month;
- GPRS_TX_BUF[40]=gps.day;
- GPRS_TX_BUF[41]=gps.hour;
- GPRS_TX_BUF[42]=gps.minute;
- GPRS_TX_BUF[43]=gps.second;
-
- //檢驗碼 從消息頭開始到結尾字節異或
- GPRS_TX_BUF[44]=0;
- for(i=4;i<=43;i++)
- {
- GPRS_TX_BUF[44]^=GPRS_TX_BUF[i];
- }
-
- GPRS_TX_BUF[45]=0x7e;//標識位
- GPRS_TX_BUF[46]=0x1A;
- GPRS_TX_BUF[47]='\r';
- GPRS_TX_BUF[48]='\n';
-
- for(i=0;i<=48;i++)
- {
- USART_SendData(USART1,GPRS_TX_BUF[i]);
- }
- while(flag_RX_ok==0);//判斷反饋是否成功
- flag_RX_ok=0;
-
- }
- }
復制代碼
所有資料51hei提供下載:
STM32.20181028.rar
(293.72 KB, 下載次數: 120)
2018-12-3 03:47 上傳
點擊文件名下載附件
基于STM32的Air800模塊GPS定位和GPRS發送到服務器程序
|