-
Notifications
You must be signed in to change notification settings - Fork 5
/
llap-cosm.py
executable file
·99 lines (78 loc) · 2.94 KB
/
llap-cosm.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# LLAP to COSM
# dpslwk 20/03/13
import sys, time, Queue
import LLAPSerial
import eeml
class LLAPCOSM:
def __init__(self):
self._running = True
self.devid = "PI"
self.port = "/dev/ttyAMA0"
#cosm details
self.COSMMQTTServer = "api.cosm.com"
self.COSMAPIKey ="YOUR API KEY HERE" #<<<YOUR FEED API KEY HERE
self.COSMFeed = 123456 #<<<YOUR FEED NUMBER HERE
self.COSMUrl = '/v2/feeds/{feednum}.xml' .format(feednum = self.COSMFeed)
#setup serial bits
self.queue = Queue.Queue()
self.serial = LLAPSerial.LLAPSerial(self.queue)
def __del__(self):
pass
def on_init(self):
# connect serial on start
self.serial.connect(self.port)
print("Serial Connected")
self._running = True
print("Running")
def main(self):
if self.on_init() == False:
self._running = False
# loop
while ( self._running ):
try:
self.on_loop()
except KeyboardInterrupt:
print("Keybord Quit")
self._running = False
self.disconnect_all()
def disconnect_all(self):
# disconnet serial
self.serial.disconnect()
def on_loop(self):
if not self.queue.empty():
llapMsg = self.queue.get()
#print(llapMsg)
devID = llapMsg[1:3]
payload = llapMsg[3:]
#main state machine to handle llapMsg's
if payload.startswith('TMPA'):
#got Temp from solar
temp = payload[4:]
cosm = eeml.Pachube(self.COSMUrl, self.COSMAPIKey)
#send celsius data
cosm.update([eeml.Data(devID + "_Temperature", temp, unit=eeml.Celsius())])
print("Cosm updated "+devID+"_Temperature with value: "+temp);
# push data to cosm
try:
cosm.put()
except :
# that didnt work now what?
print("Failed to Send")
elif payload.startswith('BATT'):
# strip temp from llap
voltage = payload[4:8]
# open cosm feed
cosm = eeml.Pachube(self.COSMUrl, self.COSMAPIKey)
#send celsius data
cosm.update([eeml.Data(devID + "_Voltage", voltage, unit=eeml.Unit('Volt', 'derivedSI', 'V'))])
print("Cosm updated "+ devID +"_Voltage with value: "+voltage);
# push data to cosm
try:
cosm.put()
except :
# that didnt work now what?
print("Failed to Send")
if __name__ == "__main__":
application = LLAPCOSM()
application.main()