Python:通过Thingspeak获取数据

时间:2016-12-11 18:21:41

标签: python sql arduino

我想通过thingspeak捕获传感器数据。 我在浏览器中使用了随api键提供的url:

http://api.thingspeak.com/update?key=MYKEY&field1=25&field2=75

我希望它会返回field1和field2,但下面的结果只显示了field1的值。

"channel":{
    "id":202242,
    "name":"DHT11",
    "latitude":"0.0",
    "longitude":"0.0",
    "field1":"Temperature ( degC ) 1",
    "field2":"Humidity ( % )",
    "created_at":"2016-12-11T17:16:21Z",
    "updated_at":"2016-12-11T18:12:00Z",
    "last_entry_id":12
},

"feeds":[
    {
        "created_at":"2016-12-11T18:12:00Z",
        "entry_id":12,
        "field1":25
    }
]

我错过了哪一步?

1 个答案:

答案 0 :(得分:0)

尝试这种方法: 在这里,您使用API​​发出请求。您会找到各种API请求here

import urllib2
import json
import time


READ_API_KEY='   '
CHANNEL_ID= '    '


while True:
    TS = urllib2.urlopen("http://api.thingspeak.com/channels/%s/feeds/last.json?api_key=%s" \
                       % (CHANNEL_ID,READ_API_KEY))

    response = TS.read()
    data=json.loads(response)


    a = data['created_at']
    b = data['field1']
    c = data['field2']
    d = data['field3']
    print a + "    " + b + "    " + c + "    " + d
    time.sleep(5)   

    TS.close()