1

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'))

Pratik Kumar
  • 2,211
  • 1
  • 17
  • 41

3 Answers3

0

I am using the raspberry-pi to send data to thingspeak. What I do is something like this:

import httplib
import urllib

    #Preparing to send the information to the cloud
            params=urllib.urlencode({'field1':liters,'key':key})
            headers={"Content-typZZe": "application/x-www-form-urlencoded","Accept":"text/plain"}
            conn=httplib.HTTPConnection("api.thingspeak.com:80")

    #Sending the data to the thingspeak platform
            try:
                    conn.request("POST", "/update", params, headers)
                    response=conn.getresponse()
                    print response.status, response.reason
                    data=response.read()
                    conn.close()
            except:
                    print "connection failed"

And it works just fine. The key is the string of your channel key.

Hope it helps.

Anoroah
  • 1,987
  • 2
  • 20
  • 31
0

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

Pratik Kumar
  • 2,211
  • 1
  • 17
  • 41
-1

Add a new User-Agent, by creating Request objects & using them as arguments for urlopen:

import urllib2

request = urllib2.Request('http://www.example.com/')
request.add_header('User-agent', 'Mozilla/5.0 (Linux i686)')

response = urllib2.urlopen(request)
DreamInBox
  • 174
  • 2
  • 12
  • Why not ??? urllib2 respects robots.txt and many sites block the default User-Agent. By the way, connect timed out may from ports is closed. If not from User-Agent, you can consider some port if not open – DreamInBox Jun 22 '17 at 08:43
  • It did not help, gives urllib2.URLError: – Pratik Kumar Jun 22 '17 at 08:55
  • f = urllib2.urlopen(url). What is your url ? It is http or https – DreamInBox Jun 22 '17 at 09:36
  • I see your web domain is https. Your connection is https so you must use ssl cetification.You can see:https://stackoverflow.com/questions/2146383/https-connection-python – DreamInBox Jun 23 '17 at 01:07