element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum MQTT client program not showing any results with Arduino
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 49 replies
  • Subscribers 395 subscribers
  • Views 4138 views
  • Users 0 members are here
Related

MQTT client program not showing any results with Arduino

Former Member
Former Member over 10 years ago

I modified the MQTT example present under MQTT library pubsubclient for Arduino.

My MQTT server address is 192.168.1.6 with port 8081.

To this MQTT server,another MQTT client in my mobile is able to connect.

But this is not working with Arduino.There is no error shown during compilation or during uploading.

Infact,I am not able to see the connection result messages such as "Connected" or "Not connected" which i given in Serial.println() in setup function.

I am resetting Arduino before uploading the program.Please let me know why this is not showing any output....

 

#include <SPI.h>

#include <Ethernet.h>

#include <PubSubClient.h>

// Update these with values suitable for your network.

byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };

byte server[] = {192, 168, 1, 6 };

//byte ip[]     = { 172, 16, 0, 100 };

 

 

void callback(char* topic, byte* payload, unsigned int length) {

  // handle message arrived

}

EthernetClient ethClient;

PubSubClient client(server, 8081, callback, ethClient);

void setup()

{

  Ethernet.begin(mac);

  if (client.connect("*")) {

     Serial.begin(9600);

    Serial.println("Connected to MQTT Server");

    client.publish("outTopic","hello world");

   // client.subscribe("inTopic");

  }

  else

  {

   Serial.println("Could not connect to MQTT Server");

  }

}

void loop()

{

  //client.loop();

 

}

  • Sign in to reply
  • Cancel

Top Replies

  • neilk
    neilk over 10 years ago in reply to Former Member +3
    generk4 and organtin Hi Radhamani and Giovanni - I had 30 minutes to spare so I copied the program from comment 19 and posted it into the Arduino IDE (I am using 1.6.1), compiled and uploaded it to a genuine…
  • Former Member
    Former Member over 10 years ago +3
    Hi Bob, Neil and Clem, I tried LM35 Temperature sensor with MQTT Arduino sketch and it works fine for me I am sooo happy Thanks once again for your wonderful support and help
  • Former Member
    Former Member over 10 years ago in reply to bobcroft +2
    Hi bob,Neil,Clem and many others who have helped and guided me in this issue, I have successfully completed MQTT program and Arduino is successfully publishing and subscribing messages Next step,I am going…
  • Robert Peter Oakes
    Robert Peter Oakes over 10 years ago in reply to Former Member

    DHCP does work with the standard Ethernet shield, I have used it on many occasions

     

    You may want to also check you DHCP server ?

     

    regards

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 10 years ago

    Hi Peter Oakes,

      Thanks..That and many others can also be added as reasons..

       I have given these lessons learnt from my side since I didnt have issues with DHCP server...

    I didnt press the Ethernet shield properly..Also,I had a faulty Ethernet shield..

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago

    #include <SPI.h>

    #include <PubSubClient.h>

    #include <Ethernet.h>

     

     

    // Pins

    // Analog 0 is the input pin

     

     

    // Variables

    const int Tempin = 0;

    int value;

    float temp,tempC;

    String pubstring;

    unsigned long time;

    char message_buffer[100];

     

     

    // Network Settings

    // MAC address of ethernet shield

    // Look for it on a sticket at the bottom of the shield.

    // Old Arduino Ethernet Shields or clones may not have a dedicated MAC address. Set any hex values here.

    byte MAC_ADDRESS[] = {  0x78, 0x45, 0xC4, 0xAA, 0xE5, 0x6F };

     

     

    // IP address of MQTT server

    //byte MQTT_SERVER[] = {0,0,0,0};

    //byte ip[] = {10,107,1,115};

    EthernetClient ethClient;

    PubSubClient client;

     

     

     

     

    void setup()

    { 

      // Initilize serial link for debugging

      Serial.begin(9600);

     

      Ethernet.begin(MAC_ADDRESS);

      Serial.print("Local IP=");

      Serial.println(Ethernet.localIP());

      client  = PubSubClient("0.0.0.0", 1883, callback, ethClient);

    }

     

     

    void loop()

    {

      if (!client.connected())

      {

        //client.connect("clientID", "mqtt_username", "mqtt_password");

        client.connect("sfo-arduino");

        client.publish("sfo/arduino/alive", "I'm alive!");

      }

      else{

        client.connect("arduino/temperature");

        Serial.println("here i am connected");

        }

     

      //tempC = dtostrf(tempF,5,2,message_buffer);

     

      // Publish sensor reading every X milliseconds

     

     

      if (millis() > (time + 5000)) {

        time = millis();

        value = analogRead(Tempin);

        temp = (value/1024.0)*5000;

        tempC = temp/10;

        pubstring = String(tempC);

         pubstring.toCharArray(message_buffer, pubstring.length() + 1);

        client.publish("arduino/temperature",message_buffer);

        //Serial.println("published!");

        //Serial.println(message_buffer);

    }

       

      // MQTT client loop processing

      client.loop();

    }

     

     

    // Handles messages arrived on subscribed topic(s)

    void callback(char* topic, byte* payload, unsigned int length) {

    }

    hey i am trying a project on reading lm35 temperature sensor and publish in a mqtt broker using arduino.. I am stuck at subscribe mid1:0 after which it starts pinging..may i know what am i doing wrong here??

     

    Thanks in advance..kindly reply asap

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 9 years ago in reply to Former Member

    Did you set the server address?

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Robert Peter Oakes
    Robert Peter Oakes over 9 years ago in reply to Former Member

    As per what clem57 said, the server address is not set for starters "client  = PubSubClient("0.0.0.0", 1883, callback, ethClient);" does not contain a valid server address even if it is a local MQTT server

    second error is

    if (!client.connected())

      {....

    client.connect("sfo-arduino");

    ....

    else

    {

    ....

    client.connect("arduino/temperature");

    ....

    }

     

    if the first connect failed, what makes you think the else clause is going to work ???

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago

    hey thanks for ur suggestions and i have updated my code like this..and i found that my arduino and mqtt is still not getting connected returning rc as -2..here is the code..any idea what is the problem??

    float temp;

    int tempPin = 0;

    #include <SPI.h>

    #include <Ethernet.h>

    #include <PubSubClient.h>

    // Update these with values suitable for your network.

    byte mac[] = { 0x78, 0x45, 0xC4, 0xAA, 0xE5, 0x6F };

    IPAddress ip(192, 168, 1, 108);

    IPAddress server(192, 168, 1, 100);

    char buff[3];

    void callback(char* topic, byte* payload, unsigned int length)

    {

    temp = analogRead(tempPin);

      temp = temp * 0.48828125;

    Serial.print("TEMPRATURE");

      Serial.print(temp);

      Serial.print("*C");

      Serial.println();

      dtostrf(temp,4,3,buff);

    }

    EthernetClient ethClient;

    PubSubClient client(ethClient);

    void reconnect() {

    // Loop until we're reconnected

    while (!client.connected()) {

    Serial.print("Attempting MQTT connection...");

    // Attempt to connect

    if (client.connect("arduinoClient"))

    { Serial.println("connected");

    // Once connected, publish an announcement...

    client.publish("/home/temperature","alive");

    // ... and resubscribe

    client.subscribe("/home/temperature"); }

    else { Serial.print("failed, rc=");

    Serial.print(client.state());

    Serial.println(" try again in 5 seconds");

    // Wait 5 seconds before retrying

    delay(5000); } } }

    void setup()

    { Serial.begin(9600);

    client.setServer(server, 1883);

    client.setCallback(callback);

    Ethernet.begin(mac, ip);

    // Allow the hardware to sort itself out

    delay(1500); }

    void loop()

    { if (!client.connected())

    { reconnect();

    } client.loop();

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago

    hey guys mqtt_basic program itself is failing for me...and arduino mqtt function almost always fails..any ideas??

    kindly reply asap

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • Former Member
    Former Member over 9 years ago

    arduino mqtt connection*

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 9 years ago in reply to Former Member

    How to Ask Questions using the Create Discussion tool

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
<
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube