diff options
Diffstat (limited to 'button/space_button.ino')
| -rw-r--r-- | button/space_button.ino | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/button/space_button.ino b/button/space_button.ino new file mode 100644 index 0000000..eefdd15 --- /dev/null +++ b/button/space_button.ino @@ -0,0 +1,101 @@ +#include "WiFi.h" +#include "HTTPClient.h" + +/* + D0 - in - big switch toggling open and closed + D1 - in - small switch toggling public/private + D10 - out - led showing small switch state +*/ + +#define LOG(msg) Serial.print(msg); + +#define WIFI_PASSWORD "PASSWORD" +#define WIFI_SSID "SSID" + +#define API_HOST "space.api.binhacken.de" + +#define API_PATH_STARTUP "/startup" +#define API_PATH_OPEN_PUBLIC "/open?public=true" +#define API_PATH_CLOSE_PUBLIC "/close?public=true" +#define API_PATH_OPEN_PRIVATE "/open?public=false" +#define API_PATH_CLOSE_PRIVATE "/close?public=false" + +void setup(void); +void loop(void); + +bool d0_state; +bool d0_prev; +bool d1_state; + +void setup(void) { + HTTPClient http; + + Serial.begin(115200); + LOG("Lets get down to business"); + + /*connect to wifi*/ + WiFi.begin(WIFI_SSID, WIFI_PASSWORD); + + while (WiFi.status() != WL_CONNECTED) { + LOG(".") + delay(500); + } + + LOG("\n:peepohacker: I'm in \n") + + /*setup pins*/ + pinMode(D10, OUTPUT); + pinMode(D0, INPUT); + pinMode(D1, INPUT); + + /*capture initial state*/ + d0_state = d0_prev = digitalRead(D0); + d1_state = digitalRead(D1); + + /*setup led correctly after restart*/ + digitalWrite(D10, d1_state); + + /*send startup message*/ + http.begin(API_HOST API_PATH_STARTUP); + http.GET(); + http.end(); +} + + +void loop(void) { + HTTPClient http; + + /*light up led accordingly*/ + digitalWrite(D10, d1_state = digitalRead(D1)); + + /* + you may ask me why I use get for things that should be post. + it's simple. I'm lazy as fuck and 1 minute of skimming the HTTPClient header + did not teach me how to do posts, because they are way more involved than just get. + if it bothers you so much, go touch some grass and then open a PR. + */ + if ((d0_state = digitalRead(D0)) != d0_prev) /*someone toggled D0*/ { + d0_prev = d0_state; + if (d0_state) { /*D0 true -> open*/ + if (d1_state) { /*D1 true -> private*/ + http.begin(API_HOST API_PATH_OPEN_PRIVATE); + http.GET(); + http.end(); + } else { /*D1 false -> public*/ + http.begin(API_HOST API_PATH_OPEN_PUBLIC); + http.GET(); + http.end(); + } + } else { /*D0 false -> closed*/ + if (d1_state) { /*D1 true -> private*/ + http.begin(API_HOST API_PATH_CLOSE_PRIVATE); + http.GET(); + http.end(); + } else { /*D1 false -> public*/ + http.begin(API_HOST API_PATH_CLOSE_PUBLIC); + http.GET(); + http.end(); + } + } + } +}
\ No newline at end of file |
