How to read data from a Thingspeak channel using a Raspberry Pi | Raspberry Pi | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
How to read data from a Thingspeak channel using a Raspberry Pi
No permission to create posts
March 28, 2016
4:52 pm
Avatar
MohanadOdema

Silver
Members
Forum Posts: 9
Member Since:
March 18, 2016
sp_UserOfflineSmall Offline

Hello,

I'm trying to read data from an existing channel on Thingspeak using a Python Code running on a Raspberry Pi in order to automate some devices connected to the Pi but I'm having problems finding the proper library and function that facilitates these readings.

Help anyone ?

March 28, 2016
6:15 pm
Avatar
Hans
Natick, MA

Admin
Forum Posts: 396
Member Since:
January 18, 2011
sp_UserOfflineSmall Offline

CheerLights is a project where devices read data from the ThingSpeak channel 1417. That might point you in the right direction.

Also, this library looks to be API complete. You can read and write to ThingSpeak channels.

https://github.com/ninjawil/pythingspeak

The problem is that there are zero examples with the lib.

What version of Python are you using?

March 28, 2016
11:37 pm
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Basic example using standard python libraries, fetching last update from a given channel. (also at https://gist.github.com/rw950431/d186bb72f7d531b76a0b in case the forum mucks up the indentation)
See https://au.mathworks.com/help/thingspeak/get-a-channel-feed.html for other variants.

#!/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()

April 3, 2016
6:11 pm
Avatar
MohanadOdema

Silver
Members
Forum Posts: 9
Member Since:
March 18, 2016
sp_UserOfflineSmall Offline

Thank you both for your support.

I'm using Python 2.7 and it worked just fine with the URL and JSON libraries as rw950431 advised. 

Much much gratitude.

June 7, 2016
3:40 am
Avatar
shubham_chopra3
New Member
Members
Forum Posts: 1
Member Since:
June 7, 2016
sp_UserOfflineSmall Offline

The code only reads one value , can someone provide me with the code which reads values from thing speak continuously as it gets updated?

June 9, 2016
3:22 am
Avatar
rw950431

Top
Members
Forum Posts: 262
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

If you wish to read data continuously you have 2 options

1.  Wrap the above code in a loop that continually re-reads the data after a delay (at least 15 seconds, possibly more depending on your data).

2. If you have an internet-accessible device set it up as a HTTP server and set up a thingspeak react to push the data out to your device. This will (obviously) require much different code to the above.

Unfortunately, no matter how nicely everyone asks, thingspeak does not yet support MQTT or similar pub/sub protocols that would make this task very easy.Cry

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
24 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

BarryMaync, ThomasRougs, DonaldGen, prince, JrGordon, Elamsweems

Administrators: Hans: 387, lee: 457