updates to door.py
This commit is contained in:
parent
e341a473c8
commit
b748db985e
117
TEMPMOSQUITTOGPS.py
Normal file
117
TEMPMOSQUITTOGPS.py
Normal file
@ -0,0 +1,117 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding:utf-8 -*-
|
||||
import RPi.GPIO as GPIO
|
||||
import minimalmodbus
|
||||
import serial
|
||||
import time
|
||||
import random
|
||||
import paho.mqtt.client as mqtt
|
||||
|
||||
# Modbus Configuration
|
||||
mb_address = 1
|
||||
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
|
||||
|
||||
# GPIO Setup
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(17, GPIO.IN)
|
||||
|
||||
# MQTT Configuration
|
||||
broker_address = "65.108.199.212" # IP or hostname
|
||||
port = 1883
|
||||
topic = "iiot"
|
||||
|
||||
def on_publish(client, userdata, result):
|
||||
print("Data published!")
|
||||
|
||||
def send_at(command, expected_response, timeout):
|
||||
ser.write((command + '\r\n').encode())
|
||||
time.sleep(timeout)
|
||||
rec_buff = ser.read(ser.inWaiting()).decode()
|
||||
if expected_response in rec_buff:
|
||||
print(rec_buff)
|
||||
return True
|
||||
else:
|
||||
print(f"{command} ERROR: {rec_buff}")
|
||||
return False
|
||||
|
||||
def get_gps_position():
|
||||
print("Initializing GPS...")
|
||||
if send_at("AT+CGPS=1,1", "OK", 1):
|
||||
while True:
|
||||
if send_at("AT+CGPSINFO", "+CGPSINFO: ", 1):
|
||||
if ",,,,,," in rec_buff:
|
||||
print("GPS not acquired")
|
||||
break
|
||||
else:
|
||||
print(f"Error getting GPS data: {rec_buff}")
|
||||
send_at("AT+CGPS=0", "OK", 1)
|
||||
break
|
||||
time.sleep(1.5)
|
||||
|
||||
def power_on(power_key):
|
||||
print('SIM7600X is starting...')
|
||||
GPIO.setup(power_key, GPIO.OUT)
|
||||
GPIO.output(power_key, GPIO.HIGH)
|
||||
time.sleep(2)
|
||||
GPIO.output(power_key, GPIO.LOW)
|
||||
time.sleep(20)
|
||||
ser.flushInput()
|
||||
print('SIM7600X is ready')
|
||||
|
||||
def power_down(power_key):
|
||||
print('SIM7600X is logging off...')
|
||||
GPIO.output(power_key, GPIO.HIGH)
|
||||
time.sleep(3)
|
||||
GPIO.output(power_key, GPIO.LOW)
|
||||
time.sleep(18)
|
||||
print('Goodbye')
|
||||
|
||||
def write_to_influxdb(measurement, tags, fields):
|
||||
point = influxdb_client.Point(measurement).tag(**tags).field(**fields)
|
||||
write_api = client.write_api(write_options=SYNCHRONOUS)
|
||||
write_api.write(bucket=bucket, record=point)
|
||||
|
||||
def read_sensor_data():
|
||||
try:
|
||||
client = mqtt.Client("iiot")
|
||||
client.on_publish = on_publish
|
||||
client.connect(broker_address, port)
|
||||
|
||||
while True:
|
||||
if GPIO.input(17) == GPIO.HIGH:
|
||||
data = sensy_boi.read_registers(0, 2, 3)
|
||||
humidity = data[0] / 10
|
||||
temperature = data[1] / 10
|
||||
|
||||
print("-------------------------------------")
|
||||
print(f"Temperature: {temperature}°C")
|
||||
print(f"Humidity: {humidity}%")
|
||||
print("-------------------------------------")
|
||||
|
||||
payload = f"Temperature: {temperature}°C, Humidity: {humidity}%"
|
||||
client.publish(topic, payload)
|
||||
|
||||
# Prepare data for InfluxDB
|
||||
measurement_name = "sensor_data"
|
||||
location_tag = "your_location" # Replace with actual location
|
||||
fruit_tag = "your_fruit" # Replace with actual fruit
|
||||
id_tag = "your_id" # Replace with actual ID
|
||||
|
||||
write_to_influxdb(
|
||||
measurement=measurement_name,
|
||||
tags={
|
||||
"location": location_tag,
|
||||
"fruit": fruit_tag,
|
||||
"id": id_tag,
|
||||
},
|
||||
fields={
|
||||
"temperature": temperature, # Corrected variable name
|
||||
"humidity": humidity, # Add humidity field if needed
|
||||
}
|
||||
)
|
31
door.py
Normal file
31
door.py
Normal file
@ -0,0 +1,31 @@
|
||||
#!/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:
|
||||
if GPIO.input(PIN_17) == GPIO.HIGH:
|
||||
print("Puerta abierta")
|
||||
GPIO.output(PIN_18, GPIO.HIGH) # Luz Roja encendida
|
||||
else:
|
||||
GPIO.output(PIN_18, GPIO.LOW)
|
||||
|
71
minimalmodbus.py
Normal file
71
minimalmodbus.py
Normal file
@ -0,0 +1,71 @@
|
||||
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)
|
||||
client.publish("iiot/"+ myhost +"/door", door)
|
||||
|
||||
# Print the processed data
|
||||
print("-------------------------------------")
|
||||
print(f"Temperatura = {temp}\u00B0C")
|
||||
print(f"Humedad relativa = {hum}%")
|
||||
print("-------------------------------------")
|
||||
print("")
|
||||
|
||||
|
||||
|
||||
time.sleep(1)
|
||||
|
||||
except KeyboardInterrupt:
|
||||
print("Programa terminado por el usuario")
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error: {str(e)}")
|
||||
finally:
|
||||
GPIO.cleanup()
|
||||
print("Programa finalizado")
|
Loading…
Reference in New Issue
Block a user