{"id":365,"date":"2018-02-03T14:03:53","date_gmt":"2018-02-03T19:03:53","guid":{"rendered":"http:\/\/factormystic.net\/blog\/?p=365"},"modified":"2018-02-03T14:06:46","modified_gmt":"2018-02-03T19:06:46","slug":"a-fun-little-mqtt-program-for-the-esp8266","status":"publish","type":"post","link":"https:\/\/factormystic.net\/blog\/a-fun-little-mqtt-program-for-the-esp8266","title":{"rendered":"A fun little MQTT program for the ESP8266"},"content":{"rendered":"<p>I know that <a href=\"https:\/\/factormystic.net\/blog\/hello-world-with-a-wemos-d1-esp8266\">last time<\/a>\u00a0I said I was going to start soldering, but I really wanted to play with the networking capabilities of the ESP8266 first.<\/p>\n<p>There&#8217;s a bunch of example programs to do web\u00a0requests, such as BasicHttpClient, HTTPSRequest, WifiClient, StreamHttpClient, etc. I had trouble getting these working because there&#8217;s no built in certificate store or TLS validation capabilities. That means you can&#8217;t do a &#8220;normal&#8221; HTTPS request to test services like RequestBin (since they&#8217;re HTTPS only). The example programs have you type in the server&#8217;s SHA1 thumbprint, but\u00a0that didn&#8217;t seem to work for me. The certs I inspected were SHA256, which I assume is the problem.<\/p>\n<p>Anyway, I&#8217;m not interested in doing HTTP in any of my project ideas right now, so I moved on to what I actually want to do, which is <a href=\"https:\/\/en.wikipedia.org\/wiki\/MQTT\">MQTT<\/a>. Once again <a href=\"https:\/\/hackaday.com\/2016\/05\/09\/minimal-mqtt-building-a-broker\/\">it was Hack-a-day that clued me in<\/a> to this protocol, which is very popular for small devices &amp; home automation. I started out looking for a &#8220;simplest possible MQTT example for ESP8266&#8221; and didn&#8217;t find anything simple enough initially. Later I realized that there&#8217;s two great places to start looking for libraries &amp; examples. First is the <a href=\"https:\/\/github.com\/esp8266\/Arduino\/blob\/master\/doc\/libraries.rst\">esp8266\/Arduino<\/a>\u00a0repo on Github, which has a list of miscellaneous third party projects compatible with this specific chip. Second is in the Arduino IDE itself; the Library Manager is searchable:<\/p>\n<div id=\"attachment_366\" style=\"width: 994px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/factormystic.net\/blog\/?attachment_id=366\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-366\" class=\"wp-image-366 size-full\" src=\"\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/arduino-library-manager-mqtt-search.png\" alt=\"Arduino Library Manager - Searching for &quot;MQTT&quot;\" width=\"984\" height=\"578\" srcset=\"https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/arduino-library-manager-mqtt-search.png 984w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/arduino-library-manager-mqtt-search-300x176.png 300w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/arduino-library-manager-mqtt-search-624x366.png 624w\" sizes=\"auto, (max-width: 984px) 100vw, 984px\" \/><\/a><p id=\"caption-attachment-366\" class=\"wp-caption-text\">Arduino Library Manager &#8211; Searching for &#8220;MQTT&#8221;<\/p><\/div>\n<p>The problem here is that which (if any) are actually good, useful, or correct for the ESP8266. The first search result in that screenshot is only for the &#8220;Arduino Uno Wifi Developer Edition&#8221;, for example.<\/p>\n<p>Another challenge here is working through all the company branding. The second library listed, &#8220;Adafruit MQTT Library&#8221;, is ESP8266 compatible and comes with a &#8220;simple&#8221; example program to get started. However, it&#8217;s oriented around the\u00a0<a href=\"https:\/\/io.adafruit.com\">Adafruit IO<\/a>\u00a0IoT web service (which is apparently a thing). I did get it to work with the local MQTT broker I&#8217;m running here on my PC, but I had to guess if might and try to peel away their extra stuff just to get to the bones.<\/p>\n<p>The ESP8266 Github linked to <a href=\"https:\/\/github.com\/Imroy\/pubsubclient\">lmroy\/pubsubclient<\/a>, which itself is a fork of\u00a0<a href=\"https:\/\/github.com\/knolleary\/pubsubclient\">knolleary\/pubsubclient<\/a>\u00a0which seems more up to date. I don&#8217;t know why they&#8217;re linking to an out of date fork, except that it appears to more easily support &#8220;large&#8221; messages. The original has a default max packet size of 128 bytes, which might be too small for real apps, but I&#8217;m looking for a simple example so it should be fine.<\/p>\n<p>Here&#8217;s a link to the example program from that repo:\u00a0<a href=\"https:\/\/github.com\/knolleary\/pubsubclient\/blob\/master\/examples\/mqtt_esp8266\/mqtt_esp8266.ino\">https:\/\/github.com\/knolleary\/pubsubclient\/blob\/master\/examples\/mqtt_esp8266\/mqtt_esp8266.ino<\/a><\/p>\n<p><a href=\"https:\/\/factormystic.net\/blog\/?attachment_id=375\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-375 size-full\" src=\"\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/mqtt-server-hostname.png\" alt=\"mqtt-server-hostname\" width=\"554\" height=\"204\" srcset=\"https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/mqtt-server-hostname.png 554w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/mqtt-server-hostname-300x110.png 300w\" sizes=\"auto, (max-width: 554px) 100vw, 554px\" \/><\/a><\/p>\n<p>The example is easy to set up; just punch in your Wifi access info &amp; MQTT broker host name. Interestingly it does apparently support DNS&#8230; from working with the HTTP examples earlier, some of them used IP addresses rather than host names, so it wasn&#8217;t clear if DNS was supported in some of these network libraries. This one does, apparently.<\/p>\n<p>Here&#8217;s what the output looks like. I&#8217;m running mosquitto 1.4.8 on my PC, with mosquitto_sub running in the lower panel subscribed to # (a wildcard, so all topic messages are shown).<\/p>\n<div id=\"attachment_376\" style=\"width: 1081px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/factormystic.net\/blog\/a-fun-little-mqtt-program-for-the-esp8266\/basic-mqtt-example\" rel=\"attachment wp-att-376\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-376\" class=\"size-full wp-image-376\" src=\"\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/basic-mqtt-example.png\" alt=\"Basic MQTT Example on the ESP8266\" width=\"1071\" height=\"704\" srcset=\"https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/basic-mqtt-example.png 1071w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/basic-mqtt-example-300x197.png 300w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/basic-mqtt-example-1024x673.png 1024w, https:\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/basic-mqtt-example-624x410.png 624w\" sizes=\"auto, (max-width: 1071px) 100vw, 1071px\" \/><\/a><p id=\"caption-attachment-376\" class=\"wp-caption-text\">Basic MQTT Example on the ESP8266<\/p><\/div>\n<div id=\"attachment_378\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/factormystic.net\/blog\/a-fun-little-mqtt-program-for-the-esp8266\/its-working\" rel=\"attachment wp-att-378\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-378\" class=\"size-full wp-image-378\" src=\"\/\/factormystic.net\/blog\/wp-content\/uploads\/2018\/02\/its-working.gif\" alt=\"Actual footage of me as this program was running\" width=\"400\" height=\"170\" \/><\/a><p id=\"caption-attachment-378\" class=\"wp-caption-text\">Actual footage of me as this program was running<\/p><\/div>\n<p>I thought it would be fun to give the messages a little personality, so I found a list of all the voice lines from the turrets in Portal 2, copied them into a giant array, and now instead of &#8220;Hello World #37&#8221; it&#8217;ll send something like &#8220;So what am I, uh, supposed to do here?&#8221; or &#8220;Well, I tried. Best of Luck!&#8221; once every few seconds.<\/p>\n<p>Additionally, I made it so that the power LED blinks as it&#8217;s sending a message, as a little visual chirp to let you know it&#8217;s alive.<\/p>\n<p><iframe loading=\"lazy\" class=\"giphy-embed\" src=\"https:\/\/giphy.com\/embed\/xThta4QG3HEuA16qMo\" width=\"480\" height=\"270\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><br \/>\n<a href=\"https:\/\/giphy.com\/gifs\/xThta4QG3HEuA16qMo\">via GIPHY<\/a><\/p>\n<p>The code is <a href=\"https:\/\/gist.github.com\/factormystic\/8d9de7baf1bb583f1b6c7a2d3978f118\">here<\/a>, and this version is a modification of the Adafruit MQTT example, rather than the other library linked above, because I wrote it before I discovered that simpler example.\u00a0(Found the list of voice lines <a href=\"http:\/\/combineoverwiki.net\/wiki\/Aperture_Science_Sentry_Turret\/Quotes\">here<\/a>, and removed a few dupes).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know that last time\u00a0I said I was going to start soldering, but I really wanted to play with the networking capabilities of the ESP8266 first. There&#8217;s a bunch of example programs to do web\u00a0requests, such as BasicHttpClient, HTTPSRequest, WifiClient, StreamHttpClient, etc. I had trouble getting these working because there&#8217;s no built in certificate store [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-esp8266"],"_links":{"self":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":13,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"predecessor-version":[{"id":384,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/365\/revisions\/384"}],"wp:attachment":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}