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

 

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95

News