Search code examples
pythonurllib2raspberry-pi3

raspberry pi can't make any url requests, connection timed out


I need to update my sensor data to thingspeak , so I was doing it with the python thingspeak library. Two days ago it was working fine but now it doesn't work, the connection times out , I also tried to update it with urllib2 ,that too doesnt' work.

My net connection is fine, I am able to open webpages on Pi and I can update the channel from my laptop using thingspeak library as well as urllib2. could someone kindly help me.

my code with thingspeak library:

node = False
channel_id = ""
write = ""
if data['MAC'] in ':ab:35':
 channel_id = "XXXXX"
 write = "XXXXXXXXXXX"
 node = True

if node:
 channel = thingspeak.Channel(id=channel_id,write_key=write)
 try :
     response=channel.update({1:data['TEMP'],2:data['VOLT'],3:data['PRES'],4:data['HUM']})
    print response
    print 'Thingspeak updated!!!'
 except :
    print "connection failed"

when i try urllib2 :

f = urllib2.urlopen(url)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 431, in open
  response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 449, in _open
  '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
  result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open
  context=self._context)
  File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
  raise URLError(err)
  urllib2.URLError: <urlopen error [Errno 110] Connection timed out>

when I try thingspeak library :

>>> channel = thingspeak.Channel(id=c,write_key=w)
>>> res = channel.update({1:50,2:30,3:70,4:20})
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.linux-armv7l/egg/thingspeak/thingspeak.py", line 116,         in update
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 407, in send
    raise ConnectionError(err, request=request)

requests.exceptions.ConnectionError: ('Connection aborted.', error(110, 'Connection timed out'))


Solution

  • If I use SSH and try to run the script, it works .