/* * File : onenet_sample.c * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * * Change Logs: * Date Author Notes * 2018-04-24 chenyong first version */ #include #include #include #include #define DBG_ENABLE #define DBG_COLOR #define DBG_SECTION_NAME "onenet.sample" #if ONENET_DEBUG #define DBG_LEVEL DBG_LOG #else #define DBG_LEVEL DBG_INFO #endif /* ONENET_DEBUG */ #include #ifdef FINSH_USING_MSH #include /* upload random value to temperature*/ static void onenet_upload_entry(void *parameter) { int value = 0; while (1) { value = rand() % 100; if (onenet_mqtt_upload_digit("temperature", value) < 0) { LOG_E("upload has an error, stop uploading"); break; } else { LOG_D("buffer : {\"temperature\":%d}", value); } rt_thread_delay(rt_tick_from_millisecond(5 * 1000)); } } int onenet_upload_cycle(void) { rt_thread_t tid; tid = rt_thread_create("onenet_send", onenet_upload_entry, RT_NULL, 2 * 1024, RT_THREAD_PRIORITY_MAX / 3 - 1, 5); if (tid) { rt_thread_startup(tid); } return 0; } MSH_CMD_EXPORT(onenet_upload_cycle, send data to OneNET cloud cycle); int onenet_publish_digit(int argc, char **argv) { if (argc != 3) { LOG_E("onenet_publish [datastream_id] [value] - mqtt pulish digit data to OneNET."); return -1; } if (onenet_mqtt_upload_digit(argv[1], atoi(argv[2])) < 0) { LOG_E("upload digit data has an error!\n"); } return 0; } MSH_CMD_EXPORT_ALIAS(onenet_publish_digit, onenet_mqtt_publish_digit, send digit data to onenet cloud); int onenet_publish_string(int argc, char **argv) { if (argc != 3) { LOG_E("onenet_publish [datastream_id] [string] - mqtt pulish string data to OneNET."); return -1; } if (onenet_mqtt_upload_string(argv[1], argv[2]) < 0) { LOG_E("upload string has an error!\n"); } return 0; } MSH_CMD_EXPORT_ALIAS(onenet_publish_string, onenet_mqtt_publish_string, send string data to onenet cloud); /* onenet mqtt command response callback function */ static void onenet_cmd_rsp_cb(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size) { char res_buf[] = { "cmd is received!\n" }; LOG_D("recv data is %.*s\n", recv_size, recv_data); /* user have to malloc memory for response data */ *resp_data = (uint8_t *) ONENET_MALLOC(strlen(res_buf)); strncpy((char *)*resp_data, res_buf, strlen(res_buf)); *resp_size = strlen(res_buf); } /* set the onenet mqtt command response callback function */ int onenet_set_cmd_rsp(int argc, char **argv) { onenet_set_cmd_rsp_cb(onenet_cmd_rsp_cb); return 0; } MSH_CMD_EXPORT(onenet_set_cmd_rsp, set cmd response function); #endif /* FINSH_USING_MSH */