2024-07-19 -

This commit is contained in:
Adolfo Delorenzo 2024-07-19 14:19:01 -06:00
parent bc9616624f
commit 0dc703b531
3 changed files with 110 additions and 0 deletions

BIN
.modbus_door.py.swp Normal file

Binary file not shown.

42
door2.py Normal file
View File

@ -0,0 +1,42 @@
#!/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)

68
modbus_door.py Normal file
View File

@ -0,0 +1,68 @@
import minimalmodbus
import time, os
import serial
import RPi.GPIO as GPIO
import paho.mqtt.client as mqtt
# MQTT Broker settings
mqttBroker ="65.108.199.212"
myhost = os.uname()[1]
client = mqtt.Client(myhost)
client.connect(mqttBroker, 1883)
# Modbus configuration
mb_address = 1 # Modbus address
sensy_boi = minimalmodbus.Instrument('/dev/ttyUSB0', mb_address)
sensy_boi.serial.baudrate = 9600
sensy_boi.serial.bytesize = 8
sensy_boi.serial.parity = minimalmodbus.serial.PARITY_NONE
sensy_boi.serial.stopbits = 1
sensy_boi.serial.timeout = 0.5
sensy_boi.mode = minimalmodbus.MODE_RTU
# Configure buffer clearing and port closing settings
sensy_boi.clear_buffers_before_each_transaction = True
sensy_boi.close_port_after_each_call = True
# GPIO configuration
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
try:
while True:
if GPIO.input(17) == GPIO.HIGH:
print("Adquiriendo datos...")
# Read data from multiple registers
data = sensy_boi.read_registers(0, 2, 3)
print("")
print(f"Datos crudos {data}")
# Process the raw data
hum = data[0] / 10
temp = data[1] / 10
client.publish("iiot/"+ myhost +"/temperature", temp)
client.publish("iiot/"+ myhost +"/humidity", hum)
# Print the processed data
print("-------------------------------------")
print(f"Temperatura = {temp}\u00B0C")
print(f"Humedad relativa = {hum}%")
print("-------------------------------------")
print("")
time.sleep(1.5)
except KeyboardInterrupt:
print("Programa terminado por el usuario")
except Exception as e:
print(f"Error: {str(e)}")
finally:
GPIO.cleanup()
print("Programa finalizado")