In this blog we’re trying to get current location of our device and display it in Blynk’s Map’s Widget via Blynk Restful HTTP GET request. So basically we’re going to see how to fetch current coordinates i.e. Latitude and Longitudes via AT commands and also send these parameters to Blynk’s server via Blynk Restful HTTP GET API.

Note: Kindly refer our Blynk Restful API to know more.


Hardware:
• Raspberry Pi
• SIM808
• 5V source
• 16 V source

Interfacing:

Raspberry PiSIM808
GPIO1TX
GPIO2RX
GNDGND

Note: You can also use CP210x and directly connect the TX, RX, GND and VCC pins to Raspberry Pi’s USP port for that the connection will be following

SIM808CP210x
TXRX
RXTX
GNDGND
5 Vcc5 Vcc

What is an AT command?
AT an abbreviation for Attention are the commands used to control MODEMs. These commands come from Hayes commands that were used by the Hayes smart modems. The Hayes commands started with AT to indicate the attention from the MODEM. The dial up and wireless MODEMs (devices that involve machine to machine communication) need AT commands to interact with a computer. These include the Hayes command set as a subset, along with other extended AT commands.


AT commands with a GSM/GPRS MODEM or mobile phone can be used to access following information and services:

  1. Information and configuration pertaining to mobile device or MODEM and SIM card
  2. SMS services
  3. MMS services
  4. Fax services
  5. Data and Voice link over mobile network
  6. Location etc

All the GSM modules SIM8xx, SIM9xx, SIM10xx etc work on serial communication protocols. The baud rate may differ for instance SIM808 works on baud rate 9600.
AT commands can easily be communicated either by Serial Monitor of Arduino or any such tool that does serial communication.

Let’s try it for getting location

  1. AT
  2. AT+CGPSPWR=1
  3. AT+CGPSSTATUS?
  4. AT+CGPSINF=0
  5. AT+CGPSINF=32

Here the number in single NMEA sentence .
Coding the Same Logic in Python:
We will use serial library

def coordinates(com_port):
    coor=0
    lat=0
    lon=0
    port=serial.Serial(com_port,baudrate=9600,timeout=1)
    port.write(str.encode('AT'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSPWR=1'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSSTATUS?'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSINF=0'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSINF=32'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        coor=port.readline().decode("utf-8")
        print(coor)
        a=coor.find("A")
        e=coor.find("E")    
        coor=coor[a+2:e-1]
        n=coor.find("N")
        lat=coor[0:n-1]
        lon=coor[n+4:]

In the above code we were successful in getting coordinates that are in variables lat and lon.

Now Let’s look at How to make HTTP GET requests using GSM Module

The AT commands that we’ll be using are

  1. AT
  2. AT+CGATT=1
  3. AT+SAPBR=3,1,”Contype”,”GPRS”
  4. AT+SAPBR=3,1,”APN”,”Internet”
  5. AT+SAPBR=1,1
  6. AT+HTTPINIT
  7. AT+HTTPPARA = “CID”,1
  8. AT+HTTPPARA=”URL”,””
  9. AT+HTTPACTION=0
  10. AT+HTTPREAD

Blynk:
Download and Registration

Create a new Project

• Select the hardware model you will use

Note: You can choose any hardware model as we’re not using any hardware. We’re only going to use HTTP requests.

• Auth Token
o Every new project you create will have its own Auth Token. You’ll get Auth Token automatically on your email after project creation.
o This is a unique Identifier and this need to be noted down. We will require this in future.

• Click on Create button to Create Project.

Add a Widget and Button Configuration
Widget is a relatively simple and easy-to-use component that has a specific functionality. Your project canvas is empty. Cick anywhere you will be able to open widget box
• All the available widget are available in widget box
• There is also energy balance. This indicates how many more widget can you use. Every widget consume some of it’s energy


• Drag-n-Drop – Tap and hold the Widget to drag it to the new position.
• Widget Settings – Each Widget has it’s own settings. Tap on the widget to get to them.


Value Display Widget
• Drag and drop two Value Display and Widget and choose the virtual pins to V0 and V1 resp.

HTTP GET
• Your Endpoint URL will be of this type
o http://blynk-cloud.com//update/V1?value=
o http://blynk-cloud.com//update/V0?value=
• Replace with your project token of Blynk
• Replace with your Latitude Value and with your Longitude Value

HTTP GET Code

def request_get_lat(lat):
    port.write(str.encode('AT'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGATT=1'+'\r\n'))
    time.sleep(0.2)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=3,1,"Contype","GPRS"'+'\r\n'))
    time.sleep(1)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=3,1,"APN","Internet"'+'\r\n'))
    time.sleep(5)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=1,1'+'\r\n'))
    time.sleep(10)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPINIT'+'\r\n'))
    time.sleep(2)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPPARA = "CID",1'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPPARA="URL","http://blynk-cloud.com/xxxxxxxxxxxxxxxxxxx/update/V0?value='+lat+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPACTION=0'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPREAD'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))

and Similarly for Latitude.

Complete Code:

import serial
import os, time
import serial.tools.list_ports

def list_ports():
    ports = serial.tools.list_ports.comports()
    for port, desc, hwid in sorted(ports):
        print("{}: {} [{}]".format(port, desc, hwid))
def coordinates(com_port):
    coor=0
    lat=0
    lon=0
    port=serial.Serial(com_port,baudrate=9600,timeout=1)
    port.write(str.encode('AT'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSPWR=1'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSSTATUS?'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSINF=0'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGPSINF=32'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        coor=port.readline().decode("utf-8")
        print(coor)
        a=coor.find("A")
        e=coor.find("E")    
        coor=coor[a+2:e-1]
        n=coor.find("N")
        lat=coor[0:n-1]
        lon=coor[n+4:]
    return lat,lon
    

def request_get_lat(lat):
    port.write(str.encode('AT'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGATT=1'+'\r\n'))
    time.sleep(0.2)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=3,1,"Contype","GPRS"'+'\r\n'))
    time.sleep(1)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=3,1,"APN","Internet"'+'\r\n'))
    time.sleep(5)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=1,1'+'\r\n'))
    time.sleep(10)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPINIT'+'\r\n'))
    time.sleep(2)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPPARA = "CID",1'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPPARA="URL","http://blynk-cloud.com/xxxxxxxxxx/update/V0?value='+lat+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPACTION=0'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPREAD'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))

def request_get_lon(lon):
    port.write(str.encode('AT'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+CGATT=1'+'\r\n'))
    time.sleep(0.2)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=3,1,"Contype","GPRS"'+'\r\n'))
    time.sleep(1)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=3,1,"APN","Internet"'+'\r\n'))
    time.sleep(5)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+SAPBR=1,1'+'\r\n'))
    time.sleep(10)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPINIT'+'\r\n'))
    time.sleep(2)
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPPARA = "CID",1'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPPARA="URL","http://blynk-cloud.com/xxxxxxxxxx/update/V1?value='+lon+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPACTION=0'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))
    port.write(str.encode('AT+HTTPREAD'+'\r\n'))
    while(port.readline().decode("utf-8")!=""):
        print(port.readline().decode("utf-8"))

def main():
    list_ports()
    print("Write the port name");
    com_port=str(input())
    lat,lon=coordinates(com_port)
    request_get_lat(lat)
    request_get_lon(lon)

if __name__ == "__main__":
    main()

Output

If you are still facing issue regarding RPI, SIM808 AND BLYNK RESTFUL HTTP GET Feel free to Ask Doubts in the Comment Box Below and Don’t Forget to Follow us on 👍 Social Networks😉