{"id":571,"date":"2022-06-03T15:18:18","date_gmt":"2022-06-03T19:18:18","guid":{"rendered":"https:\/\/dashdrum.com\/blog\/?p=571"},"modified":"2022-06-03T15:18:18","modified_gmt":"2022-06-03T19:18:18","slug":"worldwide-mqtt-without-exposing-a-local-broker-to-the-public-internet","status":"publish","type":"post","link":"https:\/\/dashdrum.com\/blog\/2022\/06\/worldwide-mqtt-without-exposing-a-local-broker-to-the-public-internet\/","title":{"rendered":"Worldwide MQTT Without Exposing a Local Broker to the Public Internet"},"content":{"rendered":"\n<p>An <a href=\"http:\/\/www.steves-internet-guide.com\/mosquitto-bridge-configuration\/\">article on Steve\u2019s Internet Guide<\/a> got me thinking about MQTT when on the road. No, seriously!\u00a0 Here\u2019s my situation:<\/p>\n\n\n\n<p>I have a sensor that reports the usual temperature and humidity inside my travel trailer, and it also provides the same information from inside the RV refrigerator thanks to a <a href=\"https:\/\/ruuvi.com\/\">Ruuvi tag<\/a> placed within. (<a href=\"https:\/\/github.com\/dashdrum\/esphome\/blob\/master\/trailer.yaml\">Code for this sensor using ESPHome can be found in my Github repo.<\/a>)<\/p>\n\n\n\n<p>This sensor connects to my home WiFi network when the trailer is parked in the driveway, but obviously not when we are on a trip.&nbsp; We can use the Ruuvi app to read the fridge temperature using BLE when inside the unit, but if we are even across the campsite or farther away &#8211; no go.<\/p>\n\n\n\n<p>Anyone who has relied on an RV refrigerator to keep their food safe knows that they can be less than reliable.&nbsp; Knowing the temperature is rising gives us a chance to take action before food items are spoiled.&nbsp;&nbsp;<\/p>\n\n\n\n<p>My goal with this project was to provide a way to monitor the refrigerator temperature when the trailer is parked away from home, and to use Home Assistant to issue alerts that will inform us of any temperature issues.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">WiFi First<\/h2>\n\n\n\n<p>Before I get into the MQTT configuration, let\u2019s make sure we connect to WiFi when away.\u00a0 My trailer has a travel router that can bridge to RV park networks, a hotspot, or a tethered smartphone to bring Internet access inside.\u00a0 ESPHome allows one to set up more than one set of WiFi options, so I use this code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wifi:   \nnetworks:\n  - ssid: !secret rv_ssid\n    password: !secret rv_password\n  - ssid:  !secret wifi_iot_ssid\n    password: !secret wifi_iot_password\n    manual_ip:\n      static_ip: $ip_address\n      gateway: 192.168.37.1\n      subnet: 255.255.255.0\n      dns1: 8.8.8.8\n      dns2: 1.1.1.1<\/code><\/pre>\n\n\n\n<p>On the home network I use a static IP address to make connection time fast.&nbsp; This, along with deep sleep,&nbsp; helps limit the time the device is awake when using a battery operated-sensor.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MQTT in the Wild<\/h2>\n\n\n\n<p>In order to collect the telemetry data from this sensor when away, I set up a free account with the service <a href=\"https:\/\/flespi.com\/mqtt-broker\">flespi<\/a>.&nbsp; However, one can use any one of many services offering an MQTT broker service, as my solution is simple and standards-based.&nbsp; Thanks to the good folks at flespi for making this service available for trials and hobbyists.<\/p>\n\n\n\n<p>Flespi assigns a long token for authentication, and this can be provided as either a username or password when logging in.\u00a0 Here is my code:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  mqtt:\n    id: mqtt_client\n    broker: !secret flespi_broker\n    port: !secret flespi_port\n    username: !secret flespi_token\n    birth_message:\n      topic: $device_name\/birthdisable\n      payload: disable\n    will_message:\n      topic: $device_name\/willdisable\n      payload: disable<\/code><\/pre>\n\n\n\n<p>(Sorry for all the secrets; gotta keep things private and portable)<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/lEpPiLc5vDbubHf1s7yZpSvqgpIak4hxmOzx6er8vkfnZ50wb1aXB1MRLRgYojlJob2WDI7SdFl_92R91MzszeuYWg7ciYdviv3HgA11mXh8VweOB8N1GHMTIk1fIzJiRTfGTojHDRrsxpsnnw\" alt=\"MQTT Explorer Screen Capture\" width=\"675\" height=\"282\"\/><\/figure>\n\n\n\n<p>MQTT Explorer shows the broker contents once the device is connected and transmitting.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Bridge<\/h2>\n\n\n\n<p>Finally, we need to make a safe connection between the local broker and the public one.&nbsp; I use the Mosquitto broker locally, and it offers an MQTT bridge functionality that makes this task easy.<\/p>\n\n\n\n<p>What we are going to do is have the local broker subscribe to the topics created on the external broker.&nbsp; In my example, the sensor uses topics with the prefix \u201ctrailer\/\u201d.&nbsp; In addition, ESPHome uses a topic prefix to send configuration information to Home Assistant.&nbsp; The default topic for this is \u201chomeassistant\/\u201d.<\/p>\n\n\n\n<p>To define these subscriptions, we will modify the mosquitto.conf file to connect to the external broker and subscribe.&nbsp; Add this code to your file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#  Bridge to public broker\n\nconnection &lt;MQTT connection name>\naddress &lt;broker url or IP with port number, such as example.com:1883>\nremote_username &lt;use your token here>\nbridge_insecure false\ntry_private false\nbridge_protocol_version mqttv50\n\ntopic homeassistant\/# in 0\ntopic trailer\/# in 0<\/code><\/pre>\n\n\n\n<p>The top section of this snippet sets up the broker to broker connection.&nbsp; Your version may be different if using another service.&nbsp; Note that I am using an unsecured connection; you may wish to beef things up.<\/p>\n\n\n\n<p>The lower section is what defines the subscriptions.&nbsp; The topic \u201chomeassistant\/#\u201d uses the octothorpe wild card symbol to grab anything under this topic prefix.&nbsp; The configuration information for the sensor is found here.<\/p>\n\n\n\n<p>The \u201ctrailer\/#\u201d topic is used to identify the sensor readings.&nbsp; Whenever something is posted to this topic prefix, it will be retrieved through the bridge to the local broker &#8211; ready for consumption by Home Assistant or any other user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrap Up<\/h2>\n\n\n\n<p>I was surprised that I could do this without any additional cost and limited coding.&nbsp; Now that the data is feeding to Home Assistant, I can write automations, alerts, etc. to keep us informed.<\/p>\n\n\n\n<p>Thanks again to Steve of <a href=\"http:\/\/www.steves-internet-guide.com\/\">Steve\u2019s Internet Guide<\/a> for the inspiration and examples.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An article on Steve\u2019s Internet Guide got me thinking about MQTT when on the road. No, seriously!\u00a0 Here\u2019s my situation: I have a sensor that reports the usual temperature and humidity inside my travel trailer, and it also provides the same information from inside the RV refrigerator thanks to a Ruuvi tag placed within. (Code &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/dashdrum.com\/blog\/2022\/06\/worldwide-mqtt-without-exposing-a-local-broker-to-the-public-internet\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Worldwide MQTT Without Exposing a Local Broker to the Public Internet&#8221;<\/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":[5,6,7,8],"class_list":["post-571","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-homeassistant","tag-iot","tag-mqtt","tag-ruuvi"],"_links":{"self":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/comments?post=571"}],"version-history":[{"count":2,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/571\/revisions"}],"predecessor-version":[{"id":577,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/posts\/571\/revisions\/577"}],"wp:attachment":[{"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/media?parent=571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/categories?post=571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dashdrum.com\/blog\/wp-json\/wp\/v2\/tags?post=571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}