2024-07-05 15:05:28 +00:00
|
|
|
#!/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
|
2024-07-05 15:12:03 +00:00
|
|
|
result_17 = GPIO.setup(PIN_17, GPIO.IN)
|
2024-07-11 20:10:55 +00:00
|
|
|
result_18 = GPIO.setup(PIN_18, GPIO.OUT)
|
2024-07-05 15:05:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
if GPIO.input(PIN_17) == GPIO.HIGH:
|
|
|
|
print("Puerta abierta")
|
2024-07-05 15:12:03 +00:00
|
|
|
client.publish("iiot/"+ myhost +"/door", result_17)
|
2024-07-05 15:05:28 +00:00
|
|
|
GPIO.output(PIN_18, GPIO.HIGH) # Luz Roja encendida
|
|
|
|
else:
|
2024-07-05 15:12:03 +00:00
|
|
|
GPIO.output(PIN_18, GPIO.LOW)
|
|
|
|
client.publish("iiot/"+ myhost +"/door", result_18)
|
2024-07-05 15:05:28 +00:00
|
|
|
|