From 25555c7f5e8f8d20a9ce3853bcacbad6df0062d9 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Tue, 16 Mar 2021 21:09:22 +0100 Subject: [PATCH] init mqtt stuff --- mqtt/main.py | 30 ++++++++++++++++++++++++++++++ mqtt/requirements.txt | 1 + mqtt/shell.nix | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 mqtt/main.py create mode 100644 mqtt/requirements.txt create mode 100644 mqtt/shell.nix diff --git a/mqtt/main.py b/mqtt/main.py new file mode 100644 index 0000000..2ca6067 --- /dev/null +++ b/mqtt/main.py @@ -0,0 +1,30 @@ +import paho.mqtt.client as mqtt + + + +# The callback for when the client receives a CONNACK response from the server. +def on_connect(client, userdata, flags, rc): + print("Connected with result code " + str(rc)) + + # Subscribing in on_connect() means that if we lose the connection and + # reconnect then subscriptions will be renewed. + client.subscribe("control/lights/set") + + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + print(msg.topic + " " + str(msg.payload)) + + +client = mqtt.Client() +client.on_connect = on_connect +client.on_message = on_message +client.username_pw_set("homeassistant", password="password") + +client.connect("pepe.private", 1883, 60) + +# Blocking call that processes network traffic, dispatches callbacks and +# handles reconnecting. +# Other loop*() functions are available that give a threaded interface and a +# manual interface. +client.loop_forever() diff --git a/mqtt/requirements.txt b/mqtt/requirements.txt new file mode 100644 index 0000000..d173f65 --- /dev/null +++ b/mqtt/requirements.txt @@ -0,0 +1 @@ +paho-mqtt \ No newline at end of file diff --git a/mqtt/shell.nix b/mqtt/shell.nix new file mode 100644 index 0000000..66f3f9e --- /dev/null +++ b/mqtt/shell.nix @@ -0,0 +1,20 @@ +{ pkgs ? import { } }: +let + + myPython = pkgs.python3.withPackages (python-packages: + with python-packages; [ + paho-mqtt + ]); + + startServer = pkgs.writers.writeBashBin "start-server" '' + ${myPython}/bin/python ./main.py + ''; + +in pkgs.mkShell { + + buildInputs = with pkgs; [ + myPython + startServer + ]; + +}