<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RTOS Archives - Digital Me</title>
	<atom:link href="https://ba0sh1.com/tag/rtos/feed/" rel="self" type="application/rss+xml" />
	<link>https://ba0sh1.com/tag/rtos/</link>
	<description>Electronics, Computers at leisure time</description>
	<lastBuildDate>Sat, 29 Aug 2015 16:04:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
	<item>
		<title>ESP8266 MQTT client on RTOS</title>
		<link>https://ba0sh1.com/2015/08/30/esp8266-mqtt-rtos/</link>
					<comments>https://ba0sh1.com/2015/08/30/esp8266-mqtt-rtos/#comments</comments>
		
		<dc:creator><![CDATA[Baoshi]]></dc:creator>
		<pubDate>Sat, 29 Aug 2015 16:04:49 +0000</pubDate>
				<category><![CDATA[Project]]></category>
		<category><![CDATA[ESP8266]]></category>
		<category><![CDATA[MQTT]]></category>
		<category><![CDATA[RTOS]]></category>
		<guid isPermaLink="false">https://www.ba0sh1.com/?p=1133</guid>

					<description><![CDATA[<p>In the recent Maker Faire I demonstrated an ESP8266 MQTT VFD clock. Some readers had wrote to ask for the soure code. Because the project was builtup within only 2 days, the...</p>
<p>The post <a rel="nofollow" href="https://ba0sh1.com/2015/08/30/esp8266-mqtt-rtos/">ESP8266 MQTT client on RTOS</a> appeared first on <a rel="nofollow" href="https://ba0sh1.com">Digital Me</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In the recent <a href="https://www.ba0sh1.com/maker-faire-yearly-review-rant/" target="_blank" rel="noopener">Maker Faire</a> I demonstrated an ESP8266 MQTT VFD clock. Some readers had wrote to ask for the soure code. Because the project was builtup within only 2 days, the actual source code is more or less of spaghette type. However the underlying MQTT client may be useful to someone who likes the RTOS SDK.</p>
<p><span id="more-1133"></span></p>
<p>If you search MQTT and ESP8266 on the intraweb, most likely all hits can be traced back to the great work done by TuanPM. However Tuan&#8217;s code is based on Espressif&#8217;s NON-OS SDK. There has been some <a href="http://thesparkgap.net/post/92275918408/the-spark-gap-podcast-episode-4" target="_blank" rel="noopener">great debates</a> about embedded programming with-or-without an OS. To me programming with OS vs NON-OS is like programming with C vs Assembly. I like programming in C, so I wrote a new MQTT client for Espressif&#8217;s <a href="https://github.com/espressif/ESP8266_RTOS_SDK" target="_blank" rel="noopener">RTOS SDK.</a></p>
<p>The source code is published at https://github.com/baoshi/ESP-RTOS-Paho</p>
<p>Here are some notes:</p>
<ul>
<li>The code is based on the <a href="http://www.eclipse.org/paho/" target="_blank" rel="noopener">Eclipse Paho project</a>, specifically the embedded C client.</li>
<li>Socket-level APIs used, the code is thread-safe.</li>
<li>Many error handlings were added to the original Paho client, including time-out for most of the network functions. These are designed to work with an RTOS to support automatic error correction at various abused conditions.</li>
<li>I&#8217;m compiling using the &#8220;<a href="http://programs74.ru/udkew-en.html" target="_blank" rel="noopener">Unofficial ESP8266 DevKit</a>&#8220;. Other toolchain such as <a href="https://github.com/pfalcon/esp-open-sdk" target="_blank" rel="noopener">esp-open-sdk</a> can be used as well (adjust pathes in Makefile).</li>
<li>Skeleton code for connecting a MQTT broker is as follows:</li>
</ul>
<p>[code]<br />
struct Network network;<br />
MQTTClient client = DefaultClient;<br />
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;<br />
unsigned char mqtt_buf[100];<br />
unsigned char mqtt_readbuf[100];</p>
<p>NewNetwork(&amp;network);<br />
ConnectNetwork(&amp;network, MQTT_HOST, MQTT_PORT);<br />
NewMQTTClient(&amp;client, &amp;network, 5000, mqtt_buf, 100, mqtt_readbuf, 100);<br />
data.willFlag = 0;<br />
data.MQTTVersion = 3;<br />
data.clientID.cstring = mqtt_client_id; // you client&#8217;s unique identifier<br />
data.username.cstring = MQTT_USER;<br />
data.password.cstring = MQTT_PASS;<br />
data.keepAliveInterval = 10; // interval for PING message to be sent (seconds)<br />
data.cleansession = 0;<br />
MQTTConnect(&amp;client, &amp;data);<br />
[/code]</p>
<p>To subscribe to a MQTT topic, use</p>
<p>[code]<br />
MQTTSubscribe(&amp;client, &quot;/mytopic&quot;, QOS1, topic_received);<br />
[/code]</p>
<p>The parameter topic_received is a callback function handling the received message:</p>
<p>[code]<br />
// Callback when receiving subscribed message<br />
LOCAL void ICACHE_FLASH_ATTR topic_received(MessageData* md)<br />
{<br />
    int i;<br />
    MQTTMessage* message = md-&gt;message;<br />
    dmsg_puts(&quot;Received Topic &quot;);<br />
    for (i = 0; i t; md-&gt;topic-&gt;lenstring.len; ++i)<br />
        dmsg_putchar(md-&gt;topic-&gt;lenstring.data[i]);<br />
    dmsg_puts(&quot;, Message &quot;);<br />
    for (i = 0; i &lt; (int)message-&gt;payloadlen; ++i)<br />
        dmsg_putchar(((char*)message-&gt;payload)[i]);<br />
    dmsg_puts(&quot;rn&quot;);<br />
}<br />
[/code]</p>
<p>To publish a MQTT topic, use</p>
<p>[code]<br />
char msg[PUB_MSG_LEN];<br />
MQTTMessage message;<br />
message.payload = msg;<br />
message.payloadlen = PUB_MSG_LEN;<br />
message.dup = 0;<br />
message.qos = QOS1;<br />
message.retained = 0;<br />
MQTTPublish(&amp;client, &quot;topic&quot;, &amp;message);<br />
[/code]</p>
<p>The demo project included in the library shows how the MQTT related functions can be organized inside a FreeRTOS task and interact with other tasks, such as retry connection after server error, WiFi error, etc. The following diagram may be helpful to understand the code. (Imaging how this can be done using NON-OS SDK)</p>
<figure id="attachment_1141" aria-describedby="caption-attachment-1141" style="width: 600px" class="wp-caption aligncenter"><img decoding="async" class="wp-image-1141" src="http://blog.ba0sh1.com/wordpress/wp-content/uploads/2015/11/28705-mqtt_wifi_threads.png?w=1024&#038;h=576" alt="MQTT and WiFi Thread" width="600" height="337" srcset="https://ba0sh1.com/wp-content/uploads/2015/11/28705-mqtt_wifi_threads.png 1425w, https://ba0sh1.com/wp-content/uploads/2015/11/28705-mqtt_wifi_threads-300x169.png 300w, https://ba0sh1.com/wp-content/uploads/2015/11/28705-mqtt_wifi_threads-1024x576.png 1024w, https://ba0sh1.com/wp-content/uploads/2015/11/28705-mqtt_wifi_threads-768x432.png 768w" sizes="(max-width: 600px) 100vw, 600px" /><figcaption id="caption-attachment-1141" class="wp-caption-text">MQTT and WiFi Thread</figcaption></figure>
<p>As Espressif had just teased us with the new ESP32 chip and RTOS is rumored to be the default SDK, I hope this piece of code will be useful.</p>
<p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://ba0sh1.com/2015/08/30/esp8266-mqtt-rtos/">ESP8266 MQTT client on RTOS</a> appeared first on <a rel="nofollow" href="https://ba0sh1.com">Digital Me</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://ba0sh1.com/2015/08/30/esp8266-mqtt-rtos/feed/</wfw:commentRss>
			<slash:comments>9</slash:comments>
		
		
			</item>
	</channel>
</rss>
