基于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)
|