onenet_sample.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 <onenet.h>
  25. #define DBG_ENABLE
  26. #define DBG_COLOR
  27. #define DBG_SECTION_NAME "onenet.sample"
  28. #if ONENET_DEBUG
  29. #define DBG_LEVEL DBG_LOG
  30. #else
  31. #define DBG_LEVEL DBG_INFO
  32. #endif /* ONENET_DEBUG */
  33. #include <rtdbg.h>
  34. #ifdef FINSH_USING_MSH
  35. #include <finsh.h>
  36. /* upload random value to temperature*/
  37. static void onenet_upload_entry(void *parameter)
  38. {
  39. int value = 0;
  40. while (1)
  41. {
  42. value = rand() % 100;
  43. if (onenet_mqtt_upload_digit("temperature", value) < 0)
  44. {
  45. LOG_E("upload has an error, stop uploading");
  46. break;
  47. }
  48. else
  49. {
  50. LOG_D("buffer : {\"temperature\":%d}", value);
  51. }
  52. rt_thread_delay(rt_tick_from_millisecond(5 * 1000));
  53. }
  54. }
  55. int onenet_upload_cycle(void)
  56. {
  57. rt_thread_t tid;
  58. tid = rt_thread_create("onenet_send",
  59. onenet_upload_entry,
  60. RT_NULL,
  61. 2 * 1024,
  62. RT_THREAD_PRIORITY_MAX / 3 - 1,
  63. 5);
  64. if (tid)
  65. {
  66. rt_thread_startup(tid);
  67. }
  68. return 0;
  69. }
  70. MSH_CMD_EXPORT(onenet_upload_cycle, send data to OneNET cloud cycle);
  71. int onenet_publish_digit(int argc, char **argv)
  72. {
  73. if (argc != 3)
  74. {
  75. LOG_E("onenet_publish [datastream_id] [value] - mqtt pulish digit data to OneNET.");
  76. return -1;
  77. }
  78. if (onenet_mqtt_upload_digit(argv[1], atoi(argv[2])) < 0)
  79. {
  80. LOG_E("upload digit data has an error!\n");
  81. }
  82. return 0;
  83. }
  84. MSH_CMD_EXPORT_ALIAS(onenet_publish_digit, onenet_mqtt_publish_digit, send digit data to onenet cloud);
  85. int onenet_publish_string(int argc, char **argv)
  86. {
  87. if (argc != 3)
  88. {
  89. LOG_E("onenet_publish [datastream_id] [string] - mqtt pulish string data to OneNET.");
  90. return -1;
  91. }
  92. if (onenet_mqtt_upload_string(argv[1], argv[2]) < 0)
  93. {
  94. LOG_E("upload string has an error!\n");
  95. }
  96. return 0;
  97. }
  98. MSH_CMD_EXPORT_ALIAS(onenet_publish_string, onenet_mqtt_publish_string, send string data to onenet cloud);
  99. /* onenet mqtt command response callback function */
  100. static void onenet_cmd_rsp_cb(uint8_t *recv_data, size_t recv_size, uint8_t **resp_data, size_t *resp_size)
  101. {
  102. char res_buf[] = { "cmd is received!\n" };
  103. LOG_D("recv data is %.*s\n", recv_size, recv_data);
  104. /* user have to malloc memory for response data */
  105. *resp_data = (uint8_t *) ONENET_MALLOC(strlen(res_buf));
  106. strncpy((char *)*resp_data, res_buf, strlen(res_buf));
  107. *resp_size = strlen(res_buf);
  108. }
  109. /* set the onenet mqtt command response callback function */
  110. int onenet_set_cmd_rsp(int argc, char **argv)
  111. {
  112. onenet_set_cmd_rsp_cb(onenet_cmd_rsp_cb);
  113. return 0;
  114. }
  115. MSH_CMD_EXPORT(onenet_set_cmd_rsp, set cmd response function);
  116. #endif /* FINSH_USING_MSH */