Es ist lange her, seit ich etwas in IPS eingerichtet habe. Ich versuche aber, mich wieder einzuarbeiten. Ich habe ein Rasp mit 4 relais welche mit einem Python-Script (server.py, siehe unten) angesprochen werden. Diese können mit einem PHP-Script geschaltet und auch abgefragt werden (RelaisControl.php).
Meine Frage ist: welche ist die „best practice“ um die Relais zu schalten mit verschiedenen Triggern (Alexa-Szenen, Buttons auf Webfront, Skripte etc.)? Soll ich für jedes Relais eine Bool Variable und Aktionsskripte einrichten? Ein Skript pro Relais und Zustand? Oder ein Skript für alles, und die Variablen aus $IPS_SENDER auslesen? Wohl Letzteres, oder?
Sorry für die naiven Fragen, ich wusste das alles aber ich hab alles vergessen…
RelaisControl.php:
<?php
$ch = curl_init();
$raspberryIpAddress = "http://10.10.11.49:5000/";
$relayCommand = "off";
$relayNr = 3;
$raspCommand = $raspberryIpAddress.$relayCommand."/".$relayNr;
curl_setopt($ch, CURLOPT_URL, $raspCommand);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
server.py
from __future__ import print_function
from flask import Flask
from flask import make_response
from flask import render_template
from flask_bootstrap import Bootstrap
from relay_lib_seeed import *
error_msg = '{msg:"error"}'
success_msg = '{msg:"success"}'
NUM_RELAY_PORTS = 4
app = Flask(__name__)
bootstrap = Bootstrap(app)
@app.route('/status/<int:relay>')
def api_get_status(relay):
res = relay_get_port_status(relay)
if res:
print("Relay is ON")
return make_response("1", 200)
else:
print("Relay is OFF")
return make_response("0", 200)
@app.route('/on/<int:relay>')
def api_relay_on(relay):
print("Executing api_relay_on:", relay)
if validate_relay(relay):
print("valid relay")
relay_on(relay)
return make_response(success_msg, 200)
else:
print("invalid relay")
return make_response(error_msg, 404)
@app.route('/off/<int:relay>')
def api_relay_off(relay):
print("Executing api_relay_off:", relay)
if validate_relay(relay):
print("valid relay")
relay_off(relay)
return make_response(success_msg, 200)
else:
print("invalid relay")
return make_response(error_msg, 404)
@app.errorhandler(404)
def page_not_found(e):
print("ERROR: 404")
return render_template('404.html', the_error=e), 404
@app.errorhandler(500)
def internal_server_error(e):
print("ERROR: 500")
return render_template('500.html', the_error=e), 500
def validate_relay(relay):
# Make sure the port falls between 1 and NUM_RELAY_PORTS
return (relay > 0) and (relay <= NUM_RELAY_PORTS)
if __name__ == "__main__":
# On the Pi, you need to run the app using this command to make sure it
# listens for requests outside of the device.
# app.run(host='0.0.0.0')
app.run(host='0.0.0.0', port=5000, debug=True)