34 lines
773 B
Python
34 lines
773 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
|
|
result_17 = GPIO.setup(PIN_17, GPIO.IN)
|
|
result_18 = GPIO.setup(PIN_18, GPIO.OUT)
|
|
|
|
|
|
while True:
|
|
if GPIO.input(PIN_17) == GPIO.HIGH:
|
|
print("Puerta abierta")
|
|
client.publish("iiot/"+ myhost +"/door", result_17)
|
|
GPIO.output(PIN_18, GPIO.HIGH) # Luz Roja encendida
|
|
else:
|
|
GPIO.output(PIN_18, GPIO.LOW)
|
|
client.publish("iiot/"+ myhost +"/door", result_18)
|
|
|