Giter VIP home page Giter VIP logo

mqcontrol's Introduction

mqcontrol

Docker build

mqcontrol is a lightweight and cross-platform utility which subscribes to an MQTT topic and executes a predefined command on your computer whenever a message appears. It's an easy way to make your PC part of your home automation or IoT system!

Installation

See the releases page and download a binary for your operating system and architecture.

Or install it with Go:

go install github.com/albertnis/mqcontrol

To complete local installation, add mqcontrol to PATH and/or run it at startup.

Docker

mqcontrol is available on Docker Hub: albertnis/mqcontrol. The images are cross-platform and available in a variety of flavours. See the Docker Hub overview for more information.

docker run albertnis/mqcontrol --help

Usage

Configuration is via command-line arguments.

mqcontrol --help
  -c string
        Command to run when any message received on topic
  -h string
        Address and port of MQTT broker (default "127.0.0.1:1883")
  -i string
        ID to use for this client (default "mqcontrol-{randomString}")
  -t string
        Topic to subscribe to (default "computer/command")
  -u string
        Username for MQTT connection
  -p string
        Password for MQTT connection

Examples

  • Make a topic to hibernate your PC

    mqcontrol -c "systemctl hibernate" -t desktop/command/hibernate
  • Dim laptop screen when the lights are turned off

    mqcontrol -c "brightnessctl 50%-" -t lights/bedroom/turnoff
  • Close gzdoom when the office door is opened

    mqcontrol -c "killall gzdoom" -t work/office/door/open

Notes

  • The command argument does not include any shell processing. If you're having problems getting commands to run or want them to run in a shell, specify the shell in the command. For example:

    mqcontrol -c "/bin/sh -c \"echo message received\"" -t desktop/command/hibernate
  • An error in the executed command will cause the entire program to terminate. Stderr and an exit code from the executed command will be available.

Run the code locally

Get then run with Go:

go get github.com/albertnis/mqcontrol
go run github.com/albertnis/mqcontrol -c "echo Message received"

Run with Go in cloned repo:

go run main.go -c "echo Message received"

With Docker (BuildKit):

DOCKER_BUILDKIT=1 docker build -t mqcontrol .
docker run -it --rm --network=host mqcontrol -c "echo Message received"

With docker-compose (BuildKit):

COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build
docker-compose run mqcontrol -c "echo Message received"

Run it at startup

On Windows using Task Scheduler

  1. Open Task Scheduler and select Action -> Create Task from the menu bar.
  2. On the "General" tab, select "Run whether the user is logged in or not" and check "Do not store password...".
  3. On the "Triggers" tab, create a new trigger. Use "On startup" or "At log on".
  4. On the "Actions" tab, create a new action pointing to your mqcontrol binary with desired arguments.
  5. Configure the remaining tabs as desired then click "OK".
  6. Browse to the newly created task under the "Task Scheduler Library". Right click on the task and select "Run".

On Linux using systemd

  1. Create a systemd unit file as below, customise the ExecStart line, then save it at /usr/lib/systemd/system/mqcontrol.service:

    [Unit]
    Description=mqcontrol remote control
    
    [Service]
    Type=simple
    ExecStart=/home/user/go/bin/mqcontrol -i mqclinux -c "systemctl hibernate" -h 192.168.1.110:1883
    
    [Install]
    WantedBy=multi-user.target
  2. Start and enable the mqcontrol service

    systemctl start mqcontrol
    systemctl enable mqcontrol

mqcontrol's People

Contributors

albertnis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mqcontrol's Issues

Activating every 25 seconds without MQTT messaging?

Hi (from Auckland), great utility thanks.

I'm running mqcontrol on Ubuntu 20.04.2 with systemd. Mainly wanting screen locking for the desktop. I struggled to figure out how to get root to lock a general user's screen but I have success with running the command as the user with su

-c "su - dave -c\"gnome-screensaver-command -l\"" 

The MQTT server is on a separate machine.

However, I get the situation that the screen locks every 25 seconds (seems consistent) even though there are no MQTT messages to that topic. I can systemctl stop and restart mqcontrol and it will stop that behaviour, but if the machine reboots it will do the same.

monitoring journalctl shows me the below, and mosquitto_sub shows nothing at all to the topic (and I'm not publishing anything):

Apr 22 10:28:40 ubuntu2 mqcontrol[72165]: 2021/04/22 10:28:40 Message received on topic computers/ubuntu2/lockscreen:
Apr 22 10:28:40 ubuntu2 su[108743]: (to dave) root on none
Apr 22 10:28:40 ubuntu2 su[108743]: pam_unix(su-l:session): session opened for user dave by (uid=0)
Apr 22 10:28:41 ubuntu2 mqcontrol[72165]: 2021/04/22 10:28:41 Message received on topic computers/ubuntu2/lockscreen:
Apr 22 10:28:41 ubuntu2 su[108774]: (to dave) root on none
Apr 22 10:28:41 ubuntu2 su[108774]: pam_unix(su-l:session): session opened for user dave by (uid=0)
Apr 22 10:29:06 ubuntu2 mqcontrol[72165]: 2021/04/22 10:29:06 Message received on topic computers/ubuntu2/lockscreen:
Apr 22 10:29:06 ubuntu2 su[108953]: (to dave) root on none
Apr 22 10:29:06 ubuntu2 su[108953]: pam_unix(su-l:session): session opened for user dave by (uid=0)
Apr 22 10:29:31 ubuntu2 mqcontrol[72165]: 2021/04/22 10:29:31 Message received on topic computers/ubuntu2/lockscreen:
Apr 22 10:29:31 ubuntu2 su[109515]: (to dave) root on none
Apr 22 10:29:31 ubuntu2 su[109515]: pam_unix(su-l:session): session opened for user dave by (uid=0)
Apr 22 10:29:56 ubuntu2 mqcontrol[72165]: 2021/04/22 10:29:56 Message received on topic computers/ubuntu2/lockscreen:
Apr 22 10:29:56 ubuntu2 su[109690]: (to dave) root on none

Any ideas on what I'm missing? Also, had you thought of running the command on a particular message to the topic only (rather than 'any' message? I can have a look... this might be a simple way to fix it.

MQTT replies

Albert,
Great software for home automation. The question is if there would be a way to post a message to a topic on the MQTT server when the command is activated, like a reply or a status change on a topic.
Regards,

Federico

! in password does not work

Trying to connect to a broker with userid and password will not work if the password contains an exclamation point.

-u chris -p "P5sdfs!DFGgdfgDFG"
bash: !DFGgdfgDFG": event not found

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.