-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphantBridge.py
executable file
·63 lines (48 loc) · 1.73 KB
/
phantBridge.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-i","--input",dest="jsonSettings",help="Input settings file, JSON format.",metavar="FILE")
parser.add_option("-c","--clear",action="store_true",dest="clearFlag",default=False,help="Clear data from server")
(options,args) = parser.parse_args()
import json
settings = json.load(open(options.jsonSettings))
if options.clearFlag:
import requests
r = requests.get('http://data.sparkfun.com/input/'+settings["dataPublicKey"]+'/clear?private_key='+settings["dataPrivateKey"])
print r.url
print r.content
exit()
from pprint import pprint
pprint(settings)
import serial
port = serial.Serial(settings["port"],settings["baudrate"],timeout=0.5,writeTimeout=0)
import requests
getURL = 'http://data.sparkfun.com/input/'+settings["dataPublicKey"]
import time
lastCommand = time.time()
lastSend = time.time()
import sys
while True:
try:
data = port.read(256)
except IOError:
print "Unexpected error:", sys.exc_info()[0]
if len(data) > 0 and time.time() - lastSend > 5.0:
lastSend = time.time()
try:
print data
r = requests.get(getURL+'?private_key='+settings["dataPrivateKey"]+'&'+data)
print r.text
except ValueError:
print "Unexpected error:", sys.exc_info()[0]
if time.time() - lastCommand > 2.0:
lastCommand = time.time()
try:
r = requests.get('http://data.sparkfun.com/output/'+settings['cmdPublicKey']+'.json')
command = r.json()
print "PulseWidths: "+command[0]["pulsewidtha"]+", "+command[0]["pulsewidthb"]
commandOut = "a"+command[0]["pulsewidtha"]+"\nb"+command[0]["pulsewidthb"]+"\n"
print "Sending:\n" + commandOut
port.write(str(commandOut))
except ValueError:
print "Unexpected error:", sys.exc_info()[0]