1776浏览
查看: 1776|回复: 2

[求助] ESP32的MQTT通讯显示出错

[复制链接]
我编了一段代码,如下:
  1. /*!
  2. * MindPlus
  3. * mpython
  4. *
  5. */
  6. #include <MPython.h>
  7. #include <DFRobot_Iot.h>
  8. #include <DFRobot_NFC0231.h>
  9. #include <MPython_NtpTime.h>
  10. // 动态变量
  11. String mind_s_my_string_variable;
  12. // 函数声明
  13. void obloqMqttEventT0(String& message);
  14. void obloqMqttEventT1(String& message);
  15. // 静态常量
  16. const String topics[5] = {"3e2n6Qv4R","TfSME_DVR","","",""};
  17. const MsgHandleCb msgHandles[5] = {obloqMqttEventT0,obloqMqttEventT1,NULL,NULL,NULL};
  18. // 创建对象
  19. DFRobot_PN532_IIC nfc;
  20. DFRobot_Iot       myIot;
  21. MPython_NtpTime   ntptime;
  22. // 主程序开始
  23. void setup() {
  24.         mPython.begin();
  25.         myIot.setMqttCallback(msgHandles);
  26.         nfc.begin();
  27.         rgb.brightness(round(1));
  28.         myIot.wifiConnect("CU_kr33", "enhtm6p2");
  29.         delay(5000);
  30.         for (int index = 0; index < 10; index++) {
  31.                 delay(1000);
  32.                 if (myIot.wifiStatus()) {
  33.                         rgb.write(1, 0x00FF00);
  34.                         delay(1000);
  35.                         rgb.write(1, 0x000000);
  36.                         display.setCursorLine(1);
  37.                         display.printLine("WiFi连接成功");
  38.                         myIot.init("iot.dfrobot.com.cn","RxXMewvVR","","RxXM6QD4gz",topics,1883);
  39.                         ntptime.setNtpTime(ntptime.UTCEast8_t, "ntp.ntsc.ac.cn");
  40.                         while (1) {
  41.                                 display.setCursorLine(2);
  42.                                 display.printLine((String((String((String(ntptime.localTime(ntptime.Year)) + String((String("\") + String(ntptime.localTime(ntptime.Month)))))) + String((String((String("\") + String(ntptime.localTime(ntptime.Date)))) + String("\"))))) + String((String(ntptime.localTime(ntptime.Hour)) + String((String("\") + String((String(ntptime.localTime(ntptime.Minute)) + String((String("\") + String(ntptime.localTime(ntptime.Second))))))))))));
  43.                                 if (nfc.scan()) {
  44.                                         display.setCursorLine(3);
  45.                                         display.printLine(nfc.readUid());
  46.                                 }
  47.                                 if ((touchPadP.isTouched())) {
  48.                                         display.fillScreen(0);
  49.                                         display.setCursor(42, 22);
  50.                                         display.print("MQTT");
  51.                                         myIot.connect();
  52.                                         while (!myIot.connected()) {
  53.                                                 display.setCursor(42, 40);
  54.                                                 display.print("连接中.");
  55.                                                 delay(1000);
  56.                                                 display.setCursor(42, 40);
  57.                                                 display.print("连接中..");
  58.                                                 delay(1000);
  59.                                                 display.setCursor(42, 40);
  60.                                                 display.print("连接中...");
  61.                                                 yield();
  62.                                         }
  63.                                         display.fillScreen(0);
  64.                                         display.setCursorLine(1);
  65.                                         display.printLine("MQTT连接成功");
  66.                                         while (1) {
  67.                                                 if ((touchPadP.isTouched())) {
  68.                                                         display.setCursorLine(2);
  69.                                                         display.printLine("确定发送:成功");
  70.                                                         while (!((buttonA.isPressed()) || (buttonB.isPressed()))) {yield();}
  71.                                                         delay(100);
  72.                                                         if ((buttonA.isPressed())) {
  73.                                                                 myIot.publish(topic_0, "成功");
  74.                                                                 display.fillInLine(2, 0);
  75.                                                         }
  76.                                                         if ((buttonB.isPressed())) {
  77.                                                                 display.fillInLine(2, 0);
  78.                                                         }
  79.                                                 }
  80.                                                 else if ((touchPadY.isTouched())) {
  81.                                                         display.setCursorLine(2);
  82.                                                         display.printLine("Mind+");
  83.                                                 }
  84.                                                 else if ((touchPadT.isTouched())) {
  85.                                                         display.setCursorLine(2);
  86.                                                         display.printLine("Mind+");
  87.                                                 }
  88.                                                 else if ((touchPadH.isTouched())) {
  89.                                                         display.setCursorLine(2);
  90.                                                         display.printLine("Mind+");
  91.                                                 }
  92.                                                 else if ((touchPadO.isTouched())) {
  93.                                                         display.setCursorLine(2);
  94.                                                         display.printLine("Mind+");
  95.                                                 }
  96.                                                 else if ((touchPadN.isTouched())) {
  97.                                                         display.fillScreen(1);
  98.                                                 }
  99.                                                 else {
  100.                                                         display.fillInLine(2, 0);
  101.                                                 }
  102.                                                 yield();
  103.                                         }
  104.                                 }
  105.                                 yield();
  106.                         }
  107.                 }
  108.                 yield();
  109.         }
  110.         if ((!myIot.wifiStatus())) {
  111.                 rgb.write(1, 0xFF0000);
  112.                 display.setCursorLine(1);
  113.                 display.printLine("错误");
  114.                 delay(1000);
  115.                 rgb.write(1, 0x000000);
  116.                 while (1) {
  117.                         if (nfc.scan()) {
  118.                                 display.setCursorLine(2);
  119.                                 display.printLine(nfc.readUid());
  120.                         }
  121.                         yield();
  122.                 }
  123.         }
  124. }
  125. void loop() {
  126. }
  127. // 事件回调函数
  128. void obloqMqttEventT0(String& message) {
  129.         mind_s_my_string_variable = (String("0:") + String(message));
  130.         for (int index = 0; index < 5; index++) {
  131.                 delay(1000);
  132.                 display.setCursorLine(3);
  133.                 display.printLine(mind_s_my_string_variable);
  134.                 yield();
  135.         }
  136.         while (!(buttonA.isPressed())) {yield();}
  137.         display.fillInLine(4, 0);
  138.         mind_s_my_string_variable = "N";
  139. }
  140. void obloqMqttEventT1(String& message) {
  141.         mind_s_my_string_variable = (String("1:") + String(message));
  142.         for (int index = 0; index < 5; index++) {
  143.                 delay(1000);
  144.                 display.setCursorLine(3);
  145.                 display.printLine(mind_s_my_string_variable);
  146.                 yield();
  147.         }
  148. }
复制代码
在编辑并上传后使用MQTT网页发送消息,主控板有反应,但显示参差不齐,有遗漏,重复5次,次次差的不一样。

各位大神能帮我看看是什么问题吗?

车子翔  初级技师
 楼主|

发表于 2022-11-22 23:31:20

最终发现是板子的bug,只有前面几行都显示的情况下才能正常显示
回复

使用道具 举报

三春牛-创客  初级技神

发表于 2023-1-29 08:59:46

不知道啊。。。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail