This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

MQTT blocks restarting GPS?

Hi,

I'm writing a program that swaps between LTE and GPS to send the device's location to AWS over MQTT. I am able to connect to LTE and AWS, then swap to GPS and get a fix, and swap back to LTE. However, when I then try to go back to GPS, I get a bsd recoverable error 3, and it appears I cannot communicate over the GPS socket.

If I have everything EXCEPT connecting to MQTT, I am able to freely swap between LTE and GPS.

I've attached my code below:

called by main():

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void test_gps(){
spoof(false);
led_setup();
button_setup();
// Storage
// Set up certificates before nvs or else permission errors
provision_certificates();
nvs_setup();
printk("\nNVS set up\n");
//---
k_sleep(1000);
//// MQTT
int err;
struct mqtt_client client;
struct sockaddr_storage broker;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

GPS code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void GPS_State_Machine(struct nvs_fs* fs){
switch(gps_state){
case GPS_IDLE:
// Record a coordinate every gps_update_rate period
if(k_uptime_get()-t_last_location_recorded >= GPS_Update_Rate(-1)){
t_last_location_recorded = k_uptime_get();
gps_state = RECORD_COORDINATE;
}
break;
case RECORD_COORDINATE:{
// Record a location in memory
toggle_led(1);
update_gps_data(&gps_data);
if(log_data){
save_gps_data(&last_fix, fs);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

AT command swapping code:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int gps_on_lte_off(){
const char* cmds[] = {"AT+CFUN=4", "AT\%XSYSTEMMODE=0,0,1,0", "AT+CFUN=1"};
int at_socket = socket(AF_LTE, 0, NPROTO_AT);
if(at_socket < 0){
printk("Socket error");
return at_socket;
}
char buf[2];
int bytes_received;
s64_t timer;
for(int ix=0; ix < 3; ix++){
if(send(at_socket, cmds[ix], strlen(cmds[ix]), 0) < 0){
printk("send error: %s", cmds[ix]);
return -1;
}
do {
bytes_received = recv(at_socket, buf, 2, 0);
} while (bytes_received == 0);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
***** Booting Zephyr OS build v1.14.99-ncs3-snapshot2 *****
nrf_inbuilt_key_delete(16842753, 0) => result=0
nrf_inbuilt_key_delete(16842753, 1) => result=0
nrf_inbuilt_key_delete(16842753, 2) => result=0
nrf_inbuilt_key_write => result=0
nrf_inbuilt_key_write => result=0
nrf_inbuilt_key_write => result=0
NVS set up
[00:00:04.551,025] <inf> fs_nvs: 3 Sectors of 4096 bytes
[00:00:04.551,025] <inf> fs_nvs: alloc wra: 1, 7e8
[00:00:04.551,025] <inf> fs_nvs: data wra: 1, 604
LTE Link Connecting ...
LTE Link Connected! 0
IPv4 Address 0xaa271203
client_id: nrf-352656100266173
MQTT Connected
Loop starting
gps on
GPS Initialized
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Using the debugger, I was able to find that the error occurred after the call to  retval = nrf_setsockopt(gps_socket, NRF_SOL_GNSS, NRF_SO_GNSS_START, NULL, 0);

What's going wrong?

Thanks.