US Home
Automation
(Last updated: Sunday June, 28, 2015)
Automation
(Last updated: Sunday June, 28, 2015)
mqtt.pm - Misterhouse MQTT Perl module
Temporarary
.mht file: # MQTT stuff CODE, require mqtt; #noloop # CODE, $mqtt_1 = new mqtt('mqtt_1', '127.0.0.1', 1883, 'home/ha/#', "", "", 121); CODE, $mqtt_2 = new mqtt('mqtt_2', 'test.mosquitto.org', 1883, 'home/test/#', "", "", 122); CODE, $mqtt_3 = new mqtt('mqtt_3', '127.0.0.1', 1883, 'home/network/#', "", "", 122); #noloop # CODE, $CR_Temp = new mqtt_Item($mqtt_1, "home/ha/text/x10/1" ); CODE, $M2_Temp = new mqtt_Item($mqtt_2, "test.mosquitto.org/test/x10/1" ); CODE, $M3_Temp = new mqtt_Item($mqtt_3, "home/network/test/x10/1" ); # CODE, $CR_Temp->set( "On" ); CODE, $M2_Temp->set( "Off" ); CODE, $M3_Temp->set( "On" ); and my mqtt.pl in my code dir: # if ($New_Minute and !($Minute % 30)) { my $state = ('on' eq state $M2_Temp) ? 'off' : 'on'; set $M2_Temp $state; my $remark = "M2 Light set to $state"; print_log "$remark"; } CLI generation of a command to the CR_Temp mosquitto_pub -d -h test.mosquitto.org -q 0 -t test.mosquitto.org/test/x10/1 -m "Off" Example initialization: $myMQTT = new mqtt(<instance_name>,<host>,<port>,<topic>,<user_name>,<password>,<keepalive>); Notes: The test.mosquitto.org server listens on ports 1883, 8883, 8884 and 8885. Port 1883 is the standard unencrypted MQTT port and can be used with any MQTT client. Ports 8883 and 8884 use certificate based SSL/TLS encryption (TLSv1.2, TLSv1.1 or TLSv1.0) and require client support to connect. Web sockets are not supported here in MH Topics (examples) # ha/# ha/house/livingroom/lamp ha/weather/temp ha/weather/windspeed For now we'll use the wildcard. I'll think about a rewrite later without the wild card support. I don't recommend using the '#' if the MQTT is test.mqtt.org Rather pick something a bit more unique like /username/ha/# and play from there Because of the wildcard it probably makes sense to support multiple mqtt connections on 1 or more servers. This would allow for: home/ha/x10/# home/ha/z-wave/# home/ha/zigbee/# instead of: home/# or # Which could cover things like this: home/email/... home/statistics/... offsite/... and just about everything else on this server too. :-) If you're using a home mqtt server then this might not be such as issue. Give this command a try and see the amount of traffic generated: mosquitto_sub -d -h test.mosquitto.org -t "#" The intial device needs some kind of way to tell that it's still connected to the MQTT server (MQTT Ping comes to mind).