#include <DFRobot_sim808.h>
#include <SoftwareSerial.h>
//#define PIN_TX 1
//#define PIN_RX 0
//SoftwareSerial mySerial(PIN_TX,PIN_RX);
//DFRobot_SIM808 sim808(&mySerial);//Connect RX,TX,PWR,
//make sure that the baud rate of SIM900 is 9600!
//you can use the AT Command(AT+IPR=9600) to set it through SerialDebug
DFRobot_SIM808 sim808(&Serial);
char buffer[512];
int i = 0;
void setup(){
//mySerial.begin(9600);
Serial.begin(9600);
//******** Initialize sim808 module *************
while(!sim808.init()) {
delay(1000);
Serial.print("Sim808 init error\r\n");
}
delay(3000);
//*********** Attempt DHCP *******************
while(!sim808.join(F("mobitel3g"))) {
Serial.println("Sim808 join network error");
delay(2000);
}
//************ Successful DHCP ****************
Serial.print("IP Address is ");
Serial.println(sim808.getIPAddress());
//************* Turn on the GPS power************
while(!sim808.attachGPS()) {
delay(1000);
Serial.print("Open the GPS power failure\r\n");
}
delay(3000);
Serial.println("Open the GPS power success");
for(int i = 0; i<=40;i++)
{
for(int y = 0;y<=i;y++)
{
Serial.print(".");
delay(60);
}
Serial.println("");
}
}
void loop(){
tcp();
delay(2000);
//Calls the function every 2 seconds
}
void tcp()
{
// Update the GPS data
float lati, longi;
if (sim808.getGPS()) {
Serial.print(sim808.GPSdata.year);
Serial.print("/");
Serial.print(sim808.GPSdata.month);
Serial.print("/");
Serial.print(sim808.GPSdata.day);
Serial.print(" ");
Serial.print(sim808.GPSdata.hour);
Serial.print(":");
Serial.print(sim808.GPSdata.minute);
Serial.print(":");
Serial.print(sim808.GPSdata.second);
Serial.print(":");
Serial.println(sim808.GPSdata.centisecond);
lati = sim808.GPSdata.lat,6;
longi = sim808.GPSdata.lon,6;
//print the variables
Serial.print("LAT =");
Serial.println(lati);
Serial.print("LON =");
Serial.println(longi);
sim808.detachGPS();
}
else {
// No gps, abort
Serial.println("No GPS");
return;
}
//*********** Establish a TCP connection ************
if (!sim808.connect(TCP,"http://syntiq.lk", 80)) {
Serial.println("Connect error");
return;
}
else {
Serial.println("Connect mbed.org success");
}
//*********** Send a GET request *****************
char http_cmd[100];
sprintf(http_cmd, "GET /GPS/Addtoserver.php?data2=%f&data1=%f HTTP/1.0\r\n\r\n\0", lati, longi);
sim808.send(http_cmd, strlen(http_cmd));
/*
int ret = sim808.recv(buffer, sizeof(buffer) - 1);
if (ret <= 0){
Serial.println("error receiving");
}
else {
buffer[ret] = '\0';
Serial.print(buffer);
}
*/
//************* Close TCP or UDP connections **********
sim808.close();
//*** Disconnect wireless connection, Close Moving Scene *******
sim808.disconnect();
}
这是我已经用于检索GPS数据的代码和我这些数据发送到MySQL服务器。但我不从GPS模块接收的任何数据。
我敢肯定,该模块工作,因为我个人尝试了GPS和TCP代码和它们运作良好。
有人可以告诉我我哪里出问题了。
答案 0 :(得分:0)
如果您使用两个串行端口(每个端口我是指一组串行引脚),一个用于计算机(串行),一个用于GPS模块(例如串行1),那会更好,即假设微控制器是Arduino Mega。但是,如果是带一个串行端口的Arduino Uno或其他任何Arduino,则需要使用软件串行。默认情况下,Arduino IDE中附带有软件串行示例,您也可以直接here。
祝您在项目中一切顺利。