2017-06-22 128 views
0

我需要将我的传感器数据更新为thingspeak,所以我正在使用python thingspeak库进行操作。前两天它工作正常,但现在它不起作用,连接超时,我也尝试用urllib2更新它,这也不起作用。Raspberry Pi无法发出任何URL请求,连接超时

我的网络连接很好,我可以在Pi上打开网页,我可以使用thingspeak库和urllib2从我的笔记本电脑更新频道。 有人可以帮助我。

我与thingspeak库代码:

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" 

当我尝试的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> 

当我尝试thingspeak库:

>>> 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:(”连接中止',错误(110,'连接超时'))

+0

添加异常/错误日志,否则我们将无法提供帮助。 – moritzg

+0

我已经更新了erroe日志请看看 –

+0

昨天我做了一些tcp套接字编程,并使用了很多端口,是不知何故对这个错误负责? –

回答

0

如果我使用SSH并尝试运行该脚本,它将起作用。

-1

添加一个新的用户代理,用它们作为论据的urlopen创建的请求对象&:

import urllib2 

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

response = urllib2.urlopen(request) 
+0

为什么不? urllib2尊重robots.txt,许多网站会阻止默认的User-Agent。顺便说一下,连接超时可能从端口关闭。如果不是来自User-Agent,你可以考虑一些端口,如果没有打开 – DreamInBox

+0

它没有帮助,给urllib2.URLError:

+0

你可以显示你的源代码? – DreamInBox

0

我现在用的覆盆子PI将数据发送到thingspeak。 我所做的是这样的:

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" 

它工作得很好。 关键是您的频道键的字符串。

希望它有帮助。

+0

不起作用,打印连接失败 –

+0

它在我的设备中工作。尝试查看您是否联网阻止此连接。例如端口80. – imoutidi

+0

我想,这是问题,我该如何检查它? –