Skip to content

Instantly share code, notes, and snippets.

@rw950431
Created March 29, 2016 03:35
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rw950431/d186bb72f7d531b76a0b to your computer and use it in GitHub Desktop.
Save rw950431/d186bb72f7d531b76a0b to your computer and use it in GitHub Desktop.
Read last update from thingspeak
#!/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()
@deeksha1665
Copy link

print(data['field1'], data['created_at'])
TypeError: 'int' object is not subscriptable
It is giving this error. Please help

@rw950431
Copy link
Author

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