您的位置:首頁>正文

用個8位元單片機+sim900a+gps模組自製建議定位裝置

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卡。 詳細跳帽和引腳請下載查閱產品手冊和原理圖

然後對GPS shield擴展板操作, 開關撥到5V, 跳線帽按照RXD->D1, TXD->D0如圖連接, 接上GPS天線

最後按照順序把三個傢伙給堆起來

在編譯器之前我們需要下載arduino的gsm和gps的支援函式庫, 解壓後放在Arduinolibraries的目錄下

下麵是程式碼複製粘帖過去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("status=READY");

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

started=true;

} else Serial.println("status=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("sending SMS");

if(newData)

{

if (sms.SendSMS(number_incoming, gps_sms))

Serial.println("SMS sent OK");

else

Serial.println("SMS sent error");

}

else

{

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

Serial.println("SMS sent OK");

else

Serial.println("SMS 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,"");

strcat(gps_sms,"lon:");

strcat(gps_sms,gps_lon);

strcat(gps_sms,"");

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衛星, 等一下或者是換個地方。

同類文章
Next Article
喜欢就按个赞吧!!!
点击关闭提示