您所在的位置: > 主页 > 大众创业网 > 资讯 > 正文
用个8位单片机+sim900a+gps模块自制建议定位装置!来源: 日期:2021-01-20 04:28:24  阅读:-

    gps定位器是一个被广泛使用的小工具,说不定你老婆就在你的身上放了一个。这玩意很容易买的到。当然自己做一个也可以,毕竟看这内容的人一般都是单身狗。

    需要材料,一款diy界网红arduino uno,采用8位AVR单片机,自带bootloader和一大推的支持库。配合arduino开发环境可以快速简单的编写arduino的程序。

    一个ICOMSAT扩展板,集成收发短信的sim900模块

    一个GPS shield扩展板

    这些都是扩展版,直接堆叠插在一起就好了,第一层是arduino uno(这是废话),第二层是ICOMSAT扩展板,第三层是GPS shield扩展板。这就是arduino的魅力,拥有大量的周边扩展模块,直接连接。让没有硬件开发知识人,只要会简单的c语言就可以玩转arduino。

    首先对ICOMSAT扩展板操作,开关拨到UART的一端,跳线帽按照RXD->D2,TXD->D3如图,接上GSM天线和插上手机SIM卡。详细跳帽和引脚请下载查阅产品手册和原理图

    用个8位单片机+sim900a+gps模块自制建议定位装置

    然后对GPS shield扩展板操作,开关拨到5V,跳线帽按照RXD->D1,TXD->D0如图连接,接上GPS天线

    用个8位单片机+sim900a+gps模块自制建议定位装置

    最后按照顺序把三个家伙给堆起来

    用个8位单片机+sim900a+gps模块自制建议定位装置

    在编译程序之前我们需要下载arduino的gsm和gps的支持库,解压后放在Arduino\libraries的目录下

    下面是程序代码复制粘帖过去IDE中,编译好后下载到arduino中就好了

    #include "SIM900.h"

    #include

    //#include "inetGSM.h"

    #include "sms.h"

    //#include "call.h"

    #include

    #include

    /* This sample code demonstrates the normal use of a TinyGPS object.

    It requires the use of SoftwareSerial, and assumes that you have a

    4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).

    */

    TinyGPS gps;

    #define ledpin 13

    #define pwrkey 27

    //**************************************************************************

    char sms_rx[122]; //Received text SMS

    byte type_sms=SMS_ALL; //Type of SMS

    byte del_sms=1; //0: No deleting sms - 1: Deleting SMS

    char number_incoming[20];

    //**************************************************************************

    SMSGSM sms;

    int error;

    boolean started=false;

    bool newData = false;

    char gps_year[8];

    char gps_mon[3];

    char gps_day[3];

    char gps_hour[3];

    char gps_min[3];

    char gps_sec[3];

    char gps_lon[20];

    char gps_lat[20];

    char gps_sms[100];

    void setup()

    {

    //software power sim900 up

    pinMode(pwrkey,OUTPUT);

    digitalWrite(pwrkey,HIGH);

    delay(600);

    digitalWrite(pwrkey,LOW);

    Serial.begin(115200);

    Serial2.begin(9600);

    if (gsm.begin(9600)) {

    Serial.println("\nstatus=READY");

    gsm.forceON(); //To ensure that SIM908 is not only in charge mode

    started=true;

    } else Serial.println("\nstatus=IDLE");

    if(started)

    {

    //delete all sms message

    Serial.println("Deleting SMS");

    char error = DeleteAllSMS();

    if (error==1)Serial.println("All SMS deleted");

    else Serial.println("SMS not deleted");

    }

    else

    {Serial.println("SIM900 NOT EXISTED"); while(1);}

    delay(10000);

    }

    void loop()

    {

    if(started)

    {

    check_gps();

    Check_SMS();

    }

    }

    void Check_SMS() //Check if there is an sms 'type_sms'

    {

    char pos_sms_rx; //Received SMS position

    pos_sms_rx=sms.IsSMSPresent(type_sms);

    if (pos_sms_rx!=0)

    {

    //Read text/number/position of sms

    sms.GetSMS(pos_sms_rx,number_incoming,sms_rx,120);

    Serial.print("Received SMS from ");

    Serial.print(number_incoming);

    Serial.print("(sim position: ");

    Serial.print(word(pos_sms_rx));

    Serial.println(")");

    Serial.println(sms_rx);

    if (del_sms==1) //If 'del_sms' is 1, i delete sms

    {

    error=sms.DeleteSMS(pos_sms_rx);

    if (error==1)Serial.println("SMS deleted");

    else Serial.println("SMS not deleted");

    }

    if((strstr(sms_rx,"gps")!=0)&&(strlen(sms_rx)==3))

    {

    Serial.println("\nsending SMS");

    if(newData)

    {

    if (sms.SendSMS(number_incoming, gps_sms))

    Serial.println("\nSMS sent OK");

    else

    Serial.println("\nSMS sent error");

    }

    else

    {

    if (sms.SendSMS(number_incoming, "gps not ready"))

    Serial.println("\nSMS sent OK");

    else

    Serial.println("\nSMS sent error");

    }

    }

    Serial2.flush();

    }

    newData=false;

    return;

    }

    char check_gps()

    {

    newData=false;

    unsigned long chars;

    unsigned short sentences, failed;

    // For one second we parse GPS data and report some key values

    for (unsigned long start = millis(); millis() - start < 1000;)

    {

    while (Serial2.available())

    {

    char c = Serial2.read();

    // Serial.write(c); // uncomment this line if you want to see the GPS data flowing

    if (gps.encode(c)) // Did a new valid sentence come in?

    newData = true;

    }

    }

    if (newData)

    {

    float flat, flon;

    unsigned long age;

    int _year;

    byte _month, _day,_hour,_minute,_second,_hundredths;

    gps.f_get_position(&flat, &flon, &age);

    gps.crack_datetime(&_year,&_month,&_day,&_hour,&_minute,&_second,&_hundredths,&age);

    flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6;

    flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6;

    dtostrf(flat, 11, 6, gps_lat);

    dtostrf(flon, 10, 6, gps_lon);

    strcpy(gps_sms,"lat:");

    strcat(gps_sms,gps_lat);

    strcat(gps_sms,"\n");

    strcat(gps_sms,"lon:");

    strcat(gps_sms,gps_lon);

    strcat(gps_sms,"\n");

    strcat(gps_sms,"time:");

    itoa(_year,gps_year,10);

    strcat(gps_sms,gps_year);

    itoa(_month,gps_mon,10);

    if(strlen(gps_mon)==1)

    strcat(gps_sms,"0");

    strcat(gps_sms,gps_mon);

    itoa(_day,gps_day,10);

    if(strlen(gps_day)==1)

    strcat(gps_sms,"0");

    strcat(gps_sms,gps_day);

    itoa(_hour,gps_hour,10);

    if(strlen(gps_hour)==1)

    strcat(gps_sms,"0");

    strcat(gps_sms,gps_hour);

    itoa(_minute,gps_min,10);

    if(strlen(gps_min)==1)

    strcat(gps_sms,"0");

    strcat(gps_sms,gps_min);

    itoa(_second,gps_sec,10);

    if(strlen(gps_sec)==1)

    strcat(gps_sms,"0");

    strcat(gps_sms,gps_sec);

    Serial.println(gps_sms);

    }

    }

    char DeleteAllSMS()

    {

    char ret_val = -1;

    if (CLS_FREE != gsm.GetCommLineStatus()) return (ret_val);

    gsm.SetCommLineStatus(CLS_ATCMD);

    ret_val = 0; // still not present

    gsm.SimpleWriteln(F("AT+CMGDA=\"DEL ALL\""));

    switch (gsm.WaitResp(8000, 50, "OK")) {

    case RX_TMOUT_ERR:

    // response was not received in specific time

    ret_val = -2;

    break;

    case RX_FINISHED_STR_RECV:

    // OK was received => SMS deleted

    ret_val = 1;

    break;

    case RX_FINISHED_STR_NOT_RECV:

    // other response: e.g. ERROR => SMS was not deleted

    ret_val = 0;

    break;

    }

    gsm.SetCommLineStatus(CLS_FREE);

    return (ret_val);

    }

    通过9v电池供电或者是充电宝usb供电,把做好的三层不明物体放在开阔的地方,想arduino的手机号码发送“gps”短信,之后会收到一个回信,内容为

    lat:23.036960

    lon:114.418800

    time:20170919114516

    lat表示纬度,lon表示经度,time表示时间,不过是“格林尼治时间”(本初子午线)不是北京时间,和北京时间相差8小时。不会算的回去学地理。

    如果你收到“gps not ready”回信,那就是说明没有搜到gps卫星,等一下或者是换个地方。

    (正文已结束)

    免责声明及提醒:此文内容为本网所转载企业宣传资讯,该相关信息仅为宣传及传递更多信息之目的,不代表本网站观点,文章真实性请浏览者慎重核实!任何投资加盟均有风险,提醒广大民众投资需谨慎!