2024-07-19 20:19:01 +00:00
|
|
|
import minimalmodbus
|
|
|
|
import time, os
|
|
|
|
import serial
|
|
|
|
import RPi.GPIO as GPIO
|
2024-07-22 01:55:14 +00:00
|
|
|
import subprocess
|
2024-07-19 20:19:01 +00:00
|
|
|
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)
|
|
|
|
|
2024-07-25 19:28:42 +00:00
|
|
|
preferred_device = '/dev/ttyUSB0'
|
|
|
|
fallback_device = '/dev/ttyUSB5'
|
2024-07-23 03:02:56 +00:00
|
|
|
|
|
|
|
device_path = preferred_device if os.path.exists(preferred_device) else fallback_device
|
2024-07-25 19:28:42 +00:00
|
|
|
modbus_device = '/dev/modbus'
|
2024-07-23 03:02:56 +00:00
|
|
|
|
2024-07-19 20:19:01 +00:00
|
|
|
# Modbus configuration
|
|
|
|
mb_address = 1 # Modbus address
|
2024-07-25 19:28:42 +00:00
|
|
|
#sensy_boi = minimalmodbus.Instrument(device_path, mb_address)
|
|
|
|
sensy_boi = minimalmodbus.Instrument(modbus_device, mb_address)
|
|
|
|
#sensy_boi = minimalmodbus.Instrument(preferred_device, mb_address)
|
2024-07-19 20:19:01 +00:00
|
|
|
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)
|
2024-07-22 01:55:14 +00:00
|
|
|
GPIO.setwarnings(False)
|
2024-07-19 20:19:01 +00:00
|
|
|
|
2024-07-19 20:29:45 +00:00
|
|
|
# Define pin numbers
|
|
|
|
PIN_17 = 17
|
2024-07-19 20:34:00 +00:00
|
|
|
PIN_23 = 23
|
2024-07-22 01:55:14 +00:00
|
|
|
LuzPuerta = 18
|
|
|
|
LuzEncendido = 25
|
2024-07-19 20:29:45 +00:00
|
|
|
|
|
|
|
# Set GPIO
|
|
|
|
GPIO.setup(PIN_17, GPIO.IN)
|
2024-07-19 20:34:00 +00:00
|
|
|
GPIO.setup(PIN_23, GPIO.IN)
|
2024-07-22 01:55:14 +00:00
|
|
|
GPIO.setup(LuzPuerta, GPIO.OUT)
|
|
|
|
GPIO.setup(LuzEncendido, GPIO.OUT)
|
|
|
|
|
2024-07-19 20:19:01 +00:00
|
|
|
|
2024-07-25 19:28:42 +00:00
|
|
|
try:
|
2024-07-19 20:19:01 +00:00
|
|
|
while True:
|
2024-07-19 20:29:45 +00:00
|
|
|
status_17 = GPIO.input(PIN_17)
|
2024-07-19 20:34:00 +00:00
|
|
|
if GPIO.input(23) == GPIO.HIGH:
|
2024-07-19 20:19:01 +00:00
|
|
|
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)
|
2024-07-19 20:29:45 +00:00
|
|
|
client.publish(f"iiot/{myhost}/door/pin17", str(status_17))
|
2024-07-19 20:19:01 +00:00
|
|
|
|
|
|
|
# Print the processed data
|
|
|
|
print("-------------------------------------")
|
|
|
|
print(f"Temperatura = {temp}\u00B0C")
|
|
|
|
print(f"Humedad relativa = {hum}%")
|
|
|
|
print("-------------------------------------")
|
|
|
|
print("")
|
|
|
|
|
2024-07-25 01:10:28 +00:00
|
|
|
time.sleep(15)
|
2024-07-23 00:25:16 +00:00
|
|
|
|
2024-07-22 01:55:14 +00:00
|
|
|
if GPIO.input(PIN_17) == GPIO.LOW:
|
2024-07-25 01:10:28 +00:00
|
|
|
GPIO.output(LuzPuerta, GPIO.HIGH) # Luz Roja encendida
|
2024-07-22 01:55:14 +00:00
|
|
|
else:
|
2024-07-25 01:10:28 +00:00
|
|
|
GPIO.output(LuzPuerta, GPIO.LOW)
|
|
|
|
GPIO.output(LuzEncendido, GPIO.HIGH)
|
2024-07-25 19:28:42 +00:00
|
|
|
|
2024-07-19 20:19:01 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(f"Error: {str(e)}")
|
2024-07-25 19:28:42 +00:00
|
|
|
|
2024-07-19 20:19:01 +00:00
|
|
|
finally:
|
|
|
|
GPIO.cleanup()
|
|
|
|
print("Programa finalizado")
|