-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwater pump
More file actions
106 lines (95 loc) · 3.27 KB
/
water pump
File metadata and controls
106 lines (95 loc) · 3.27 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
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
100
101
102
103
104
105
106
import RPi.GPIO as GPIO
import I2C_LCD_driver
import time
import spidev
from subprocess import call
from datetime import datetime
# 드라이버 모터와 연결한 핀 번호 지정 (BCM 기준)
IN1= 5 #GPIO 5 (Board모드의 29번) - 워터펌프 핀
IN2 = 6 #GPIO 6 (Board모드의 31번)
# ENA = 13
R = 16 #GPIO 16 (Board모드의 36번)
G = 20 #GPIO 20 (PCM_DIN, Board모드의 28번)
B = 21 #GPIO 21 (PCM_DOUT, Board모드의 40번)
btnPin = 22 #GPIO 22 (Board모드의 15번)
shutdown_sec = 2
HUM_THRESHOLD = 20 # 습도(%) 임계치 (변경O)
HUM_MAX = 0 # 토양습도센서 출력 값
# GPIO 설정
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(IN1, GPIO.OUT)
GPIO.setup(IN2, GPIO.OUT)
GPIO.setup(R, GPIO.OUT)
GPIO.setup(G, GPIO.OUT)
GPIO.setup(B, GPIO.OUT)
GPIO.setup(btnPin, GPIO.IN)
# GPIO.output(IN1, GPIO.LOW)
# GPIO.output(IN2, GPIO.LOW)
# GPIO.setup(ENA, GPIO.OUT)
# GPIO.output(ENA, GPIO.LOW)
# spi 설정
spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz = 500000 # 모터 속도
# ADC 값을 가져오는 함수
def read_spi_adc(adcChannel):
adcValue = 0
buff = spi.xfer2([1,(8+adcChannel)<<4,0])
adcValue = ((buff[1]&3)<<8)+buff[2]
return adcValue
# 센서 값을 백분율로 변환하는 함수
def map(value, min_adc, max_adc, min_hum, max_hum):
adc_range = max_adc - min_adc
hum_range = max_hum - min_hum
scale_factor = float(adc_range) / float(hum_range)
return min_hum + ((value - min_adc) / scale_factor)
#
def getPressTime():
elapsed = 0
if pressTime is not None:
elapsed = (datetime.now() - pressTime).total_seconds()
return elapsed
try:
strPer = "%"
mylcd = I2C_LCD_driver.lcd()
adcChannel = 0
strCur = ""
# 워터 펌프를 돌릴지 말지 결정하는 while문
while True:
input = GPIO.input(btnPin)
prevInput = input
if input == 0:
if prevInput == -1 or prevInput == 1:
pressTime = datetime.now()
elif prevInput == 0:
if getPressTime() >= shutdown_sec:
call(['shutdown', '-h', 'now'], shell=False)
break
adcValue = read_spi_adc(adcChannel)
hum = 100 - int(map(adcValue, HUM_MAX, 1023, 0, 100)) # 가져온 데이터를 % 단위로 변환, 습도가 높을수록 낮은 값을 반환 (100에서 뺀 이유)
# 임계치보다 수분 값이 작을 경우, 워터 펌프 작동
if hum < HUM_THRESHOLD:
GPIO.output(IN1, GPIO.HIGH) # 워터펌프 가동
GPIO.output(IN2, GPIO.LOW)
GPIO.output(R, GPIO.HIGH)
GPIO.output(G, GPIO.LOW)
GPIO.output(B, GPIO.LOW)
print('yes')
strCur = "Warning"
# 임계치보다 수분 값이 같거나 클 경우, 워터 펌프 작동 X
else:
GPIO.output(IN1, GPIO.LOW) # 워터펌프 가동 X
GPIO.output(IN2, GPIO.LOW)
GPIO.output(R, GPIO.LOW)
GPIO.output(G, GPIO.HIGH)
GPIO.output(B, GPIO.LOW)
print('no')
strCur = "Normal"
mylcd.lcd_clear()
mylcd.lcd_display_string("HUM:%d%s" % (hum, strPer), 1)
mylcd.lcd_display_string("STATUS:%s" % strCur, 2)
time.sleep(0.5)
finally:
GPIO.cleanup()
spi.close()