Seting up RTC based on GPS data



  • Hello,
    I want to connect to the GPS, and wait until the find the correct satellites to get the time and date.
    My code for that is look like this. This is sometimes working sometimes not...
    Issues:
    my_gps.date is always (0,0,0) inside the while loop but however sometimes the rtc set correctly.
    I could use my_gps.timestamp for conditions for the while loop, but sometimes it's got some
    wrong values beside (0,0,0)
    Other cases it's works perfectly. (Usually after few run..(Is the rtc still working after the code is finished?)
    Can you gave me some tip how can I improve it? What is the best practice for that?
    (I am using a lopy4 with a pytrack)
    Thank you, :)

    #Setting up the gps:
    py = Pytrack()
    my_gps = MicropyGPS(local_offset=1,location_formatting='dd')
    L76micropyGPS = L76micropyGPS(my_gps, py)
    gpsThread = L76micropyGPS.startGPSThread()
    print("Waiting for gps signal")
    counter = 0
    while (my_gps.date == (0, 0, 0) and counter < 10):
        print("Waiting for gps signal: "+str(counter)+" "+str(my_gps.date)+" "+str(my_gps.timestamp))
        counter +=1
        time.sleep(1)
    time.sleep(2)
    print("Date: "+str(my_gps.date))
    print("GPS signal is found")
    print("Time: "+str(my_gps.timestamp))
    time.sleep(2)
    print(my_gps.date[0])
    print(my_gps.date[1])
    print(my_gps.date[2])
    
    
    print("Setting up the RTC")
    rtc = machine.RTC()
    print("RTC before init: {}".format(rtc.now()))
    dateYear = int(str(20) + str(my_gps.date[2]))
    rtc.init((dateYear, my_gps.date[1], my_gps.date[0], my_gps.timestamp[0], my_gps.timestamp[1], int(my_gps.timestamp[2]), 0, 0)) #seting up the rtc 
    print("RTC is set : {}".format(rtc.now()))
    

Log in to reply
 

Pycom on Twitter