【问题标题】:Blink Led using MQTT in arduinoBlink Led 在 arduino 中使用 MQTT
【发布时间】:2016-08-17 19:11:36
【问题描述】:

我必须使用 MQTT 协议在 arduino 中实现使 LED 闪烁的原型设计方案。我已经尝试了几个 MQTT 库,但没有一个不能完美运行。与 MQTT 代理的连接成功,但是当我发布带有我在 arduino 中设置的主题的消息时,LED 不会闪烁。 Arduno 必须在成功连接后发布消息,但此发布部分也无法正常工作

这是我的代码

    #include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

// Set the MAC address
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 100);
IPAddress server(192, 168, 1, 20);

// Set what PINs our Led's are connected to
int redPin = 13;                
//int greenPin = 6;
//int bluePin = 7;

// Set a generic code that will trigger our Blue Led
// think of this as a set of codes for automation you might write
byte triggerRed[13] = "12345";

// handles messages that are returned from the broker on our subscribed channel
void callback(char* topic, byte* payload, unsigned int length) {

  Serial.print("New message from broker on topic:");
  Serial.println(topic);

  Serial.print("Payload:");
  Serial.write(payload, length);



  // Check and see if our payload matches our simple trigger test
  if ((length == 5) & (memcmp(payload, triggerRed, 5) == 0) )
  {
    //blink(redPin);

  }

}
EthernetClient ethClient;
PubSubClient client(ethClient);
// Fire up our PubSub client
//PubSubClient client(server, 1883, callback);

void setup()
{

  // Open serial communications
  Serial.begin(9600);

  client.setServer(server, 1883);
  client.setCallback(callback);

  // Setup our Leds
  pinMode(redPin, OUTPUT);
 // pinMode(greenPin, OUTPUT);
 // pinMode(bluePin, OUTPUT);

  // attempt a DHCP connection
  Serial.println("Attempting to get an IP address using DHCP:");
  if (!Ethernet.begin(mac)) 
  {
    // if DHCP fails, start with a hard-coded address:
    Serial.println("failed to get an IP address using DHCP, trying manually");
    Ethernet.begin(mac, ip);
  }

  Serial.print("My address:");
  Serial.println(Ethernet.localIP());

  // Connect to Broker, give it arduino as the name
  if (client.connect("arduino")) {

    // Good, we connected turn on the red led
    digitalWrite(redPin, HIGH);

    // Publish a message to the status topic
    client.publish("status","Arduino is now online");

    // Listen for messages on the control topic
    client.subscribe("ultra");
  }

}

void loop()
{
  client.loop();
}

// Anything with flashing lights.
void blink(int targetLed) 
{
  digitalWrite(redPin, HIGH);

}

我该如何解决这个问题?

【问题讨论】:

  • 您是否尝试从 Arduino MQTT 库运行示例草图?我建议您将客户端连接例程放在一个循环中。为什么不尝试连接到网络上的公共服务器,例如test.mosquitto.org,然后再连接到本地服务器?
  • 我已经用 test.mosquitto.org 进行了测试,但这没有用。你的意思是让客户端连接循环?当我发布具有正确主题的消息时,LED 闪烁。但我需要它始终保持“开启”

标签: arduino mqtt broker


【解决方案1】:

将连接例程放在一个循环中,并首先尝试使用 test.mosquitto.org。 这是适用于我的代码(以太网屏蔽硬件):

定义:

#define CLIENT_NAME "myclientname"
#define TOPIC "mytopic"
byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 105);
IPAddress server(85, 119, 83, 194);// test.mosquitto.org
#define MQTT_PORT 1883
IPAddress myDns(8, 8, 8, 8);
EthernetClient ethClient;
PubSubClient client(ethClient);

设置:

client.setServer(server, MQTT_PORT);
client.setCallback(callback);
Ethernet.begin(mac);

循环:

if (!client.connected()) {
            reconnect();
        }
client.loop();

重新连接例程

void reconnect() {
    if (millis() - reconnectionTimer >reconnection_period){// Loop until we're reconnected
        reconnectionTimer = millis();
        if (!client.connected()) {
            // Attempt to connect
            if (client.connect(CLIENT_NAME)) {
                client.subscribe(TOPIC);
            }
            else {
                Serial.print(client.state());
                Serial3.print(client.state());
            }
        }
    }
}

更新眨眼:

void blink(){
  digitalWrite(led, LOW);
  delay(500);
  digitalWrite(led, HIGH);
}

【讨论】:

  • 这是建立连接的代码。您可能有连接问题,而不是 LED 问题。从您的代码中,我看到您已注释掉 blink 例程。无论如何,这个例程不包含闪烁代码。
【解决方案2】:

我浪费了很多时间,因为我遇到了 mqtt 服务器的问题。确保您的服务器使用 mqtt 协议,因为我使用的是 ws 协议,而我尝试过的任何库都不适用于此协议

【讨论】:

    猜你喜欢
    • 2020-04-13
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多