私はラズベリーで簡単なホームオートメーションを作成しています。今私はガレージのドアを開き、ゲートを開き、温度を読むことができます。 サーバーはノードjsです。 今、ガレージドアが開いているときを検出したいと思います。私はマイクロスイッチを使用すると思いますが、いつ押されたかを検出する最適な方法はありますか? Pythonスクリプトの無限ループですか?ポーリングスクリプト? 私を助けることができますか? ありがとうラズベリー待ち受けマイクロスイッチ
答えて
基本的にはGPIO割り込みを実装しています。必要なのはRPi.GPIO
モジュールです。このモジュールは、リアルタイムまたはタイミングクリティカルなアプリケーションに適していないことを
注:
しかしRPi.GPIOモジュールの説明では、警告あります。これは、Pythonがビジー状態のガベージコレクションを予測することができないためです。また、リアルタイムアプリケーションには適していないLinuxカーネルでも動作します。マルチタスクO/Sであり、別のプロセスがCPUよりも優先され、プログラムにジッタが発生する可能性があります。あなたが本当のリアルタイム性能と予測可能性の後であれば、Arduinoを買ってください!私はあなたのタスクを実装する3つの方法(私はもっとあるはずと信じて)見つけた
:
- 最初の方法はpollingです:
ポーリングが継続的にチェックされている何かのためにing。たとえば、プログラムがボタンプレスにできるだけ早く反応するようにするには、ボタンの状態を1秒あたり約10,000回確認できます。これはすばやい反応が必要な場合に最適ですが、コンピュータの処理能力のかなりの部分を使用します。配信を期待しているときに完全にフォーカスすることができないのと同じ方法で、CPUの大部分がこのポーリングで使い切られます。
#!/usr/bin/env python2.7 # script by Alex Eames http://RasPi.tv/ # http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # GPIO 23 set up as input. It is pulled up to stop false signals GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) print "Make sure you have a button connected so that when pressed" print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n" raw_input("Press Enter when ready\n>") print "Waiting for falling edge on port 23" # now the program will do nothing until the signal on port 23 # starts to fall towards zero. This is why we used the pullup # to keep the signal high and prevent a false interrupt print "During this waiting time, your computer is not" print "wasting resources by polling for a button press.\n" print "Press your button when ready to initiate a falling edge interrupt." try: GPIO.wait_for_edge(23, GPIO.FALLING) print "\nFalling edge detected. Now your program can continue with" print "whatever was waiting for a button press." except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit GPIO.cleanup() # clean up GPIO on normal exit
- 第二の方法thread callbacksを使用している:イベントが第二のスレッドで検出された場合
、それは(メインスレッドにこれをバック通信はを呼び出します)。 RPi.GPIOには、割り込みのために新しいスレッドを開始し、割り込みが2番目のスレッドで発生したときに実行される一連の命令(関数)を指定する機能があります。 これはスレッドコールバック関数です。
#!/usr/bin/env python2.7 # script by Alex Eames http://RasPi.tv import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # GPIO 23 & 24 set up as inputs. One pulled up, the other down. # 23 will go to GND when button pressed and 24 will go to 3V3 (3.3V) # this enables us to demonstrate both rising and falling edge detection GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # now we'll define the threaded callback function # this will run in another thread when our event is detected def my_callback(channel): print "Rising edge detected on port 24 - even though, in the main thread," print "we are still waiting for a falling edge - how cool?\n" print "Make sure you have a button connected so that when pressed" print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n" print "You will also need a second button connected so that when pressed" print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)" raw_input("Press Enter when ready\n>") # The GPIO.add_event_detect() line below set things up so that # when a rising edge is detected on port 24, regardless of whatever # else is happening in the program, the function "my_callback" will be run # It will happen even while the program is waiting for # a falling edge on the other button. GPIO.add_event_detect(24, GPIO.RISING, callback=my_callback) try: print "Waiting for falling edge on port 23" GPIO.wait_for_edge(23, GPIO.FALLING) print "Falling edge detected. Here endeth the second lesson." except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit GPIO.cleanup() # clean up GPIO on normal exit
まあ、実際に、我々は除いて、前回とは大きく違うことはあまりやっていない、今がありますそれ以上のこと。もう1つのボタンと別のスレッドコールバック関数を最初のものと同じにします(ただし、別のGPIOポート上にあります)。これは、1つのプログラムで複数のスレッドコールバックを実行できることを示しています。その後、あなたの想像力は限界です。 (まあ実際にはGPIOポートの数は、おそらく制限です。)
#!/usr/bin/env python2.7 # script by Alex Eames http://RasPi.tv # http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3 import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) # GPIO 23 & 17 set up as inputs, pulled up to avoid false detection. # Both ports are wired to connect to GND on button press. # So we'll be setting up falling edge detection for both GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP) # GPIO 24 set up as an input, pulled down, connected to 3V3 on button press GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # now we'll define two threaded callback functions # these will run in another thread when our events are detected def my_callback(channel): print "falling edge detected on 17" def my_callback2(channel): print "falling edge detected on 23" print "Make sure you have a button connected so that when pressed" print "it will connect GPIO port 23 (pin 16) to GND (pin 6)\n" print "You will also need a second button connected so that when pressed" print "it will connect GPIO port 24 (pin 18) to 3V3 (pin 1)\n" print "You will also need a third button connected so that when pressed" print "it will connect GPIO port 17 (pin 11) to GND (pin 14)" raw_input("Press Enter when ready\n>") # when a falling edge is detected on port 17, regardless of whatever # else is happening in the program, the function my_callback will be run GPIO.add_event_detect(17, GPIO.FALLING, callback=my_callback, bouncetime=300) # when a falling edge is detected on port 23, regardless of whatever # else is happening in the program, the function my_callback2 will be run # 'bouncetime=300' includes the bounce control written into interrupts2a.py GPIO.add_event_detect(23, GPIO.FALLING, callback=my_callback2, bouncetime=300) try: print "Waiting for rising edge on port 24" GPIO.wait_for_edge(24, GPIO.RISING) print "Rising edge detected on port 24. Here endeth the third lesson." except KeyboardInterrupt: GPIO.cleanup() # clean up GPIO on CTRL+C exit GPIO.cleanup() # clean up GPIO on normal exit
うまくいけば、チュートリアルのこのシーケンスは、あなたがラズベリーパイGPIOはもう少し割り込みが、私はArduinoにいくつかの研究を行うことをお勧め理解する助け、または少なくともCやC++のような低レベル言語の場合は、great tutorial on interrupts thereがあります。
また、Raspberry Pi Stack Exchange communityに参加すると、より多くのヘルプがあります。
- 1. Wordpressサイト受信待ち/待ち受け時間が遅い
- 2. 子プロセスの待ち受け
- 3. ノードホーズマンコールの待ち受け
- 4. Javaの待ち受けスレッド
- 5. C#非同期タスク待機待ちvs待ち受け
- 6. C#スレッドと待ち受けのフォーム
- 7. asyncio待ち受けオブジェクト - 基本例
- 8. 非同期、待ち受け、ToListのタスク
- 9. タスク非同期と待ち受け
- 10. リスト<MyClass>待ち受けタスク
- 11. 待ち受けができないときの待ち合わせ方法
- 12. C#UDPソケットは応答を待ち受けませんか?
- 13. ドッカーでポートを待ち受けるには?
- 14. MSフェイクを使った単体テスト:HttpClientと待ち受け
- 15. ビューを待ち受け画面に変更する
- 16. asyncの詳細/ typescriptでの待ち受け
- 17. .load()コンテンツのイベントを待ち受けます。
- 18. クレジットカードリーダーからのデカップリングクレジットカードの待ち受けウィンドウ
- 19. リアクタールーター - 歴史が最初に待ち受けてくる
- 20. FUSEでAMQブローカーの開始を待ち受ける方法
- 21. Ember 2、受け入れテスト、websocket保留中のハングアップ、および完了待ちのための待ち時間
- 22. ソケット受信データの非同期/待機待ち
- 23. NodeJS - 予期せぬ識別子が待ち受けています
- 24. alamofireの画像リクエストが完了するまでの待ち受け方法
- 25. ウサギ待ち行列が接続を受け入れるのをブロックする
- 26. 静脈内のfinish()関数でACKを受け取るのを待ちます。
- 27. AJAXリクエストがExtJSで完了するまでの待ち受け方法
- 28. Nito.AsyncEx.AsyncLockスタックオーバフローで多数の待ち受け員と同期高速パス
- 29. async /ノード/エクスプレスで待ち受けていないとプロミスが解決するのを待たずにいる
- 30. ラズベリーでPHP7でマルチキャストを受信
ありがとうございました。今私は温度を得るために、私は15分ごとにcron pythonスクリプトを予定していました。しかし、私はボタンが押されたときに警告する必要があります私はスケジュールされた仕事を待つことができません。要約すると、私は起動時にイベントを待つスクリプトを準備しますか? – robyg72
はい、[この質問](https://askubuntu.com/questions/396654/how-to-run-the-python-program-in-the-background-in-ubuntu- machine#396655)Answer(python 2.7を使っているなら '#!/ usr/bin/env python2.7'のように環境変数を入力するのを忘れないでください)、crontab以外の方法もあります。 – Lycopersicum
と幸せな新年! – robyg72