-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoorbell.go
100 lines (83 loc) · 1.83 KB
/
doorbell.go
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
package main
import (
"log"
"net/http"
"net/url"
"os"
"os/exec"
"time"
"gopkg.in/yaml.v2"
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/gpio/gpioreg"
"periph.io/x/host/v3"
)
type Config struct {
Pin string `yaml:"pin"`
WavFile string `yaml:"wav_file"`
Pushover map[string]string `yaml:"pushover"`
}
var cfg *Config
func LoadConfig(filename string) (*Config, error) {
bytes, err := os.ReadFile(filename)
if err != nil {
return nil, err
}
var config Config
if err = yaml.Unmarshal(bytes, &config); err != nil {
return nil, err
}
return &config, nil
}
func tellPushover() {
data := url.Values{
"token": {cfg.Pushover["token"]},
"user": {cfg.Pushover["user"]},
"message": {cfg.Pushover["message"]},
}
resp, err := http.PostForm("https://api.pushover.net:443/1/messages.json", data)
if err != nil {
log.Printf("Error sending push notification: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("Received non-OK response when sending push notification: %v", resp.Status)
}
}
func main() {
var err error
cfg, err = LoadConfig("config.yaml")
if err != nil {
log.Fatalf("Failed to load config: %v", err)
}
if _, err := host.Init(); err != nil {
log.Fatalf("Failed to initialize periph: %v", err)
}
p := gpioreg.ByName(cfg.Pin)
if p == nil {
log.Fatalf("Failed to find pin: %s", cfg.Pin)
}
var count int
for {
if p.Read() == gpio.Low {
count++
} else {
count = 0
}
if p.Read() == gpio.Low && count == 2 {
count = 0
log.Println("Button press")
tellPushover()
go func() {
cmd := exec.Command("aplay", cfg.WavFile)
if err := cmd.Run(); err != nil {
log.Printf("Error playing sound: %v", err)
}
}()
}
time.Sleep(100 * time.Millisecond)
if count > 0 {
log.Println(count)
}
}
}