jdz/door2.py
2024-07-19 14:19:01 -06:00

43 lines
1007 B
Python

#!/usr/bin/python
# -*- coding:utf-8 -*-
import time, os
import paho.mqtt.client as mqtt
import RPi.GPIO as GPIO
# MQTT Broker settings
mqttBroker = "65.108.199.212"
myhost = os.uname()[1]
client = mqtt.Client(myhost)
client.connect(mqttBroker, 1883)
# Set up GPIO mode (BCM numbering)
GPIO.setmode(GPIO.BCM)
# Define pin numbers
PIN_17 = 17
PIN_18 = 18
# Set GPIO
GPIO.setup(PIN_17, GPIO.IN)
GPIO.setup(PIN_18, GPIO.OUT)
while True:
# Read status of PIN_17 (input)
status_17 = GPIO.input(PIN_17)
# Set PIN_18 based on PIN_17 status
if status_17 == GPIO.HIGH:
GPIO.output(PIN_18, GPIO.HIGH) # Luz Roja encendida
else:
GPIO.output(PIN_18, GPIO.LOW)
# Read status of PIN_18 (output)
status_18 = GPIO.input(PIN_18)
# Publish status of both pins to MQTT
client.publish(f"iiot/{myhost}/door/pin17", str(status_17))
client.publish(f"iiot/{myhost}/door/pin18", str(status_18))
# Add a small delay to avoid flooding the broker
time.sleep(1)