gps定位器是一個(gè)被廣泛使用的小工具,說(shuō)不定你老婆就在你的身上放了一個(gè)。這玩意很容易買的到。當(dāng)然自己做一個(gè)也可以,畢竟看這內(nèi)容的人一般都是單身狗。
需要材料,一款diy界網(wǎng)紅arduino uno,采用8位AVR單片機(jī),自帶bootloader和一大推的支持庫(kù)。配合arduino開發(fā)環(huán)境可以快速簡(jiǎn)單的編寫arduino的程序。
一個(gè)ICOMSAT擴(kuò)展板,集成收發(fā)短信的sim900模塊
一個(gè)GPS shield擴(kuò)展板
這些都是擴(kuò)展版,直接堆疊插在一起就好了,第一層是arduino uno(這是廢話),第二層是ICOMSAT擴(kuò)展板,第三層是GPS shield擴(kuò)展板。這就是arduino的魅力,擁有大量的周邊擴(kuò)展模塊,直接連接。讓沒有硬件開發(fā)知識(shí)人,只要會(huì)簡(jiǎn)單的c語(yǔ)言就可以玩轉(zhuǎn)arduino。
首先對(duì)ICOMSAT擴(kuò)展板操作,開關(guān)撥到UART的一端,跳線帽按照RXD->D2,TXD->D3如圖,接上GSM天線和插上手機(jī)SIM卡。詳細(xì)跳帽和引腳請(qǐng)下載查閱產(chǎn)品手冊(cè)和原理圖
然后對(duì)GPS shield擴(kuò)展板操作,開關(guān)撥到5V,跳線帽按照RXD->D1,TXD->D0如圖連接,接上GPS天線
最后按照順序把三個(gè)家伙給堆起來(lái)
在編譯程序之前我們需要下載arduino的gsm和gps的支持庫(kù),解壓后放在Arduino\libraries的目錄下
下面是程序代碼復(fù)制粘帖過(guò)去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);
}
通過(guò)9v電池供電或者是充電寶usb供電,把做好的三層不明物體放在開闊的地方,想arduino的手機(jī)號(hào)碼發(fā)送“gps”短信,之后會(huì)收到一個(gè)回信,內(nèi)容為
lat:23.036960
lon:114.418800
time:20170919114516
lat表示緯度,lon表示經(jīng)度,time表示時(shí)間,不過(guò)是“格林尼治時(shí)間”(本初子午線)不是北京時(shí)間,和北京時(shí)間相差8小時(shí)。不會(huì)算的回去學(xué)地理。
如果你收到“gps not ready”回信,那就是說(shuō)明沒有搜到gps衛(wèi)星,等一下或者是換個(gè)地方。
本文為企業(yè)推廣,本網(wǎng)站不做任何建議,僅提供參考,作為信息展示!
推薦閱讀:中華企業(yè)新聞網(wǎng)
網(wǎng)友評(píng)論
請(qǐng)登錄后進(jìn)行評(píng)論|
0條評(píng)論
請(qǐng)文明發(fā)言,還可以輸入140字
您的評(píng)論已經(jīng)發(fā)表成功,請(qǐng)等候?qū)徍?/p>
小提示:您要為您發(fā)表的言論后果負(fù)責(zé),請(qǐng)各位遵守法紀(jì)注意語(yǔ)言文明