-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThingspeak1.py
49 lines (45 loc) · 1.3 KB
/
Thingspeak1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#getting data from thingspeak
#=================================
"""
chID = Channel Id
api = read api key
"""
def retrivedata(chID,api):
try:
import json
from urllib.request import urlopen
data = "https://api.thingspeak.com/channels/"+chID+"/feeds.json?api_key="+api+"&results=1"
data = urlopen(data)
data = json.loads(data.read())
#get the FEED only
data = data['feeds']
#bcoz data is at 0 index
data = data[0] #give a dict
#retriving the data
field=list()
for x in range(len(data)-2):
field.append(data['field'+str(x+1)])
return(field)
except Exception as e:
print("Error Found\n ",e)
return None
"""
api = Write api key
n = total number of fields
"""
def uploaddata(api,n,*data):
try:
import json
from urllib.request import urlopen
url = "https://api.thingspeak.com/update?api_key="+api
for z in range(n):
url+="&field"+str(z+1)+"="+str(data[z])
data = urlopen(url)
data = json.loads(data.read())
if(data > 0):
return True
else:
return False
except Exception as e:
print("Error Found\n ",e)
return False