| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- /*
- * 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 <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #include <onenet.h>
- #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 <rtdbg.h>
- #ifdef FINSH_USING_MSH
- #include <finsh.h>
- /* 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 */
|