Created
March 29, 2016 03:35
-
-
Save rw950431/d186bb72f7d531b76a0b to your computer and use it in GitHub Desktop.
Read last update from thingspeak
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import urllib2,json | |
READ_API_KEY='your_read_api_key' | |
CHANNEL_ID=<your channel id> | |
def main(): | |
conn = urllib2.urlopen("http://api.thingspeak.com/channels/%s/feeds/last.json?api_key=%s" \ | |
% (CHANNEL_ID,READ_API_KEY)) | |
response = conn.read() | |
print "http status code=%s" % (conn.getcode()) | |
data=json.loads(response) | |
print data['field1'],data['created_at'] | |
conn.close() | |
if __name__ == '__main__': | |
main() |
The error is telling you that 'data' is an integer rather than a dictionary- most likely something has failed when retrieving the URL.
What does the http status code tell you?
What does 'print(data)' tell you about the information retrieved?
Its also possible that thingspeak have changed their API in the 5 years since I posted this. I no longer use it so cannot suggest any updates.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
print(data['field1'], data['created_at'])
TypeError: 'int' object is not subscriptable
It is giving this error. Please help