| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
| <html xmlns="http://www.w3.org/1999/xhtml"> |
| <head> |
| <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> |
| <meta http-equiv="X-UA-Compatible" content="IE=9"/> |
| <meta name="generator" content="Doxygen 1.8.14"/> |
| <meta name="viewport" content="width=device-width, initial-scale=1"/> |
| <title>Paho Asynchronous MQTT C Client Library: Publication example</title> |
| <link href="tabs.css" rel="stylesheet" type="text/css"/> |
| <script type="text/javascript" src="jquery.js"></script> |
| <script type="text/javascript" src="dynsections.js"></script> |
| <link href="search/search.css" rel="stylesheet" type="text/css"/> |
| <script type="text/javascript" src="search/searchdata.js"></script> |
| <script type="text/javascript" src="search/search.js"></script> |
| <link href="doxygen.css" rel="stylesheet" type="text/css" /> |
| </head> |
| <body> |
| <div id="top"><!-- do not remove this div, it is closed by doxygen! --> |
| <div id="titlearea"> |
| <table cellspacing="0" cellpadding="0"> |
| <tbody> |
| <tr style="height: 56px;"> |
| <td id="projectlogo"><img alt="Logo" src="pahologo.png"/></td> |
| <td id="projectalign" style="padding-left: 0.5em;"> |
| <div id="projectname">Paho Asynchronous MQTT C Client Library |
| </div> |
| </td> |
| </tr> |
| </tbody> |
| </table> |
| </div> |
| <!-- end header part --> |
| <!-- Generated by Doxygen 1.8.14 --> |
| <script type="text/javascript"> |
| /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ |
| var searchBox = new SearchBox("searchBox", "search",false,'Search'); |
| /* @license-end */ |
| </script> |
| <script type="text/javascript" src="menudata.js"></script> |
| <script type="text/javascript" src="menu.js"></script> |
| <script type="text/javascript"> |
| /* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */ |
| $(function() { |
| initMenu('',true,false,'search.php','Search'); |
| $(document).ready(function() { init_search(); }); |
| }); |
| /* @license-end */</script> |
| <div id="main-nav"></div> |
| <!-- window showing the filter options --> |
| <div id="MSearchSelectWindow" |
| onmouseover="return searchBox.OnSearchSelectShow()" |
| onmouseout="return searchBox.OnSearchSelectHide()" |
| onkeydown="return searchBox.OnSearchSelectKey(event)"> |
| </div> |
| |
| <!-- iframe showing the search results (closed by default) --> |
| <div id="MSearchResultsWindow"> |
| <iframe src="javascript:void(0)" frameborder="0" |
| name="MSearchResults" id="MSearchResults"> |
| </iframe> |
| </div> |
| |
| </div><!-- top --> |
| <div class="header"> |
| <div class="headertitle"> |
| <div class="title">Publication example </div> </div> |
| </div><!--header--> |
| <div class="contents"> |
| <div class="textblock"><div class="fragment"><div class="line"><span class="preprocessor">#include <stdio.h></span></div><div class="line"><span class="preprocessor">#include <stdlib.h></span></div><div class="line"><span class="preprocessor">#include <string.h></span></div><div class="line"><span class="preprocessor">#include "<a class="code" href="_m_q_t_t_async_8h.html">MQTTAsync.h</a>"</span></div><div class="line"></div><div class="line"><span class="preprocessor">#define ADDRESS "tcp://localhost:1883"</span></div><div class="line"><span class="preprocessor">#define CLIENTID "ExampleClientPub"</span></div><div class="line"><span class="preprocessor">#define TOPIC "MQTT Examples"</span></div><div class="line"><span class="preprocessor">#define PAYLOAD "Hello World!"</span></div><div class="line"><span class="preprocessor">#define QOS 1</span></div><div class="line"><span class="preprocessor">#define TIMEOUT 10000L</span></div><div class="line"></div><div class="line"><span class="keyword">volatile</span> <a class="code" href="_m_q_t_t_async_8h.html#a7ca6d2a1813f2bbd0bc3af2771e46ba4">MQTTAsync_token</a> deliveredtoken;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> finished = 0;</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> connlost(<span class="keywordtype">void</span> *context, <span class="keywordtype">char</span> *cause)</div><div class="line">{</div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client = (<a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a>)context;</div><div class="line"> <a class="code" href="struct_m_q_t_t_async__connect_options.html">MQTTAsync_connectOptions</a> conn_opts = <a class="code" href="_m_q_t_t_async_8h.html#ae18b51f22784a43803eb809d6a0c2492">MQTTAsync_connectOptions_initializer</a>;</div><div class="line"> <span class="keywordtype">int</span> rc;</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"\nConnection lost\n"</span>);</div><div class="line"> printf(<span class="stringliteral">" cause: %s\n"</span>, cause);</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Reconnecting\n"</span>);</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;</div><div class="line"> <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#a0388b226a414b09fa733f6d65004ec32">MQTTAsync_connect</a>(client, &conn_opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line"> {</div><div class="line"> printf(<span class="stringliteral">"Failed to start connect, return code %d\n"</span>, rc);</div><div class="line"> finished = 1;</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onDisconnect(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__success_data.html">MQTTAsync_successData</a>* response)</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">"Successful disconnection\n"</span>);</div><div class="line"> finished = 1;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onSend(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__success_data.html">MQTTAsync_successData</a>* response)</div><div class="line">{</div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client = (<a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a>)context;</div><div class="line"> <a class="code" href="struct_m_q_t_t_async__disconnect_options.html">MQTTAsync_disconnectOptions</a> opts = <a class="code" href="_m_q_t_t_async_8h.html#a2fd5d6df31928ae468f3f2e522b9c707">MQTTAsync_disconnectOptions_initializer</a>;</div><div class="line"> <span class="keywordtype">int</span> rc;</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Message with token value %d delivery confirmed\n"</span>, response-><a class="code" href="struct_m_q_t_t_async__success_data.html#af8f771e67d284379111151b003c0d810">token</a>);</div><div class="line"></div><div class="line"> opts.<a class="code" href="struct_m_q_t_t_async__disconnect_options.html#ac13fb68f736854fcab131b34756bfceb">onSuccess</a> = onDisconnect;</div><div class="line"> opts.<a class="code" href="struct_m_q_t_t_async__disconnect_options.html#ae376f130b17d169ee51be68077a89ed0">context</a> = client;</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#adc69afa4725f8321bdaa5a05aec5cfd5">MQTTAsync_disconnect</a>(client, &opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line"> {</div><div class="line"> printf(<span class="stringliteral">"Failed to start sendMessage, return code %d\n"</span>, rc);</div><div class="line"> exit(EXIT_FAILURE);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onConnectFailure(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__failure_data.html">MQTTAsync_failureData</a>* response)</div><div class="line">{</div><div class="line"> printf(<span class="stringliteral">"Connect failed, rc %d\n"</span>, response ? response-><a class="code" href="struct_m_q_t_t_async__failure_data.html#a45a5b7c00a796a23f01673cef1dbe0a9">code</a> : 0);</div><div class="line"> finished = 1;</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">void</span> onConnect(<span class="keywordtype">void</span>* context, <a class="code" href="struct_m_q_t_t_async__success_data.html">MQTTAsync_successData</a>* response)</div><div class="line">{</div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client = (<a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a>)context;</div><div class="line"> <a class="code" href="struct_m_q_t_t_async__response_options.html">MQTTAsync_responseOptions</a> opts = <a class="code" href="_m_q_t_t_async_8h.html#a3f8b408243b5c2369bc9758f2edf0878">MQTTAsync_responseOptions_initializer</a>;</div><div class="line"> <a class="code" href="struct_m_q_t_t_async__message.html">MQTTAsync_message</a> pubmsg = <a class="code" href="_m_q_t_t_async_8h.html#a6a85061dadab532f28e96e5ab3c600e9">MQTTAsync_message_initializer</a>;</div><div class="line"> <span class="keywordtype">int</span> rc;</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Successful connection\n"</span>);</div><div class="line"></div><div class="line"> opts.<a class="code" href="struct_m_q_t_t_async__response_options.html#ac13fb68f736854fcab131b34756bfceb">onSuccess</a> = onSend;</div><div class="line"> opts.<a class="code" href="struct_m_q_t_t_async__response_options.html#ae376f130b17d169ee51be68077a89ed0">context</a> = client;</div><div class="line"></div><div class="line"> pubmsg.<a class="code" href="struct_m_q_t_t_async__message.html#a9eff55064941fb604452abb0050ea99d">payload</a> = PAYLOAD;</div><div class="line"> pubmsg.<a class="code" href="struct_m_q_t_t_async__message.html#aa3cb44feb3ae6d11b3a4cad2d94cb33a">payloadlen</a> = strlen(PAYLOAD);</div><div class="line"> pubmsg.<a class="code" href="struct_m_q_t_t_async__message.html#a35738099155a0e4f54050da474bab2e7">qos</a> = QOS;</div><div class="line"> pubmsg.<a class="code" href="struct_m_q_t_t_async__message.html#a6a4904c112507a43e7dc8495b62cc0fc">retained</a> = 0;</div><div class="line"> deliveredtoken = 0;</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#a5687171e67e98f9ea590c9e3b64cde18">MQTTAsync_sendMessage</a>(client, TOPIC, &pubmsg, &opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line"> {</div><div class="line"> printf(<span class="stringliteral">"Failed to start sendMessage, return code %d\n"</span>, rc);</div><div class="line"> exit(EXIT_FAILURE);</div><div class="line"> }</div><div class="line">}</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>* argv[])</div><div class="line">{</div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#a0db1d736cdc0c864fe41abb3afd605bd">MQTTAsync</a> client;</div><div class="line"> <a class="code" href="struct_m_q_t_t_async__connect_options.html">MQTTAsync_connectOptions</a> conn_opts = <a class="code" href="_m_q_t_t_async_8h.html#ae18b51f22784a43803eb809d6a0c2492">MQTTAsync_connectOptions_initializer</a>;</div><div class="line"> <a class="code" href="struct_m_q_t_t_async__message.html">MQTTAsync_message</a> pubmsg = <a class="code" href="_m_q_t_t_async_8h.html#a6a85061dadab532f28e96e5ab3c600e9">MQTTAsync_message_initializer</a>;</div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#a7ca6d2a1813f2bbd0bc3af2771e46ba4">MQTTAsync_token</a> token;</div><div class="line"> <span class="keywordtype">int</span> rc;</div><div class="line"></div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#a5462c4618d0a229116db5fbadacf95d2">MQTTAsync_create</a>(&client, ADDRESS, CLIENTID, <a class="code" href="_m_q_t_t_client_persistence_8h.html#ae01e089313a65ac4661ed216b6ac00fa">MQTTCLIENT_PERSISTENCE_NONE</a>, NULL);</div><div class="line"></div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#ae9ae8d61023e7029ef5a19f5219c3599">MQTTAsync_setCallbacks</a>(client, NULL, connlost, NULL, NULL);</div><div class="line"></div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ac8dd0930672a9c7d71fc645aa1f0521d">keepAliveInterval</a> = 20;</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#a036c36a2a4d3a3ffae9ab4dd8b3e7f7b">cleansession</a> = 1;</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ac13fb68f736854fcab131b34756bfceb">onSuccess</a> = onConnect;</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#a09ce26d7cff24e14a6844eaae7b15290">onFailure</a> = onConnectFailure;</div><div class="line"> conn_opts.<a class="code" href="struct_m_q_t_t_async__connect_options.html#ae376f130b17d169ee51be68077a89ed0">context</a> = client;</div><div class="line"> <span class="keywordflow">if</span> ((rc = <a class="code" href="_m_q_t_t_async_8h.html#a0388b226a414b09fa733f6d65004ec32">MQTTAsync_connect</a>(client, &conn_opts)) != <a class="code" href="_m_q_t_t_async_8h.html#afe0cffcce8efe25186f79c51ac44e16f">MQTTASYNC_SUCCESS</a>)</div><div class="line"> {</div><div class="line"> printf(<span class="stringliteral">"Failed to start connect, return code %d\n"</span>, rc);</div><div class="line"> exit(EXIT_FAILURE);</div><div class="line"> }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Waiting for publication of %s\n"</span></div><div class="line"> <span class="stringliteral">"on topic %s for client with ClientID: %s\n"</span>,</div><div class="line"> PAYLOAD, TOPIC, CLIENTID);</div><div class="line"> <span class="keywordflow">while</span> (!finished)</div><div class="line"><span class="preprocessor"> #if defined(WIN32) || defined(WIN64)</span></div><div class="line"> Sleep(100);</div><div class="line"><span class="preprocessor"> #else</span></div><div class="line"> usleep(10000L);</div><div class="line"><span class="preprocessor"> #endif</span></div><div class="line"></div><div class="line"> <a class="code" href="_m_q_t_t_async_8h.html#ad5562f9dc71fbd93d25ad20b328cb887">MQTTAsync_destroy</a>(&client);</div><div class="line"> <span class="keywordflow">return</span> rc;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents --> |
| <!-- start footer part --> |
| <hr class="footer"/><address class="footer"><small> |
| Generated on Thu Sep 13 2018 13:40:20 for Paho Asynchronous MQTT C Client Library by  <a href="http://www.doxygen.org/index.html"> |
| <img class="footer" src="doxygen.png" alt="doxygen"/> |
| </a> 1.8.14 |
| </small></address> |
| </body> |
| </html> |