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

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





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

Register Lost password?
sp_Feed sp_TopicIcon
How to read data from a Thingspeak channel using a Raspberry Pi
Avatar
MohanadOdema

Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
1
March 28, 2016 - 4:52 pm
sp_Permalink sp_Print

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 ?

Avatar
Hans
Natick, MA

Forum Posts: 390
sp_UserOfflineSmall Offline
2
March 28, 2016 - 6:15 pm
sp_Permalink sp_Print

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?

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
3
March 28, 2016 - 11:37 pm
sp_Permalink sp_Print sp_EditHistory

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()

Avatar
MohanadOdema

Silver
Forum Posts: 9
sp_UserOfflineSmall Offline
4
April 3, 2016 - 6:11 pm
sp_Permalink sp_Print

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.

Avatar
shubham_chopra3
New Member
Forum Posts: 1
sp_UserOfflineSmall Offline
5
June 7, 2016 - 3:40 am
sp_Permalink sp_Print

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

Avatar
rw950431

Forum Posts: 252
sp_UserOfflineSmall Offline
6
June 9, 2016 - 3:22 am
sp_Permalink sp_Print

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:
26 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

candacele1

WilliamBer

roxieob18

Metawatch

ozayagw

ubabeutiti

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee