diff --git a/gps_coordinates.csv b/gps_coordinates.csv new file mode 100644 index 0000000..27be7a3 --- /dev/null +++ b/gps_coordinates.csv @@ -0,0 +1,47 @@ +Timestamp,Latitude,Longitude +2024-07-26 19:14:54,9.939996,-14.771567 +2024-07-26 19:14:56,9.939996,-14.771566 +2024-07-26 19:14:59,9.939996,-14.771566 +2024-07-26 19:15:01,9.939996,-14.771566 +2024-07-26 19:15:04,9.939996,-14.771566 +2024-07-26 19:15:06,9.939996,-14.771566 +2024-07-26 19:15:09,9.939996,-14.771566 +2024-07-26 19:15:11,9.939996,-14.771566 +2024-07-26 19:15:14,9.939996,-14.771566 +2024-07-26 19:15:16,9.939995,-14.771566 +2024-07-26 19:15:19,9.939995,-14.771566 +2024-07-26 19:15:22,9.939995,-14.771566 +2024-07-26 19:15:24,9.939995,-14.771566 +2024-07-26 19:15:27,9.939995,-14.771566 +2024-07-26 19:15:29,9.939995,-14.771566 +2024-07-26 19:15:32,9.939995,-14.771566 +2024-07-26 19:15:34,9.939995,-14.771566 +2024-07-26 19:15:37,9.939995,-14.771566 +2024-07-26 19:15:39,9.939995,-14.771566 +2024-07-26 19:15:42,9.939995,-14.771566 +2024-07-26 19:15:44,9.939995,-14.771565 +2024-07-26 19:15:47,9.939995,-14.771565 +2024-07-26 19:15:49,9.939995,-14.771565 +2024-07-26 19:15:52,9.939995,-14.771565 +2024-07-26 19:15:54,9.939995,-14.771565 +2024-07-26 19:15:57,9.939995,-14.771566 +2024-07-26 19:15:59,9.939995,-14.771565 +2024-07-26 19:19:09,9.939989,-14.771559 +2024-07-26 19:19:12,9.939989,-14.771559 +2024-07-26 19:19:14,9.939989,-14.771559 +2024-07-26 19:19:17,9.939988,-14.771559 +2024-07-26 19:19:19,9.939988,-14.771558 +2024-07-26 19:19:22,9.939988,-14.771558 +2024-07-26 19:19:24,9.939988,-14.771558 +2024-07-26 19:20:24,9.939986,-14.771555 +2024-07-26 19:20:27,9.939986,-14.771555 +2024-07-26 19:20:29,9.939986,-14.771555 +2024-07-26 19:20:32,9.939986,-14.771555 +2024-07-26 19:20:34,9.939986,-14.771555 +2024-07-26 19:20:37,9.939986,-14.771554 +2024-07-26 19:20:39,9.939985,-14.771554 +2024-07-26 19:20:42,9.939985,-14.771554 +2024-07-26 19:20:44,9.939985,-14.771554 +2024-07-26 19:20:47,9.939985,-14.771554 +2024-07-26 19:20:49,9.939985,-14.771554 +2024-07-26 19:20:52,9.939985,-14.771554 diff --git a/gps_data_raspberrypi.csv b/gps_data_raspberrypi.csv index 1291a1b..2e5eb73 100644 --- a/gps_data_raspberrypi.csv +++ b/gps_data_raspberrypi.csv @@ -6035,3 +6035,171 @@ Timestamp,Latitude,Longitude 2024-07-26 19:11:12,9.939990,-84.104887 2024-07-26 19:11:27,9.939989,-84.104886 2024-07-26 19:11:42,9.939991,-84.104889 +2024-07-26 19:11:57,9.939997,-84.104900 +2024-07-26 19:12:12,9.939998,-84.104901 +2024-07-26 19:12:27,9.939998,-84.104902 +2024-07-26 19:12:42,9.939998,-84.104902 +2024-07-26 19:12:57,9.939997,-84.104902 +2024-07-26 19:13:12,9.939997,-84.104901 +2024-07-26 19:13:27,9.939996,-84.104900 +2024-07-26 19:13:42,9.939996,-84.104900 +2024-07-26 19:13:57,9.939996,-84.104900 +2024-07-26 19:14:12,9.939996,-84.104900 +2024-07-26 19:14:27,9.939996,-84.104900 +2024-07-26 19:14:42,9.939996,-84.104900 +2024-07-26 19:37:31,9.939860,-84.105164 +2024-07-26 19:37:45,9.939898,-84.104974 +2024-07-26 19:38:00,9.939887,-84.104929 +2024-07-26 19:38:15,9.939890,-84.104924 +2024-07-26 19:38:30,9.939891,-84.104923 +2024-07-26 19:38:45,9.939893,-84.104922 +2024-07-26 19:39:00,9.939911,-84.104968 +2024-07-26 19:39:15,9.939910,-84.104967 +2024-07-26 19:39:31,9.939910,-84.104967 +2024-07-26 19:39:46,9.939910,-84.104967 +2024-07-26 19:40:01,9.939910,-84.104967 +2024-07-26 19:40:16,9.939910,-84.104966 +2024-07-26 19:40:31,9.939910,-84.104966 +2024-07-26 19:40:46,9.939911,-84.104966 +2024-07-26 19:41:01,9.939911,-84.104966 +2024-07-26 19:41:16,9.939911,-84.104966 +2024-07-26 19:41:31,9.939911,-84.104966 +2024-07-26 19:41:46,9.939912,-84.104966 +2024-07-26 19:42:01,9.939912,-84.104966 +2024-07-26 19:42:16,9.939912,-84.104966 +2024-07-26 19:42:31,9.939913,-84.104966 +2024-07-26 19:42:46,9.939913,-84.104965 +2024-07-26 19:43:01,9.939913,-84.104966 +2024-07-26 19:43:16,9.939914,-84.104966 +2024-07-26 19:43:31,9.939914,-84.104966 +2024-07-26 19:43:46,9.939914,-84.104966 +2024-07-26 19:44:01,9.939914,-84.104966 +2024-07-26 19:44:16,9.939914,-84.104966 +2024-07-26 19:44:31,9.939914,-84.104966 +2024-07-26 19:44:46,9.939914,-84.104966 +2024-07-26 19:45:01,9.939914,-84.104966 +2024-07-26 19:45:16,9.939914,-84.104966 +2024-07-26 19:45:31,9.939914,-84.104966 +2024-07-26 19:45:46,9.939914,-84.104966 +2024-07-26 19:46:01,9.939914,-84.104966 +2024-07-26 19:46:16,9.939914,-84.104966 +2024-07-26 19:46:31,9.939914,-84.104966 +2024-07-26 19:46:46,9.939914,-84.104966 +2024-07-26 19:47:01,9.939914,-84.104966 +2024-07-26 19:47:16,9.939914,-84.104966 +2024-07-26 19:47:31,9.939914,-84.104965 +2024-07-26 19:47:46,9.939914,-84.104965 +2024-07-26 19:48:01,9.939914,-84.104965 +2024-07-26 19:48:16,9.939913,-84.104965 +2024-07-26 19:48:31,9.939913,-84.104965 +2024-07-26 19:48:46,9.939913,-84.104965 +2024-07-26 19:49:01,9.939914,-84.104965 +2024-07-26 19:49:16,9.939914,-84.104965 +2024-07-26 19:49:31,9.939914,-84.104965 +2024-07-26 19:49:46,9.939914,-84.104965 +2024-07-26 19:50:01,9.939914,-84.104964 +2024-07-26 19:50:16,9.939914,-84.104964 +2024-07-26 19:50:31,9.939914,-84.104964 +2024-07-26 19:50:46,9.939914,-84.104963 +2024-07-26 19:51:01,9.939914,-84.104963 +2024-07-26 19:51:16,9.939914,-84.104963 +2024-07-26 19:51:31,9.939914,-84.104963 +2024-07-26 19:51:46,9.939914,-84.104962 +2024-07-26 19:52:02,9.939914,-84.104963 +2024-07-26 19:52:17,9.939914,-84.104962 +2024-07-26 19:52:32,9.939914,-84.104962 +2024-07-26 19:52:47,9.939914,-84.104962 +2024-07-26 19:53:02,9.939914,-84.104962 +2024-07-26 19:53:17,9.939914,-84.104962 +2024-07-26 19:53:32,9.939915,-84.104962 +2024-07-26 19:53:47,9.939915,-84.104961 +2024-07-26 19:54:02,9.939915,-84.104961 +2024-07-26 19:54:17,9.939915,-84.104961 +2024-07-26 19:54:32,9.939915,-84.104961 +2024-07-26 19:54:47,9.939915,-84.104960 +2024-07-26 19:55:02,9.939915,-84.104960 +2024-07-26 19:55:17,9.939915,-84.104959 +2024-07-26 19:55:32,9.939916,-84.104960 +2024-07-26 19:55:47,9.939916,-84.104960 +2024-07-26 19:56:02,9.939916,-84.104960 +2024-07-26 19:56:17,9.939916,-84.104960 +2024-07-26 19:56:32,9.939916,-84.104960 +2024-07-26 19:56:47,9.939916,-84.104960 +2024-07-26 19:57:02,9.939916,-84.104960 +2024-07-26 19:57:17,9.939916,-84.104960 +2024-07-26 19:57:32,9.939916,-84.104960 +2024-07-26 19:57:47,9.939916,-84.104960 +2024-07-26 19:58:02,9.939916,-84.104960 +2024-07-26 19:58:17,9.939916,-84.104960 +2024-07-26 19:58:32,9.939916,-84.104960 +2024-07-26 19:58:47,9.939916,-84.104959 +2024-07-26 19:59:02,9.939916,-84.104959 +2024-07-26 19:59:17,9.939916,-84.104959 +2024-07-26 19:59:32,9.939916,-84.104959 +2024-07-26 19:59:47,9.939916,-84.104959 +2024-07-26 20:00:16,9.939916,-84.104959 +2024-07-26 20:00:31,9.939916,-84.104959 +2024-07-26 20:00:46,9.939916,-84.104959 +2024-07-26 20:01:01,9.939916,-84.104959 +2024-07-26 20:04:45,9.940801,-84.105177 +2024-07-26 20:05:00,9.940204,-84.105229 +2024-07-26 20:05:15,9.940104,-84.105180 +2024-07-26 20:05:30,9.940137,-84.105109 +2024-07-26 20:05:45,9.940143,-84.105094 +2024-07-26 20:06:00,9.940134,-84.105094 +2024-07-26 20:06:15,9.940117,-84.105091 +2024-07-26 20:06:30,9.940112,-84.105090 +2024-07-26 20:06:45,9.940112,-84.105090 +2024-07-26 20:08:19,9.940108,-84.105090 +2024-07-26 20:08:34,9.940107,-84.105090 +2024-07-26 20:08:49,9.940106,-84.105090 +2024-07-26 20:09:04,9.940106,-84.105089 +2024-07-26 20:09:19,9.940107,-84.105089 +2024-07-26 20:09:34,9.940107,-84.105089 +2024-07-26 20:09:49,9.940107,-84.105089 +2024-07-26 20:10:04,9.940106,-84.105089 +2024-07-26 20:10:19,9.940105,-84.105088 +2024-07-26 20:10:34,9.940106,-84.105087 +2024-07-26 20:10:49,9.940107,-84.105086 +2024-07-26 20:11:04,9.940107,-84.105084 +2024-07-26 20:11:19,9.940108,-84.105083 +2024-07-26 20:11:34,9.940108,-84.105083 +2024-07-26 20:11:49,9.940108,-84.105082 +2024-07-26 20:12:04,9.940108,-84.105082 +2024-07-26 20:12:19,9.940108,-84.105082 +2024-07-26 20:12:34,9.940107,-84.105082 +2024-07-26 20:12:49,9.940107,-84.105082 +2024-07-26 20:13:04,9.940107,-84.105080 +2024-07-26 20:13:19,9.940106,-84.105078 +2024-07-26 20:13:34,9.940105,-84.105077 +2024-07-26 20:13:49,9.940104,-84.105075 +2024-07-26 20:14:05,9.940103,-84.105075 +2024-07-26 20:33:50,9.939988,-84.104917 +2024-07-26 20:34:05,9.940113,-84.105012 +2024-07-26 20:34:20,9.940111,-84.105010 +2024-07-26 20:34:35,9.940109,-84.105008 +2024-07-26 20:34:50,9.940134,-84.105028 +2024-07-26 20:35:05,9.940137,-84.105030 +2024-07-26 20:35:20,9.940135,-84.105028 +2024-07-26 20:35:35,9.940114,-84.105014 +2024-07-26 20:35:50,9.940101,-84.105006 +2024-07-26 20:36:05,9.940071,-84.104986 +2024-07-26 20:36:20,9.940037,-84.104963 +2024-07-26 20:36:35,9.940045,-84.104970 +2024-07-26 20:36:50,9.940045,-84.104972 +2024-07-26 20:37:05,9.940046,-84.104973 +2024-07-26 20:37:20,9.940046,-84.104974 +2024-07-26 20:37:35,9.940030,-84.104962 +2024-07-26 20:38:05,9.940025,-84.104960 +2024-07-26 20:38:20,9.940023,-84.104959 +2024-07-26 20:38:35,9.940020,-84.104958 +2024-07-26 20:38:50,9.940019,-84.104957 +2024-07-26 20:39:05,9.940018,-84.104957 +2024-07-26 20:39:20,9.940017,-84.104957 +2024-07-26 20:39:50,9.940014,-84.104957 +2024-07-26 20:40:05,9.940014,-84.104957 +2024-07-26 20:41:08,9.939992,-84.104942 +2024-07-26 20:41:23,9.939986,-84.104937 +2024-07-26 20:41:38,9.939980,-84.104933 +2024-07-26 20:41:53,9.939978,-84.104930 +2024-07-26 20:42:08,9.939977,-84.104930 diff --git a/gpsmqtt.py b/gpsmqtt.py new file mode 100644 index 0000000..109d783 --- /dev/null +++ b/gpsmqtt.py @@ -0,0 +1,150 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +import RPi.GPIO as GPIO +import serial +import time +import os +import csv +from datetime import datetime +import paho.mqtt.client as mqtt + +myhost = os.uname()[1] + +# GPIO setup +GPIO.setmode(GPIO.BCM) +GPIO.setwarnings(False) +gpio_initialized = False + +ser = serial.Serial('/dev/ttyUSB3', 9600) +ser.flushInput() + +rec_buff = '' +time_count = 0 + +# CSV file setup +csv_filename = 'gps_coordinates.csv' +csv_header = ['Timestamp', 'Latitude', 'Longitude'] + +# MQTT setup +mqtt_broker = "65.108.199.212" +mqtt_port = 1883 # Default MQTT port +mqtt_topic = "iiot/coordinates" +mqtt_client = mqtt.Client() + +def mqtt_connect(): + mqtt_client.connect(mqtt_broker, mqtt_port, 60) + mqtt_client.loop_start() + +def mqtt_publish(message): + mqtt_client.publish(mqtt_topic, message) + +def write_to_csv(lat, lon): + timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + with open(csv_filename, 'a', newline='') as csvfile: + csv_writer = csv.writer(csvfile) + if os.stat(csv_filename).st_size == 0: + csv_writer.writerow(csv_header) + csv_writer.writerow([timestamp, lat, lon]) + + # Publish to MQTT + mqtt_message = f"{timestamp},{lat},{lon}" + mqtt_publish(mqtt_message) + +def parse_gps_data(gps_data): + parts = gps_data.split(',') + if len(parts) >= 4: + lat = convert_to_decimal(parts[0], parts[1]) + lon = convert_to_decimal(parts[2], parts[3]) + return lat, lon + return None, None + +def convert_to_decimal(coord, direction): + degrees = float(coord[:2]) + minutes = float(coord[2:]) + decimal = degrees + (minutes / 60) + if direction in ['S', 'W']: + decimal = -decimal + return round(decimal, 6) + +def send_at(command, back, timeout): + rec_buff = '' + ser.write((command+'\r\n').encode()) + time.sleep(timeout) + if ser.inWaiting(): + time.sleep(0.01) + rec_buff = ser.read(ser.inWaiting()) + if rec_buff != '': + if back not in rec_buff.decode(): + print(command + ' ERROR') + print(command + ' back:\t' + rec_buff.decode()) + return 0 + else: + gps_data = rec_buff.decode()[13:-36] + print(gps_data) + lat, lon = parse_gps_data(gps_data) + if lat is not None and lon is not None: + write_to_csv(lat, lon) + return 1 + else: + print('GPS is not ready') + return 0 + +def get_gps_position(): + rec_null = True + answer = 0 + print('Starting GPS') + rec_buff = '' + send_at('AT+CGPS=1','OK',1) + time.sleep(2) + while rec_null: + answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1) + if 1 == answer: + answer = 0 + if ',,,,,,' in rec_buff: + print('GPS is not ready') + rec_null = False + time.sleep(1) + else: + print('error %d'%answer) + rec_buff = '' + send_at('AT+CGPS=0','OK',1) + return False + time.sleep(1.5) + +def power_on(power_key): + print('SIM7600X is starting:') + GPIO.setmode(GPIO.BCM) + GPIO.setwarnings(False) + GPIO.setup(power_key,GPIO.OUT) + time.sleep(0.1) + 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('Good bye') + +try: + mqtt_connect() + #power_on(power_key) + get_gps_position() + #power_down(power_key) +except: + if ser != None: + ser.close() + #power_down(power_key) + GPIO.cleanup() +finally: + if ser != None: + ser.close() + GPIO.cleanup() + mqtt_client.loop_stop() + mqtt_client.disconnect() diff --git a/modbus_door.py b/modbus_door.py index 2c25690..d0f3928 100644 --- a/modbus_door.py +++ b/modbus_door.py @@ -38,7 +38,7 @@ sensy_boi.close_port_after_each_call = True # GPIO configuration PIN_17 = 17 LuzPuerta = 18 -LuzEncendido = 23 +LuzEncendido = 25 GPIO.setup(PIN_17, GPIO.IN) GPIO.setup(LuzPuerta, GPIO.OUT) GPIO.setup(LuzEncendido, GPIO.OUT)