-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
73 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,50 @@ | ||
(ns com.oursky.doorlock.core | ||
(:require [taoensso.timbre :as log] | ||
[gpio.core :refer [open-port open-channel-port write-value! toggle!]] | ||
[clojure.java.shell :refer [sh]] | ||
[clojure.core.async :refer [<! go-loop timeout]] | ||
)) | ||
[clojure.core.async :refer [<! >! >!! alts! go-loop chan timeout]] | ||
[org.httpkit.server :refer [run-server]] | ||
) | ||
(:gen-class)) | ||
|
||
; setup GPIO via wiringpi CLI interface | ||
; the clj-gpio library does not support internal pull-up | ||
(sh "gpio" "mode" "0" "up") | ||
(sh "gpio" "mode" "1" "out") | ||
; unlock triggering channel | ||
; identify the trigger source by sending {:source <source>} | ||
(def unlock-chan (chan)) | ||
|
||
(def button-chan (open-channel-port 0)) | ||
(def unlock-port (open-port 1)) | ||
(defn http-handler [req] | ||
(>!! unlock-chan {:source :network}) | ||
{:status 200}) | ||
|
||
(go-loop | ||
[] | ||
(<! (timeout 2000)) | ||
(toggle! unlock-port) | ||
(recur)) | ||
(defn -main [& args] | ||
; setup GPIO via wiringpi CLI interface | ||
; the clj-gpio library does not support internal pull-up | ||
(sh "gpio" "mode" "0" "up") | ||
(sh "gpio" "mode" "1" "out") | ||
|
||
(go-loop | ||
[] | ||
(log/info (<! button-chan)) | ||
(recur)) | ||
; button event listener | ||
; hold down for atleast 200ms to trigger | ||
; will emit event every 25000ms if held down | ||
(go-loop [] | ||
(if (= 1 (read-string (:out (sh "gpio" "read" "0")))) | ||
(sh "gpio" "wfi" "0" "falling") | ||
(<! (timeout 2500))) | ||
(<! (timeout 200)) | ||
(when (= 0 (read-string (:out (sh "gpio" "read" "0")))) | ||
(>! unlock-chan {:source :button})) | ||
(recur)) | ||
|
||
(log/info "=== Daemon Started ===") | ||
; listen on unlock-chan for unlock events | ||
; if a new unlock event is revieved before the 3000ms timeout, the door is kept open. | ||
(go-loop [unlock nil] | ||
(when unlock | ||
(sh "gpio" "write" "1" "1") | ||
(loop [[trigger _] [unlock nil]] | ||
(when trigger | ||
(log/info (str "Unlock triggered by " (:source trigger))) | ||
(recur (alts! [unlock-chan (timeout 3000)])))) | ||
(sh "gpio" "write" "1" "0") | ||
(log/info "Door Locked")) | ||
(recur (<! unlock-chan))) | ||
|
||
(run-server http-handler {:ip "0.0.0.0" :port 8090}) | ||
|
||
(log/info "=== Daemon Started ===")) |