我想连接到GPS,然后等到找到正确的卫星以获取时间和日期。 我的代码如下所示。有时这有时是行不通的... 问题: my_gps.date始终在while循环内(0,0,0),但有时rtc设置正确。 我可以使用my_gps.timestamp作为while循环的条件,但有时它有些 (0,0,0)旁边的值错误 在其他情况下,它可以完美运行。 (通常经过几次运行..(代码完成后,rtc是否仍在工作?) 您能给我一些建议吗?最佳做法是什么? (我正在使用带有pytrack的lopy4) 谢谢你,:)
#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()))