Example Code for Arduino-Positioning
This example demonstrates how to use the SIM7000's GNSS functionality to get the current position (longitude and latitude). Users will learn how to initialize the positioning function and retrieve location data.
Hardware Preparation
- DFR0216 DFRduino UNO R3 - Arduino Compatible x1
- DFR0505 SIM7000C Arduino NB-IoT/LTE/GPRS Expansion Shield x1
- USB Wire x1
- 7V~12V DC Power Supply x1
- Active antenna (for positioning)
Software Preparation
- Arduino IDE, Click to Download Arduino IDE from Arduino®
- Please click to download DFRobot General SIM library.
- Please click to download NB-IoT SIM7000 Shield Library. How to install Libraries in Arduino IDE.
Wiring Diagram
Connect to an active antenna and use it outdoors.
Other Preparation Work
Ensure the SIM7000 is powered on and the baud rate is set to 19200.
Sample Code
#include <Wire.h>
#include <DFRobot_SIM7000.h>
DFRobot_SIM7000 sim7000;
SoftwareSerial mySerial(8,7); //Set serial
void setup(){
int signalStrength,dataNum;
Serial.begin(115200);
sim7000.begin(mySerial);
sim7000.turnOFF();
delay(5000);
if(sim7000.turnON()){ //Turn ON SIM7000
Serial.println("Turn NO SIM7000");
}
if(sim7000.setBaudRate(19200)){ //Set baud rate from 115200 to 19200
Serial.println("Set baud rate:19200");
}else{
Serial.println("faile to set baud rate");
}
while(1){
if(sim7000.init()){ //Init SIM7000
Serial.println("AT command READY");
break;
}else{
Serial.println("AT command ERROR");
delay(500);
}
}
while(1){
if(sim7000.initPos()){
Serial.println("Positioning function initialized");
break;
}else{
Serial.println("Fail to init positioning function");
delay(500);
}
}
}
void loop(){
Serial.println("Enter anything to get positioning ");
char loge[10];
readSerial(loge);
Serial.println("Getting position......");
if(sim7000.getPosition()){ //Get the current position
Serial.print("Longtude :");
Serial.println(sim7000.getLongitude()); //Get longitude
Serial.print("Latitude :");
Serial.println(sim7000.getLatitude()); //Get latitude
}else{
Serial.println("Wrong data try again");
}
}
int readSerial(char result[]){
int i = 0;
while(1){
while(Serial.available() > 0){
char inChar = Serial.read();
if(inChar == '\n'){
result[i] = '\0';
Serial.flush();
return 0;
}
if(inChar != '\r'){
result[i] = inChar;
i++;
}
}
}
}
Result
When the user enters any input in the serial monitor, the module will attempt to get the position. If successful, it will print the longitude and latitude. Expected output:
Enter anything to get positioning
Getting position......
Longtude : [value]
Latitude : [value]
Additional Information
- Function descriptions for positioning:
- Initialize the position function. The function back to true when initialized successfully.
initPos(); - Get current position and the function back to true when initialized successfully.
getPosition(); - Get current longitude: the east one is a positive number and a west longitude is a negative number (-180,180).
getLongitude(); - Get current latitude: the east one is a positive number and a west longitude is a negative number (-90, 90).
getlatitude();
- Initialize the position function. The function back to true when initialized successfully.
Was this article helpful?
