{"id":343,"date":"2016-05-24T23:21:45","date_gmt":"2016-05-24T23:21:45","guid":{"rendered":"http:\/\/fenstar.co\/wp\/?p=343"},"modified":"2023-01-21T17:57:58","modified_gmt":"2023-01-21T17:57:58","slug":"programming-the-esp-01-to-control-rgb-strips-via-mqtt","status":"publish","type":"post","link":"https:\/\/fenstar.co\/wp\/?p=343","title":{"rendered":"Programming the ESP-01 to control RGB strips via MQTT"},"content":{"rendered":"<p>Now that I have shown you two different hardware setups for the RGB strips, it is time to talk about the chip firmware and software.<\/p>\n<p>I have chosen to use <a href=\"http:\/\/nodemcu.com\/index_en.html\" target=\"_blank\">NodeMCU<\/a> which uses <a href=\"https:\/\/www.lua.org\/start.html\" target=\"_blank\">Lua<\/a> as the programming language. The good news is that all the functions you need are already there. As a result, even someone like me who doesn&#8217;t know Lua can easily write it.<\/p>\n<p>And, thanks to a number of online blogs detailing what is going on, it is mostly copypasting existing code. Mostly this is adapted from the blog of\u00a0<a href=\"http:\/\/openhardware.co.za\" target=\"_blank\">openhardware.co.za<\/a>. There were a few things unclear, but I suggest looking at it as he has much more verbose documentation.<\/p>\n<p><!--more--><\/p>\n<p>For the most part, what is going on (at least on the surface) is fairly self explanatory, so I am just going to talk about how it takes the MQTT stuff and knows what to do with it. When the device gets a message this line in the code is triggered.<\/p>\n<p><span style=\"margin: auto; font-family: 'Courier New'; width: 100%; font-size: 80%; border: 1px solid grey; padding: 10px 10px 10px 0px;\"><span style=\"background-color: lightgrey; padding: 10px;\">46<\/span> m:on(&#8220;message&#8221;, function(conn, topic, data)<\/span><\/p>\n<p>There are two\u00a0vars returned,\u00a0<span style=\"font-family: 'Courier New';\">topic<\/span>\u00a0and\u00a0<span style=\"font-family: 'Courier New';\">data<\/span>. In MQTT the <span style=\"font-family: 'Courier New';\">topic<\/span> works\u00a0a\u00a0lot\u00a0like a folder structure in HTML, only instead of asking for the data in the specified folder<a href=\"#Note1\"><sup>1<\/sup><\/a>\u00a0you get a message with the <span style=\"font-family: 'Courier New';\">data<\/span> telling you what &#8220;folder&#8221; it is in.<\/p>\n<p>In order to parse this message, I first check to be sure the data is valid. Because the <span style=\"font-family: 'Courier New';\">data<\/span> is being sent as text, I use <span style=\"font-family: 'Courier New';\">tonumber()<\/span> to get an int<a href=\"#Note2\"><sup>2<\/sup><\/a> value from the data. In addition, the int needs to be in a certain range in order to work.<a href=\"#Note3\"><sup>3<\/sup><\/a> Thus, the <span style=\"font-family: 'Courier New';\">data<\/span> check looks like this:<\/p>\n<p><span style=\"margin: auto; font-family: 'Courier New'; width: 100%; font-size: 80%; border: 1px solid grey; padding: 10px 10px 10px 0px;\"><span style=\"background-color: lightgrey; padding: 10px;\">48<\/span>\u00a0if ( (tonumber(data) &gt;= 0) and (tonumber(data) &lt;=102) ) then<\/span><\/p>\n<p>After this, it is just sorting out which &#8220;folder&#8221; the data is in so that we know which pin to change. If the message\u00a0is, for example, telling it to change the red LED then the <span style=\"font-family: 'Courier New';\">topic<\/span> will be <span style=\"font-family: 'Courier New';\">\/home\/Living\/esp01\/RED<\/span>\u00a0if you are\u00a0using the preset values in the script.<\/p>\n<p><span style=\"margin: auto; font-family: 'Courier New'; font-size: 80%; width: 100%; border: 1px solid grey; padding: 10px 10px 10px 0px;\"><span style=\"background-color: lightgrey; padding: 10px;\">49<\/span> if (topic == &#8220;\/home\/&#8221;.. RoomID ..&#8221;\/&#8221; .. DeviceID .. &#8220;\/RED&#8221;) then<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><a id=\"Note1\"><\/a>1\u00a0Well, you do specify what &#8220;folders&#8221; you want the server to notify you about when you subscribe.<\/p>\n<p><a id=\"Note2\"><\/a>2 It may be a float, but that is not super important for this context<\/p>\n<p><a id=\"Note3\"><\/a>3\u00a0The max value I can send to the PWM pin is 1023 because of how I used !! pwm.setup() and I am multiplying it by 10 because I have openHAB sending it a value from 0 to 100. As a result, my range of valid int values is 0 to 102. It is convoluted, but I didn&#8217;t want to delve into pwm.setup() and this works.<\/p>\n<p><a href=\"http:\/\/fenstar.co\/Content\/code\/init.lua\">Here is the Lua code.<\/a><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Now that I have shown you two different hardware setups for the RGB strips, it is time to talk about the chip firmware and software. I have chosen to use NodeMCU which uses Lua as the programming language. The good &hellip; <a class=\"more-link\" href=\"https:\/\/fenstar.co\/wp\/?p=343\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[36,3],"_links":{"self":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts\/343"}],"collection":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=343"}],"version-history":[{"count":17,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":360,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=\/wp\/v2\/posts\/343\/revisions\/360"}],"wp:attachment":[{"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fenstar.co\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}