diff --git a/GPS.py b/GPS.py new file mode 100755 index 0000000..9d50aa7 --- /dev/null +++ b/GPS.py @@ -0,0 +1,98 @@ +#!/usr/bin/python +# -*- coding:utf-8 -*- +import RPi.GPIO as GPIO +import paho.mqtt.client as mqtt +import serial +import time, os + +# MQTT Broker settings + +mqttBroker ="65.108.199.212" +myhost = os.uname()[1] +client = mqtt.Client(myhost) +client.connect(mqttBroker, 1883) + +ser = serial.Serial('/dev/ttyS0',115200) +ser.flushInput() + + +rec_buff = '' +rec_buff2 = '' +time_count = 0 + +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: + print(rec_buff.decode()) + return 1 + else: + print('GPS no está listo') + return 0 + +def get_gps_position(): + rec_null = True + answer = 0 + print('Iniciando GPS') + rec_buff = '' + send_at('AT+CGPS=1,1','OK',1) + time.sleep(2) + while rec_null: + answer = send_at('AT+CGPSINFO','+CGPSINFO: ',1) + client.publish("iiot/"+ myhost +"/gps", answer, 2) + if 1 == answer: + answer = 0 + if ',,,,,,' in rec_buff: + print('GPS no está listo') + 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 loging off:') + GPIO.output(power_key,GPIO.HIGH) + time.sleep(3) + GPIO.output(power_key,GPIO.LOW) + time.sleep(18) + print('Good bye') + +try: + #power_on(power_key) + get_gps_position() + #power_down(power_key) +except: + if ser != None: + ser.close() + #power_down(power_key) + GPIO.cleanup() +if ser != None: + ser.close() + GPIO.cleanup() diff --git a/door.py b/door.py index 7732668..61921d3 100644 --- a/door.py +++ b/door.py @@ -19,7 +19,7 @@ PIN_18 = 18 # Set GPIO result_17 = GPIO.setup(PIN_17, GPIO.IN) -result_18 - GPIO.setup(PIN_18, GPIO.OUT) +result_18 = GPIO.setup(PIN_18, GPIO.OUT) while True: diff --git a/gps.py b/gps.py index 6c5c10b..5e8264f 100644 --- a/gps.py +++ b/gps.py @@ -95,4 +95,4 @@ except: GPIO.cleanup() if ser != None: ser.close() - GPIO.cleanup() \ No newline at end of file + GPIO.cleanup() diff --git a/modbus.py b/modbus.py index 36137d9..9a487ab 100644 --- a/modbus.py +++ b/modbus.py @@ -48,7 +48,6 @@ try: client.publish("iiot/"+ myhost +"/temperature", temp) client.publish("iiot/"+ myhost +"/humidity", hum) - client.publish("iiot/"+ myhost +"/door", door) # Print the processed data print("-------------------------------------") @@ -57,9 +56,7 @@ try: print("-------------------------------------") print("") - - - time.sleep(1) + time.sleep(1.5) except KeyboardInterrupt: print("Programa terminado por el usuario") @@ -68,4 +65,4 @@ except Exception as e: print(f"Error: {str(e)}") finally: GPIO.cleanup() - print("Programa finalizado") \ No newline at end of file + print("Programa finalizado") diff --git a/push.sh b/push.sh index 29cfb4c..777027d 100755 --- a/push.sh +++ b/push.sh @@ -1,4 +1,4 @@ -#!/opt/homebrew/bin/bash +#!/bin/bash env=`date -I` git add .