이론
***pulse_duration * 17000 ?***
속도 공식(속도 = 거리 / 시간)에 따라, 속도(340m/s라고 함) = 거리(x) / 시간(duration/2)
duration/2 : 초음파센서의 원리가 초음파를 내보내고(1번) 돌아오기까지의(2번) 시간을 측정하는 것이므로 거리를 2번지난것과 같음
따라서 거리(x) = 속도(340) * 시간(duration/2)
duartion * (340m/s / 2) -> duration * (170m/s) -> m를 cm로 바꾸면 -> duration * 17000
[Flask(플라스크) 서버 구축]
sudo pip3 install flask
*** index.html 코드 내용도 정상, 파일경로도 정상인데 자꾸 Internal Server Error가 뜨면, 넷빈즈에서 Webbrower로 새 프로젝트 만든후, 디폴트로 생기는 index.html에 작성해서 fileZilla로 업로드 해볼것!!! ***
실습
hc_demo.py
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 | import RPi.GPIO as gpio import time gpio.setmode(gpio.BCM) trig = 13 echo = 19 led_pin = 18 buz_pin = 12 print ( "start" ) gpio.setwarnings( False ) gpio.setup(trig,gpio.OUT) gpio.setup(led_pin, gpio.OUT) gpio.setup(echo, gpio.IN) gpio.setup(buz_pin, gpio.OUT) try : while True : gpio.output(trig, False ) time.sleep( 0.5 ) gpio.output(trig, True ) time.sleep( 0.00001 ) gpio.output(trig, False ) pulse_end = 0 pulse_start = 0 while gpio. input (echo) = = 0 : pulse_start = time.time() while gpio. input (echo) = = 1 : pulse_end = time.time() pulse_duration = pulse_end - pulse_start distance = pulse_duration * 17000 distance = round (distance, 2 ) if distance > = 10 : #cm gpio.output(led_pin, False ) gpio.output(buz_pin, False ) else : gpio.output(led_pin, True ) gpio.output(buz_pin, True ) print ( "Distance : " , distance, "cm" ) except KeyboardInterrupt as e: print (e) finally : print ( "clean up..." ) gpio.cleanup() |
hcr0531_java
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 | package ultrawave0531; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import jdk.dio.gpio.GPIOPin; public class Hcr0531 { public static int getDistance(GPIOPin trigPin, GPIOPin echoPin){ int distance = 0 ; try { //펄스를 10마이크로초 동안 발생 trigPin.setValue( true ); Thread.sleep( 0 , 10000 ); trigPin.setValue( false ); //high 수신 시작일 때 시간 측정 while (echoPin.getValue() == false ){} double startTime = System.nanoTime(); //low 수신 시작일 때 시간 측정 while (echoPin.getValue() == true ){} double endTime = System.nanoTime(); double distance_val = (endTime - startTime)/ 1000000000 / 2 ; distance = ( int ) (distance_val * 34000 ); //cm단위 } catch (Exception ex) { ex.printStackTrace(); } return distance; } } |
FXMLDocumentController.java
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 | package ultrawave0531; import java.io.IOException; import java.net.URL; import java.util.ResourceBundle; import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Platform; import javafx.event.ActionEvent; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; import jdk.dio.ClosedDeviceException; import jdk.dio.DeviceManager; import jdk.dio.gpio.GPIOPinConfig; import jdk.dio.gpio.GPIOPin; /** * * @author kosta */ public class FXMLDocumentController implements Initializable { @FXML private Label lbDistance; private boolean flag; private LED led; private LED buz; @FXML private void handleButtonAction(ActionEvent event) { try { led = new LED( 27 ); buz = new LED( 12 ); System.out.println( "You clicked me!" ); GPIOPin trigPin = DeviceManager.open( new GPIOPinConfig.Builder() .setPinNumber( 13 ) .setDirection(GPIOPinConfig.DIR_OUTPUT_ONLY).build()); //gpio.setup(변수명, gpio.OUT)와 동일 GPIOPin echoPin = DeviceManager.open( new GPIOPinConfig.Builder() .setPinNumber( 19 ) .setDirection(GPIOPinConfig.DIR_INPUT_ONLY).build()); //스레드!!! Thread th = new Thread( new Runnable() { @Override public void run() { System.out.println( "thread start..." ); while (!flag) { try { Thread.sleep( 200 ); } catch (Exception e) { } int distance = Hcr0531.getDistance(trigPin, echoPin); Platform.runLater( new Runnable() { @Override public void run() { try { if (distance < 10 ) { led.on(); buz.on(); } else { led.off(); buz.off(); } } catch (Exception e) { } lbDistance.setText(String.valueOf(distance)); } }); } //while문 끝 try { trigPin.close(); echoPin.close(); } catch (IOException ex) { } } }); th.setDaemon( true ); th.start(); } catch (Exception ex) { } //종료시... Runtime.getRuntime().addShutdownHook( new Thread(){ @Override public void run() { try { led.close(); buz.close(); } catch (IOException ex) { } } }); } @Override public void initialize(URL url, ResourceBundle rb) { try { } catch (Exception ex) { } } } |
web_server.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from flask import Flask app = Flask(__name__) @app .route( '/' ) def index(): return "This is main page" @app .route( '/sub' ) def sub(): return "This is sub page" @app .route( '/led/<state>' ) def led(state): if state = = "on" or state = = "off" : str = "Led is now %s." % state else : str = "Invalid Pages" return str if __name__ = = "__main__" : print ( "Webserver stasrts..." ) app.run(host = "192.168.0.164" ) |
web_server2.py
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 | from flask import Flask, render_template, request import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setwarnings( False ) buz_pin = 12 # buz GPIO.setup(buz_pin, GPIO.OUT) app = Flask(__name__) @app .route( '/' ) def index(): return render_template( 'index.html' ) #index.html에서 on/off를 클릭 했을 경우 파라미터를 처리하기 위한 route @app .route( '/param' ,methods = [ 'GET' , 'POST' ]) def params(): if request.method = = 'GET' : paramv = request.args.get( 'status' , '') print ( 'status:' + paramv) if paramv = = "on" : GPIO.output(buz_pin, True ) elif paramv = = "off" : GPIO.output(buz_pin, False ) return paramv if __name__ = = '__main__' : print ( 'Webserver starts' ) app.run(host = '0.0.0.0' ) |
'학원수업 > 파이썬' 카테고리의 다른 글
학원 50일차 복습(5/30) (0) | 2018.05.30 |
---|---|
학원 49일차 복습(5/29) (0) | 2018.05.29 |
학원 48일차 복습(5/28) (0) | 2018.05.28 |
학원 45일차 복습(5/18) (0) | 2018.05.23 |
학원 44일차 복습(5/17) (0) | 2018.05.17 |