Raspberry Pi Zero W Relay Control
Recently I've discovered the power of controlling relays with a Raspberry Pi and take been putting it to good use. I have a Raspberry Pi ii setup in my shed doing a few things including controlling three relays. 2 of them are for my shed roller doors (bank check out my YouTube Video on that) and I have the 3rd controlling my water tank pump. On top of this the onetime Pi 2 is doing daily backups of my NAS as semi-offsite storage.
To control the relays from my phone I am running running a python script on the Pi. Using MQTT the Pi connects to my Home Assistant server which connects to Apple'south HomeKit. Also this tin be setup with Google Assistant if you run Android. If y'all don't know what MQTT or Home Assistant is don't worry, I'll cover that for you hither.
Using Relays With A Raspberry Pi's GPIO
First upwardly, you can't command relays direct. You will need to use a relay lath that triggers on 5v. These 5v boards have no trouble triggering on the Raspberry Pi'south iii.3v GPIO.
These relay boards work with the Raspberry Pi (chapter link).
Controlling a relay from your Pi's GPIO using Python and Raspbian is rather simple. You need to utilise 5V Power, Footing and one GPIO pivot.
import RPi.GPIO as GPIO import time # BCM Pin No relay = 17 GPIO.setmode(GPIO.BCM) GPIO.setup(relay, GPIO.OUT) while True: GPIO.output(relay, GPIO.HIGH) time.sleep(1) GPIO.output(relay, GPIO.LOW) time.sleep(i) This code volition turn the relay on and off every second, effectively making a ticking clock.
Home Assistant & MQTT
While it'southward great to control your relay with Python we need to connect the link betwixt your smart phone and the python code. Here is what the advice menses looks like.
- Smart Phone (Apply HomeKit or Google Assistant)
- Home Assistant (Running MQTT Server)
- Python Code (MQTT Client & Relay Trigger)
Installing Abode Assistant
How you get dwelling assistant up and running is going to depend on your situation. Do yous accept a home server? Run it in a VM or Container. All the same if you lot don't have a abode server then yous can run information technology on a Raspberry Pi iv and performance is pretty practiced. Home Assistant themselves have guides for installation on many platforms including running on windows directly. Here is a link to their installation page.
Home Assistant and your Smart Phone
If you lot have an iPhone I've already made a guide on using Home Assistant with Apple HomeKit here. This lets yous get buttons directly on your drop downwardly screen and allows voice command with Siri.
If you're using an Android telephone here is a good guide to follow. Alternatively yous can run the Domicile Assistant app direct on either platform, still you demand to open the app to control anything.
Using MQTT and Adding Buttons
We need 2 install two addons here from the addon store. These are Mosquitto Broker and Visual Studio Lawmaking.
You can click on the addon once information technology's installed to configure basic settings like Automobile Start and Watchdog (auto restart if addon crashes). Read through Documentation tab for Mosquitto Banker to complete the setup. It gives articulate steps that aren't worth repeating hither.
On the left yous will have Visual Studio Lawmaking now. Go in hither and click on configeration.yaml.
You can add together code here that will create a button in domicile assistant. This button will trigger MQTT event based on the name and topics. The beneath code is what I will use to control a relay to turn on/off my water tank pump.
switch: - platform: mqtt proper name: "Pump" state_topic: "/shed/pump" command_topic: "/shed/pump" qos: two retain: truthful
Raspberry Pi, MQTT & Python
Paho MQTT is the module we will utilise to get MQTT working in Python on a Raspberry Pi. Install it with this control:
Once you lot take that installed you can modify the below python code to adjust yourself. The IP, topic, username and password demand irresolute. Too you lot need to change the actions under the subscribe function. I am leaving my code every bit an case for unlike situations. It's specific to my employ nevertheless information technology may give y'all ideas on how to handle MQTT communications and do dissimilar things.
import random import time import RPi.GPIO as GPIO from paho.mqtt import client as mqtt_client time.sleep(10) port = 1883 client_id = f'python-mqtt-{random.randint(0, 1000)}' #Change These broker = '192.168.40.3' topic = "/shed/#" username = 'mqttuser' password = 'password' door_pin_1 = 17 door_pin_2 = 18 pump_pin = 27 GPIO.setmode(GPIO.BCM) GPIO.setup(door_pin_1, GPIO.OUT) GPIO.setup(door_pin_2, GPIO.OUT) GPIO.setup(pump_pin, GPIO.OUT) GPIO.output(door_pin_1, GPIO.LOW) GPIO.output(door_pin_2, GPIO.LOW) GPIO.output(pump_pin, GPIO.LOW) # Set start time start_time = time.time() def connect_mqtt(): def on_connect(client, userdata, flags, rc): if rc == 0: print("Connected to MQTT Broker!") else: print("Failed to connect, return code %d\n", rc) # Set Connecting Client ID client = mqtt_client.Client(client_id) client.username_pw_set(username, password) client.on_connect = on_connect client.connect(broker, port) return client def subscribe(client: mqtt_client): def on_message(client, userdata, msg): print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic") if (time.time() - start_time) > 10: if msg.topic == '/shed/door1': GPIO.output(door_pin_1, GPIO.High) time.sleep(0.25) GPIO.output(door_pin_1, GPIO.LOW) time.sleep(0.25) elif msg.topic == '/shed/door2': GPIO.output(door_pin_2, GPIO.Loftier) time.sleep(0.25) GPIO.output(door_pin_2, GPIO.LOW) time.sleep(0.25) elif msg.topic == '/shed/pump': if msg.payload.decode() == "ON": GPIO.output(pump_pin, GPIO.HIGH) pump_state = True print("Pump ON") if msg.payload.decode() == "OFF": GPIO.output(pump_pin, GPIO.Depression) pump_state = False print("Pump OFF") client.subscribe(topic) customer.on_message = on_message def receive(): client = connect_mqtt() subscribe(client) client.loop_forever() receive() Source: https://www.johnkeen.tech/controlling-relays-with-a-raspberry-pi-from-your-phone/
Posted by: hornbackfortell.blogspot.com

0 Response to "Raspberry Pi Zero W Relay Control"
Post a Comment