################################################################################
# Nombre del Archivo: main.py
# Autor: Elías Martínez Jesús
# Correo: [email protected]
# Fecha: 12/11/2023
# Institución: Tecnológico Nacional de México (TECNM) - Campus ITT
# Curso: Sistemas Programables
#
# Objetivo:
# Este programa está diseñado para poder obtener información del módulo GPS, y mostrarlo en una pantalla oled.
#
# Historial de Revisiones:
# 12/11/2023 Elías Martínez Jespus - Creado.
#
# Enlace a GitHub Repository ó GIST:
#
#
# Enlace a Wokwi :
# https://wokwi.com/projects/381248764866046977
#
# Licencia:
# Este programa es software libre y puede ser redistribuido y/o modificado bajo los términos de la Licencia Pública General GNU
# como está publicado por la Free Software Foundation, ya sea la versión 3 de la Licencia, o (a tu elección) cualquier versión posterior.
#
# Este programa se distribuye con la esperanza de que sea útil, pero SIN GARANTÍA ALGUNA; incluso sin la garantía implícita de
# COMERCIALIZACIÓN o APTITUD PARA UN PROPÓSITO PARTICULAR. Consulte la Licencia Pública General GNU para obtener más detalles.
#
# Deberías haber recibido una copia de la Licencia Pública General GNU junto con este programa. Si no es así, consulte <http://www.gnu.org/licenses/>.
#
################################################################################
# Importación de módulos necesarios.
from machine import Pin, UART, I2C
from ssd1306 import SSD1306_I2C
import utime, time
import framebuf, sys
# Conexión a Oled I2C.
i2c=I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
# Lista de cadenas NMEA de ejemplo.
nmea_strings = [
b'$GPGGA,120000.000,3700.0000,N,12000.0000,W,1,09,0.9,102.1,M,0.0,M,,*6C\r\n',
b'$GPGGA,120100.000,3700.0015,N,12000.0030,W,1,09,0.9,102.2,M,0.0,M,,*6D\r\n',
b'$GPGGA,120200.000,3700.0030,N,12000.0060,W,1,09,0.9,102.3,M,0.0,M,,*6E\r\n',
b'$GPGGA,120300.000,3700.0045,N,12000.0090,W,1,09,0.9,102.4,M,0.0,M,,*6F\r\n',
b'$GPGGA,120400.000,3700.0060,N,12000.0120,W,1,09,0.9,102.5,M,0.0,M,,*60\r\n',
b'$GPGGA,120500.000,3700.0080,N,12000.0150,W,1,09,0.9,102.6,M,0.0,M,,*61\r\n',
b'$GPGGA,120600.000,3700.0100,N,12000.0180,W,1,09,0.9,102.7,M,0.0,M,,*62\r\n',
b'$GPGGA,120700.000,3700.0115,N,12000.0210,W,1,09,0.9,102.8,M,0.0,M,,*63\r\n',
b'$GPGGA,120800.000,3700.0130,N,12000.0240,W,1,09,0.9,102.9,M,0.0,M,,*64\r\n',
b'$GPGGA,120900.000,3700.0145,N,12000.0270,W,1,09,0.9,103.0,M,0.0,M,,*65\r\n',
b'$GPGGA,121000.000,3700.0160,N,12000.0300,W,1,09,0.9,103.1,M,0.0,M,,*66\r\n',
b'$GPGGA,121100.000,3700.0175,N,12000.0330,W,1,09,0.9,103.2,M,0.0,M,,*67\r\n',
b'$GPGGA,121200.000,3700.0190,N,12000.0360,W,1,09,0.9,103.3,M,0.0,M,,*68\r\n',
b'$GPGGA,121300.000,3700.0205,N,12000.0390,W,1,09,0.9,103.4,M,0.0,M,,*69\r\n',
b'$GPGGA,121400.000,3700.0220,N,12000.0420,W,1,09,0.9,103.5,M,0.0,M,,*6A\r\n',
]
# Guardado del estado del satélite.
FIX_STATUS = False
# Variables para guardar los parámetros de las coordenadas.
latitude = ""
longitude = ""
satellites = ""
gpsTime = ""
# Función para obtener las coordenadas del GPS.
def getPositionData(nmea_string):
global FIX_STATUS, latitude, longitude, satellites, gpsTime
parts = nmea_string.decode().split(',')
if parts[0] == '$GPGGA' and len(parts) == 15:
if parts[1] and parts[2] and parts[3] and parts[4] and parts[5] and parts[6] and parts[7]:
print(nmea_string)
latitude = convertToDegree(parts[2])
if parts[3] == 'S':
latitude = -latitude
longitude = convertToDegree(parts[4])
if parts[5] == 'W':
longitude = -longitude
satellites = parts[7]
gpsTime = parts[1][0:2] + ":" + parts[1][2:4]
FIX_STATUS = True
# Función para convertir la longitud y latitud.
def convertToDegree(raw_degrees):
raw_as_float = float(raw_degrees)
first_digits = int(raw_as_float / 100)
next_two_digits = raw_as_float - float(first_digits * 100)
converted = float(first_digits + next_two_digits / 60.0)
return converted
# Bucle externo para procesar cada cadena NMEA.
for nmea_string in nmea_strings:
getPositionData(nmea_string)
# Si los datos son encontrados, se imprimen en el Oled display.
if FIX_STATUS:
print("Fix......")
oled.fill(0)
oled.text("" + str(latitude), 0, 40)
oled.text("" + str(longitude), 0, 50)
oled.text("" + str(satellites), 0, 0)
oled.text("" + str(gpsTime), 83, 50)
oled.text("40 KM/H", 30, 20)
oled.text("S", 110, 0)
oled.show()
print(latitude)
print(longitude)
print(satellites)
print(gpsTime)
# Si no se encuentra ningún fix en las cadenas NMEA.
if not FIX_STATUS:
print("No GPS fix found.")
oled.fill(0)
oled.text("No GPS fix found", 0, 0)
oled.show()