driver_rg11.py
2.3 KB
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
# -*- coding: utf-8 -*-
import RPi.GPIO as io
def get_data(pin_rain_bcm=19, pin_dry_bcm=20, pin_vin_bcm=25, debug_level=0):
"""Lecture d'une trame du capteur meteo RG11.
Retourne un tuple, dans l'ordre: Un code de validité, etat (-1=unknown, 0=dry 1=rain), bit rain, bit dry. Le code de validité =0 si la trame est valide, =1 si Vin n'est pas detecte, =2 si les bits sont tous deux egaux a 0, =2 si les bits sont tous deux egaux à 1.
Exemple:
import driver_rg11 ; driver_rg11.get_data(19,20,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
io.setwarnings(False)
io.setmode(io.BCM) #set up GPIO using BCM numbering
io.setup(pin_rain_bcm,io.IN, pull_up_down=io.PUD_DOWN) # make pin an input (=arduino 46->36 rain)
io.setup(pin_dry_bcm,io.IN, pull_up_down=io.PUD_DOWN) # make pin an input (=arduino 47->38 dry)
pin36 = io.input(pin_rain_bcm)
pin38 = io.input(pin_dry_bcm)
if (debug_level==2):
print("=== Bit states ===")
disp ="pin36,46,gpio19,rain ="+str(pin36)+" pin38,47,gpio20,dry ="+str(pin38)
print(disp)
# === return
err = 0
rain_state_msg = "Unknown"
rain_state = 2
if (power_mife==1):
if ( (pin36==0) and (pin38==0) ):
err = err + 2
elif ( (pin36==1) and (pin38==1) ):
err = err + 4
elif ( (pin36==0) and (pin38==1) ):
rain_state_msg = "Rain"
rain_state = 1
elif ( (pin36==1) and (pin38==0) ):
rain_state_msg = "Dry"
rain_state = 0
if (debug_level>=1):
print("=== RG11 states ===")
disp = " Vin ="+power_mife_msg + " RG11 ="+rain_state_msg
print(disp)
return err, power_mife, rain_state, rain_state, power_mife_msg, rain_state_msg, pin34, pin36, pin38