onenet_sample.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * File : onenet_sample.c
  3. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Change Logs:
  20. * Date Author Notes
  21. * 2018-04-24 chenyong first version
  22. */
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdint.h>
  26. #include <onenet.h>
  27. #define DBG_ENABLE
  28. #define DBG_COLOR
  29. #define DBG_SECTION_NAME "onenet.sample"
  30. #if ONENET_DEBUG
  31. #define DBG_LEVEL DBG_LOG
  32. #else
  33. #define DBG_LEVEL DBG_INFO
  34. #endif /* ONENET_DEBUG */
  35. #include <rtdbg.h>
  36. #ifdef FINSH_USING_MSH
  37. #include <finsh.h>
  38. /* upload random value to temperature*/
  39. static void onenet_upload_entry(void *parameter)
  40. {
  41. int value = 0;
  42. while (1)
  43. {
  44. value = rand() % 100;
  45. if (onenet_mqtt_upload_digit("temperature", value) < 0)
  46. {
  47. LOG_E("upload has an error, stop uploading");
  48. break;
  49. }
  50. else
  51. {
  52. LOG_D("buffer : {\"temperature\":%d}", value);
  53. }
  54. rt_thread_delay(rt_tick_from_millisecond(5 * 1000));
  55. }
  56. }
  57. int onenet_upload_cycle(void)
  58. {
  59. rt_thread_t tid;
  60. tid = rt_thread_create("onenet_send",
  61. onenet_upload_entry,
  62. RT_NULL,
  63. 2 * 1024,
  64. RT_THREAD_PRIORITY_MAX / 3 - 1,
  65. 5);
  66. if (tid)
  67. {
  68. rt_thread_startup(tid);
  69. }
  70. return 0;
  71. }
  72. MSH_CMD_EXPORT(onenet_upload_cycle, send data to OneNET cloud cycle);
  73. int onenet_publish_digit(int argc, char **argv)
  74. {
  75. if (argc != 3)
  76. {
  77. LOG_E("onenet_publish [datastream_id] [value] - mqtt pulish digit data to OneNET.");
  78. return -1;
  79. }
  80. if (onenet_mqtt_upload_digit(argv[1], atoi(argv[2])) < 0)
  81. {
  82. LOG_E("upload digit data has an error!\n");
  83. }
  84. return 0;
  85. }
  86. MSH_CMD_EXPORT_ALIAS(onenet_publish_digit, onenet_mqtt_publish_digit, send digit data to onenet cloud);
  87. int onenet_publish_string(int argc, char **argv)
  88. {
  89. if (argc != 3)
  90. {
  91. LOG_E("onenet_publish [datastream_id] [string] - mqtt pulish string data to OneNET.");
  92. return -1;
  93. }
  94. if (onenet_mqtt_upload_string(argv[1], argv[2]) < 0)
  95. {
  96. LOG_E("upload string has an error!\n");
  97. }
  98. return 0;
  99. }
  100. MSH_CMD_EXPORT_ALIAS(onenet_publish_string, onenet_mqtt_publish_string, send string data to onenet cloud);
  101. /* onenet mqtt command response callback function */
  102. static void onenet_cmd_rsp_cb(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size)
  103. {
  104. char res_buf[] = { "cmd is received!\n" };
  105. LOG_D("recv data is %.*s\n", recv_size, recv_data);
  106. /* user have to malloc memory for response data */
  107. *resp_data = (uint8_t *) ONENET_MALLOC(strlen(res_buf));
  108. strncpy((char *)*resp_data, res_buf, strlen(res_buf));
  109. *resp_size = strlen(res_buf);
  110. }
  111. /* set the onenet mqtt command response callback function */
  112. int onenet_set_cmd_rsp(int argc, char **argv)
  113. {
  114. onenet_set_cmd_rsp_cb(onenet_cmd_rsp_cb);
  115. return 0;
  116. }
  117. MSH_CMD_EXPORT(onenet_set_cmd_rsp, set cmd response function);
  118. #endif /* FINSH_USING_MSH */