Google, unfortunately for many users of GSM-modules, 2-3 months ago blocked and transferred to a paid basis the API for determining the location by the coordinates of the cell towers visible by the module. Because of this, on modules of the SIM800 series manufactured
Let's skip how Yandex gets the coordinates of cell towers, the main thing is that we can use this free service and get the following data: Latitude, Longitude, Altitude, as well as an approximate error for each parameter. The main purpose of the article is to give a small tutorial on how to quickly switch to the API from Yandex, instead of the no longer available service from Google.
Below, as an example, only the latitude and longitude of the location of the module will be obtained.
So let's get started
First you need to read the user agreement located at:
Go to address
Write down or otherwise save the received key.
After completing these steps, access to the page will be obtained.
To generate an XML request in cURL format to Yandex.Locator services, you will need to obtain information on cell towers “visible” by the module:
- countrycode - country code
- operatorid - mobile network code
- cellid - cell identifier
- lac - location code
This information can be obtained from the module by sending the 'AT+CNETSCAN' command.
Information received from the module
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:59,Cellid:2105,Arfcn:96,Lac:1E9E,Bsic:31<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:54,Cellid:2107,Arfcn:18,Lac:1E9E,Bsic:3A<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:45,Cellid:10A9,Arfcn:97,Lac:1E9E,Bsic:11<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:41,Cellid:2108,Arfcn:814,Lac:1E9E,Bsic:1F<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:43,Cellid:5100,Arfcn:13,Lac:1E9E,Bsic:2B<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:39,Cellid:5102,Arfcn:839,Lac:1E9E,Bsic:1A<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:38,Cellid:2106,Arfcn:104,Lac:1E9E,Bsic:0A<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:37,Cellid:0FE7,Arfcn:12,Lac:1E9E,Bsic:24<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:44,Cellid:14C8,Arfcn:91,Lac:1E9E,Bsic:24<CR><LF>
Operator:"MegaFon",MCC:250,MNC:02,Rxlev:37,Cellid:04B3,Arfcn:105,Lac:1E9E,Bsic:3A<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:47,Cellid:29A0,Arfcn:70,Lac:39BA,Bsic:09<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:43,Cellid:0FDD,Arfcn:590,Lac:39BA,Bsic:09<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:44,Cellid:29A1,Arfcn:84,Lac:39BA,Bsic:10<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:40,Cellid:8F95,Arfcn:81,Lac:39BA,Bsic:03<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:43,Cellid:0FDF,Arfcn:855,Lac:39BA,Bsic:24<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:37,Cellid:299C,Arfcn:851,Lac:39BA,Bsic:17<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:37,Cellid:0FDE,Arfcn:852,Lac:39BA,Bsic:1B<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:35,Cellid:299F,Arfcn:72,Lac:39BA,Bsic:10<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:33,Cellid:28A5,Arfcn:66,Lac:396D,Bsic:25<CR><LF>
Operator:"Bee Line GSM",MCC:250,MNC:99,Rxlev:33,Cellid:2A8F,Arfcn:71,Lac:39BA,Bsic:23<CR><LF>
Operator:"MOTIV",MCC:250,MNC:20,Rxlev:46,Cellid:39D2,Arfcn:865,Lac:4D0D,Bsic:14<CR><LF>
Operator:"MOTIV",MCC:250,MNC:20,Rxlev:36,Cellid:09EE,Arfcn:866,Lac:4D0D,Bsic:25<CR><LF>
Operator:"MOTIV",MCC:250,MNC:20,Rxlev:28,Cellid:09ED,Arfcn:869,Lac:4D0D,Bsic:22<CR><LF>
Operator:"MOTIV",MCC:250,MNC:20,Rxlev:28,Cellid:09EF,Arfcn:861,Lac:4D0D,Bsic:17<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:66,Cellid:58FE,Arfcn:1021,Lac:00EC,Bsic:0A<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:50,Cellid:58FD,Arfcn:1016,Lac:00EC,Bsic:08<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:49,Cellid:58FF,Arfcn:1023,Lac:00EC,Bsic:09<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:46,Cellid:F93B,Arfcn:59,Lac:00EC,Bsic:20<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:50,Cellid:381B,Arfcn:1020,Lac:00EC,Bsic:0A<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:37,Cellid:3819,Arfcn:42,Lac:00EC,Bsic:08<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:34,Cellid:4C0F,Arfcn:43,Lac:00EC,Bsic:0A<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:33,Cellid:0817,Arfcn:26,Lac:00EC,Bsic:27<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:34,Cellid:3A5D,Arfcn:1017,Lac:00E9,Bsic:34<CR><LF>
Operator:"MTS",MCC:250,MNC:01,Rxlev:33,Cellid:3D05,Arfcn:1018,Lac:00EC,Bsic:1F<CR><LF>
It is important to note that in the future it will be necessary to convert the data from the module's answer about Cellid and Lac from hexadecimal to decimal format.
Now we need to generate XML data for accessing the Yandex server, which, later, will be combined into one element.
data table
Data
Comment
xml=<ya_lbs_request><common><version>1.0</version><api_key>
...
This should contain an 88-digit key received from Yandex
</api_key></common>
<gsm_cells>
<cell><countrycode>
250
Country Code (MCC)
</countrycode><operatorid>
2
Operator Code (MNC)
</operatorid><cellid>
8453
Cellid of the first tower in the list received from the module and converted from a number with base 16 to a number with base 10 (the value received from the module is 2105)
</cellid><lac>
7838
Lac of the first tower, also converted from a number with base 16 to a number with base 10 (the value received from the module is 1E9E)
</lac></cell>
...
The group, united by the cell tag, can be repeated as many times as necessary to increase the reliability of a certain location
</gsm_cells>
<ip><address_v4>
10.137.92.60
The IP address assigned to the module by the network after opening the GPRS context can be obtained by sending the command 'AT+SAPBR=2,1' to the module – see below
</address_v4></ip></ya_lbs_request>
Thus, an XML message with a length of 1304 characters of the following form will be generated:
Message
xml=<ya_lbs_request><common><version>1.0</version><api_key>{здесь необходимо указать свой ключ}</api_key></common><gsm_cells><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>8453</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>8455</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>4265</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>8456</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>20736</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>20738</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>8454</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>4071</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>5320</cellid><lac>7838</lac></cell><cell><countrycode>250</countrycode><operatorid>2</operatorid><cellid>1203</cellid><lac>7838</lac></cell></gsm_cells><ip><address_v4>10.137.92.60</address_v4></ip></ya_lbs_request>
This message was generated on the database of cell towers of the Megafon operator, it can be supplemented with data, incl. on other towers visible to the module received by the 'AT + CNETSCAN' command to increase the reliability of the output coordinates.
Working with the module and getting current coordinates
AT-log of work with the module
>AT+SAPBR=3,1,”Contype”,”GPRS” // конфигурирование профиля доступа в Интернет
<OK
>AT+SAPBR=3,1,”APN”,”internet” // конфигурирование APN
<OK
>AT+SAPBR=1,1 // запрос на открытие GPRS контекста
<OK // контекст открыт
>AT+SAPBR=2,1 // запрос текущего IP адреса присвоенного оператором сотовой связи
<+SAPBR: 1,1,”10.137.92.60” // данный IP адрес потребуется вставить в XML-сообщение
<
<OK
>AT+HTTPINIT
<OK
>AT+HTTPPARA=”CID”,1
<OK
>AT+HTTPPARA=”URL”,”http://api.lbs.yandex.net/geolocation”
<OK
>AT+HTTPDATA=1304,10000 // первое число – длина сформированного XML-сообщения
<DOWNLOAD // приглашение к вводу XML-сообщения
< // вводим сформированное нами XML-сообщение
<OK
>AT+HTTPACTION=1
<OK
<
<+HTTPACTION: 1,200,303 // 200 – сообщение отправлено, 303 – получено 303 байт данных
>AT+HTTPREAD=81,10
<+HTTPREAD: 10
<60.0330963 // широта на которой расположен модуль
<OK
>AT+HTTPREAD=116,10
<+HTTPREAD: 10
<30.2484303 // долгота на которой расположен модуль
>AT+HTTPTERM
<OK
Thus, we got the current coordinates of the module: 60.0330963, 30.2484304.
With an increase in the number of data sent by cell towers, the accuracy of positioning will increase proportionally.
More detailed information about the content of the response from the Yandex.Locator service and the selection of the data you need can be found at the link:
Conclusion
I hope this material will be a good help to developers. Ready to answer your questions in the comments.
Source: habr.com