driver_cv7.py 3.46 KB
# -*- coding: utf-8 -*-
# == pour donner les droits d'acces a /dev/ttS0
# gedit /boot/cmdline.txt &
# effacer console=serial0,115200
# == a priori il n'est pas necessaire de faire les deux lignes suivantes
# usermod -a -G tty pi
# chmod 777 /dev/ttyS0

import time
import serial
import RPi.GPIO as io
    
def get_data(port="/dev/ttyS0",pin_vin_bcm=25, debug_level=0):
   """Lecture d'une trame du capteur meteo CV7. 
Retourne un tuple, dans l'ordre: Un code de validité, la vitesse du vent en m/s, la direction relative du vent en degre, la température en °C, la trame IIMWV lue, la trame WIXDR lue. Le code de validité =0 si la trame est valide, =1 si Vin n'est pas detecte, =2 si les trames ne sont pas valides.

Exemple:

import driver_cv7 ; driver_cv7.get_data("/dev/ttyS0", 25,2)
"""

   # === verify 12V power supply if it is possible
   power_mife = 1
   if (pin_vin_bcm>0):
      io.setmode(io.BCM) #set up GPIO using BCM numbering
      io.setup(pin_vin_bcm,io.IN, pull_up_down=io.PUD_DOWN) # make pin an input (=arduino 35->34 SWO_T60 = Vin)
      pin34  = io.input(pin_vin_bcm)
      power_mife = 1-pin34
      if (debug_level==2):
         print("=== Voltage intput ===")
         disp ="pin34,35,gpio25,Vin="+str(pin34)+ " (0=12V 1=0V)"
         print(disp)
   err = 0
   if ( power_mife==1 ):
      power_mife_msg = "12Volts"
      power_mife = 1
   else:
      power_mife_msg = "0Volt"
      power_mife = 0
      err = err +1

   # === get the raw frame
   serialerror=0
   try:
      ser = serial.Serial(
         port=port,
         baudrate = 4800,
         parity=serial.PARITY_NONE,
         stopbits=serial.STOPBITS_ONE,
         bytesize=serial.EIGHTBITS,
         timeout=0,
      )
      if ser.isOpen():
         serialerror=0
      else:
         serialerror=1
   except:
      serialerror=2
   else:
      if ser.isOpen():
         time.sleep(0.1)
         ser.flush()
         raw_str=""
         for i in range(0,30000):
            x=ser.read()
            if (len(x)>0):
                raw_str+= "{:c}".format(x[0])
         if (debug_level==2):
            print("=== Raw frame ===")
            print(raw_str)
         ser.close()

   # === decode
   valid=0b00
   wind_dir_deg = 0
   wind_dir_kt = 0
   wind_dir_ms = 0
   wind_tmp_degC = 0
   msg_wind = ""
   msg_temp = ""
   raw_list = raw_str.split()
   if (debug_level==2):
      print("=== Raw list ===")
      print(raw_list)
   for message in raw_list:
      msg = str(message)
       # $IIMWV,135.0,R,000.0,N,A*3A
      key = "$IIMWV"
      k = msg.find(key)
      if ( (k==0) and (len(msg)>=27) ):
         msg_list = msg.split(",");
         if (debug_level==2):
            print("=== IIMWV ===")
            print(msg)
            print(msg_list)
         wind_dir_deg = float(msg_list[1])
         wind_speed_kt = float(msg_list[3])
         wind_speed_ms = wind_speed_kt*0.514444
         valid = valid | 0b01
         msg_wind = msg
      # $WIXDR,C,021.0,C,,*51
      key = "$WIXDR"
      k = msg.find(key)
      if ( (k==0) and (len(msg)>=21) ):
         msg_list = msg.split(",");
         if (debug_level==2):
            print("=== WIXDR ===")
            print(msg)
            print(msg_list)
         wind_tmp_degC = float(msg_list[2])
         valid = valid | 0b10
         msg_temp = msg
   # === return
   err = 0
   if (power_mife==0):
      err += 1
   if (serialerror==1):
      err += 2
   if (valid==0):
      err += 4

   return err, power_mife, wind_dir_deg, wind_speed_ms, wind_tmp_degC, power_mife_msg, msg_wind, msg_temp