MisterHouse is an active, community-driven, open source home automation project
example.mht
The local/code/example.mht'
file I've provided contains a great deal more than you will need. Most of the file is lines of comments. At the start is a Format = A
and a #
noloop=start
statement. These statements tell MisterHouse two important things. First is that MisterHouse will treat this mht
file as a Format A file (there used to be an Format = XML
option also). The noloop
statement is the more important statement. As I stated earlier, MisterHouse is a large loop that runs code over and over again.
At times, you might want to run a series of commands only once. When that occurs, you can put the statements #
noloop=start
and #
noloop=stop
around the code to tell MisterHouse not to include that code in the loop section but, instead, run it once at startup (and restart). I have trimmed the example.mht
to include a noloop
section.
Note the noloop
and Format
statements in Listing 2. Lines that start with #
are comments, and the lines that start with the keyword CODE
contain Perl code. Future releases of MisterHouse will integrate automatic code generation (.mht
to .mhp
) of special MQTT statements into MisterHouse.
Listing 2
A Section of example.mht
I have required the Perl module mqtt.pm
, then initialized the MQTT interface with the subscription to the topic home/weather/#
(MQTT wild card) and set the Perl object $weatherSummary
to the home/weather/txt/summary
topic. I've done something similar for $weatherWind
, but for the topic home/weather/txt/wind
. These are the two objects that I will use later in my user code. For further information on the mqtt
module, visit MisterHouse MQTT web page [5]. In this particular example, the interface $mqtt_1
subscribes to the wild card topic home/ha/#
. The MQTT items $weatherSummary
and $weatherWind
variables correspond to the topics home/weather/txt/summary
and home/weather/txt/wind
, respectively. When either topic gets updated, MisterHouse gets those updates and sets the respective objects to the update values. Your code can then take those values and perform some action.
User Code: mqtt.pl
My user code uses a few built-in MisterHouse routines. If you visit the URL: http://IP_Address:8080/docs/index.html, click on the Doc link and you'll be taken to the web page with the information about these routines for handling events. One of those routines is the state_now()
, which returns the current state only for one pass after the object state is set. This approach is important, because you often only want to run some code when the state of an object has changed and not on every pass through the loop. Other routines can check for a new second, new minute, and new hour. MisterHouse provides a dizzying catalog of routines.
The user code in Listing 3 is not too complex. The noloop
statements are around the code that requires the use of JSON. A noloop
statement tells MisterHouse to keep the code out of the main loop and run it only once. I'm taking advantage of one of Perl's strengths, its libraries: In this case, the JSON library to decode the expected JSON string in the topic. The if()
condition checks for when the $weatherJSONSummary2
object has be updated and, if true, then decodes the JSON and sets the Wind
and Summary_short
variables in the Weather object accordingly. Much of the user code looks like this: a simple if
-then
-else
statement.
Listing 3
A Section of mqtt.pl
Publishing to a MQTT Topic
Publishing to a topic is really easy (see Listing 4). The two important options for the mosquitto_pub
command are -t
topic and -m
message. In this case, my code in the mqtt.pl
file expects a JSON string in the format shown with the -m
option. I could have just as easily used XML or plain text. The reason I chose to use JSON was that it was delivered from another source to MQTT in JSON. So, in my user Perl code, I needed to decode the JSON. If you want to publish the data to the MQTT topic, you need to send valid JSON.
Listing 4
Command-Line Publishing
« Previous 1 2 3 4 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.