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 Arduino and MQTT
  • 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
  • State Suggested Answer
  • Replies 9 replies
  • Answers 2 answers
  • Subscribers 392 subscribers
  • Views 698 views
  • Users 0 members are here
Related

Arduino and MQTT

l1t7l3ph0o7
l1t7l3ph0o7 over 10 years ago

So I've started to play with MQTT to allow my arduinos and other things to communicate with each other, it's got great potential to allow me to have my projects communicate with each other.

So let me break it down.

I set up an MQTT broker and tested it with an android app on my phone and an application on my laptop. I can send and receive messages no problem. So I loaded the example sketch and set the appropriate server ip, local ip, and mac address's; Then power up, I get the HELLO WORLD from outTopic on my other devices.

 

But what I can't figure out, is how can I trigger an event from a received MQTT message?

I have read through the mqtt basic and publish in callback examples over and over, but can't figure this out.

lets say I send a message to inTopic with a value, what do I need to write to tell it "hey look for this message under this topic, when received, do whatever action"?

it's not very clear from the example.

 

Thank you to anyone who wishes to help or give me a link to a useful tutorial, all I've been able to find only seem to cover sending messages rather than receiving them.

  • Sign in to reply
  • Cancel
Parents
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago

    I guess more specifically, looking at this bit:

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

      // handle message arrived

    }

    And this bit:

    void setup()

    {

      Ethernet.begin(mac, ip);

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

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

        client.subscribe("inTopic");

      }

    }

     

    I assume if I wish to turn on an led I'd write :

    digitalwrite (ledPin, HIGH0;

    inside the curly braces of the first bit

    and from the second bit I assume it's subscribing to the topic "inTopic"

    but how do I define the Message at which the led would turn on?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to l1t7l3ph0o7

    the example you provide has no setup of the callback, do you have some code missing or not posted ?

     

    Can you post the complete sketch so I can see what your doing

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Reply
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to l1t7l3ph0o7

    the example you provide has no setup of the callback, do you have some code missing or not posted ?

     

    Can you post the complete sketch so I can see what your doing

     

    Thanks

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
Children
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago in reply to Robert Peter Oakes

    Here is the complete sketch, it's just the example provided from the library with a few minor alterations:

    /*

    Basic MQTT example

     

      - connects to an MQTT server

      - publishes "hello world" to the topic "outTopic"

      - subscribes to the topic "inTopic"

    */

     

     

    #include <SPI.h>

    #include <Ethernet.h>

    #include <PubSubClient.h>

    int ledPin = 5;

     

     

    // Update these with values suitable for your network.

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

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

    byte ip[]     = { 192, 168, 1, 50 };

     

     

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

      // handle message arrived

    }

     

     

    EthernetClient ethClient;

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

     

     

    void setup()

    {

      Ethernet.begin(mac, ip);

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

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

        client.subscribe("inTopic");

      }

    }

     

     

    void loop()

    {

      client.loop();

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to l1t7l3ph0o7

    Do you have a server configured somewhere ?, this is a client example and in that is ok, but it needs to talk to an MQTT Server in order for  all this to work

     

    I am currently playing with MQTT servers with the SAMA5D3 boards and have no issues but you do need the server and he client for it all to work

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago in reply to Robert Peter Oakes

    Yes, I have a Broker set up on my server at 192.168.1.199.

    I have an mqtt client on my android phone and a client on my computer. I can send mqtt messages from either device and receive mqtt messages from subscribed topics on either device.

    I have another sketch that sends mqtt message on a button press and that works well, but right now I'm trying to learn how to execute an action when a specific message is received from the subscribed topic.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to l1t7l3ph0o7

    ah, well this may help then

    http://www.element14.com/community/groups/arduino/blog/2014/06/09/fast-track-to-arduino-programming

     

    a series of videos for the arduino and the one for Ethernet deals with interpreting messages received from the network

     

    Hoe you find it useful

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • Cancel
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago in reply to Robert Peter Oakes

    Thank you very Much, Really appreciate it, just started looking and I can already tell this will help a lot. Really appreciate it!

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • l1t7l3ph0o7
    0 l1t7l3ph0o7 over 10 years ago in reply to l1t7l3ph0o7

    Seriously, really thank you. I have much better understanding now.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • Robert Peter Oakes
    0 Robert Peter Oakes over 10 years ago in reply to l1t7l3ph0o7

    your welcome

     

    Peter

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Reject Answer
    • 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