2024-07-24 -

This commit is contained in:
Adolfo Delorenzo 2024-07-24 19:10:28 -06:00
parent fddcb5ad31
commit 483bd83feb
5 changed files with 7193 additions and 11 deletions

BIN
.modbus_door.py.swp Normal file

Binary file not shown.

View File

@ -53,7 +53,7 @@ def write_to_csv(lat, lon):
with open(csv_filename, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([timestamp, lat, lon])
time.sleep(12)
def send_at(command, back, timeout):
rec_buff = ''
@ -100,7 +100,7 @@ def get_gps_position():
rec_buff = ''
send_at('AT+CGPS=0', 'OK', 1)
return False
time.sleep(1.5)
time.sleep(2)
# Create CSV file with headers if it doesn't exist
if not os.path.exists(csv_filename):
@ -110,7 +110,6 @@ if not os.path.exists(csv_filename):
try:
get_gps_position()
time.sleep(10)
except Exception as e:
print(f"Error: {e}")
finally:

File diff suppressed because it is too large Load Diff

49
gps_nx.py Normal file
View File

@ -0,0 +1,49 @@
import serial
import pynmea2
import paho.mqtt.client as mqtt
# Configuration
GPS_PORT = '/dev/ttyS0' # change this if your GPS device is on a different port
MQTT_BROKER = '65.108.199.212'
MQTT_PORT = 1883
TOPIC = 'iiot/coordinates'
def on_connect(client, userdata, flags, rc):
"""Callback for when the client connects to the broker."""
print(f"Connected with result code {rc}")
client.subscribe(TOPIC)
def on_message(client, userdata, msg):
"""Callback for when a message is received from the broker."""
print(f"Received message: {msg.payload.decode('utf-8')}")
# Create MQTT client
client = mqtt.Client()
# Connect to MQTT broker
client.on_connect = on_connect
client.on_message = on_message
try:
client.connect(MQTT_BROKER, MQTT_PORT)
except Exception as e:
print(f"Error connecting to MQTT broker: {e}")
exit()
# Open GPS device serial port
try:
gps_dev = serial.Serial(GPS_PORT, 9600, timeout=1) # change baudrate and timeout if needed
except serial.SerialException as e:
print(f"Error opening GPS device serial port: {e}")
exit()
while True:
try:
line = gps_dev.readline().decode('utf-8')
if line.startswith('$GPGGA'):
msg = pynmea2.parse(line)
latitude = msg.latitude
longitude = msg.longitude
print(f"Latitude: {latitude}, Longitude: {longitude}")
client.publish(TOPIC, f"{latitude},{longitude}")
except Exception as e:
print(f"Error parsing GPS data or publishing to MQTT broker: {e}")

View File

@ -12,8 +12,8 @@ myhost = os.uname()[1]
client = mqtt.Client(myhost)
client.connect(mqttBroker, 1883)
preferred_device = '/dev/ttyUSB5'
fallback_device = '/dev/ttyUSB0'
preferred_device = '/dev/ttyUSB0'
fallback_device = '/dev/ttyUSB5'
device_path = preferred_device if os.path.exists(preferred_device) else fallback_device
@ -54,7 +54,6 @@ try:
status_17 = GPIO.input(PIN_17)
if GPIO.input(23) == GPIO.HIGH:
print("Adquiriendo datos...")
# subprocess.run(['python3', '/home/pi/jdz/gps001.py'])
# Read data from multiple registers
data = sensy_boi.read_registers(0, 2, 3)
@ -76,15 +75,15 @@ try:
print("-------------------------------------")
print("")
#time.sleep(5)
time.sleep(15)
if GPIO.input(PIN_17) == GPIO.LOW:
GPIO.output(LuzPuerta, GPIO.HIGH) # Luz Roja encendida
GPIO.output(LuzPuerta, GPIO.HIGH) # Luz Roja encendida
else:
GPIO.output(LuzPuerta, GPIO.LOW)
GPIO.output(LuzEncendido, GPIO.HIGH)
GPIO.output(LuzPuerta, GPIO.LOW)
GPIO.output(LuzEncendido, GPIO.HIGH)
time.sleep(15)
#time.sleep(1)
except Exception as e:
print(f"Error: {str(e)}")