Hello Blynk,
I believe this is more a Python question but I would like to check if you have faced this before and could give me some help.
I am trying to build a Python script which monitors the device state (online) and get some pin values from Blynk Server using the code below:
import urllib.request
import time
while True:
#Checks if device is connected and ONLINE
device_status_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/isHardwareConnected')
time.sleep(2) # Delay for 2 seconds.
device_status = device_status_buffer.read()
device_status = str(device_status,'utf-8')
#Checks if SMS button is active (SMS notifications YES/NO)
sms_notification_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/get/V122')
time.sleep(2) # Delay for 2 seconds.
sms_notification = sms_notification_buffer.read()
sms_notification = str(sms_notification,'utf-8')
#Gets Celphone numbers from App
cel_1_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/get/V117')
time.sleep(2) # Delay for 2 seconds.
cel_1 = cel_1_buffer.read()
cel_1 = str(cel_1,'utf-8')
cel_2_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/get/V118')
time.sleep(2) # Delay for 2 seconds.
cel_2 = cel_2_buffer.read()
cel_2 = str(cel_2,'utf-8')
cel_3_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/get/V119')
time.sleep(2) # Delay for 2 seconds.
cel_3 = cel_3_buffer.read()
cel_3 = str(cel_3,'utf-8')
cel_4_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/get/V120')
time.sleep(2) # Delay for 2 seconds.
cel_4 = cel_4_buffer.read()
cel_4 = str(cel_4,'utf-8')
cel_5_buffer = urllib.request.urlopen('http://blynk-cloud.com/AUTH_TOKEN/get/V121')
time.sleep(2) # Delay for 2 seconds.
cel_5 = cel_5_buffer.read()
cel_5 = str(cel_5,'utf-8')
print(device_status)
print(sms_notification)
print(cel_1)
print(cel_2)
print(cel_3)
print(cel_4)
print(cel_5)
As you can see I have added some time.sleep of 2 seconds and the script works fine for some loops, but after a short while it gets 2 errors (below strong) I suspect this is caused by several url request in short time but I dont know how to solve yet any idea?
Traceback (most recent call last):
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 1317, in do_open
encode_chunked=req.has_header(‘Transfer-encoding’))
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1016, in _send_output
self.send(msg)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 956, in send
self.connect()
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\socket.py”, line 727, in create_connection
raise err
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\socket.py”, line 716, in create_connection
sock.connect(sa)
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “C:\Users\clucas\Desktop\Blynk_Test.py”, line 21, in
cel_1_buffer = urllib.request.urlopen(‘http://blynk-cloud.com/XXXXX/get/V117’)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 222, in urlopen
return opener.open(url, data, timeout)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 525, in open
response = self._open(req, data)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 543, in _open
‘_open’, req)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 503, in _call_chain
result = func(*args)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File “C:\Users\clucas\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>