aboutsummaryrefslogtreecommitdiff
path: root/button/space_button.ino
blob: eefdd15ca5f293bfd6547fb8e2c3f01465af675d (plain)
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
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();
      }
    }
  }
}