1
2 import serial
3
4
5
6 import osc
7 import decimal
8 import time
9 import datetime
10
11 tiempo = 0.0
12 cont = 0
13 temp = 0
14 hum = 0
15 lat = 0.0
16 lon = 0.0
17 alt = 0.0
18 dato = datetime.datetime.now()
19 filename = "reg-"+str(dato.strftime("%Y-%m-%d-%H-%M-%S"))+".txt"
20
21
22 def lee():
23 global tiempo
24 ser = serial.Serial('/dev/ttyUSB0', 115200, timeout=None)
25
26 ser.write("A")
27 print("escribi A")
28 ser.write("B")
29 print("escribi B")
30
31 dato = ser.readline()
32 lista = dato.split(',')
33 print lista
34
35 global temp
36 temp = lista[0]
37 global hum
38 hum = lista [1]
39 print "temp = ",temp, " hum = ",hum
40
41 ser.write("D")
42 ser.write("D")
43
44 """salida = open("reg.txt","a")
45 salida.write("/sensor/temperatura/ i " + str(temp) + "\n")
46 salida.write("/sensor/humedad/ i " + str(hum) + "\n")
47 print "escribi el archivo"""
48
49
50 """tiempo = tiempo +0.01
51 salida.write(str(tiempo))
52 print repr(tiempo)
53 salida.close()
54 ser.close()"""
55
56 def saveLat(*lt):
57 """deals with "print" tagged OSC addresses """
58
59 global lat
60 print "printing in the printStuff function ", lt
61 dire = lt[0][0]
62 print "the oscaddress is ", dire
63 tipo = lt[0][1]
64 print tipo
65 lat = lt[0][2]
66 print "the value is ", lat
67 """out = open("reg.txt","a")
68 out.write(dire + " "+ tipo + " " + str(val) + "\n")
69 out.close()"""
70
71 def saveLon(*ln):
72 """deals with "print" tagged OSC addresses """
73 global lon
74 print "printing in the printStuff function ", ln
75 dire = ln[0][0]
76 print "the oscaddress is ", ln
77 tipo = ln[0][1]
78 print tipo
79 lon = ln[0][2]
80 print "the value is ", lon
81 """out = open("reg.txt","a")
82 out.write(dire +" "+tipo+" "+str(val)+ "\n")
83 out.close()"""
84
85 def saveAlt(*al):
86 """deals with "print" tagged OSC addresses """
87 global alt
88 print "printing in the printStuff function ", al
89 dire = al[0][0]
90 print "the oscaddress is ", dire
91 tipo = al[0][1]
92 print tipo
93 alt = al[0][2]
94 print "the value is ", alt
95 """out = open("reg.txt","a")
96 out.write(dire + " " + tipo +" "+str(val)+ "\n")
97 out.close()"""
98
99
100
101 def escribe():
102
103 global cont
104 global tiempo
105 global filename
106
107 tiempo = time.clock()
108 salida = open(filename, "a")
109 salida.write(str(tiempo)+" /sensor/"+str(cont)+"/temperatura/ i "+ str(temp)+"\n")
110 salida.write(str(tiempo)+" /sensor/"+str(cont)+"/humedad/ i "+ str(hum)+"\n")
111 salida.write(str(tiempo)+" /sensor/"+str(cont)+"/gps/lat/ f "+ str(lat)+"\n")
112 salida.write(str(tiempo)+" /sensor/"+str(cont)+"/gps/lon/ f "+ str(lon)+"\n")
113 salida.write(str(tiempo)+" /sensor/"+str(cont)+"/gps/alt/ f "+ str(alt)+"\n")
114 salida.close()
115 cont = cont + 1
116 time.sleep(5.0)
117
118
119 def oscInput():
120
121 osc.init()
122 inSocket = osc.createListener("127.0.0.1", 54321)
123
124 osc.bind(saveLat, "/kismet/gps/lat")
125 osc.bind(saveLon, "/kismet/gps/lon")
126 osc.bind(saveAlt, "/kismet/gps/alt")
127
128 while 1:
129 osc.getOSC(inSocket)
130
131 lee()
132 escribe()
133
134
135
136
137
138 if __name__ == '__main__': oscInput()