1 
   2 import serial ##jala la libreria serial
   3 
   4 #jala la libreria simpleosc
   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         #while 1:
  26         ser.write("A")  ##escribe A
  27         print("escribi A")
  28         ser.write("B")  ##escribe B
  29         print("escribi B")
  30 
  31         dato = ser.readline()
  32         lista = dato.split(',') #rompe lalista por el caracter ","
  33         print lista
  34         #hace globales las variables y les asigna los valores
  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") ##escribe D para parar
  42         ser.write("D") ##el prog en arduino
  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         #hace globales la variable y le asigna un valor
  59         global lat
  60         print "printing in the printStuff function ", lt
  61         dire = lt[0][0] # la direccion del mensaje (lista anidada)
  62         print "the oscaddress is ", dire
  63         tipo = lt[0][1] # el tipo de mensaje
  64         print tipo
  65         lat = lt[0][2]  # el valor del mensaje (lista anidada)
  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         #escribe los valoes en una linea con formato de mensje OSC
 103         global cont
 104         global tiempo
 105         global filename
 106         #dia = datetime.datetime.now()
 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         # Maneja la conexion y los mensajes entrantes
 121         osc.init()
 122         inSocket = osc.createListener("127.0.0.1", 54321)
 123         # osc.bind(funcion, mensaje), llama la funcion especificada cuando el mensaje especificado entra  
 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) # listen to incomming OSC in this socket
 130                 # llama las dos funciones de lectura y escritura de una vez escribe todo
 131                 lee()
 132                 escribe()
 133 
 134 
 135 
 136 
 137 
 138 if __name__ == '__main__': oscInput()

Bere.Bere: BereBere/pyBereBere (last edited 2007-09-13 16:11:44 by zea)