onenet_sample.c 3.5 KB

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