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 4092 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…
  • bobcroft
    bobcroft over 10 years ago

    Radhamani,

                             you do not appear to have set an IP address for your Ethernet shield and you do not appear to be using DHCP to get an IP address.  So you are not sending anything to the MQTT server.  You can prove whether or not your   Ethernet card / shield is working once you have an IP address by either 'pinging' from a Windows 'cmd' line.  Type cmd in the Windows search box, this will bring up cmd.exe, click on that to bring up the command window and in the window type "ping 192.168.1.xx" Do not type the ""  and enter your own IP address at xx.

     

    Hope that helps

     

    Bob

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 10 years ago in reply to bobcroft

    bobcroft is right. Look at Ethernet.begin(mac); and change appropriately. Further along, your callback does not handle any messages! This looks like skeleton code at best.


    Cheers,

    Clem

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

    Thanks Bob...

    I tried the below program and connected RJ45 cable to my router.

     

    imageimage

    #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[]     = { 192, 168, 1, 20 };

    byte gateway[]     = { 192, 168, 1, 1 };

    byte subnet[]     = { 255, 255, 255, 0 };

     

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

      // handle message arrived

    }

     

    EthernetClient ethClient;

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

    EthernetServer Eserver(80);

    void setup()

    {

      Ethernet.begin(mac,ip,gateway,subnet);

       Serial.println(Ethernet.localIP());

       if (Ethernet.begin(mac) == 0) {

        Serial.println("Failed to configure Ethernet using DHCP");

        }

      // print your local IP address:

     

     

     

    /*  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();

     

    }

     

    I uploaded the sketch without any issues.But there is no output still and no results in Serial monitor.

    I tried ping,

    C:\Users\Radhamani>ping 192.168.1.20

     

    Pinging 192.168.1.20 with 32 bytes of data:

    Reply from 192.168.1.6: Destination host unreachable.

    Reply from 192.168.1.6: Destination host unreachable.

    Reply from 192.168.1.6: Destination host unreachable.

    Reply from 192.168.1.6: Destination host unreachable.

    Ping statistics for 192.168.1.20:

        Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

     

    Results of ipconfig is:

    Wireless LAN adapter Wi-Fi:

       Connection-specific DNS Suffix  . :

       Link-local IPv6 Address . . . . . : fe80::901:3297:48ad:9e7d%3

       IPv4 Address. . . . . . . . . . . : 192.168.1.6

       Subnet Mask . . . . . . . . . . . : 255.255.255.0

       Default Gateway . . . . . . . . . : 192.168.1.1

     

    Not sure whether IP address is still not getting set for Ethernet address.

    Arduino is still not showing any output regarding Serial.println image .I am missing something here...Kindly help me....

    Let me know if you need any more details...

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

    Hi Clem,I am just doing basic setup of Ethernet configuration in this sketch.Hence,I hope callback is not required at this stage..

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

    Is this the exact code your using, if so then you still have most of the significant code commented out, it is probably showing green on your IDE

     

    Based on what is commented out, I'm not surprised its not showing anything

     

    for instance

     

    /*  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");
      }*/

    will do nothing as it is all commented out

     

    Can you confirm exactly what your uploading

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

    Radhamani, it looks to me as though you have you Ethernet addresses muddled.  The address 192.168.1.6 is your PC address.  You have assigned address 20 to your Ethernet adapter but do you know for sure that address is not used by something else?

     

    I strongly recommend you try the Arduino examples for the Ethernet card in an unmodified form.  That way you can confirm your Ethernet is working and that you can get a connection.  I would also urge you to try the examples that obtain an IP address automatically.  Then you can modify the code to add the MQTT code.  I find that when developing code it is best to start with something that works then gradually build the code to achieve what you want to do.  If you hit a snag and something stops working you only have to look at your latest changes.  Once you have the Ethernet card working you can try the MQTT example code changing only the necessary addresses.

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

    Hi Bob,

      Sure...Thanks much..I am following as you have adviced...

    I tested with DHCPAddressPrinter sample.Still there is no results on Serial.println.

    By executing ipconfig in cmd.exe,I am not seeing 192.168.1.20 in the output.

    Complete result of ipconfig:

    Wireless LAN adapter Wi-Fi:

       Connection-specific DNS Suffix  . :

       Link-local IPv6 Address . . . . . : fe80::901:3297:48ad:9e7d%3

       IPv4 Address. . . . . . . . . . . : 192.168.1.6

       Subnet Mask . . . . . . . . . . . : 255.255.255.0

       Default Gateway . . . . . . . . . : 192.168.1.1

    Ethernet adapter VMware Network Adapter VMnet1:

       Connection-specific DNS Suffix  . :

       Link-local IPv6 Address . . . . . : fe80::c954:40b2:c8c1:de0%14

       IPv4 Address. . . . . . . . . . . : 192.168.198.1

       Subnet Mask . . . . . . . . . . . : 255.255.255.0

       Default Gateway . . . . . . . . . :

    Ethernet adapter VMware Network Adapter VMnet8:

       Connection-specific DNS Suffix  . :

       Link-local IPv6 Address . . . . . : fe80::2837:7e01:c799:d627%15

       IPv4 Address. . . . . . . . . . . : 192.168.234.1

       Subnet Mask . . . . . . . . . . . : 255.255.255.0

       Default Gateway . . . . . . . . . :

     

    Please help me if I am missing something..

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

    Hi Peter,

    I am trying to first setup MAC and IP address for the EthernetShield .Thatswhy I have commented out the MQTT portion of code in the sketch.

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

    Are you using VMware? What is running in it? That can change things if you are using it for the connections.

    Clem

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

    I am using VMWare for MessageSight which acts as MQTT server.

    But for this initial setup for Ethernet Shield,I have not even started the Appliance in VMWare.

    I am just running the below DHCPAddressPrinter sketch in Arduino example to setup EthernetShield with MAC and IP

     

     

    #include <SPI.h>

    #include <Ethernet.h>

     

     

    // Enter a MAC address for your controller below.

    // Newer Ethernet shields have a MAC address printed on a sticker on the shield

    byte mac[] = {

      0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02

    };

     

     

    // Initialize the Ethernet client library

    // with the IP address and port of the server

    // that you want to connect to (port 80 is default for HTTP):

    EthernetClient client;

     

     

    void setup() {

      // Open serial communications and wait for port to open:

      Serial.begin(9600);

      // this check is only needed on the Leonardo:

      while (!Serial) {

        ; // wait for serial port to connect. Needed for Leonardo only

      }

     

     

      // start the Ethernet connection:

      if (Ethernet.begin(mac) == 0) {

        Serial.println("Failed to configure Ethernet using DHCP");

        // no point in carrying on, so do nothing forevermore:

        for (;;)

          ;

      }

      // print your local IP address:

      Serial.print("My IP address: ");

      for (byte thisByte = 0; thisByte < 4; thisByte++) {

        // print the value of each byte of the IP address:

        Serial.print(Ethernet.localIP()[thisByte], DEC);

        Serial.print(".");

      }

      Serial.println();

    }

     

     

    void loop() {

     

     

    }

     

    After uploading the sketch to arduino,below is the output shown in console

     

    Sketch uses 13,614 bytes (42%) of program storage space. Maximum is 32,256 bytes.

    Global variables use 539 bytes (26%) of dynamic memory, leaving 1,509 bytes for local variables. Maximum is 2,048 bytes.

     

    And there is nothing shown in Serial monitor

    In my previous post,I have attached the image of Ethernetshield's some leds glowing after connecting to Router.

    My router is Binatone Wireless ADSL2+ Router.

    I checked arp -a,

    C:\Users\Radhamani>arp -a

     

     

    Interface: 192.168.1.6 --- 0x3

      Internet Address      Physical Address      Type

      192.168.1.1           6c-fd-b9-24-c6-fe     dynamic

      192.168.1.255         ff-ff-ff-ff-ff-ff     static

      224.0.0.22            01-00-5e-00-00-16     static

      224.0.0.251           01-00-5e-00-00-fb     static

      224.0.0.252           01-00-5e-00-00-fc     static

      239.255.255.250       01-00-5e-7f-ff-fa     static

      255.255.255.255       ff-ff-ff-ff-ff-ff     static

     

     

    Interface: 192.168.198.1 --- 0xe

      Internet Address      Physical Address      Type

      192.168.198.255       ff-ff-ff-ff-ff-ff     static

      224.0.0.22            01-00-5e-00-00-16     static

      224.0.0.251           01-00-5e-00-00-fb     static

      224.0.0.252           01-00-5e-00-00-fc     static

      239.255.255.250       01-00-5e-7f-ff-fa     static

     

     

    Interface: 192.168.234.1 --- 0xf

      Internet Address      Physical Address      Type

      192.168.234.255       ff-ff-ff-ff-ff-ff     static

      224.0.0.22            01-00-5e-00-00-16     static

      224.0.0.251           01-00-5e-00-00-fb     static

      224.0.0.252           01-00-5e-00-00-fc     static

      239.255.255.250       01-00-5e-7f-ff-fa     static

     

    I tried many ways.But till now no luck..I am not sure where I am wrong

    • 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