LocReport
This commit is contained in:
parent
c3c3d9bc6f
commit
edc48494de
|
@ -0,0 +1,124 @@
|
|||
#include "SIM7000.h"
|
||||
#include <SoftwareSerial.h>
|
||||
#define RXD2 10
|
||||
#define TXD2 11
|
||||
|
||||
//你需要修改以下信息
|
||||
#define NetworkAPN "cmnbiot3"
|
||||
#define ServerIP "54.255.233.63"
|
||||
#define ServerPort 5055
|
||||
#define DeviceID 123456
|
||||
|
||||
SoftwareSerial mySS = SoftwareSerial(RXD2, TXD2);
|
||||
SIM7000 sim7000;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
mySS.begin(19200);
|
||||
|
||||
//连接到sim7000
|
||||
if (sim7000.begin(mySS))
|
||||
Serial.println("Connected to Sim7000 ok.");
|
||||
else{
|
||||
Serial.println("Couldn't connect to Sim7000 module!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
//探测sim卡已插入
|
||||
if(sim7000.check_send_cmd("AT+CPIN?\r\n","CPIN: READY"))
|
||||
Serial.println("SIM card READY.");
|
||||
else{
|
||||
Serial.println("SIM card ERROR, Check if you have insert SIM card and restart SIM7000!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
//选择LTE,NB-IOT,打开扰码
|
||||
if(sim7000.check_send_cmd("AT+CNMP=38\r\n","OK"))
|
||||
Serial.println("Select LTE OK.");
|
||||
else{
|
||||
Serial.println("Select LTE, return error!");
|
||||
while(1);
|
||||
}
|
||||
if(sim7000.check_send_cmd("AT+CMNB=2\r\n","OK"))
|
||||
Serial.println("Select NB-IOT OK.");
|
||||
else{
|
||||
Serial.println("Select NB-IOT, return error!");
|
||||
while(1);
|
||||
}
|
||||
if(sim7000.check_send_cmd("AT+NBSC=1\r\n","OK"))
|
||||
Serial.println("Enable the scrambling feature OK.");
|
||||
else{
|
||||
Serial.println("Enable the scrambling feature, return error!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
//获得可用的APN
|
||||
sim7000.check_send_cmd("AT+CGNAPN\r\n","OK");
|
||||
|
||||
//设置APN,连接网络
|
||||
char command[100];
|
||||
sprintf(command, "AT+CSTT=\"%s\"\r\n",NetworkAPN);
|
||||
if(sim7000.check_send_cmd(command,"OK"))
|
||||
Serial.println("Set APN OK.");
|
||||
else{
|
||||
Serial.println("Set APN, not return OK!");
|
||||
}
|
||||
|
||||
if(sim7000.check_send_cmd("AT+CIICR\r\n","OK"))
|
||||
Serial.println("CIICR OK.");
|
||||
else
|
||||
Serial.println("Set CIICR, not return OK!");
|
||||
|
||||
//打开卫星定位电源
|
||||
if(sim7000.check_send_cmd("AT+CGNSPWR=1\r\n","OK"))
|
||||
Serial.println("CGNSPWR OK.");
|
||||
else{
|
||||
Serial.println("CGNSPWR, return error!");
|
||||
while(1);
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
char GNSSBuffer[150];
|
||||
char command[200];
|
||||
char *p, *lat, *lon, *datetime;
|
||||
|
||||
// 获得时间与经纬度
|
||||
sim7000.sendCmd("AT+CGNSINF\r\n");
|
||||
sim7000.readBuffer(GNSSBuffer, 150);
|
||||
p = strstr(GNSSBuffer, ",");
|
||||
p = strstr(p+1, ",");
|
||||
datetime = p+1;
|
||||
p = strstr(p+1, ",");
|
||||
*p = NULL;
|
||||
lat = p+1;
|
||||
p = strstr(p+1, ",");
|
||||
*p = NULL;
|
||||
lon = p+1;
|
||||
p = strstr(p+1, ",");
|
||||
*p = NULL;
|
||||
|
||||
Serial.print("Datetime: ");
|
||||
Serial.println(datetime);
|
||||
Serial.print("Latitude: ");
|
||||
Serial.println(lat);
|
||||
Serial.print("Longitude: ");
|
||||
Serial.println(lon);
|
||||
|
||||
sim7000.check_send_cmd("AT+CIFSR\r\n", "OK");
|
||||
// 连接traccar服务器,发送数据
|
||||
if(strlen(datetime)>0){
|
||||
Serial.println("Connect to server...");
|
||||
sprintf(command, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", ServerIP, ServerPort);
|
||||
sim7000.check_send_cmd(command,"OK", 20000, 10000);
|
||||
|
||||
if(sim7000.check_send_cmd("AT+CIPSTATUS\r\n","CONNECT OK")){
|
||||
sprintf(command, "GET /?id=%ld&lat=%s&lon=%s HTTP/1.1\r\n\r\n", DeviceID, lat, lon);
|
||||
sim7000.check_send_cmd("AT+CIPSEND\r\n", ">");
|
||||
sim7000.sendCmd(command);
|
||||
sim7000.check_send_cmd("\x1a", "SEND OK", 20000, 10000);
|
||||
sim7000.check_send_cmd("AT+CIPCLOSE\r\n", "OK", 20000, 10000);
|
||||
}
|
||||
}
|
||||
delay(5000);
|
||||
}
|
|
@ -0,0 +1,97 @@
|
|||
#include "SIM7000.h"
|
||||
|
||||
bool SIM7000::begin(Stream &port)
|
||||
{
|
||||
mySerial = &port;
|
||||
|
||||
int16_t timeout = 7000;
|
||||
while (timeout > 0) {
|
||||
while (mySerial->available()) mySerial->read();
|
||||
if (check_send_cmd("AT\r\n","OK"))
|
||||
return true;
|
||||
delay(500);
|
||||
timeout-=500;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SIM7000::checkSIMStatus(void)
|
||||
{
|
||||
int count = 0;
|
||||
while(count < 3){
|
||||
if(check_send_cmd("AT\r\n","OK")){
|
||||
break;
|
||||
}else{
|
||||
count++;
|
||||
delay(300);
|
||||
}
|
||||
}
|
||||
if(count == 3){
|
||||
return false;
|
||||
}
|
||||
count = 0;
|
||||
while(count < 3){
|
||||
if(check_send_cmd("AT+CPIN?\r\n","READY")){
|
||||
break;
|
||||
}else{
|
||||
count++;
|
||||
delay(300);
|
||||
}
|
||||
}
|
||||
if(count == 3){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SIM7000::check_send_cmd(const char* cmd, const char* resp, unsigned int timeout, unsigned int chartimeout)
|
||||
{
|
||||
char SIMbuffer[100];
|
||||
sendCmd(cmd);
|
||||
readBuffer(SIMbuffer,100, timeout, chartimeout);
|
||||
if(NULL != strstr(SIMbuffer,resp)){
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SIM7000::sendCmd(const char* cmd)
|
||||
{
|
||||
mySerial->write(cmd);
|
||||
Serial.println("================>>>>>>");
|
||||
Serial.print("SendCommand: ");
|
||||
Serial.print(cmd);
|
||||
}
|
||||
|
||||
int SIM7000::readBuffer(char *buffer, int count, unsigned int timeout, unsigned int chartimeout)
|
||||
{
|
||||
int i = 0;
|
||||
unsigned long timerStart, prevChar;
|
||||
timerStart = millis();
|
||||
prevChar = 0;
|
||||
while(1){
|
||||
while(mySerial->available()>0){
|
||||
buffer[i++] = mySerial->read();
|
||||
prevChar = millis();
|
||||
if(i >= count){
|
||||
buffer[i] = '\0';
|
||||
Serial.println("<<<<<<<================");
|
||||
Serial.println(buffer);
|
||||
return i;
|
||||
}
|
||||
}
|
||||
if(timeout){
|
||||
if((unsigned long) (millis() - timerStart) > timeout){
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(((unsigned long) (millis() - prevChar) > chartimeout) && (prevChar != 0)){
|
||||
break;
|
||||
}
|
||||
}
|
||||
buffer[i] = '\0';
|
||||
Serial.println("<<<<<<<================");
|
||||
Serial.println(buffer);
|
||||
return i;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef __SIM7000_H__
|
||||
#define __SIM7000_H__
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <SoftwareSerial.h>
|
||||
#define DEFAULT_TIMEOUT 5000 //miliseconds
|
||||
#define DEFAULT_INTERCHAR_TIMEOUT 1500 //miliseconds
|
||||
|
||||
class SIM7000
|
||||
{
|
||||
public:
|
||||
bool check_send_cmd(const char* cmd, const char* resp, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int chartimeout = DEFAULT_INTERCHAR_TIMEOUT);
|
||||
void sendCmd(const char* cmd);
|
||||
int readBuffer(char *buffer, int count, unsigned int timeout = DEFAULT_TIMEOUT, unsigned int chartimeout = DEFAULT_INTERCHAR_TIMEOUT);
|
||||
bool setBaudRate(long rate);
|
||||
bool checkSIMStatus(void);
|
||||
bool begin(Stream &port);
|
||||
private:
|
||||
char latitude[8];
|
||||
char longitude[8];
|
||||
SoftwareSerial* mySerial;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue