我有这个问题,我什么时候运行这段python代码:
import sys
import time
import geolocation
import publisher
SleepTime = 10 # seconds
_lat = 0.00
_lon = 0.00
def maintain():
global _lat
global _lon
(lat, lon, accuracy) = geolocation.getLocation()
if lat != _lat or lon != _lon:
data = str(lat) + "," + str(lon) + "," + str(accuracy)
print("publishing ", data)
publisher.publishtoInternet(data)
_lat = lat
_lon = lon
else:
print("no change in coordinates")
print("program begins")
while True:
try:
maintain()
except Exception as inst:
print(type(inst), ' exception captured')
print(inst)
sys.stdout.flush()
# file = open('/tmp/locktracker.error.log','a')
# file.write('exception occurred, trying to reboot')
# file.close()
# call(["sudo","reboot"])
# break
for i in range(0, SleepTime):
sys.stdout.write("\restarting in %d seconds " % (SleepTime - i))
sys.stdout.flush()
time.sleep(1)
使用这些名为geolocation和publisher的其他python脚本: 地理位置:
import json
import requests
def getLocation():
# print lac, cellTower, operator
payload = {'considerIp': 'true'}
jsonPayload = json.dumps(payload)
# print jsonPayload
headers = {'content-type': 'application/json'}
privateKey = "<>"
url = 'https://www.googleapis.com/geolocation/v1/geolocate?key=' +
privateKey
r = requests.post(url, data=jsonPayload, headers=headers)
response = json.loads(r.text)
# print response
return response["location"]["lat"], response["location"]["lng"],
response["accuracy"]
出版商:
import time
import urllib
def publishtoInternet(temp):
url = "https://api.thingspeak.com/update?api_key=" + temp
try:
print(url)
result = urllib.urlopen(url).read()
print(result + str(temp))
except:
print('exception uploading, logging to file')
log(temp)
def log(temp):
f = open('log', 'a')
t = time.localtime(time.time())
msg = str(t.tm_hour) + ' ' + str(t.tm_min)
f.write(msg)
f.write('temp: ')
f.write(temp)
f.write('\n')
f.close()
我得到这个错误,就是对于位置字,我得到然后代码重新启动。这真让我烦恼,因为这是我学校的一个项目,我必须完成。此外,似乎如果我删除位置字并将纬度和经度保持为lat和long,它会返回相同的错误。