-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfakeUpdate.py
37 lines (36 loc) · 1.29 KB
/
fakeUpdate.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
__author__ = 'Luke'
import numpy as np
import time
import http.client, urllib.parse
from pprint import pprint
API_KEY = "H671BFO41N0TP246"
n_spine = 8.0
fields = list(map(lambda x: "field"+x, map(str, range(1, 9))))
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
def main():
period = np.arange(0, 2, 2.0/n_spine) * np.pi
print (2.0/n_spine)
while True:
for dt in period:
sensors = np.cos(period+dt)*5 + 25
#print sensors
data = dict(zip(fields, sensors))
data["key"] = API_KEY
params = urllib.parse.urlencode(data)
conn = http.client.HTTPConnection("api.thingspeak.com:80")
try:
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
#data = response.read()
conn.close()
print(response.status, response.reason)
except (KeyboardInterrupt, SystemExit):
raise
except:
#catch all errors, handle nothing for now
#multiple devices uploading to the same channel will cause errors
print ("an error occurred")
time.sleep(16)
if __name__ == "__main__":
main()