Plugwise-2-py icon indicating copy to clipboard operation
Plugwise-2-py copied to clipboard

Question: Any way to control circles without MQTT? REST call? Local shell script?

Open MagnusThome opened this issue 6 years ago • 6 comments

I log activity using MQTT over a VPN. And I can turn devices on and off over the same VPN using both MQTT and the web page you've created for this. But this is for non vital activities only, the VPN runs over mobile network and can occasionally be down.

Now I have a device I want to automate turning of and on locally from the same linuxbox that P2py runs on and don't want to risk missing these events/activities if VPN is down temporarily.

Can I turn devices on/off using some call to the control web page? Or via a shell script directly to some part of P2py?

Thanks for any pointers!

MagnusThome avatar Apr 02 '19 21:04 MagnusThome

I saw that I can turn on and off circles with

./Plugwise-2-py/plugwise_util -m 000D6F000076BBED -s 1 ./Plugwise-2-py/plugwise_util -m 000D6F000076BBED -s 0

but it seems it might be a bad way of doing it since on the web page the power consumption is updated but the state of the switch is not (my guess is that this command just sends the request right out on the serial port bypassing Plugwise-2.py and web code in the running service?

MagnusThome avatar Feb 15 '20 12:02 MagnusThome

Hi Magnus, The first method I implemented for controlling switches is writing one of the json configure file. From the top of my mind pw-control.json. I think it is checked every ten seconds. You could try this.

As I never actively promoted this way it is also not tested a lot. Fingers crossed.

SevenW avatar Feb 15 '20 13:02 SevenW

THANKS! Will give it a shot!

I saw in the web server Plugwise-2-web.py file this

           elif path == '/mqtt/':
                length = int(self.headers.getheader('content-length'))
                raw = self.rfile.read(length)
                data = json.loads(raw)
                info("POST mqtt message: %s" % data)
                topic = str(data['topic'])
                msg = json.dumps(data['payload'])
                qpub.put((topic, msg))
                self.send_response(200, "ok")
                self.send_header('Content-type', 'application/json')
                self.end_headers()
                self.wfile.write(json.dumps({ "result": "ok"}))
                return

And some seemingly corresponding (unused since it alwasy seem to use websockets) in the pw2py.js

	if ($scope.websockets) {
		$scope.send(JSON.stringify(msg))
	} else {
		$http.post("/mqtt/", msg).
			success(function(data, status, headers, config) {
				console.log("scheduled POST successful")
			}).
			error(function(data, status, headers, config) {
			  // log error
				console.log("scheduled POST error")
			});
	}

But I can't get a ajax or jquery POST to localhost:8000/mqtt/ to work... so far anyway :-)

Thanks for your tip! I'll check it out!!!!

MagnusThome avatar Feb 15 '20 14:02 MagnusThome

Seems you moved on from your code polling/checking the pw-control.json where, as you correctly remembered, the status on/off is stored. If I change it by editing it, the circle will not change. Thanks anyway :-) !!

PS: The only visible change when I've changed the on/off status in the file is that when the web page initially loads it seems to read this value but after half a second the web page updates with the actual status. So if it is on but I wrote off, the page will load showing it as off but immediately visibly update to on which is the actual real status.

PPS: I'm installing it with two mqtt brokers (one local and one remote) and bridging them now :-)

MagnusThome avatar Feb 15 '20 14:02 MagnusThome

@MagnusThome did you make progress with this one. Currently the most reliable way is MQTT.

BTW, I moved on to python3.9 myself. I expect some stability issues to iron out generated form code seldomly used.

SevenW avatar Oct 27 '20 15:10 SevenW

@SevenW Works super with mqtt via the localhost broker :-)

MagnusThome avatar Oct 28 '20 06:10 MagnusThome