dirty 发表于 2024-2-23 12:39

【安信可BW16-Kit】MQTT点灯

<div class='showpostmsg'><p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;本篇讲述在Arduino IDE环境通过MQTT实现远程控制板载LED.</p>

<p>一.原理了解及准备</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MQTT是一个基于客户端-服务器的消息发布/订阅传输协议。MQTT是基于发布/订阅范式的消息协议。MQTT应用需要服务器地址与端口,发布、订阅Topic、客户ID等。准备工作,在手机端下载安装&ldquo;MQTT调试器&rdquo;APP.</p>

<p>二.代码及工具准备</p>

<p>1.代码模板获取</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;打开Arduino IDE,按如下菜单路径打开MQTT_Basic工程。</p>

<div style="text-align: center;"></div>

<div style="text-align: center;">图1:打开并创建工程</div>

<p>2.代码修改</p>

<p>(1)填入WIFI账号密码,这里自定义了发布、订阅Topic</p>

<pre>
<code>char ssid[] = "nova6_5G";   // your network SSID (name)
char pass[] = "12345678";// your network password
int status = WL_IDLE_STATUS;      // Indicator of Wifi status

char mqttServer[]   = "test.mosquitto.org";
char clientId[]       = "amebaClient";
char publishTopic[]   = "amebaClient/outTopic";
char publishPayload[] = "hello world";
char subscribeTopic[] = "amebaClient/inTopic";</code></pre>

<p>(2)在setup()添加led初始化</p>

<pre>
<code>void setup() {

    // initialize digital pin's from all 3 colors as an output.
    pinMode(LED_R, OUTPUT);
    pinMode(LED_G, OUTPUT);
    pinMode(LED_B, OUTPUT);

    digitalWrite(LED_R, LOW);
    digitalWrite(LED_G, LOW);
    digitalWrite(LED_B, LOW);

        //......
}</code></pre>

<p>(3)在连接函数reconnect去掉设备端发布</p>

<p>(4)在发布订阅回调函数添加应用代码.当设备端收到订阅消息,执行相应控制功能,并将接收消息发布出去</p>

<pre>
<code>void callback(char* topic, byte* payload, unsigned int length) {

    char payload_r_on[]="LED_R_ON";
    char payload_g_on[]="LED_G_ON";
    char payload_b_on[]="LED_B_ON";
    char payload_all_off[]="LED_OFF";

    Serial.print("Message arrived [");
    Serial.print(topic);
    Serial.print("] ");
    for (unsigned int i = 0; i &lt; length; i++) {
      Serial.print((char)(payload));
    }
    Serial.println();

    Serial.print("payload len:");
    Serial.println(length);

    if(strcmp(topic,subscribeTopic)==0)
    {

      Serial.println("Receive subscribe.");
      if(memcmp((char *)payload,payload_r_on,strlen(payload_r_on))==0)
      {
         Serial.println("Light the Led Red");
         digitalWrite(LED_R, HIGH);
         digitalWrite(LED_G, LOW);
         digitalWrite(LED_B, LOW);
         client.publish(publishTopic,payload_r_on);
      }

      if(memcmp((char *)payload,payload_g_on,strlen(payload_g_on))==0)
      {
         Serial.println("Light the Led Green");
         digitalWrite(LED_R, LOW);
         digitalWrite(LED_G, HIGH);
         digitalWrite(LED_B, LOW);
         client.publish(publishTopic,payload_g_on);
      }

      if(memcmp((char *)payload,payload_b_on,strlen(payload_b_on))==0)
      {
         Serial.println("Light the Led Blue");
         digitalWrite(LED_R, LOW);
         digitalWrite(LED_G, LOW);
         digitalWrite(LED_B, HIGH);
         client.publish(publishTopic,payload_b_on);
      }

      if(memcmp((char *)payload,payload_all_off,strlen(payload_all_off))==0)
      {
         Serial.println("Light the Led Off");
         digitalWrite(LED_R, LOW);
         digitalWrite(LED_G, LOW);
         digitalWrite(LED_B, LOW);
         client.publish(publishTopic,payload_all_off);
      }

    }

}</code></pre>

<p>3.&ldquo;MQTT调试器&rdquo;APP.设置</p>

<p>(1)test.mosquitto.org<a href="https://tool.lu/ip/" target="_blank">服务器IP查询</a>,可获得ip&nbsp;91.121.93.94</p>

<p>(2)打开&ldquo;MQTT调试器&rdquo;APP,点&ldquo;+&rdquo;,在主机位置填写 tcp://91.121.93.94:1883 ,并保存。此时在app主界面已有添加的列表。</p>

<div style="text-align: center;"></div>

<div style="text-align: center;">图2:服务端输入配置</div>

<p>(3)点击列表进入,设置好订阅Topic:&nbsp;amebaClient/outTopic</p>

<p>(4)连接,设置发布如下图</p>

<div style="text-align: center;"></div>

<div style="text-align: center;">图3:MQTT发布</div>

<p>三.测验</p>

<p>&nbsp; &nbsp; 在APP上发布playload,可看到设备端收到后执行,手机端有收到订阅,串口日志</p>

<div style="text-align: center;"></div>

<div style="text-align: center;">&nbsp;</div>

<div style="text-align: center;">图4:手机APP端收到订阅</div>

<div style="text-align: center;"></div>

<div style="text-align: center;">图5:串口日志</div>

<p>&nbsp; &nbsp; &nbsp; &nbsp; 至此,实现MQTT Client消息发布订阅功能,实现设备控制。</p>

<p>&nbsp;</p>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</p>

<p>&nbsp;</p>

<p>&nbsp; &nbsp; &nbsp;&nbsp;</p>
</div><script>                                        var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;"   style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
                                       
                                        if(parseInt(discuz_uid)==0){
                                                                                                (function($){
                                                        var postHeight = getTextHeight(400);
                                                        $(".showpostmsg").html($(".showpostmsg").html());
                                                        $(".showpostmsg").after(loginstr);
                                                        $(".showpostmsg").css({height:postHeight,overflow:"hidden"});
                                                })(jQuery);
                                        }                </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script>

wangerxian 发表于 2024-2-23 17:21

<p>是不是物联网用MQTT协议的比较多?</p>

dirty 发表于 2024-2-23 21:09

wangerxian 发表于 2024-2-23 17:21
是不是物联网用MQTT协议的比较多?

<p>小数据量是的,负载读写数据</p>

wangerxian 发表于 2024-2-26 09:07

dirty 发表于 2024-2-23 21:09
小数据量是的,负载读写数据

<p>有机会我也去尝试一下,看看怎么样。</p>
页: [1]
查看完整版本: 【安信可BW16-Kit】MQTT点灯