Facebook
From cnk, 1 Year ago, written in Python.
Embed
Download Paste or View Raw
Hits: 94
  1. from machine import Pin, UART, I2C
  2. from ssd1306 import SSD1306_I2C
  3.  
  4. #Import utime library to implement delay
  5. import utime, time
  6.  
  7. #Oled I2C connection
  8. i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
  9. oled = SSD1306_I2C(128, 64, i2c)
  10.  
  11. #GPS Module UART Connection
  12. gps_module = UART(0, baudrate=9600, tx=Pin(12), rx=Pin(13))
  13.  
  14. #print gps module connection details
  15. print(gps_module)
  16.  
  17. #Used to Store NMEA Sentences
  18. buff = bytearray(255)
  19.  
  20. TIMEOUT = False
  21.  
  22. #store the status of satellite is fixed or not
  23. FIX_STATUS = False
  24. #Store GPS Coordinates
  25. latitude = ""
  26. longitude = ""
  27. satellites = ""
  28. gpsTime = ""
  29.  
  30.  
  31. #function to get gps Coordinates
  32. def getPositionData(gps_module):
  33.     global FIX_STATUS, TIMEOUT, latitude, longitude, satellites, gpsTime
  34.    
  35.     #run while loop to get gps data
  36.     #or terminate while loop after 5 seconds timeout
  37.     timeout = time.time() + 30   # 8 seconds from now
  38.     while True:
  39.         gps_module.readline()
  40.         buff = str(gps_module.readline())
  41.         #parse $GPGGA term
  42.         #b'$GPGGA,094840.000,2941.8543,N,07232.5745,E,1,09,0.9,102.1,M,0.0,M,,*6C\r\n'
  43.         #print(buff)
  44.         parts = buff.split(',')
  45.        
  46.         #if no gps displayed remove "and len(parts) == 15" from below if condition
  47.         if (parts[0] == "b'$GNGGA" and len(parts) == 15):
  48.             if(parts[1] and parts[2] and parts[3] and parts[4] and parts[5] and parts[6] and parts[7]):
  49.                 print(buff)
  50.                 print("Message ID  : " + parts[0])
  51.                 print("UTC time    : " + parts[1])
  52.                 print("Latitude    : " + parts[2])
  53.                 print("N/S         : " + parts[3])
  54.                 print("Longitude   : " + parts[4])
  55.                 print("E/W         : " + parts[5])
  56.                 print("Position Fix: " + parts[6])
  57.                 print("n sat       : " + parts[7])
  58.                
  59.                 latitude = convertToDigree(parts[2])
  60.                 # parts[3] contain 'N' or 'S'
  61.                 if (parts[3] == 'S'):
  62.                     latitude = -latitude
  63.                 longitude = convertToDigree(parts[4])
  64.                 # parts[5] contain 'E' or 'W'
  65.                 if (parts[5] == 'W'):
  66.                     longitude = -longitude
  67.                 satellites = parts[7]
  68.                 gpsTime = parts[1][0:2] + ":" + parts[1][2:4] + ":" + parts[1][4:6]
  69.                 FIX_STATUS = True
  70.                 break
  71.                
  72.         if (time.time() > timeout):
  73.             TIMEOUT = True
  74.             break
  75.         utime.sleep_ms(500)
  76.        
  77. #function to convert raw Latitude and Longitude
  78. #to actual Latitude and Longitude
  79. def convertToDigree(RawDegrees):
  80.  
  81.     RawAsFloat = float(RawDegrees)
  82.     firstdigits = int(RawAsFloat/100) #degrees
  83.     nexttwodigits = RawAsFloat - float(firstdigits*100) #minutes
  84.    
  85.     Converted = float(firstdigits + nexttwodigits/60.0)
  86.     Converted = '{0:.6f}'.format(Converted) # to 6 decimal places
  87.     return str(Converted)
  88.    
  89.    
  90. while True:
  91.    
  92.     getPositionData(gps_module)
  93.  
  94.     #if gps data is found then print it on lcd
  95.     if(FIX_STATUS == True):
  96.         print("fix......")
  97.         oled.fill(0)
  98.         oled.text("Lat: "+latitude, 0, 0)
  99.         oled.text("Lng: "+longitude, 0, 10)
  100.         oled.text("No of Sat: "+satellites, 0, 20)
  101.         oled.text("Time: "+gpsTime, 0, 30)
  102.         oled.show()
  103.         print(latitude)
  104.         print(longitude)
  105.         print(satellites)
  106.         print(gpsTime)
  107.        
  108.         FIX_STATUS = False
  109.        
  110.     if(TIMEOUT == True):
  111.         print("Request Timeout: No GPS data is found.")
  112.         #--------------------------------------------------
  113.         #updated on 5-May-2022
  114.         oled.fill(0)
  115.         oled.text("No GPS data is found", 0, 0)
  116.         oled.show()
  117.         #--------------------------------------------------
  118.         TIMEOUT = False
  119.        
  120.