I have a Davis Vantage Vue with a WeatherLink Live (WLL). Besides uploading to weatherlink.com (Basic) every 60 seconds, I'm also uploading to Weather Underground and CWOP every 15 minutes. All of this works great. Kudos to the Davis development folks!
Never having enough data, I decided to also use the weatherlink live api (V1) to save the json data to a daily log file every 15 minutes. That's running on a Raspberry Pi using a modified version of the "Current Conditions HTTP Request – Helper Module [Python]" listed on the
https://weatherlink.github.io/weatherlink-live-local-api/ page.
That runs and logs data ok but crashes at random times. Sometimes it runs a few hours, sometimes a whole day or more. I suspect it crashes when the API tries to access the WLL at the same exact time the WLL is uploading to one of the 3 cloud servers noted above.
It has nothing to do with the Raspberry Pi or its internet connection. The same thing happens running the same Python program on one of my Windows 10 laptop.
Here's my program:
import time
import socket
import os
import json
from multiprocessing import Process
import requests
current_conditions_url = 'http://10.0.0.42:80/v1/current_conditions'
myDate1 = time.strftime("%Y%m%d")
myDate2 = myDate1
def make_request_using_socket(url):
try:
resp = requests.get(url)
print("HTTP Response Code:", resp)
json_data = json.loads(resp.text)
if json_data["data"] == None:
print (json_data["error"])
else:
global myDate2
myDate1 = time.strftime("%Y%m%d")
if myDate1 == myDate2:
wxFile = open('wx' + myDate1 + '.txt', 'a')
print (json_data, file = wxFile)
wxFile.close()
else:
myDate2 = myDate1
wxFile = open('wx' + myDate1 + '.txt', 'a')
print (json_data, file = wxFile)
wxFile.close()
except ConnectionRefusedError:
print("Encountered 'ConnectionRefusedError'. Please Retry")
except TimeoutError:
print("Encountered 'TimeoutError'. Please Retry")
def main():
while True:
global current_conditions_url
try:
make_request_using_socket(current_conditions_url)
time.sleep(3)
except ConnectionRefusedError:
print("Encountered 'ConnectionRefusedError'. Please Retry")
except TimeoutError:
print("Encountered 'TimeoutError'. Please Retry")
time.sleep(300)
if __name__ == "__main__":
main()
Here's the error I randomly get:
HTTP Response Code: <Response [200]>
HTTP Response Code: <Response [200]>
HTTP Response Code: <Response [200]>
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.7/http/client.py", line 1336, in getresponse
response.begin()
File "/usr/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.7/http/client.py", line 275, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 449, in send
timeout=timeout
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 367, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/lib/python3/dist-packages/six.py", line 692, in reraise
raise value.with_traceback(tb)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
chunked=chunked)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 384, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 380, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.7/http/client.py", line 1336, in getresponse
response.begin()
File "/usr/lib/python3.7/http/client.py", line 306, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.7/http/client.py", line 275, in _read_status
raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "HttpCurrentConditions2.py", line 48, in <module>
main()
File "HttpCurrentConditions2.py", line 39, in main
make_request_using_socket(current_conditions_url)
File "HttpCurrentConditions2.py", line 13, in make_request_using_socket
resp = requests.get(url)
File "/usr/lib/python3/dist-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/usr/lib/python3/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 498, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
pi@raspberrypi:~ $
Not being quite sure what error I should try to add an except for, I'm going to add a 3rd except to wait 3 seconds and try again. It's been running about 3 hours, but I'm not sure that will do the trick or not.
except ConnectionRefusedError:
print("Encountered 'ConnectionRefusedError'. Please Retry")
except TimeoutError:
print("Encountered 'TimeoutError'. Please Retry")
except:
time.sleep(3)
Any ideas would be appreciated?