-
Notifications
You must be signed in to change notification settings - Fork 0
/
thingspeak.py
48 lines (39 loc) · 1.23 KB
/
thingspeak.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
#!/usr/bin/python3
import time
import sys
from urllib.request import urlopen
import linecache
import json
import re
from random import randrange
#Thinkgpeak write key
THING_WRITE_API_KEY='<APIKEY>'
#Thinkgpeak query string base URL
baseURL='https://api.thingspeak.com/update?api_key=%s' % THING_WRITE_API_KEY
def writeData(lat, long, alt, speed):
# Sending the data to thingspeak in the query string
conn = urlopen(baseURL + '&field1=%s' % (lat) + '&field2=%s' % (long)+ '&field3=%s' % (alt)+ '&field4=%s' % (speed))
# Closing the connection
conn.close()
def getData():
gpsDataFile = './gps.txt'
lat = re.sub('[\n]', '', linecache.getline(gpsDataFile, 1))
long = re.sub('[\n]', '', linecache.getline(gpsDataFile, 2))
alt = re.sub('[\n]', '', linecache.getline(gpsDataFile, 3))
#speed = re.sub('[\n]', '', linecache.getline(gpsDataFile, 4))
# Thingspeak Test - Random speed
speed = randrange(85, 90)
data=[lat, long, alt, speed]
return data
def main():
data = getData()
lat = data[0]
long = data[1]
alt = data[2]
speed = data[3]
# print(speed)
writeData(lat, long, alt, speed)
if __name__ == "__main__":
# execute only if run as a script
while True:
main()