problem with reading data from thingspeak channel fields using Python program | ThingSpeak Projects | 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
problem with reading data from thingspeak channel fields using Python program
No permission to create posts
July 30, 2019
4:31 am
Avatar
anitahk
New Member
Members
Forum Posts: 4
Member Since:
July 25, 2019
sp_UserOfflineSmall Offline

hello friends,

I am working on IoT projects since recently. I am trying to read the data written to multiple fields from thingspeak channel using the following Python program

import urllib2
import json
import time

READ_API_KEY='D1E406A01CGAO0YA'
CHANNEL_ID= '835361'

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']

print (b)
print (c)

time.sleep(5)

TS.close()

Even though the data is available on all the fields it displays only one field data at a time and remaining fields value as None. plz help me with this urgently.

regards,
Anita

July 30, 2019
9:36 am
Avatar
Vinod

MathWorks
Members
Forum Posts: 339
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline

Why do you have a sleep for only 5 seconds? With a free account, your channel can only be updated every 15s, reading every 5s is wasteful.

A far more efficient way to read data, compared to the polling approach you have here, is to use MQTT subscribe API to subscribe to your channel. See this stackoverflow post, for example: https://stackoverflow.com/questions/44046391

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
8 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

khanhNguyen_CT, susyburr, quijano__, meosuckhoe, briskpanda, Dejw0089

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457