EzData.ino 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: EzData.
  10. * Date: 2021/10/17
  11. *******************************************************************************
  12. */
  13. #include "M5Stack.h"
  14. #include "M5_EzData.h"
  15. // Configure the name and password of the connected wifi and your token.
  16. // 配置所连接wifi的名称、密码以及你的token
  17. const char* ssid = "Explore-F";
  18. const char* password = "xingchentansuo123";
  19. const char* token = "";
  20. void setup() {
  21. M5.begin(); // Initialize M5Stack
  22. M5.Power.begin();
  23. if (setupWifi(ssid, password)) { // Connect to wifi. 连接到wifi
  24. M5.Lcd.printf("Success connecting to %s\n", ssid);
  25. } else {
  26. M5.Lcd.printf("Connecting to %s failed\n", ssid);
  27. }
  28. }
  29. void loop() {
  30. // Save the data 20 to the top of the testData topic queue. 保存数据20至
  31. // testData 队列首位
  32. if (setData(token, "testData", 20)) {
  33. M5.Lcd.printf("Success sending data to the topic\n");
  34. } else {
  35. M5.Lcd.print("Fail to save data\n");
  36. }
  37. delay(5000);
  38. // Save 3 data in sequence to the first place of testList. 依次保存3个数据至
  39. // testList首位
  40. for (int i = 0; i < 3; i++) {
  41. if (addToList(token, "testList", i)) {
  42. M5.Lcd.printf("Success sending %d to the list\n", i);
  43. } else {
  44. M5.Lcd.print("Fail to save data\n");
  45. }
  46. delay(100);
  47. }
  48. delay(5000);
  49. // Get a piece of data from a topic and store the value in result. 从一个
  50. // topic中获取一个数据,并将值存储在 result
  51. int result = 0;
  52. if (getData(token, "testData", result)) {
  53. M5.Lcd.printf("Success get data %d\n", result);
  54. } else {
  55. M5.Lcd.print("Fail to get data\n");
  56. }
  57. delay(5000);
  58. // Get a set of data from a list and store the values in the Array array.
  59. // 从一个 list中获取一组数据,并将值存储在 Array数组中
  60. int Array[3] = {};
  61. if (getData(token, "testList", Array, 0, 3)) {
  62. M5.Lcd.print("Success get list\n");
  63. for (int i = 0; i < 3; i++) {
  64. M5.Lcd.printf("Array[%d]=%d,", i, Array[i]);
  65. }
  66. M5.Lcd.println("");
  67. } else {
  68. M5.Lcd.println("Fail to get data");
  69. }
  70. delay(5000);
  71. // Remove data
  72. if (removeData(token, "testData"))
  73. M5.Lcd.printf("Success remove data\n");
  74. else
  75. M5.Lcd.println("Fail to remove data");
  76. if (removeData(token, "testList"))
  77. M5.Lcd.printf("Success remove data from the list\n");
  78. else
  79. M5.Lcd.println("Fail to remove data");
  80. delay(5000);
  81. M5.Lcd.fillScreen(BLACK);
  82. M5.Lcd.setCursor(0, 0);
  83. }