rt-thread如何上传GPS的经纬度到onenet?

发布于 2020-08-16 14:56:41

想把GPS的经纬度上传给onenet,平台上可以看到位置信息的数据点显示
image.png
但并没有显示百度地图
相关代码
image.png

查看更多

关注者
0
被浏览
141
3 个回答
921742079
921742079 2020-08-16

你有没有用这个gps模块手动拿到经纬度,然后在百度地图那边用经纬度查一下准不准?

KJUS_6193
KJUS_6193 2020-08-16

改装了两个函数

 rt_err_t onenet_mqtt_get_string_gpsDIY( char* lat,char* lon,char **out_buff, size_t *length)
{
    rt_err_t result = RT_EOK;
    cJSON *root = RT_NULL;
    cJSON *pvalue = RT_NULL;
    char *msg_str = RT_NULL;

   /* RT_ASSERT(ds_name);
    RT_ASSERT(str);*/
    RT_ASSERT(out_buff);
    RT_ASSERT(length);

    root = cJSON_CreateObject();
    pvalue = cJSON_CreateObject();
    if (!root)
    {
        LOG_E("MQTT publish string data failed! cJSON create object error return NULL!");
        return -RT_ENOMEM;
    }

    cJSON_AddStringToObject(pvalue, "lat", lat);
    cJSON_AddStringToObject(pvalue, "lon", lon);
    cJSON_AddItemToObject(root,"loc",pvalue);
    /* render a cJSON structure to buffer */
    msg_str = cJSON_PrintUnformatted(root);
    if (!msg_str)
    {
        LOG_E("MQTT publish string data failed! cJSON print unformatted error return NULL!");
        result = -RT_ENOMEM;
        goto __exit;
    }

    *out_buff = ONENET_MALLOC(strlen(msg_str) + 3);
    if (!(*out_buff))
    {
        LOG_E("ONENET mqtt upload string data failed! No memory for send buffer!");
        return -RT_ENOMEM;
    }

    strncpy(&(*out_buff)[3], msg_str, strlen(msg_str));
    *length = strlen(&(*out_buff)[3]);

    /* mqtt head and json length */
    (*out_buff)[0] = 0x03;
    (*out_buff)[1] = (*length & 0xff00) >> 8;
    (*out_buff)[2] = *length & 0xff;
    *length += 3;

__exit:
    if (root)
    {
        cJSON_Delete(root);
    }
    if (msg_str)
    {
        cJSON_free(msg_str);
    }

    return result;
}





/**
 * upload string data to OneNET cloud.
 *
 * @param   ds_name     datastream name
 * @param   str         string data
 *
 * @return  0 : upload digit data success
 *         -5 : no memory
 */
rt_err_t onenet_mqtt_upload_GPSDIY(char* lat,char* lon)
{
    char *send_buffer = RT_NULL;
    rt_err_t result = RT_EOK;
    size_t length = 0;


    result = onenet_mqtt_get_string_gpsDIY(lat,lon,&send_buffer, &length);
    if (result < 0)
    {
        goto __exit;
    }

    result = onenet_mqtt_publish(ONENET_TOPIC_DP, (uint8_t *) send_buffer, length);
    if (result < 0)
    {
        LOG_E("onenet publish failed (%d)!", result);
        goto __exit;
    }

    __exit: if (send_buffer)
    {
        ONENET_FREE(send_buffer);
    }

    return result;
}



然后在发送线程里调用

onenet_mqtt_upload_GPSDIY(info->coord.location.longitude.string,info->coord.location.latitude.string)

OK

chenyingchun
chenyingchun 2020-08-18

肯定是数据发送的格式有点问题,你多对比下, 看看是不是字符串类型,json类型,缺不缺字段等等

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友