1

I have to implement a prototyping scenario that blink LED in the arduino with MQTT protocol. I already tried with several MQTT libraries but non of them not work perfectly. Connection to the MQTT broker working successfully but when I publish the message with topic which I set in arduino not blink the LED. Arduno have to publish a message when it successfully connect but this publishing part also not working

this is my code

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

}

how can I fix this ?

2
  • Did you try to run example sketch from Arduino MQTT library? I recommend you to put client connection routine in a loop. Why not try to connect to a public server on web, for example test.mosquitto.org before connecting to a local one? Commented Apr 24, 2016 at 12:27
  • I already test with test.mosquitto.org but that didn't work. What you mean put client connection in loop ? LED blink when I publish a message with correct topic. but I need it to keep it "ON" always Commented Apr 24, 2016 at 12:35

2 Answers 2

0

Put connection routine in a loop and try with test.mosquitto.org first. Here is code that works for me (Ethernet shield hardware):

definitions:

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

setup:

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

loop:

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

reconnect routine

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());
            }
        }
    }
}

update for blink:

void blink(){
  digitalWrite(led, LOW);
  delay(500);
  digitalWrite(led, HIGH);
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is code for establishing connection. You may have issue with connection, not with led. From your code i see you have commented out blink routine. And anyway this routine not contains code for blinking.
0

I lost a lot of time because i had the problem with the mqtt server. ensure that your server is using mqtt protocol, because i was using ws protocol and any library that I tried doesn't works with this protocol

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.