Hello,
I have some issues with activating the gps-module for the "second-time" through mqtt.
See uart logging below.
First GPS attempt works, socket is created successfully! :), second time I get an error when trying to open the GPS-socket.
Could not init socket (err: -1)
Entering STATE [APP_STATE_CONFIGURED] from [APP_STATE_CONFIGURE_WAIT] Initiating GPS Interval... Entering STATE [APP_STATE_GNSS] from [APP_STATE_CONFIGURED] Entering STATE [APP_STATE_GNSS] from [APP_STATE_GNSS] Starting GPS AT+CFUN=4 +CEREG: 0,"765D","03178709",7,0,0,"11100000","11100000" +CSCON: 0 try send AT commands for GPS AT%XSYSTEMMODE=1,0,1,0 try send AT commands for GPS AT%XCOEX0=1,1,1565,1586 try send AT commands for GPS AT+CFUN=31 try send AT commands for GPS GPS Socket created Getting GPS data... [1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 1, timeout = 20 Searching [|] NMEA strings: ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 2, timeout = 19 Searching [/] NMEA strings: $GPGGA,000016.11,,,,,0,,99.99,,M,0,,*30 $GPGLL,,,,,000016.11,V,N*4D $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000016.11,V,,,,,,,060180,,,N,V*0F ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 3, timeout = 18 Searching [-] NMEA strings: $GPGGA,000018.11,,,,,0,,99.99,,M,0,,*3E $GPGLL,,,,,000018.11,V,N*43 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 4, timeout = 17 Searching [\] NMEA strings: $GPGGA,000019.11,,,,,0,,99.99,,M,0,,*3F $GPGLL,,,,,000019.11,V,N*42 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000019.11,V,,,,,,,060180,,,N,V*00 ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 5, timeout = 16 Searching [|] NMEA strings: $GPGGA,000020.11,,,,,0,,99.99,,M,0,,*35 $GPGLL,,,,,000020.11,V,N*48 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000020.11,V,,,,,,,060180,,,N,V*0A ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 6, timeout = 15 Searching [/] NMEA strings: $GPGGA,000021.11,,,,,0,,99.99,,M,0,,*34 $GPGLL,,,,,000021.11,V,N*49 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000021.11,V,,,,,,,060180,,,N,V*0B ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 7, timeout = 14 Searching [-] NMEA strings: $GPGGA,000022.11,,,,,0,,99.99,,M,0,,*37 $GPGLL,,,,,000022.11,V,N*4A $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000022.11,V,,,,,,,060180,,,N,V*08 ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 8, timeout = 13 Searching [\] NMEA strings: $GPGGA,000023.11,,,,,0,,99.99,,M,0,,*36 $GPGLL,,,,,000023.11,V,N*4B $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000023.11,V,,,,,,,060180,,,N,V*09 ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 9, timeout = 12 Searching [|] NMEA strings: $GPGGA,000024.11,,,,,0,,99.99,,M,0,,*31 $GPGLL,,,,,000024.11,V,N*4C $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000024.11,V,,,,,,,060180,,,N,V*0E ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 11, timeout = 11 Searching [/] NMEA strings: $GPGGA,000025.11,,,,,0,,99.99,,M,0,,*30 $GPGLL,,,,,000025.11,V,N*4D $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000025.11,V,,,,,,,060180,,,N,V*0F ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 12, timeout = 10 Searching [-] NMEA strings: $GPGGA,000026.11,,,,,0,,99.99,,M,0,,*33 $GPGLL,,,,,000026.11,V,N*4E $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000026.11,V,,,,,,,060180,,,N,V*0C ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 13, timeout = 9 Searching [\] NMEA strings: $GPGGA,000027.11,,,,,0,,99.99,,M,0,,*32 $GPGLL,,,,,000027.11,V,N*4F $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000027.11,V,,,,,,,060180,,,N,V*0D ---------------------------------[1;1H[2JTracking: 0 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 14, timeout = 8 Searching [|] NMEA strings: $GPGGA,000028.11,,,,,0,,99.99,,M,0,,*3D $GPGLL,,,,,000028.11,V,N*40 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,0,,,,,,,,,,,,,,,,,1*54 $GPRMC,000028.11,V,,,,,,,060180,,,N,V*02 ---------------------------------[1;1H[2JTracking: 1 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 15, timeout = 7 Searching [/] NMEA strings: $GPGGA,000029.11,,,,,0,,99.99,,M,0,,*3C $GPGLL,,,,,000029.11,V,N*41 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,1,22,,,30,1*56 $GPRMC,000029.11,V,,,,,,,060180,,,N,V*03 ---------------------------------[1;1H[2JTracking: 1 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 16, timeout = 6 Searching [-] NMEA strings: $GPGGA,000030.11,,,,,0,,99.99,,M,0,,*34 $GPGLL,,,,,000030.11,V,N*49 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,1,22,,,28,1*5F $GPRMC,000030.11,V,,,,,,,060180,,,N,V*0B ---------------------------------[1;1H[2JTracking: 1 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 17, timeout = 5 Searching [\] NMEA strings: $GPGGA,000031.11,,,,,0,,99.99,,M,0,,*35 $GPGLL,,,,,000031.11,V,N*48 $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,1,22,,,31,1*57 $GPRMC,000031.11,V,,,,,,,060180,,,N,V*0A ---------------------------------[1;1H[2JTracking: 2 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 18, timeout = 4 Searching [|] NMEA strings: $GPGGA,000032.11,,,,,0,,99.99,,M,0,,*36 $GPGLL,,,,,000032.11,V,N*4B $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,2,22,,,32,25,,,30,1*53 $GPRMC,000032.11,V,,,,,,,060180,,,N,V*09 ---------------------------------[1;1H[2JTracking: 2 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 19, timeout = 3 Searching [/] NMEA strings: $GPGGA,000033.11,,,,,0,,99.99,,M,0,,*37 $GPGLL,,,,,000033.11,V,N*4A $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,2,22,,,32,25,,,31,1*52 $GPRMC,000033.11,V,,,,,,,060180,,,N,V*08 ---------------------------------[1;1H[2JTracking: 2 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 20, timeout = 2 Searching [-] NMEA strings: $GPGGA,000034.11,,,,,0,,99.99,,M,0,,*30 $GPGLL,,,,,000034.11,V,N*4D $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,2,22,,,32,25,,,31,1*52 $GPRMC,000034.11,V,,,,,,,060180,,,N,V*0F ---------------------------------[1;1H[2JTracking: 2 Using: 0 Unhealthy: 0 --------------------------------- Seconds since last fix: 21, timeout = 1 Searching [\] NMEA strings: $GPGGA,000035.11,,,,,0,,99.99,,M,0,,*31 $GPGLL,,,,,000035.11,V,N*4C $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99,1*2D $GPGSV,1,1,2,22,,,32,25,,,31,1*52 $GPRMC,000035.11,V,,,,,,,060180,,,N,V*0E ---------------------------------AT+CFUN=30 AT+CFUN=21 +CEREG: 2,"765D","03178709",7,0,0,"11100000","11100000" +CSCON: 1 +CEREG: 1,"765D","03178709",7,,,"11100000","00001111" AT%SHORTSWVER Modem SW version = %SHORTSWVER: nrf9160_1.2.3 AT+CEREG? Modem attach status = +CEREG: 5,1,"765D","03178709",7,,,"11100000","00001111" AT+CGDCONT? Modem IP status = +CGDCONT: 0,"IP","APN HIDDEN","IP HIDDEN",0,0 Requesting IP address for host IP HIDDEN Entering STATE [APP_STATE_CONFIGURED] from [APP_STATE_GNSS] Timer is not 0 = 70 Timer is not 0 = 70 Timer is not 0 = 70 Timer is not 0 = 69 Timer is not 0 = 69 Timer is not 0 = 68 Timer is not 0 = 68 Timer is not 0 = 67 Timer is not 0 = 67 Timer is not 0 = 66 Timer is not 0 = 66 Timer is not 0 = 65 Timer is not 0 = 65 Timer is not 0 = 64 Timer is not 0 = 64 +CSCON: 0 Timer is not 0 = 63 Timer is not 0 = 63 Timer is not 0 = 62 Timer is not 0 = 62 Timer is not 0 = 61 Timer is not 0 = 61 Timer is not 0 = 60 Timer is not 0 = 60 Timer is not 0 = 59 Timer is not 0 = 59 Timer is not 0 = 58 Timer is not 0 = 58 Timer is not 0 = 57 Timer is not 0 = 57 Timer is not 0 = 56 Timer is not 0 = 56 Timer is not 0 = 55 Timer is not 0 = 55 Timer is not 0 = 54 Timer is not 0 = 54 Timer is not 0 = 53 Timer is not 0 = 53 Timer is not 0 = 52 Timer is not 0 = 52 Timer is not 0 = 51 Timer is not 0 = 51 Timer is not 0 = 50 Timer is not 0 = 50 RT timer triggered, interval is 30 seconds, state = 6 Event = 0, Setting EVENT_RT_TIMER RT timer publish APP_STATE_CONFIGURED +CSCON: 1 Timer is not 0 = 49 Timer is not 0 = 48 Timer is not 0 = 48 Timer is not 0 = 47 Timer is not 0 = 47 Timer is not 0 = 46 Timer is not 0 = 46 Timer is not 0 = 45 Timer is not 0 = 45 Timer is not 0 = 44 Timer is not 0 = 44 Timer is not 0 = 43 Timer is not 0 = 43 Timer is not 0 = 42 Timer is not 0 = 42 Timer is not 0 = 41 +CSCON: 0 Timer is not 0 = 41 Timer is not 0 = 40 Timer is not 0 = 40 Timer is not 0 = 39 Timer is not 0 = 39 Timer is not 0 = 38 Timer is not 0 = 38 Timer is not 0 = 37 Timer is not 0 = 37 Timer is not 0 = 36 Timer is not 0 = 36 Timer is not 0 = 35 Timer is not 0 = 35 Timer is not 0 = 34 Timer is not 0 = 34 Timer is not 0 = 33 Timer is not 0 = 33 Timer is not 0 = 32 Timer is not 0 = 32 Timer is not 0 = 31 Timer is not 0 = 31 Timer is not 0 = 30 Timer is not 0 = 30 Timer is not 0 = 29 Timer is not 0 = 29 Timer is not 0 = 28 Timer is not 0 = 28 Timer is not 0 = 27 Timer is not 0 = 27 Timer is not 0 = 26 Timer is not 0 = 26 Timer is not 0 = 25 Timer is not 0 = 25 Timer is not 0 = 24 Timer is not 0 = 24 Timer is not 0 = 23 Timer is not 0 = 23 Timer is not 0 = 22 Timer is not 0 = 22 Timer is not 0 = 21 Timer is not 0 = 21 Timer is not 0 = 20 Timer is not 0 = 20 RT timer triggered, interval is 30 seconds, state = 6 Event = 0, Setting EVENT_RT_TIMER RT timer publish APP_STATE_CONFIGURED +CSCON: 1 Timer is not 0 = 19 Timer is not 0 = 19 Timer is not 0 = 18 Timer is not 0 = 18 Timer is not 0 = 17 Timer is not 0 = 17 Timer is not 0 = 16 Timer is not 0 = 16 Timer is not 0 = 15 Timer is not 0 = 15 Timer is not 0 = 14 Timer is not 0 = 14 Timer is not 0 = 13 Timer is not 0 = 13 Timer is not 0 = 12 +CSCON: 0 Timer is not 0 = 12 Timer is not 0 = 11 Timer is not 0 = 11 Timer is not 0 = 10 Timer is not 0 = 10 Timer is not 0 = 9 Timer is not 0 = 9 Timer is not 0 = 8 Timer is not 0 = 8 Timer is not 0 = 7 Timer is not 0 = 7 Timer is not 0 = 6 Timer is not 0 = 6 Timer is not 0 = 5 Timer is not 0 = 5 Timer is not 0 = 4 Timer is not 0 = 3 Timer is not 0 = 3 Timer is not 0 = 2 Timer is not 0 = 2 Timer is not 0 = 1 Timer is not 0 = 1 Initiating GPS Interval... Entering STATE [APP_STATE_GNSS] from [APP_STATE_CONFIGURED] Entering STATE [APP_STATE_GNSS] from [APP_STATE_GNSS] Starting GPS AT+CFUN=4 +CSCON: 1 +CEREG: 0,"765D","03178709",7,0,0,"11100000","11100000" +CSCON: 0 try send AT commands for GPS AT%XSYSTEMMODE=1,0,1,0 try send AT commands for GPS AT%XCOEX0=1,1,1565,1586 try send AT commands for GPS AT+CFUN=31 try send AT commands for GPS Could not init socket (err: -1) AT%SHORTSWVER Modem SW version = %SHORTSWVER: nrf9160_1.2.3 AT+CEREG? Modem attach status = +CEREG: 5,0,"765D","03178709",7,0,0,"11100000","11100000" AT+CGDCONT? Modem IP status = Requesting IP address for host IP HIDDEN .....RT timer triggered, interval is 30 seconds, state = 7 RT timer ignored, we are in state FOTA or GNSS RT timer triggered, interval is 30 seconds, state = 7
GPS module can both be activated with a command over mqtt, and a configuration parameter with a timeout value,
so that the firmware can re-initialize a new fix as-soon the timer runs == "0", thats why there is a timer in the logg above.
The GPS function is triggered as a state, se below ->
case APP_STATE_GNSS: app_application_gnss();
void app_application_gnss(void)
{
app_mqtt_run();
app_mqtt_disconnect();
hal_gps_init();
hal_gps_get_fix();
k_sleep(K_SECONDS(2)); // Give the modem some time to re-connect
app_mqtt_init();
app_mqtt_run();
app_es400_subscibe_to_topics(); // Subscribe TOPICS
app_es400_packet_gps_handler();
//app_services.gps.interval_counter = 40;
app_services.gps.interval_counter = app_services.gps.interval;
main_change_statemachine_state(APP_STATE_CONFIGURED);
}
void app_mqtt_run(void)
{
int err = poll(&fds, 1, 500);
#if 0
uint32_t _mqtt_keepalive_time_left = mqtt_keepalive_time_left(&client);
printk("mqtt_keepalive_time_left = %d\r\n", _mqtt_keepalive_time_left);
int err = poll(&fds, 1, _mqtt_keepalive_time_left); //mqtt_keepalive_time_left(&client)); // 1000); //
#endif
if (err < 0) {
LOG_ERR("poll: %d", errno);
return;
}
err = mqtt_live(&client);
if ((err != 0) && (err != -EAGAIN))
{
LOG_ERR("ERROR: mqtt_live: %d", err);
if (err == -128)
{
sys_reboot(SYS_REBOOT_COLD); // Reboot the system if we get this MQTT error
}
return;
}
if ((fds.revents & POLLIN) == POLLIN)
{
err = mqtt_input(&client);
if (err != 0)
{
LOG_ERR("mqtt_input: %d", err);
return;
}
}
if ((fds.revents & POLLERR) == POLLERR)
{
LOG_ERR("POLLERR");
return;
}
if ((fds.revents & POLLNVAL) == POLLNVAL)
{
LOG_ERR("POLLNVAL");
return;
}
}
void app_mqtt_disconnect(void)
{
mqtt_disconnect(&client);
}
uint8_t hal_gps_init(void)
{
memset(gnss_lat, 0x00, GNSS_LAT_LNG_SIZE);
memset(gnss_lng, 0x00, GNSS_LAT_LNG_SIZE);
return(0);
}
int hal_gps_get_fix(void)
{
nrf_gnss_data_frame_t gps_data;
uint8_t cnt = 0;
#ifdef CONFIG_SUPL_CLIENT_LIB
static struct supl_api supl_api = {
.read = supl_read,
.write = supl_write,
.handler = inject_agps_type,
.logger = supl_logger,
.counter_ms = k_uptime_get
};
#endif
fix_timestamp = k_uptime_get();
printk("Starting GPS\r\n");
if (init_app() != 0) {
return -1;
}
#ifdef CONFIG_SUPL_CLIENT_LIB
int rc = supl_init(&supl_api);
if (rc != 0) {
return rc;
}
#endif
printk("Getting GPS data...\n");
uint32_t fix_counter = app_cfg.gps_fixcounter; // How many verified GPS positions before OK
uint32_t timeout = app_cfg.gps_timeout; // Timeout in seconds
snprintf(gnss_lat, sizeof(gnss_lat), " ");
snprintf(gnss_lng, sizeof(gnss_lng), " ");
gnss_latitude = 0.0;
gnss_longitude = 0.0;
#if 0
timeout = 5;
printk("Adjusting timeout to %d seconds\r\n", timeout);
snprintf(gnss_lat, sizeof(gnss_lat), "%f", 59.12345678F);
snprintf(gnss_lng, sizeof(gnss_lng), "%f", 10.12345678F);
#endif
while (timeout > 0)
{
do
{
/* Loop until we don't have more
* data to read
*/
} while (process_gps_data(&gps_data) > 0);
if (IS_ENABLED(CONFIG_GPS_SAMPLE_NMEA_ONLY))
{
print_nmea_data();
nmea_string_cnt = 0;
} else
{
printk("\033[1;1H");
printk("\033[2J");
print_satellite_stats(&last_pvt);
print_gnss_stats(&last_pvt);
printk("---------------------------------\n");
if (!got_fix)
{
printk("Seconds since last fix: %lld, timeout = %d\n", ((k_uptime_get() - fix_timestamp) / 1000), timeout);
cnt++;
printk("Searching [%c]\n", update_indicator[cnt%4]);
} else
{
print_fix_data(&last_pvt);
if (fix_counter > 0)
{
fix_counter--;
} else
{
nrf_gnss_data_frame_t *pvt_data = &last_pvt;
printk("Using LAT/LNG:\r\n");
printf("Longitude: %f\n", pvt_data->pvt.longitude);
printf("Latitude: %f\n", pvt_data->pvt.latitude);
snprintf(gnss_lat, sizeof(gnss_lat), "%f", pvt_data->pvt.latitude);
snprintf(gnss_lng, sizeof(gnss_lng), "%f", pvt_data->pvt.longitude);
gnss_latitude = pvt_data->pvt.latitude;
gnss_longitude = pvt_data->pvt.longitude;
break;
}
}
printk("\nNMEA strings:\n\n");
print_nmea_data();
printk("---------------------------------");
}
watchdog_feed();
k_msleep(1000);
timeout--;
}
hal_gps_setup_modem_for_lte();
return 0;
}
uint8_t app_es400_subscibe_to_topics(void)
{
uint8_t err = app_mqtt_subscribe(subscribe_topics, ES400_TOPICS_TABLE_SIZE);
k_sleep(K_MSEC(100));
return(err);
}
uint8_t app_es400_packet_gps_handler(void)
{
int err = 1;
double lat;
double lon;
snprintf(&publish_topic_tmp[0], PUBLISH_TOPIC_TMP_SIZE, "%s%s", es400_topic_prefix, es400_publish_subtopics_table[PUBLISH_TOPIC_25].sub_topic); // Build the topic
cJSON *cjson_object = cJSON_CreateObject();
cJSON *cjson_gnss_object;
cJSON_AddItemToObject(cjson_object, "timestamp", cJSON_CreateString("0"));
cJSON *arrayJson = cJSON_CreateArray();
cJSON_AddItemToObject(cjson_object, "location", cjson_gnss_object = cJSON_CreateObject());
hal_gps_read_position(&lat, &lon);
cJSON_AddNumberToObject(cjson_gnss_object, "lat", lat);
cJSON_AddNumberToObject(cjson_gnss_object, "lon", lon);
cjson_string = cJSON_Print(cjson_object);
if (cjson_string == NULL)
{
printk("Failed to print cJSON\r\n");
} else
{
snprintf(publish_data_tmp, PUBLISH_DATA_TMP_SIZE, "%s", cjson_string);
//printk("Publishing cJSON: %s to topic: %s\r\n", publish_data_tmp, publish_topic_tmp);
err = 0;
free(cjson_string);
}
cJSON_Delete(cjson_object);
cJSON_Delete(cjson_gnss_object);
cJSON_Delete(arrayJson);
if (err == 0)
{
err = app_mqtt_data_publish(&publish_topic_tmp[0], (uint8_t *)&publish_data_tmp[0], (size_t)strlen(publish_data_tmp));
}
if (cjson_string != NULL)
{
free(cjson_string);
}
}
/* "/sensor/batt_voltage" External battery voltage
{
"sensor_id" : "batt_voltage"
"value" : "13.7"
} */
uint8_t app_es400_sensor_batt_handler(void)
{
int err = 1;
float vbat;
//char tmp[30];
if (hal_adc_read_vbat(&vbat) == 0)
{
//snprintf(tmp, 30, "%.1f", vbat);
err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_05, "sensor_id", "batt_voltage", "value", (double)vbat);
}
return(err);
}
/* "/sensor/imei"
{
"sensor_id" : "imei"
"value" : "12345678"
} */
uint8_t app_es400_sensor_imei_handler(void)
{
int err = 1;
char tmp[30];
if (hal_modem_imei(tmp, 30) == 0)
{
err = app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_06, "sensor_id", "imei", "value", tmp);
}
return(err);
}
/* "/sensor/iccid"
{
"sensor_id" : "iccid"
"value" : 12345678
} */
uint8_t app_es400_sensor_iccid_handler(void)
{
int err = 1;
char tmp[28];
//char tmp2[30];
if (hal_modem_iccid(tmp, 28) == 0)
{
//snprintf(tmp2, 30, "\"%s\"", tmp);
err = app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_07, "sensor_id", "iccid", "value", tmp);
}
return(err);
}
/* "/sensor/rsrp" modem Reference signal received power (RSRP)
{
"sensor_id" : "rsrp"
"value" : 100
} */
uint8_t app_es400_sensor_rsrp_handler(void)
{
int err = 1;
int rsrp;
char tmp[30];
if (hal_modem_rsrp(tmp, 30) == 0)
{
sscanf(tmp, "%d", &rsrp);
err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_08, "sensor_id", "rsrp", "value", (double)rsrp);
}
return(err);
}
/* "/sensor/imsi"
{
"sensor_id" : "imsi"
"value" : "242016001046705"
} */
uint8_t app_es400_sensor_imsi_handler(void)
{
int err = 1;
char tmp[30];
if (hal_modem_imsi(tmp, 30) == 0)
{
err = app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_09, "sensor_id", "imsi", "value", tmp);
}
return(err);
}
/* "/sensor/xvbat" Modem voltage
{
"sensor_id" : "xvbat"
"value" : "3.3"
} */
uint8_t app_es400_sensor_xvbat_handler(void)
{
int err = 1;
float vbat;
char tmp[20];
if (hal_modem_vbat(tmp, 20) == 0)
{
sscanf(tmp, "%f", &vbat);
err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_10, "sensor_id", "xvbat", "value", (double)vbat);
}
return(err);
}
/* "/sensor/xtemp" Modem Internal temperature
{
"sensor_id" : "xtemp"
"value" : "20"
} */
uint8_t app_es400_sensor_xtemp_handler(void)
{
int err = 1;
float temp;
char tmp[20];
if (hal_modem_temp(tmp, 20) == 0)
{
sscanf(tmp, "%f", &temp);
err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_11, "sensor_id", "xtemp", "value", (double)temp);
}
return(err);
}
/* "/sensor/plmn"
{
"sensor_id" : "plmn"
"value" : "1"
} */
uint8_t app_es400_sensor_plmn_handler(void)
{
int err = 1;
char tmp[20];
if (hal_modem_plmn(tmp, 20) == 0)
{
err = app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_12, "sensor_id", "plmn", "value", tmp);
}
return(err);
}
/* "/sensor/radaralarm"
{
"sensor_id" : "radar",
"value" : "30"
} */
uint8_t app_es400_sensor_radaralarm_handler(void)
{
// char tmp[20];
int err = 0;
if (app_services.current_speed != 1000)
{
//snprintf(tmp, 20, "%d", app_services.current_speed);
err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_13, "sensor_id", "radaralarm", "value", app_services.current_speed);
}
return(err);
}
/* "/sensor/radar"
{
"sensor_id" : "radar",
"values" : [10,15,20,...]
} */
uint8_t app_es400_sensor_radar_handler(void)
{
int err = 0;
// app_es400_cjson_build_one_value_publish(PUBLISH_TOPIC_14, "sensor_id", "radar", "value", "15,20,25");
app_radar_array_print((char *)radar_json_result, RADAR_JSON_RESULT_BUFFER_SIZE);
app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_14, "sensor_id", "radar", "value", radar_json_result);
return(err);
}
/* "/sensor/operid" USIM Operator ID
{
"sensor_id" : "operid"
"value" : "1"
} */
uint8_t app_es400_sensor_operid_handler(void)
{
int err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_15, "sensor_id", "operid", "value", (double)1);
return(err);
}
/* "/sensor/product" Product type (PCB_VERSION)
{
"sensor_id" : "product"
"value" : "1"
} */
uint8_t app_es400_sensor_product_handler(void)
{
int err = app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_16, "sensor_id", "product", "value", main_get_app_product());
return(err);
}
/* "/sensor/version" [16] Firmware version
{
"sensor_id" : "version"
"value" : "1"
} */
uint8_t app_es400_sensor_version_handler(void)
{
int err = app_es400_cjson_build_one_string_publish(PUBLISH_TOPIC_17, "sensor_id", "version", "value", main_get_app_version());
return(err);
}
/* "/sensor/mqtt_counter"
{
"sensor_id" : "mqtt_counter",
"value" : "123"
} */
uint8_t app_es400_sensor_mqtt_counter_handler(void)
{
//char tmp[20];
printk("MQTT counter = %d\r\n", app_cfg.mqtt_counter);
//snprintf(tmp, 20, "%d", app_cfg.mqtt_counter);
int err = app_es400_cjson_build_one_number_publish(PUBLISH_TOPIC_18, "sensor_id", "mqtt_counter", "value", (double)app_cfg.mqtt_counter);
app_cfg.mqtt_counter++;
return(err);
}
/*\brief Build cJSON object and publish on MQTT topic STRING
*/
uint8_t app_es400_cjson_build_one_string_publish(uint8_t topic_id, char *id, char *id_value, char *value_id, char *string)
{
int err = 1;
snprintf(&publish_topic_tmp[0], PUBLISH_TOPIC_TMP_SIZE, "%s%s", es400_topic_prefix, es400_publish_subtopics_table[topic_id].sub_topic); // Build the topic
cJSON *cjson_object = cJSON_CreateObject();
if (cJSON_AddStringToObject(cjson_object, id, id_value) != NULL)
{
if (cJSON_AddStringToObject(cjson_object, value_id, string) != NULL)
{
cjson_string = cJSON_Print(cjson_object);
if (cjson_string == NULL)
{
printk("Failed to print cJSON\r\n");
} else
{
snprintf(publish_data_tmp, PUBLISH_DATA_TMP_SIZE, "%s", cjson_string);
printk("Publishing cJSON: %s to topic: %s\r\n", publish_data_tmp, publish_topic_tmp);
err = 0;
free(cjson_string);
}
cJSON_Delete(cjson_object);
}
}
if (err == 0)
{
err = app_mqtt_data_publish(&publish_topic_tmp[0], (uint8_t *)&publish_data_tmp[0], (size_t)strlen(publish_data_tmp));
}
return(err);
}
/*\brief Build cJSON object and publish on MQTT topic one NUMBER
*/
uint8_t app_es400_cjson_build_one_number_publish(uint8_t topic_id, char *id, char *id_value, char *value_id, double number)
{
int err = 1;
snprintf(&publish_topic_tmp[0], PUBLISH_TOPIC_TMP_SIZE, "%s%s", es400_topic_prefix, es400_publish_subtopics_table[topic_id].sub_topic); // Build the topic
cJSON *cjson_object = cJSON_CreateObject();
if (cJSON_AddStringToObject(cjson_object, id, id_value) != NULL)
{
if (cJSON_AddNumberToObject(cjson_object, value_id, number) != NULL)
{
cjson_string = cJSON_Print(cjson_object);
if (cjson_string == NULL)
{
printk("Failed to print cJSON\r\n");
} else
{
snprintf(publish_data_tmp, PUBLISH_DATA_TMP_SIZE, "%s", cjson_string);
printk("Publishing cJSON: %s to topic: %s\r\n", publish_data_tmp, publish_topic_tmp);
err = 0;
free(cjson_string);
}
cJSON_Delete(cjson_object);
}
}
if (err == 0)
{
err = app_mqtt_data_publish(&publish_topic_tmp[0], (uint8_t *)&publish_data_tmp[0], (size_t)strlen(publish_data_tmp));
}
return(err);
}
/*\brief Publish string on MQTT topic
*/
uint8_t app_es400_string_publish(uint8_t topic_id, char *string)
{
int err = 1;
snprintf(&publish_topic_tmp[0], PUBLISH_TOPIC_TMP_SIZE, "%s%s", es400_topic_prefix, es400_publish_subtopics_table[topic_id].sub_topic); // Build the topic
printk("Publishing cJSON: %s to topic: %s\r\n", publish_topic_tmp, string);
err = app_mqtt_data_publish(&publish_topic_tmp[0], (uint8_t *)string, (size_t)strlen(string));
return(err);
}
/*!\brief BOOT package - transmitted after LTE and MQTT is connected
IMEI. IMSI, ICCID, RSRP, XVBAT, XTEMP, PLMN, OPERID, PRODUCT_TYPE, FW_VERSION
*/
uint8_t app_es400_boot_package_publish(void)
{
/*
app_es400_sensor_imei_handler();
app_mqtt_run();
app_es400_sensor_iccid_handler();
app_mqtt_run();
app_es400_sensor_rsrp_handler();
app_mqtt_run();
app_es400_sensor_imsi_handler();
app_mqtt_run();
app_es400_sensor_xvbat_handler();
app_mqtt_run();
app_es400_sensor_xtemp_handler();
app_mqtt_run();
app_es400_sensor_plmn_handler();
app_mqtt_run();
app_es400_sensor_operid_handler();
app_mqtt_run();
app_es400_sensor_product_handler();
app_mqtt_run();
app_es400_sensor_version_handler();
app_mqtt_run();
*/
app_es400_packet_handler();
app_mqtt_run();
return(0);
}
/*!\brief RT timer, periodic update without configuration
*/
uint8_t app_es400_rt_timer_publish(int state)
{
int err = 1;
if (state == APP_STATE_CONFIGURE_WAIT)
{
printk("RT timer publish APP_STATE_CONFIGURE_WAIT\r\n");
app_es400_services_setup_status_handler(); // Send our configuration needed request
} else
{
printk("RT timer publish APP_STATE_CONFIGURED\r\n");
app_es400_rt_timer_package_publish();
}
return(err);
}
/*!\brief RT timer package - transmitted on RT timer
*/
uint8_t app_es400_rt_timer_package_publish(void)
{
app_es400_packet_handler();
app_mqtt_run();
/*
app_es400_packet_gps_handler();
app_mqtt_run();
// app_es400_sensor_imei_handler();
// app_mqtt_run();
// app_es400_sensor_iccid_handler();
// app_mqtt_run();
// app_es400_sensor_rsrp_handler();
// app_mqtt_run();
// app_es400_sensor_imsi_handler();
// app_mqtt_run();
// app_es400_sensor_xvbat_handler();
// app_mqtt_run();
// app_es400_sensor_xtemp_handler();
// app_mqtt_run();
// app_es400_sensor_plmn_handler();
// app_mqtt_run();
// app_es400_sensor_operid_handler();
// app_mqtt_run();
// app_es400_sensor_product_handler();
// app_mqtt_run();
// app_es400_sensor_version_handler();
// app_mqtt_run();
/*
char *radar_error = "0"; // TODO get actual data from radar
app_es400_error_radar_handler(radar_error);
app_mqtt_run();
if (app_services.ps.error > 0)
{
app_es400_error_vms_handler(app_services.ps.error); // TODO get actual data from VMS
app_mqtt_run();
}
*/
/*
app_es400_sensor_gps_handler();
app_mqtt_run();
app_es400_sensor_gps_handler();
app_mqtt_run();
app_es400_sensor_temp_handler();
app_mqtt_run();
app_es400_sensor_batt_handler();
app_mqtt_run();
app_es400_sensor_4_20ma_handler();
app_mqtt_run();
app_es400_packet_handler();
app_mqtt_run();
app_es400_sensor_radaralarm_handler();
app_mqtt_run();
app_es400_sensor_radar_handler();
app_mqtt_run();
app_es400_sensor_mqtt_counter_handler();
app_mqtt_run();
*/
return(0);
}
/*\brief Build cJSON object and publish on MQTT topic GNSS STRING
"/sensor/gps"
{
"sensor_id":"gps",
"location" :
{
"lat" : 63.36,
"lon" : 10.37
}
{
"sensor_id": "gps",
"location": {
"lat": 59.123456779999998,
"lon": 10.12345678
}
}
*/
uint8_t app_es400_cjson_build_gnss_publish(void)
{
int err = 1;
double lat;
double lon;
snprintf(&publish_topic_tmp[0], PUBLISH_TOPIC_TMP_SIZE, "%s%s", es400_topic_prefix, es400_publish_subtopics_table[PUBLISH_TOPIC_03].sub_topic); // Build the topic
cJSON *cjson_object = cJSON_CreateObject();
cJSON *cjson_gnss_object;
cJSON_AddItemToObject(cjson_object, "sensor_id", cJSON_CreateString("gps"));
cJSON_AddItemToObject(cjson_object, "location", cjson_gnss_object = cJSON_CreateObject());
// TEST *****************************************
// hal_gps_read_position_test(&lat, &lon);
hal_gps_read_position(&lat, &lon);
cJSON_AddNumberToObject(cjson_gnss_object, "lat", lat);
cJSON_AddNumberToObject(cjson_gnss_object, "lon", lon);
cjson_string = cJSON_Print(cjson_object);
if (cjson_string == NULL)
{
printk("Failed to print cJSON\r\n");
} else
{
snprintf(publish_data_tmp, PUBLISH_DATA_TMP_SIZE, "%s", cjson_string);
//printk("Publishing cJSON: %s to topic: %s\r\n", publish_data_tmp, publish_topic_tmp);
err = 0;
}
cJSON_Delete(cjson_object);
if (err == 0)
{
err = app_mqtt_data_publish(&publish_topic_tmp[0], (uint8_t *)&publish_data_tmp[0], (size_t)strlen(publish_data_tmp));
}
if (cjson_string != NULL)
{
free(cjson_string);
}
return(err);
}
static int hal_gps_setup_modem_for_gps(void)
{
for (int i = 0; i < ARRAY_SIZE(at_commands_gps); i++)
{
printk("%s ", at_commands_gps[i]);
if (at_cmd_write(at_commands_gps[i], NULL, 0, NULL) != 0)
{
printk("ERROR\r\n");
printk("Feiler i hal_gps_setup_modem_for_gps\r\n");
return -1;
}
printk("try send AT commands for GPS, but nothing here\r\n");
}
return 0;
}
static int hal_gps_setup_modem_for_lte(void)
{
for (int i = 0; i < ARRAY_SIZE(at_commands_lte); i++)
{
printk("%s ", at_commands_lte[i]);
if (at_cmd_write(at_commands_lte[i], NULL, 0, NULL) != 0)
{
printk("ERROR\r\n");
printk("Feiler i hal_gps_setup_modem_for_lte\r\n");
return -1;
}
printk("\r\n");
}
return 0;
}
static int gnss_ctrl(uint32_t ctrl)
{
int retval;
nrf_gnss_fix_retry_t fix_retry = 0;
nrf_gnss_fix_interval_t fix_interval = 1;
nrf_gnss_delete_mask_t delete_mask = 0;
nrf_gnss_nmea_mask_t nmea_mask = NRF_GNSS_NMEA_GSV_MASK |
NRF_GNSS_NMEA_GSA_MASK |
NRF_GNSS_NMEA_GLL_MASK |
NRF_GNSS_NMEA_GGA_MASK |
NRF_GNSS_NMEA_RMC_MASK;
if (ctrl == GNSS_INIT_AND_START) {
gnss_fd = nrf_socket(NRF_AF_LOCAL,
NRF_SOCK_DGRAM,
NRF_PROTO_GNSS);
if (gnss_fd >= 0) {
printk("GPS Socket created\n");
}
else {
printk("Could not init socket (err: %d)\n", gnss_fd);
return -1;
}
retval = nrf_setsockopt(gnss_fd,
NRF_SOL_GNSS,
NRF_SO_GNSS_FIX_RETRY,
&fix_retry,
sizeof(fix_retry));
if (retval != 0) {
printk("Failed to set fix retry value\n");
return -1;
}
retval = nrf_setsockopt(gnss_fd,
NRF_SOL_GNSS,
NRF_SO_GNSS_FIX_INTERVAL,
&fix_interval,
sizeof(fix_interval));
if (retval != 0) {
printk("Failed to set fix interval value\n");
return -1;
}
retval = nrf_setsockopt(gnss_fd,
NRF_SOL_GNSS,
NRF_SO_GNSS_NMEA_MASK,
&nmea_mask,
sizeof(nmea_mask));
if (retval != 0) {
printk("Failed to set nmea mask\n");
return -1;
}
}
if ((ctrl == GNSS_INIT_AND_START) ||
(ctrl == GNSS_RESTART)) {
retval = nrf_setsockopt(gnss_fd,
NRF_SOL_GNSS,
NRF_SO_GNSS_START,
&delete_mask,
sizeof(delete_mask));
if (retval != 0) {
printk("Failed to start GPS\n");
return -1;
}
}
if (ctrl == GNSS_STOP) {
retval = nrf_setsockopt(gnss_fd,
NRF_SOL_GNSS,
NRF_SO_GNSS_STOP,
&delete_mask,
sizeof(delete_mask));
if (retval != 0) {
printk("Failed to stop GPS\n");
return -1;
}
}
return 0;
}
Inside function; static int gnss_ctrl(uint32_t ctrl) it fails for the second time.
returing error -1 and text ->
printk("Could not init socket (err: %d)\n", gnss_fd);
return -1;
What am I doing wrong here?