From 1d13ce7ef927bb92d5ddd98adc11e4481980b80a Mon Sep 17 00:00:00 2001 From: Joe Shaw Date: Fri, 21 Aug 2015 11:54:34 -0400 Subject: [PATCH] the Salt Mines gyro button --- .gitignore | 1 + README.md | 24 ++++++++++++++++++++++++ gyro.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 gyro.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a924640 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gyro diff --git a/README.md b/README.md new file mode 100644 index 0000000..795f17a --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +## Gryo Button + +When you press the button on an Amazon Dash, we increment the Salt +Mines gyro counter. See the current count [on the +dashboard](http://dashboard.saltmines.us/south). + +Inspired by [this article by Ted +Benson](https://medium.com/@edwardbenson/how-i-hacked-amazon-s-5-wifi-button-to-track-baby-data-794214b0bdd8). + +## Development and deployment + +This runs on the Raspberry Pi in the office. I develop it on my Mac +and cross compile it for linux/arm. You will need Go with the +linux/arm toolchain. If you're on a Mac, this is easy: + + $ brew install --with-cc-common go + +Once that's done: + + $ GOOS=linux GOARCH=arm go build + +Then scp the `gyro` binary to `pi@dash1.local` and run it: + + $ sudo ./gyro wlan0 diff --git a/gyro.go b/gyro.go new file mode 100644 index 0000000..501c5dd --- /dev/null +++ b/gyro.go @@ -0,0 +1,45 @@ +package main + +import ( + "log" + "net" + "net/http" + "net/url" + "os" + + "github.com/mdlayher/arp" +) + +const gyroURL = "http://saltmines.us/gyroup.php" +const name = "Some button pusher" + +func main() { + if len(os.Args) < 2 { + log.Fatal("Usage: arp ") + } + + iface, err := net.InterfaceByName(os.Args[1]) + if err != nil { + log.Fatal("InterfaceByName: ", err) + } + + c, err := arp.NewClient(iface) + if err != nil { + log.Fatal("arp.NewClient: ", err) + } + + for { + p, _, err := c.Read() + if err != nil { + log.Fatal(err) + } + + if p.Operation == arp.OperationRequest && p.SenderIP.Equal(net.IPv4zero) { + resp, err := http.PostForm(gyroURL, url.Values{"user_name": {name}}) + if err != nil { + log.Printf("Error updating count: %s", err) + } + resp.Body.Close() + } + } +}