2016-04-24 17 views
1

MQTTプロトコルでarduinoのLEDを点滅させる試作シナリオを実装する必要があります。私はすでにいくつかのMQTTライブラリーを試しましたが、それらのどれも完全には動作しません。 MQTTブローカーへの接続は正常に機能しましたが、arduinoで設定したトピックでメッセージを公開するとLEDが点滅しません。 Ardunoは、それが正常に接続したときにメッセージをパブリッシュする必要がありますが、この出版部分もarduinoでMQTTを使用して点滅しました

働いていない、これは私のコード私はこれをどのように問題を解決することができ

#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); 

} 

のですか?

+0

あなたはArduinoのMQTTライブラリから例のスケッチを実行しようとしましたか?クライアント接続ルーチンをループに入れることをお勧めします。ローカルのWebサーバーに接続する前に、Web上の公開サーバー(たとえばhttp://test.mosquitto.org/)に接続しようとしないでください。 –

+0

私はすでにtest.mosquitto.orgでテストしていますが、うまくいきませんでした。あなたはクライアント接続をループに入れることを意味しますか?正しいトピックでメッセージを発行すると、LEDが点滅します。私はそれを常に "ON"に保つ必要があります –

答えて

0

接続ルーチンをループに入れて、まず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(); 

ルーチン

を再接続ここで は私のために動作するコード(イーサネットシールドハードウェア)であります012点滅のため

更新:

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

これは接続を確立するためのコードです。接続で問題が発生している可能性があります。あなたのコードから、あなたは* blink *ルーチンをコメントアウトしているのを見ます。そして、とにかくこのルーチンは点滅のためのコードを含んでいません。 –

関連する問題