해커톤 2019 113조 서버

Posted by PeEn
2019. 12. 27. 18:45 Programing/Raspberry Pi)

# -*- coding: utf-8 -*- import RPi.GPIO as gpio import time import sys import warnings import threading warnings.filterwarnings('ignore') cnt_One = 0 #샴푸 내린 횟수 cnt_Two = 0 #린스 내린 횟수 cnt_Tree = 0 #바디 내린 횟수 LED_STAT = 0 #LED 기본값 default_Servo = 0 #서보모터 기본값 0 45 90 135 chang_Servo =5 #서보모터 변경값 # ___gpio 핀번호___# switch = 17 # 버튼 LED_1 = 27 # LED1 LED_2 = 19 # LED2 LED_3 = 4 # LED3 LED_4 = 9 #LED4 초음파 대응센서 SERVO_SIG = 20 # Servo TRIGER = 12 #초음파 tring ECHO = 16 #초음파 echo # ____gipo 핀 세팅___# gpio.cleanup() gpio.setmode(gpio.BCM) gpio.setup(switch, gpio.IN) gpio.setup(LED_1, gpio.OUT) gpio.setup(LED_2, gpio.OUT) gpio.setup(LED_3, gpio.OUT) gpio.setup(LED_4, gpio.OUT) gpio.output(LED_1, gpio.LOW) gpio.output(LED_2, gpio.LOW) gpio.output(LED_3, gpio.LOW) gpio.output(LED_4, gpio.LOW) gpio.setup(TRIGER, gpio.OUT) gpio.setup(ECHO,gpio.IN) gpio.setup(SERVO_SIG, gpio.OUT) pwm = gpio.PWM(SERVO_SIG, 50) startTime = time.time() def Ultra(): global cnt_One, cnt_Tree, cnt_Two while True: gpio.output(TRIGER,gpio.LOW) time.sleep(0.1) gpio.output(TRIGER,gpio.HIGH) time.sleep(0.00002) gpio.output(TRIGER,gpio.LOW) while gpio.input(ECHO) == gpio.LOW: startTime = time.time() while gpio.input(ECHO) == gpio.HIGH: endTime = time.time() period = endTime - startTime dist1 = round(period * 1000000 / 58, 2) dist2 = round(period * 17241, 2) #print('Dist1', dist1, 'cm', ', Dist2', dist2, 'cm') if dist1<=13: gpio.output(LED_4, gpio.HIGH) if LED_STAT == 1: temp = int(cnt_One) cnt_One = temp+1 if LED_STAT == 2: temp = int(cnt_Two) cnt_Two = temp+1 if LED_STAT == 3: temp = int(cnt_Tree) cnt_Tree = temp+1 #print("13이하") else: gpio.output(LED_4, gpio.LOW) #print("13이상") def Switch(): gpio.output(LED_1, gpio.LOW) gpio.output(LED_2, gpio.LOW) gpio.output(LED_3, gpio.LOW) pwm.start(default_Servo/18.0+2) while True: switch_temp = gpio.input(switch) if switch_temp == False : global LED_STAT if LED_STAT == 0: LED_STAT =1 if LED_STAT == 4: LED_STAT =0 if LED_STAT==0: pwm.ChangeDutyCycle(0/18.0+2) gpio.output(LED_1, gpio.LOW) gpio.output(LED_2, gpio.LOW) gpio.output(LED_3, gpio.LOW) if LED_STAT==1: pwm.ChangeDutyCycle(45/18.0+2) gpio.output(LED_1, gpio.HIGH) gpio.output(LED_2, gpio.LOW) gpio.output(LED_3, gpio.LOW) if LED_STAT==2: pwm.ChangeDutyCycle(90/18.0+2) gpio.output(LED_1, gpio.LOW) gpio.output(LED_2, gpio.HIGH) gpio.output(LED_3, gpio.LOW) if LED_STAT==3: pwm.ChangeDutyCycle(135/18.0+2) gpio.output(LED_1, gpio.LOW) gpio.output(LED_2, gpio.LOW) gpio.output(LED_3, gpio.HIGH) print(LED_STAT) LED_STAT = LED_STAT + 1 time.sleep(2) if __name__ == '__main__' : thread_SWITCH = threading.Thread(target=Switch, args=()) thread_ULTRA = threading.Thread(target=Ultra, args=()) thread_SWITCH.start() thread_ULTRA.start()

# -*- coding: utf-8 -*-
import RPi.GPIO as gpio
import time
import datetime
import sys
import warnings
import threading
import pymysql
warnings.filterwarnings('ignore')
nowdate = ""  # 날짜&시간
nowtime = ""  # 시간
cnt_One = 0 #샴푸 내린 횟수
cnt_Two = 0 #린스 내린 횟수
cnt_Tree = 0 #바디 내린 횟수
count=0 # 타이머 초
startTime=''
endTime =''

LED_STAT = 0 #LED 기본값
default_Servo = 0 #서보모터 기본값 0 45 90 135
chang_Servo =5 #서보모터 변경값
# ___gpio 핀번호___#

switch = 17  # 버튼
LED_1 = 27 # LED1
LED_2 = 19  # LED2
LED_3 = 4  # LED3
LED_4 = 9 #LED4 초음파 대응센서
SERVO_SIG = 20 # Servo
TRIGER = 12 #초음파 tring
ECHO = 16   #초음파 echo
BUZZER = 7


# ____gipo 핀 세팅___#
gpio.cleanup()
gpio.setmode(gpio.BCM)
gpio.setup(switch, gpio.IN)

gpio.setup(BUZZER, gpio.OUT)

gpio.setup(LED_1, gpio.OUT)
gpio.setup(LED_2, gpio.OUT)
gpio.setup(LED_3, gpio.OUT)
gpio.setup(LED_4, gpio.OUT)
gpio.output(LED_1, gpio.LOW)
gpio.output(LED_2, gpio.LOW)
gpio.output(LED_3, gpio.LOW)
gpio.output(LED_4, gpio.LOW)

gpio.setup(TRIGER, gpio.OUT)
gpio.setup(ECHO,gpio.IN)


gpio.setup(SERVO_SIG, gpio.OUT)
pwm = gpio.PWM(SERVO_SIG, 50)



# 시간 함수__DEBTOLEE
def nowdate_f():
    # DATE
    global nowdate
    nowtemp = ''
    nowtemp = datetime.datetime.now()
    nowdate = nowtemp.strftime('%Y-%m-%d')
    
def nowtime_f():
    global nowtime
    nowtemp = ''
    nowtemp = datetime.datetime.now()
    nowtime = nowtemp.strftime('%H:%M')
    return nowtime

#초음파 함수
def Ultra():
    global cnt_One, cnt_Tree, cnt_Two
    while True:
        gpio.output(TRIGER,gpio.LOW)
        time.sleep(0.1)
        gpio.output(TRIGER,gpio.HIGH)
        time.sleep(0.00002)
        gpio.output(TRIGER,gpio.LOW)
        
        while gpio.input(ECHO) == gpio.LOW:
            startTime = time.time()
            
        while gpio.input(ECHO) == gpio.HIGH:
            endTime = time.time()
          
        period = endTime - startTime
        dist1 = round(period * 1000000 / 58, 2)
        dist2 = round(period * 17241, 2)
        
        #print('Dist1', dist1, 'cm', ', Dist2', dist2, 'cm')
        if dist1<=13:
            gpio.output(LED_4, gpio.HIGH)
            if LED_STAT == 1:
                temp = int(cnt_One)
                cnt_One = temp+1
            if LED_STAT == 2:
                temp = int(cnt_Two)
                cnt_Two = temp+1
            if LED_STAT == 3:
                temp = int(cnt_Tree) 
                cnt_Tree = temp+1
            
            #print("13이하")
        else:
            gpio.output(LED_4, gpio.LOW)
            #print("13이상")
        
            
def Switch():
    gpio.output(LED_1, gpio.LOW)
    gpio.output(LED_2, gpio.LOW)
    gpio.output(LED_3, gpio.LOW)
    
    pwm.start(default_Servo/18.0+2)
    global cnt_One,cnt_Two,cnt_Tree
    while True:
        switch_temp = gpio.input(switch)
        if switch_temp == False :
            global LED_STAT, startTime, endTime
            if LED_STAT == 0:
                thread_TIMER.start()
                startTime = nowtime_f()
                LED_STAT =1
            if LED_STAT == 4:
                count = -1
                LED_STAT =0
    
            if LED_STAT==0:
                pwm.ChangeDutyCycle(0/18.0+2)
                gpio.output(LED_1, gpio.LOW)
                gpio.output(LED_2, gpio.LOW)
                gpio.output(LED_3, gpio.LOW)
                endTime = nowtime_f()
                insertDB()
                
    
            if LED_STAT==1:
                pwm.ChangeDutyCycle(45/18.0+2)
                gpio.output(LED_1, gpio.HIGH)
                gpio.output(LED_2, gpio.LOW)
                gpio.output(LED_3, gpio.LOW)
                cnt_One+=1
                
            if LED_STAT==2:
                pwm.ChangeDutyCycle(90/18.0+2)
                gpio.output(LED_1, gpio.LOW)
                gpio.output(LED_2, gpio.HIGH)
                gpio.output(LED_3, gpio.LOW)
                cnt_Two+=1;
                
            if LED_STAT==3:
                pwm.ChangeDutyCycle(135/18.0+2)
                gpio.output(LED_1, gpio.LOW)
                gpio.output(LED_2, gpio.LOW)
                gpio.output(LED_3, gpio.HIGH)
                cnt_Tree+=1
                
            print(LED_STAT)
            LED_STAT = LED_STAT + 1
            time.sleep(2)

def w_timer():
    global count
    count+=1
    timer=threading.Timer(1,w_timer)
    timer.start()
    print(count)
    if count==6 :
        thread_BUZZER.start()
    
    if count==-1 :
        timer.cancel()
cntt = 0
def Buzzer_Th():
    global cntt
    while True:
        cntt +=1
        gpio.output(BUZZER, gpio.HIGH)
        time.sleep(1)
        gpio.output(BUZZER, gpio.LOW)
        time.sleep(1)    
        if cntt == 6 :
            thread_BUZZER.cancel()

def insertDB():
    conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='8659')
    sql = "INSERT INTO hackathon.datalist ( `h_date`, `s_time`, `e_time`, `one_cnt`, `two_cnt`, `tree_cnt`)  VALUES(%s,%s,%s,%s,%s,%s);"
        
    cur = conn.cursor() 
    
    nowdate_f()
    global nowdate,startTime,endTime,cnt_One,cnt_Two,cnt_Tree
    val = (nowdate,startTime,endTime,cnt_One,cnt_Two,cnt_Tree)
    cur.execute(sql, val)
    conn.commit()
    cur.close()
    conn.close()

        
if __name__ == '__main__' :
    thread_TIMER = threading.Thread(target=w_timer, args=())
    thread_BUZZER = threading.Thread(target=Buzzer_Th, args=())
    thread_SWITCH = threading.Thread(target=Switch, args=())
    thread_ULTRA = threading.Thread(target=Ultra, args=())

    insertDB
  
    thread_SWITCH.start()
    thread_ULTRA.start()

# -*- coding: utf-8 -*-
import RPi.GPIO as gpio
import time
import datetime
import sys
import warnings
import threading
import multiprocessing
import pymysql
warnings.filterwarnings('ignore')
nowdate = ""  # 날짜&시간
nowtime = ""  # 시간
cnt_One = 0 #샴푸 내린 횟수
cnt_Two = 0 #린스 내린 횟수
cnt_Tree = 0 #바디 내린 횟수
count=0 # 타이머 초
dist1 = 0 #초음파 값
startTime=''
endTime =''

LED_STAT = 0 #LED 기본값
default_Servo = 0 #서보모터 기본값 0 45 90 135
chang_Servo =5 #서보모터 변경값
# ___gpio 핀번호___#

switch = 17  # 버튼
LED_1 = 27 # LED1
LED_2 = 19  # LED2
LED_3 = 4  # LED3
LED_4 = 9 #LED4 초음파 대응센서
SERVO_SIG = 20 # Servo
TRIGER = 12 #초음파 tring
ECHO = 16   #초음파 echo
BUZZER = 7


# ____gipo 핀 세팅___#
gpio.cleanup()
gpio.setmode(gpio.BCM)
gpio.setup(switch, gpio.IN)

gpio.setup(BUZZER, gpio.OUT)

gpio.setup(LED_1, gpio.OUT)
gpio.setup(LED_2, gpio.OUT)
gpio.setup(LED_3, gpio.OUT)
gpio.setup(LED_4, gpio.OUT)
gpio.output(LED_1, gpio.LOW)
gpio.output(LED_2, gpio.LOW)
gpio.output(LED_3, gpio.LOW)
gpio.output(LED_4, gpio.LOW)

gpio.setup(TRIGER, gpio.OUT)
gpio.setup(ECHO,gpio.IN)


gpio.setup(SERVO_SIG, gpio.OUT)
pwm = gpio.PWM(SERVO_SIG, 50)



# 시간 함수__DEBTOLEE
def nowdate_f():
    # DATE
    global nowdate
    nowtemp = ''
    nowtemp = datetime.datetime.now()
    nowdate = nowtemp.strftime('%Y-%m-%d')
    
def nowtime_f():
    global nowtime
    nowtemp = ''
    nowtemp = datetime.datetime.now()
    nowtime = nowtemp.strftime('%H:%M')
    return nowtime

#초음파 함수
def Ultra():
    global cnt_One, cnt_Tree, cnt_Two, dist1
    while True:
        gpio.output(TRIGER,gpio.LOW)
        time.sleep(0.1)
        gpio.output(TRIGER,gpio.HIGH)
        time.sleep(0.00002)
        gpio.output(TRIGER,gpio.LOW)
        
        while gpio.input(ECHO) == gpio.LOW:
            startTime = time.time()
            
        while gpio.input(ECHO) == gpio.HIGH:
            endTime = time.time()
          
        period = endTime - startTime
        dist1 = round(period * 1000000 / 58, 2)
        
        print('Dist1', dist1, 'cm')
    
        if dist1<=13:
            gpio.output(LED_4, gpio.HIGH)
            if LED_STAT == 1:
                temp = int(cnt_One)
                cnt_One = temp+1
            if LED_STAT == 2:
                temp = int(cnt_Two)
                cnt_Two = temp+1
            if LED_STAT == 3:
                temp = int(cnt_Tree) 
                cnt_Tree = temp+1
            
            #print("13이하")
        else:
            gpio.output(LED_4, gpio.LOW)
            #print("13이상")

        
def Switch():
    gpio.output(LED_1, gpio.LOW)
    gpio.output(LED_2, gpio.LOW)
    gpio.output(LED_3, gpio.LOW)
    
    pwm.start(default_Servo/18.0+2)
    global cnt_One,cnt_Two,cnt_Tree
    while True:
        switch_temp = gpio.input(switch)
        if switch_temp == False :
            global LED_STAT, startTime, endTime
            if LED_STAT == 0:
                startTime = nowtime_f()
                LED_STAT =1


            if LED_STAT == 4:
                count = -1
                LED_STAT =0

            if LED_STAT==0:
                pwm.ChangeDutyCycle(0/18.0+2)
                gpio.output(LED_1, gpio.LOW)
                gpio.output(LED_2, gpio.LOW)
                gpio.output(LED_3, gpio.LOW)
                endTime = nowtime_f()
                insertDB()
                count = -1
                
    
            if LED_STAT==1:
                pwm.ChangeDutyCycle(45/18.0+2)
                gpio.output(LED_1, gpio.HIGH)
                gpio.output(LED_2, gpio.LOW)
                gpio.output(LED_3, gpio.LOW)
                cnt_One+=1
                startTime = nowtime_f()
                
                
            if LED_STAT==2:
                pwm.ChangeDutyCycle(90/18.0+2)
                gpio.output(LED_1, gpio.LOW)
                gpio.output(LED_2, gpio.HIGH)
                gpio.output(LED_3, gpio.LOW)
                cnt_Two+=1;
                
            if LED_STAT==3:
                pwm.ChangeDutyCycle(135/18.0+2)
                gpio.output(LED_1, gpio.LOW)
                gpio.output(LED_2, gpio.LOW)
                gpio.output(LED_3, gpio.HIGH)
                cnt_Tree+=1
                
            print(LED_STAT)
            LED_STAT = LED_STAT + 1
            time.sleep(2)



def insertDB():
    conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='8659')
    sql = "INSERT INTO hackathon.datalist ( `h_date`, `s_time`, `e_time`, `one_cnt`, `two_cnt`, `tree_cnt`)  VALUES(%s,%s,%s,%s,%s,%s);"
        
    cur = conn.cursor() 
    
    nowdate_f()
    global nowdate,startTime,endTime,cnt_One,cnt_Two,cnt_Tree
    val = (nowdate,startTime,endTime,cnt_One,cnt_Two,cnt_Tree)
    cur.execute(sql, val)
    

    
    conn.commit()
    

    cur.close()
    conn.close()

        
if __name__ == '__main__' :
    thread_SWITCH = multiprocessing.Process(target=Switch, args=())
    thread_ULTRA = multiprocessing.Process(target=Ultra, args=())
    
    thread_SWITCH.start()
    thread_ULTRA.start()

'Programing > Raspberry Pi)' 카테고리의 다른 글

Ultra(초음파) Python  (0) 2019.12.28
LED Python  (0) 2019.12.28
Raspberry Pi - OpenCV 설치  (0) 2019.12.23
TCP Rasspberry Pi Python and Android Client  (0) 2019.09.21
라즈베리파이, LED와 초음파센서 DB저장  (0) 2019.09.07